Пример #1
0
 def ksr_reply_route(self, msg):
     if KSR.is_method_in("IBR"):
         # (I)NVITE, (B)YE, (R)EGITER
         KSR.xlog.xinfo("Get reply $rs:$rr")
         self.append_uuid(KSR.pv.getw("$ci"))
     scode = KSR.pv.getw("$rs")
     if 100 <= scode or scode <= 299:
         self.ksr_route_natmanage()
     return 1
    def ksr_route_relay(self, msg):
        # enable additional event routes for forwarded requests
        # - serial forking, RTP relaying handling, a.s.o.
        if KSR.is_method_in("IBSU"):
            if KSR.tm.t_is_set("branch_route") < 0:
                KSR.tm.t_on_branch("ksr_branch_manage")

        if KSR.is_method_in("ISU"):
            if KSR.tm.t_is_set("onreply_route") < 0:
                KSR.tm.t_on_reply("ksr_onreply_manage")

        if KSR.is_INVITE():
            if KSR.tm.t_is_set("failure_route") < 0:
                KSR.tm.t_on_failure("ksr_failure_manage")

        if KSR.tm.t_relay() < 0:
            KSR.sl.sl_reply_error()

        return -255
    def ksr_route_relay(self, msg):
        # enable additional event routes for forwarded requests
        # - serial forking, RTP relaying handling, a.s.o.
        if KSR.is_method_in("IBSU") :
            if KSR.tm.t_is_set("branch_route")<0 :
                KSR.tm.t_on_branch("ksr_branch_manage");

        if KSR.is_method_in("ISU") :
            if KSR.tm.t_is_set("onreply_route")<0 :
                KSR.tm.t_on_reply("ksr_onreply_manage");

        if KSR.is_INVITE() :
            if KSR.tm.t_is_set("failure_route")<0 :
                KSR.tm.t_on_failure("ksr_failure_manage");

        if KSR.tm.t_relay()<0 :
            KSR.sl.sl_reply_error();

        return -255;
    def ksr_route_auth(self, msg):

        if not KSR.is_REGISTER():
            if KSR.permissions.allow_source_address(1) > 0:
                # source IP allowed
                return 1

        if KSR.is_REGISTER() or KSR.is_myself_furi():
            # authenticate requests
            if KSR.auth_db.auth_check(KSR.pv.get("$fd"), "subscriber", 1) < 0:
                KSR.auth.auth_challenge(KSR.pv.get("$fd"), 0)
                return -255

            # user authenticated - remove auth header
            if not KSR.is_method_in("RP"):
                KSR.auth.consume_credentials()

        # if caller is not local subscriber, then check if it calls
        # a local destination, otherwise deny, not an open relay here
        if (not KSR.is_myself_furi()) and (not KSR.is_myself_ruri()):
            KSR.sl.sl_send_reply(403, "Not relaying")
            return -255

        return 1
    def ksr_route_auth(self, msg):

        if not KSR.is_REGISTER() :
            if KSR.permissions.allow_source_address(1)>0 :
                # source IP allowed
                return 1;

        if KSR.is_REGISTER() or KSR.is_myself_furi() :
            # authenticate requests
            if KSR.auth_db.auth_check(KSR.pv.get("$fd"), "subscriber", 1)<0 :
                KSR.auth.auth_challenge(KSR.pv.get("$fd"), 0);
                return -255;

            # user authenticated - remove auth header
            if not KSR.is_method_in("RP") :
                KSR.auth.consume_credentials();

        # if caller is not local subscriber, then check if it calls
        # a local destination, otherwise deny, not an open relay here
        if (not KSR.is_myself_furi()) and (not KSR.is_myself_ruri()) :
            KSR.sl.sl_send_reply(403,"Not relaying");
            return -255;

        return 1;
    def ksr_request_route(self, msg):
        # KSR.info("===== request - from kamailio python script\n");
        # KSR.info("===== method [%s] r-uri [%s]\n" % (KSR.pv.get("$rm"),KSR.pv.get("$ru")));

        # per request initial checks
        if self.ksr_route_reqinit(msg) == -255:
            return 1

        # NAT detection
        if self.ksr_route_natdetect(msg) == -255:
            return 1

        # CANCEL processing
        if KSR.is_CANCEL():
            if KSR.tm.t_check_trans() > 0:
                self.ksr_route_relay(msg)
            return 1

        # handle requests within SIP dialogs
        if self.ksr_route_withindlg(msg) == -255:
            return 1

        # -- only initial requests (no To tag)

        # handle retransmissions
        if KSR.tmx.t_precheck_trans() > 0:
            KSR.tm.t_check_trans()
            return 1

        if KSR.tm.t_check_trans() == 0:
            return 1

        # authentication
        if self.ksr_route_auth(msg) == -255:
            return 1

        # record routing for dialog forming requests (in case they are routed)
        # - remove preloaded route headers
        KSR.hdr.remove("Route")
        if KSR.is_method_in("IS"):
            KSR.rr.record_route()

        # account only INVITEs
        if KSR.pv.get("$rm") == "INVITE":
            KSR.setflag(FLT_ACC)
            # do accounting

        # dispatch requests to foreign domains
        if self.ksr_route_sipout(msg) == -255:
            return 1

        # # requests for my local domains

        # handle registrations
        if self.ksr_route_registrar(msg) == -255:
            return 1

        if KSR.corex.has_ruri_user() < 0:
            # request with no Username in RURI
            KSR.sl.sl_send_reply(484, "Address Incomplete")
            return 1

        # user location service
        self.ksr_route_location(msg)

        return 1
    def ksr_request_route(self, msg):
        # KSR.info("===== request - from kamailio python script\n");
        # KSR.info("===== method [%s] r-uri [%s]\n" % (KSR.pv.get("$rm"),KSR.pv.get("$ru")));

        # per request initial checks
        if self.ksr_route_reqinit(msg)==-255 :
            return 1;

        # NAT detection
        if self.ksr_route_natdetect(msg)==-255 :
            return 1;

        # CANCEL processing
        if KSR.is_CANCEL() :
            if KSR.tm.t_check_trans()>0 :
                self.ksr_route_relay(msg);
            return 1;

        # handle requests within SIP dialogs
        if self.ksr_route_withindlg(msg)==-255 :
            return 1;

        # -- only initial requests (no To tag)

        # handle retransmissions
        if KSR.tmx.t_precheck_trans()>0 :
            KSR.tm.t_check_trans();
            return 1;

        if KSR.tm.t_check_trans()==0 :
            return 1;

        # authentication
        if self.ksr_route_auth(msg)==-255 :
            return 1

        # record routing for dialog forming requests (in case they are routed)
        # - remove preloaded route headers
        KSR.hdr.remove("Route");
        if KSR.is_method_in("IS") :
            KSR.rr.record_route();


        # account only INVITEs
        if KSR.pv.get("$rm")=="INVITE" :
            KSR.setflag(FLT_ACC); # do accounting


        # dispatch requests to foreign domains
        if self.ksr_route_sipout(msg)==-255 :
            return 1;

        # # requests for my local domains

        # handle registrations
        if self.ksr_route_registrar(msg)==-255 :
            return 1;

        if KSR.corex.has_ruri_user() < 0 :
            # request with no Username in RURI
            KSR.sl.sl_send_reply(484,"Address Incomplete");
            return 1;


        # user location service
        self.ksr_route_location(msg);

        return 1;
