Beispiel #1
0
    def process(self, *args, **kwargs):
        with make_db(self.db) as db:
            try:
                nas_list = db.query(models.TrBas)
                for nas in nas_list:
                    if not nas.dns_name:
                        continue
                    results, _, _ = yield client.lookupAddress(nas.dns_name)
                    if not results:
                        logger.info("domain {0} resolver empty".format(
                            nas.dns_name))

                    if results[0].type == dns.A:
                        ipaddr = ".".join(
                            str(i) for i in struct.unpack(
                                "BBBB", results[0].payload.address))
                        if ipaddr:
                            nas.ip_addr = ipaddr
                            db.commit()
                            logger.info(
                                "domain {0} resolver {1}  success".format(
                                    nas.dns_name, ipaddr))
                    else:
                        logger.info("domain {0} no ip address,{1}".format(
                            nas.dns_name, repr(results)))

            except Exception as err:
                logger.error('ddns process error %s' %
                             utils.safeunicode(err.message))
        defer.returnValue(60)
Beispiel #2
0
def load_events(event_path=None,
                pkg_prefix=None,
                excludes=[],
                event_params={}):
    _excludes = ['__init__', 'settings'] + excludes
    evs = set(os.path.splitext(it)[0] for it in os.listdir(event_path))
    evs = [it for it in evs if it not in _excludes]
    for ev in evs:
        try:
            sub_module = os.path.join(event_path, ev)
            if os.path.isdir(sub_module):
                # logger.info('load sub event %s' % ev)
                load_events(
                    event_path=sub_module,
                    pkg_prefix="{0}.{1}".format(pkg_prefix, ev),
                    excludes=excludes,
                    event_params=event_params,
                )
            _ev = "{0}.{1}".format(pkg_prefix, ev)
            # logger.info('load_event %s with params:%s' % (_ev,repr(event_params)))
            dispatch.register(
                importlib.import_module(_ev).__call__(**event_params))
        except Exception as err:
            logger.error("%s, skip module %s.%s" % (str(err), pkg_prefix, ev))
            import traceback
            traceback.print_exc()
            continue
Beispiel #3
0
    def process(self, *args, **kwargs):
        with make_db(self.db) as db:
            try:
                nodes = db.query(models.TrNode)
                for node in nodes:
                    r = db.query(
                        func.sum(
                            models.TrOnline.input_total).label("input_total"),
                        func.sum(models.TrOnline.output_total).label(
                            "output_total")).filter(
                                models.TrOnline.account_number ==
                                models.TrAccount.account_number,
                                models.TrAccount.customer_id ==
                                models.TrCustomer.customer_id,
                                models.TrCustomer.node_id == node.id).first()
                    if r and all([r.input_total, r.output_total]):
                        stat = models.TrFlowStat()
                        stat.node_id = node.id
                        stat.stat_time = int(time.time())
                        stat.input_total = r.input_total
                        stat.output_total = r.output_total
                        db.add(stat)
                db.commit()
                logger.info("flow stat task done")
            except Exception as err:
                db.rollback()
                logger.error('flow_stat_job err,%s' % (str(err)))

        return 120.0
Beispiel #4
0
 def parse_request(self):
     try:
         return apiutils.parse_request(self.settings.config.system.secret,
                                       self.request.body)
     except Exception as err:
         logger.error(u"api authorize parse error, %s" %
                      utils.safeunicode(traceback.format_exc()))
         raise ValueError(u"Error: %s" % utils.safeunicode(err.message))
Beispiel #5
0
    def process(self, *args, **kwargs):
        try:
            self.statdata.run_stat()
            if self.cache.get(radius_statcache_key):
                self.cache.update(radius_statcache_key,self.statdata)
            else:
                self.cache.set(radius_statcache_key,self.statdata)
        except Exception as err:
            logger.error('radius stat process error %s' % utils.safeunicode(err.message))

        return 10.0
Beispiel #6
0
 def init_route_permit(self):
     with make_db(self.db) as conn:
         try:
             oprs = conn.query(models.TrOperator)
             for opr in oprs:
                 if opr.operator_type > 0:
                     for rule in self.db.query(models.TrOperatorRule).filter_by(operator_name=opr.operator_name):
                         permit.bind_opr(rule.operator_name, rule.rule_path)
                 elif opr.operator_type == 0:  # 超级管理员授权所有
                     permit.bind_super(opr.operator_name)
         except Exception as err:
             logger.error("init route error , %s" % str(err))
