opensips.cfg 9.66 KB
####### Global Parameters #########

log_level=3
log_stderror=no
log_facility=LOG_LOCAL0

children=6

/* uncomment the following lines to enable debugging */
#debug_mode=yes

/* uncomment the next line to enable the auto temporary blacklisting of 
   not available destinations (default disabled) */
#disable_dns_blacklist=no

/* uncomment the next line to enable IPv6 lookup after IPv4 dns 
   lookup failures (default disabled) */
#dns_try_ipv6=yes

/* comment the next line to enable the auto discovery of local aliases
   based on revers DNS on IPs */
auto_aliases=no

listen=udp:172.31.1.130:5060   # CUSTOMIZE ME

####### Modules Section ########

#set module path
mpath="/usr/local/lib64/opensips/modules"

#### SIGNALING module
loadmodule "signaling.so"

#### StateLess module
loadmodule "sl.so"

#### Transaction Module
loadmodule "tm.so"
modparam("tm", "fr_timeout", 5)
modparam("tm", "fr_inv_timeout", 30)
modparam("tm", "restart_fr_on_each_reply", 0)
modparam("tm", "onreply_avp_mode", 1)

#### Record Route Module
loadmodule "rr.so"
modparam("rr", "append_fromtag", 1)

#### MAX ForWarD module
loadmodule "maxfwd.so"

#### SIP MSG OPerationS module
loadmodule "sipmsgops.so"

#### FIFO Management Interface
loadmodule "mi_fifo.so"
modparam("mi_fifo", "fifo_name", "/tmp/opensips_fifo")
modparam("mi_fifo", "fifo_mode", 0666)

#### URI module
loadmodule "uri.so"
modparam("uri", "use_uri_table", 0)

#### USeR LOCation module
loadmodule "usrloc.so"
modparam("usrloc", "nat_bflag", "NAT")
#modparam("usrloc", "db_mode",   0)

#### REGISTRAR module
loadmodule "registrar.so"
modparam("registrar", "tcp_persistent_flag", "TCP_PERSISTENT")
/* uncomment the next line not to allow more than 10 contacts per AOR */
#modparam("registrar", "max_contacts", 10)

#### ACCounting module
loadmodule "acc.so"
/* what special events should be accounted ? */
modparam("acc", "early_media", 0)
modparam("acc", "report_cancels", 0)
/* by default we do not adjust the direct of the sequential requests.
   if you enable this parameter, be sure the enable "append_fromtag"
   in "rr" module */
modparam("acc", "detect_direction", 0)

loadmodule "proto_udp.so"

####################################################
#loadmodule "nathelper.so"
#modparam("nathelper","natping_interval", 15)
#modparam("nathelper", "received_avp", "$avp(received)")

#loadmodule "nat_traversal.so"
loadmodule "db_postgres.so"
loadmodule "avpops.so"
modparam("avpops", "db_url", "postgres://opensips:opensips@172.31.1.130/opensips")

loadmodule "dialog.so"
#modparam("dialog", "db_mode", 1)
#modparam("dialog", "db_url", "postgres://opensips:opensips@172.31.1.130/opensips")
modparam("dialog", "hash_size", 32768)
#modparam("dialog", "hash_size", 16384)
modparam("dialog", "log_profile_hash_size", 20)
modparam("dialog", "default_timeout", 3600)


#loadmodule "dispatcher.so"
#modparam("dispatcher", "db_url", "postgres://opensips:opensips@172.31.1.130/opensips")
#modparam("dispatcher", "ds_ping_method", "OPTIONS")
#modparam("dispatcher", "ds_ping_interval", 5)
#modparam("dispatcher", "ds_probing_threshhold", 2)
#modparam("dispatcher", "ds_probing_mode", 0)
#modparam("dispatcher", "ds_probing_mode", 1)