Пример #8
0
def ksr_request_route(self, msg):
    # per request initial checks
    if self.ksr_route_reqinit() == -255:
        return 1

    # NAT detection
    if self.ksr_route_natdetect() == -255:
        return 1

    # CANCEL processing
    if KSR.is_CANCEL():
        self.manage_call_status(KSR.pv.getw("$fU"), 'del')
        KSR.xlog.xinfo("CANCELing request from $fU:$si")
        if KSR.tm.t_check_trans() > 0:
            self.ksr_route_relay()
        return 1

    if KSR.is_BYE():
        self.manage_call_status(KSR.pv.getw("$fU"), 'del')

    # handle requests within SIP dialogs
    if self.ksr_route_withindlg() == -255:
        return 1

    # handle retransmissions
    if (not KSR.is_ACK()) and (KSR.tmx.t_precheck_trans() > 0):
        KSR.tm.t_check_trans()
        return 1

    if KSR.tm.t_check_trans() == 0:
        return 1

    ### only initial requests (no To tag)

    # record routing for dialog forming requests (in case they are routed)
    # - remove preloaded route headers
    KSR.hdr.remove("Route")
    #if KSR.is_method_in("IS") :
    #    KSR.rr.record_route()

    if KSR.is_INVITE() and KSR.pv.is_null("$rU"):
        KSR.sl.sl_send_reply(484, "Address Incomplete")
        return 1

    if KSR.is_method_in("IR"):
        #r = redis.StrictRedis(host='127.0.0.1', port=6379, db=10, password='******')
        r = redis.StrictRedis(host='127.0.0.1', port=6379, db=10, decode_responses=True)
        if r.get("{}:{}".format(KSR.pv.getw("$fU"), KSR.pv.getw("$rU"))) or r.get(KSR.pv.getw("$rU")) or r.get(KSR.pv.getw("$tU")):
            KSR.sl.sl_send_reply(503, "There is no money.")
            return -255
        if KSR.is_INVITE():
            if (KSR.dispatcher.ds_is_from_list(self.DSIDS['MEDIASERVERS']) > 0 or KSR.dispatcher.ds_is_from_list(self.DSIDS['CALLERS']) > 0):
                KSR.setflag(self.FLAGS['FLT_FROM_ASTERISK'])
                KSR.setflag(self.FLAGS['FLT_SKIP_AUTH'])
            if (KSR.dispatcher.ds_is_from_list(self.DSIDS['GW']) > 0):
                KSR.setflag(self.FLAGS['FLT_FROM_GW'])
                KSR.setflag(self.FLAGS['FLT_SKIP_AUTH'])
        if self.GLOBALS['WITH_ASYNC_FRAMEWORK']:
            KSR.asynk.task_route("ksr_route_async_auth")
        else:
            if self.ksr_route_async_auth(msg) == -255:
                return 1
    else:
        KSR.xlog.xerr("Запрос $ru от $fu :: $si дошел до конца request_route и не был обработан")
        KSR.sl.sl_send_reply("404", "Out of order")
    return 1