Beispiel #7
0
    def billing(self, online):
        product = self.get_product_by_id(self.account.product_id)
        if not product:
            logger.error('product <%s> not exists' % self.account.product_id)
            return

        if product.product_policy not in (PPTimes,BOTimes,PPFlow,BOFlows):
            self.update_online(self.request.nas_addr,
                self.request.acct_session_id,
                billing_times=self.request.acct_session_time,
                input_total=self.get_input_total(),
                output_total=self.get_output_total())
        else:
            self.bill_funcs[product.product_policy](online, product)
Beispiel #8
0
    def processAcct(self, datagram, host, port):
        try:
            bas = self.find_nas(host)
            if not bas:
                raise PacketError(
                    '[Radiusd] :: Dropping packet from unknown host %s' % host)

            secret, vendor_id = bas['bas_secret'], bas['vendor_id']
            req = self.createAcctPacket(packet=datagram,
                                        dict=self.dict,
                                        secret=six.b(str(secret)),
                                        vendor_id=vendor_id)

            self.do_stat(req.code, req.get_acct_status_type())

            logger.info("[Radiusd] :: Received radius request: %s" %
                        (repr(req)))
            if self.config.system.debug:
                logger.debug(req.format_str())

            if req.code != packet.AccountingRequest:
                raise PacketError(
                    'non-AccountingRequest packet on authentication socket')

            if not req.VerifyAcctRequest():
                raise PacketError('VerifyAcctRequest error')

            reply = req.CreateReply()
            self.pusher.push(msgpack.packb([reply.ReplyPacket(), host, port]))
            self.do_stat(reply.code)
            logger.info("[Radiusd] :: Send radius response: %s" % repr(reply))
            if self.config.system.debug:
                logger.debug(reply.format_str())

            status_type = req.get_acct_status_type()
            if status_type in self.acct_class:
                acct_func = self.acct_class[status_type](
                    self.db_engine, self.mcache, None,
                    req.get_ticket()).acctounting
                reactor.callLater(0.1, acct_func)
            else:
                logger.error('status_type <%s> not support' % status_type)
        except Exception as err:
            self.do_stat(0)
            errstr = 'RadiusError:Dropping invalid acct packet from {0} {1},{2}'.format(
                host, port, utils.safeunicode(err))
            logger.error(errstr)
            import traceback
            traceback.print_exc()
Beispiel #9
0
def check_sign(api_secret, msg):
    """
        >>> check_sign("123456",dict(code=1,s='2',msg=u'中文',sign='33C9065427EECA3490C5642C99165145'))
        True

    """
    if "sign" not in msg:
        return False
    sign = msg['sign']
    params = [utils.safestr(msg[k]) for k in msg if k != 'sign']
    local_sign = make_sign(api_secret, params)
    result = (sign == local_sign)
    if not result:
        logger.error("check_sign failure, sign:%s != local_sign:%s" %(sign,local_sign))
    return result
Beispiel #10
0
def check_sign(api_secret, msg):
    """
        >>> check_sign("123456",dict(code=1,s='2',msg=u'中文',sign='33C9065427EECA3490C5642C99165145'))
        True

    """
    if "sign" not in msg:
        return False
    sign = msg['sign']
    params = [utils.safestr(msg[k]) for k in msg if k != 'sign']
    local_sign = make_sign(api_secret, params)
    result = (sign == local_sign)
    if not result:
        logger.error("check_sign failure, sign:%s != local_sign:%s" %
                     (sign, local_sign))
    return result
Beispiel #11
0
    def acctounting(self):
        if not self.account:
            return logger.error(
                "[Acct] Received an accounting update request but user[%s] not exists"% self.request.account_number)  

        ticket = Storage(**self.request)
        _datetime = datetime.datetime.now() 
        online = self.get_online(ticket.nas_addr,ticket.acct_session_id)    
        if not online:
            session_time = ticket.acct_session_time 
            stop_time = _datetime.strftime( "%Y-%m-%d %H:%M:%S")
            start_time = (_datetime - datetime.timedelta(seconds=int(session_time))).strftime( "%Y-%m-%d %H:%M:%S")
            ticket.acct_start_time = start_time
            ticket.acct_stop_time = stop_time
            ticket.start_source= STATUS_TYPE_STOP
            ticket.stop_source = STATUS_TYPE_STOP
            self.add_ticket(ticket)
        else:
            self.del_online(ticket.nas_addr,ticket.acct_session_id)
            ticket.acct_start_time = online.acct_start_time
            ticket.acct_stop_time= _datetime.strftime( "%Y-%m-%d %H:%M:%S")
            ticket.start_source = online.start_source
            ticket.stop_source = STATUS_TYPE_STOP
            self.add_ticket(ticket)

        self.billing(online)
        logger.info('%s Accounting stop request, remove online'% self.account.account_number)