loadmodule "drouting.so"
modparam("drouting", "db_url", "postgres://opensips:opensips@172.31.1.130/opensips")
modparam("drouting", "use_domain", 0)
#modparam("drouting", "default_group", 1)

loadmodule "load_balancer.so"
modparam("load_balancer", "db_url", "postgres://opensips:opensips@172.31.1.130/opensips")

#loadmodule "cachedb_local.so"
#modparam("cachedb_local", "cachedb_url", "local://")
#modparam("cachedb_local", "cache_collections", "default=9")
#modparam("cachedb_local", "cache_clean_period", 1200)
####################################################
# main request routing logic

route{
    xlog("L_INFO", "main route start|rm:$rm|du:$du|ru:$ru|fu:$fu|si:$si|sp:$sp|\n");
    # kill any requests that we do not handle
    if (!is_method("INVITE|ACK|BYE|CANCEL|REFER|OPTIONS|INFO")) {
        sl_send_reply("405","Method Not Allowed");
        exit;
    }

    if (!mf_process_maxfwd_header("10")) {
        send_reply("483","Too Many Hops");
        exit;
    }

    if (is_method("OPTIONS") && is_myself("$rd")) {
        sl_send_reply("200","NAT Ping");
        exit;
    }

    if (!has_totag()) {
        record_route();
    } else {
        xlog("L_INFO", "has to tag|rs:$rs|rm:$rm|du:$du|ru:$ru|fu:$fu|si:$si|sp:$sp|\n");
        loose_route();
        if (!t_relay()) {
            sl_reply_error();
        }
        exit;
    }

    if (is_method("INVITE")) {
        if (lb_is_destination("$si", "$sp", "6")) {
            xlog("L_INFO", "rm:$rm|originated from vos sources $si \n");
            #测试
			route(cti);
			route(verto);
        } else {
            xlog("L_INFO", "rm:$rm|originated from cti freeswitch sources $si \n");
            if ($(ru{uri.user}) =~ '^r4\S{11}$') {
                route(kxjl);
            } else if ($(ru{uri.user}) =~ '^r2\S{11}$') {
                route(lingban);
            } else if ($(ru{uri.user}) =~ '^r5\S{11}$') {
                route(jths);
            } else if ($(ru{uri.user}) =~ '^[01]\S{10}$') {
                route(vos);
            } else if ($(ru{uri.user}) =~ '^(2\S{9})$') {
                route(verto);
            } else {
                xlog("L_ERR", "ERROR|ru:$ru|destnation invalid\n");
                send_reply("481","destnation invalid");
                exit;
            }
        }
    }
    exit;
}

route[kxjl] {
    xlog("L_INFO", "ru:$ru|route to kxjl_AI\n");
    if (lb_start("2","pstn")) {
        t_relay();
        xlog("L_INFO", "ru:$ru|du:$du|route to kxjl_AI success\n");
        exit;
    }
    #warning
    exit;
}

route[jths] {
    xlog("L_INFO", "ru:$ru|route to jths_AI\n");
    if (lb_start("3","pstn")) {
        t_relay();
        xlog("L_INFO", "ru:$ru|du:$du|route to jths_AI success\n");
        exit;
    }
    #warning
    exit;
}

route[lingban] {
    xlog("L_INFO", "ru:$ru|route to lingban_AI\n");
    if (lb_start("4","pstn")) {
        t_relay();
        xlog("L_INFO", "ru:$ru|du:$du|route to lingban_AI success\n");
        exit;
    }
    #warning
    exit;
}

route[vos] {
    xlog("L_INFO", "ru:$ru|route to VOS\n");
    t_on_reply("vos_reply");
    if (!is_present_hf("X-cti_tenantid")) {
        #xlog("L_ERR", "ERROR:get tenantid from sip header X-cti_tenantid failed\n");
        #send_reply("401","user Unauthorized");
        #exit;
    }

    xlog("L_INFO", "get tenantid from sip header X-cti_tenantid:$(hdr(X-cti_tenantid))\n");
    $var(tenantid) = $(hdr(X-cti_tenantid));
    $var(tenantid) = "100001";

    if (avp_db_query("select gwlist from tenant_gw where tenantid='$var(tenantid)'","$avp(gwlist)")) {
        xlog("L_INFO", "use tenantid gwlist:$avp(gwlist) from table tenant_gw\n");
        if (avp_db_query("select caller_number from gw_caller_number where gw_attr_id='$var(tenantid)' order by random() limit 1","$avp(caller_number)")) {
            xlog("L_INFO", "use gw_id:$var(gw_id) get caller_number:$avp(caller_number) from table gw_caller_number\n");
            if(is_present_hf("Remote-Party-ID")) {
                $var(Remote-Party-ID) = $(hdr(Remote-Party-ID));
                $var(remote_url) = $(var(Remote-Party-ID){nameaddr.uri});
                $var(rp_remote_url) = "sip:" + $avp(caller_number) + "@" + $(var(remote_url){uri.domain});
                if ($(var(remote_url){uri.port})) {
                    $var(rp_remote_url) = $var(rp_remote_url) + ":" + $(var(remote_url){uri.port});
                }
                $var(remote_pama) = $(var(Remote-Party-ID){nameaddr.params});
                remove_hf("Remote-Party-ID");
                append_hf("Remote-Party-ID: \"$avp(caller_number)\" <$var(rp_remote_url)>;$var(remote_pama)\r\n","X-FS-Support");
            } else {
                $var(rp_remote_url) = "sip:" + $avp(caller_number) + "@" + $(fu{uri.domain});
                if ($(fu{uri.port})) {
                    $var(rp_remote_url) = $var(rp_remote_url) + ":" + $(fu{uri.port});
                }
                append_hf("Remote-Party-ID: \"$avp(caller_number)\" <$var(rp_remote_url)>;party=calling;screen=yes;privacy=off\r\n","X-FS-Support");
            }
        } else {
            xlog("L_INFO", "ERROR:use gw_id get caller_number by random from table gw_caller_number failed\n");
            exit;
        }
    } else {
        xlog("L_INFO", "ERROR:use tenantid get caller_number:$avp(caller_number) gwlist:$avp(gwlist) from table tenant_attr\n");
        send_reply("401","user Unauthorized");
        exit;
    }

    if (route_to_gw("$avp(gwlist)","$var(gw_id)")) {
        if(lb_start("$(var(gw_id){s.int})","pstn")){
            t_relay();
            xlog("L_INFO", "ru:$ru|du:$du|fu:$fu|route to VOS success\n");
            exit;
        }
    }
}

route[verto] {
    xlog("L_INFO", "ru:$ru|route to webrtc freeswitch\n");
    if (lb_start("10","pstn")) {
        t_relay();
        xlog("L_INFO", "ru:$ru|du:$du|route to webrtc freeswitch success\n");
        exit;
    }
    #warning
    exit;
}

route[cti] {
    xlog("L_INFO", "ru:$ru|route to cti freeswitch\n");
    if (lb_start("1","pstn")) {
        t_relay();
        xlog("L_INFO", "ru:$ru|du:$du|route to freeswitch success\n");
        exit;
    }
    #warning
    exit;
}

onreply_route[vos_reply] {
    xlog("L_INFO", "VOS return status:$rs onreply\n");
    if (t_check_status("[56][0-9][0-9]") || (t_check_status("408") && t_local_replied("all"))) {
        route(vos_next);
    }
    exit;
}

route[vos_next] {
    xlog("L_INFO", "ru:$ru|vos next\n");
    t_on_reply("vos_reply");
    if (use_next_gw(,"$var(gw_next_id)")) {
        if(lb_start("$(var(gw_next_id){s.int})","pstn")) {
            t_relay();
            xlog("L_INFO", "ru:$ru|du:$du|fu:$fu|route to next VOS success\n");
            exit;
        }
    }
}