Beispiel #12
0
    def acctounting(self):
        if not self.account:
            return logger.error(
                "[Acct] Received an accounting update request but user[%s] not exists"
                % self.request.account_number)

        ticket = Storage(**self.request)
        online = self.get_online(ticket.nas_addr, ticket.acct_session_id)
        if not online:
            sessiontime = ticket.acct_session_time
            updatetime = datetime.datetime.now()
            _starttime = updatetime - datetime.timedelta(seconds=sessiontime)
            online = Storage(
                account_number=self.account.account_number,
                nas_addr=ticket.nas_addr,
                acct_session_id=ticket.acct_session_id,
                acct_start_time=_starttime.strftime("%Y-%m-%d %H:%M:%S"),
                framed_ipaddr=ticket.framed_ipaddr,
                mac_addr=ticket.mac_addr,
                nas_port_id=ticket.nas_port_id,
                billing_times=ticket.acct_session_time,
                input_total=self.get_input_total(),
                output_total=self.get_output_total(),
                start_source=STATUS_TYPE_UPDATE)
            self.add_online(online)

        self.billing(online)
        logger.info('%s Accounting update request, update online' %
                    self.account.account_number)
Beispiel #13
0
def load_handlers(handler_path=None, pkg_prefix=None, excludes=[]):
    _excludes = ['__init__', 'base', '.svn', '.DS_Store', 'views'] + excludes
    hds = set(os.path.splitext(it)[0] for it in os.listdir(handler_path))
    hds = [it for it in hds if it not in _excludes]
    for hd in hds:
        try:
            sub_module = os.path.join(handler_path, hd)
            if os.path.isdir(sub_module):
                # logger.info('load sub module %s' % hd)
                load_handlers(handler_path=sub_module,
                              pkg_prefix="{0}.{1}".format(pkg_prefix, hd),
                              excludes=excludes)

            _hd = "{0}.{1}".format(pkg_prefix, hd)
            # logger.info('load_module %s' % _hd)
            importlib.import_module(_hd)
        except Exception as err:
            logger.error("%s, skip module %s.%s" % (str(err), pkg_prefix, hd))
            import traceback
            traceback.print_exc()
            continue
Beispiel #14
0
def load_events(event_path=None,pkg_prefix=None,excludes=[],event_params={}):
    _excludes = ['__init__','settings'] + excludes
    evs = set(os.path.splitext(it)[0] for it in os.listdir(event_path))
    evs = [it for it in evs if it not in _excludes]
    for ev in evs:
        try:
            sub_module = os.path.join(event_path, ev)
            if os.path.isdir(sub_module):
                # logger.info('load sub event %s' % ev)
                load_events(
                    event_path=sub_module,
                    pkg_prefix="{0}.{1}".format(pkg_prefix, ev),
                    excludes=excludes,
                    event_params=event_params,
                )
            _ev = "{0}.{1}".format(pkg_prefix, ev)
            # logger.info('load_event %s with params:%s' % (_ev,repr(event_params)))
            dispatch.register(importlib.import_module(_ev).__call__(**event_params))
        except Exception as err:
            logger.error("%s, skip module %s.%s" % (str(err),pkg_prefix,ev))
            import traceback
            traceback.print_exc()
            continue
Beispiel #15
0
def load_handlers(handler_path=None, pkg_prefix=None, excludes=[]):
    _excludes = ['__init__', 'base', '.svn', '.DS_Store', 'views'] + excludes
    hds = set(os.path.splitext(it)[0] for it in os.listdir(handler_path))
    hds = [it for it in hds if it not in _excludes]
    for hd in hds:
        try:
            sub_module = os.path.join(handler_path, hd)
            if os.path.isdir(sub_module):
                # logger.info('load sub module %s' % hd)
                load_handlers(
                    handler_path=sub_module,
                    pkg_prefix="{0}.{1}".format(pkg_prefix, hd),
                    excludes=excludes
                )

            _hd = "{0}.{1}".format(pkg_prefix, hd)
            # logger.info('load_module %s' % _hd)
            importlib.import_module(_hd)
        except Exception as err:
            logger.error("%s, skip module %s.%s" % (str(err),pkg_prefix,hd))
            import traceback
            traceback.print_exc()
            continue
Beispiel #16
0
    def process(self, *args, **kwargs):
        with make_db(self.db) as db:
            try:
                nodes = db.query(models.TrNode)
                for node in nodes:
                    online_count = db.query(models.TrOnline.id).filter(
                        models.TrOnline.account_number ==
                        models.TrAccount.account_number,
                        models.TrAccount.customer_id ==
                        models.TrCustomer.customer_id,
                        models.TrCustomer.node_id == node.id).count()
                    stat = models.TrOnlineStat()
                    stat.node_id = node.id
                    stat.stat_time = int(time.time())
                    stat.total = online_count
                    db.add(stat)
                db.commit()
                logger.info("online stat task done")
            except Exception as err:
                db.rollback()
                logger.error('online_stat_job err,%s' % (str(err)))

        return 120.0
Beispiel #17
0
    def acctounting(self):
        if self.is_online(self.request.nas_addr, self.request.acct_session_id):
            return logger.error('online %s is exists' %
                                self.request.acct_session_id)

        if not self.account:
            return logger.error('user %s not exists' %
                                self.request.account_number)

        online = Storage(account_number=self.request.account_number,
                         nas_addr=self.request.nas_addr,
                         acct_session_id=self.request.acct_session_id,
                         acct_start_time=datetime.datetime.now().strftime(
                             "%Y-%m-%d %H:%M:%S"),
                         framed_ipaddr=self.request.framed_ipaddr,
                         mac_addr=self.request.mac_addr,
                         nas_port_id=self.request.nas_port_id,
                         billing_times=0,
                         input_total=0,
                         output_total=0,
                         start_source=STATUS_TYPE_START)
        self.add_online(online)
        logger.info('%s Accounting start request, add new online' %
                    online.account_number)
Beispiel #18
0
    def processAuth(self, datagram, host, port):
        try:
            bas = self.find_nas(host)
            if not bas:
                raise PacketError(
                    '[Radiusd] :: Dropping packet from unknown host %s' % host)

            secret, vendor_id = bas['bas_secret'], bas['vendor_id']
            req = self.createAuthPacket(packet=datagram,
                                        dict=self.dict,
                                        secret=six.b(str(secret)),
                                        vendor_id=vendor_id)

            self.do_stat(req.code)

            logger.info("[Radiusd] :: Received radius request: %s" %
                        (repr(req)))
            if self.config.system.debug:
                logger.debug(req.format_str())

            if req.code != packet.AccessRequest:
                raise PacketError(
                    'non-AccessRequest packet on authentication socket')

            reply = req.CreateReply()
            reply.vendor_id = req.vendor_id

            aaa_request = dict(account_number=req.get_user_name(),
                               domain=req.get_domain(),
                               macaddr=req.client_mac,
                               nasaddr=req.get_nas_addr(),
                               vlanid1=req.vlanid1,
                               vlanid2=req.vlanid2)

            auth_resp = RadiusAuth(self.db_engine, self.mcache, self.aes,
                                   aaa_request).authorize()

            if auth_resp['code'] > 0:
                reply['Reply-Message'] = auth_resp['msg']
                reply.code = packet.AccessReject
                return reply

            if 'bypass' in auth_resp and int(auth_resp['bypass']) == 0:
                is_pwd_ok = True
            else:
                is_pwd_ok = req.is_valid_pwd(auth_resp.get('passwd'))

            if not is_pwd_ok:
                reply['Reply-Message'] = "password not match"
                reply.code = packet.AccessReject
                return reply
            else:
                if u"input_rate" in auth_resp and u"output_rate" in auth_resp:
                    reply = rate_process.process(
                        reply,
                        input_rate=auth_resp['input_rate'],
                        output_rate=auth_resp['output_rate'])

                attrs = auth_resp.get("attrs") or {}
                for attr_name in attrs:
                    try:
                        # todo: May have a type matching problem
                        reply.AddAttribute(utils.safestr(attr_name),
                                           attrs[attr_name])
                    except Exception as err:
                        errstr = "RadiusError:current radius cannot support attribute {0},{1}".format(
                            attr_name, utils.safestr(err.message))
                        logger.error(errstr)

                for attr, attr_val in req.resp_attrs.iteritems():
                    reply[attr] = attr_val

            reply['Reply-Message'] = 'success!'
            reply.code = packet.AccessAccept
            if not req.VerifyReply(reply):
                raise PacketError('VerifyReply error')
            return reply
        except Exception as err:
            self.do_stat(0)
            errstr = 'RadiusError:Dropping invalid auth packet from {0} {1},{2}'.format(
                host, port, utils.safeunicode(err))
            logger.error(errstr)
            import traceback
            traceback.print_exc()