opensips(三):load_balancer与NAT

一、opensips.cfg文件

####### Global Parameters #########

log_level=4
log_stderror=no
log_facility=LOG_LOCAL0

children=4

/* uncomment the following lines to enable debugging */
debug_mode=no

/* 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=yes


listen=udp:192.168.1.100:5060 as 172.29.50.9:5060
listen=tcp:192.168.1.100:5060 as 172.29.50.9:5060
listen=ws:192.168.1.100:8090


 

####### 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"
/* do not append from tag to the RR (no need for this script) */
modparam("rr", "append_fromtag", 0)

#### 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)


loadmodule "proto_udp.so"
loadmodule "proto_tcp.so"
loadmodule "proto_ws.so"
modparam("proto_ws","ws_port",8080)
modparam("proto_ws","ws_send_timeout",20000) 
 
#### MYSQL module
loadmodule "db_mysql.so"



#### USeR LOCation module
loadmodule "usrloc.so"
modparam("usrloc", "nat_bflag", "NAT")
modparam("usrloc", "db_mode",   2)
modparam("usrloc", "db_url",
    "mysql://root:123456@localhost/opensips") # CUSTOMIZE ME


#### REGISTRAR module
loadmodule "registrar.so"
modparam("registrar", "tcp_persistent_flag", "TCP_PERSISTENT")
modparam("registrar", "received_avp", "$avp(received_nh)")
/* 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)
modparam("acc", "db_url",
    "mysql://root:123456@localhost/opensips") # CUSTOMIZE ME


#### AUTHentication modules
loadmodule "auth.so"
loadmodule "auth_db.so"
modparam("auth_db", "calculate_ha1", yes)
modparam("auth_db", "password_column", "password")
modparam("auth_db|uri", "db_url",
    "mysql://root:123456@localhost/opensips") # CUSTOMIZE ME
modparam("auth_db", "load_credentials", "")


#### ALIAS module
loadmodule "alias_db.so"
modparam("alias_db", "db_url",
    "mysql://root:123456@localhost/opensips") # CUSTOMIZE ME






#### DIALOG module
loadmodule "dialog.so"
modparam("dialog", "dlg_match_mode", 1)
modparam("dialog", "default_timeout", 21600)  # 6 hours timeout
modparam("dialog", "db_mode", 2)
modparam("dialog", "db_url",
    "mysql://root:123456@localhost/opensips") # CUSTOMIZE ME


####  NAT modules
loadmodule "nathelper.so"
modparam("nathelper", "natping_interval", 10)
modparam("nathelper", "ping_nated_only", 1) 
modparam("nathelper", "received_avp", "$avp(received_nh)")
modparam("nathelper", "sipping_from", "sip:pinger@192.168.1.100") 
modparam("nathelper", "natping_interval", 30) 
modparam("nathelper", "sipping_bflag", "SIP_PING_FLAG") #OPTIONS ping

######################################################################
## Path Module Parameters
######################################################################
loadmodule "path.so"
modparam("path", "use_received", 1)

 
loadmodule "dispatcher.so"
modparam("dispatcher", "ds_ping_method", "OPTIONS") # CUSTOMIZE ME
modparam("dispatcher", "ds_ping_interval", 3) # CUSTOMIZE ME
modparam("dispatcher", "ds_probing_threshhold", 2) # CUSTOMIZE ME
modparam("dispatcher", "ds_probing_mode", 1) # CUSTOMIZE ME
modparam("dispatcher", "db_url", "mysql://root:123456@localhost/opensips") # CUSTOMIZE ME

loadmodule "load_balancer.so"
modparam("load_balancer", "db_url", "mysql://root:123456@localhost/opensips") # CUSTOMIZE ME
modparam("load_balancer", "probing_method", "OPTIONS")
modparam("load_balancer", "probing_interval", 3)


loadmodule "rtpproxy.so"
modparam("rtpproxy", "rtpproxy_sock", "udp:192.168.1.100:1111")
loadmodule "mediaproxy.so"
modparam("mediaproxy","disable", 0)
modparam("mediaproxy", "mediaproxy_socket", "/var/run/mediaproxy/dispatcher.sock")
modparam("mediaproxy", "mediaproxy_timeout", 1000)
modparam("mediaproxy", "signaling_ip_avp", "$avp(nat_ip)")
modparam("mediaproxy", "media_relay_avp", "$avp(media_relay)")
modparam("mediaproxy", "ice_candidate", "low-priority")


####### Routing Logic ########

#main request routing logic
route{ 
    force_rport();
    if (nat_uac_test("23")) {
        if (is_method("REGISTER")) {
            fix_nated_register();
            setbflag(NAT);
        }else {
            fix_nated_contact();
            setflag(NAT);
        }
    }
 
    if (!mf_process_maxfwd_header("10")) {
        sl_send_reply("483","Too Many Hops");
        exit;
    }  

    if (has_totag()) {
       if ( is_method("ACK") && t_check_trans() ) { 
           t_relay();
           exit;
       }
       if ( !loose_route() ) {
           send_reply("404","Not here");
           exit;
       }
       if ( $DLG_status!=NULL && !validate_dialog() ) {
           xlog("In-Dialog $rm from $si (callid=$ci) is not valid according to dialog
"); 
       }
       if (is_method("BYE")) {
           do_accounting("db","failed");
       }
       if (check_route_param("nat=yes")) 
           setflag(NAT); 
       route(relay);
       exit; 
    } 
    
    if (ds_is_in_list("$si", "", "1"))
    {
        route(relay);
        exit;
    } 
    
    # check if the clients are using WebSockets or WebSocketSecure
    
 
    # CANCEL processing
    if (is_method("CANCEL"))
    {
        if (t_check_trans())
            t_relay();
        exit;
    }
 
    t_check_trans(); 
    # preloaded route checking
    if (loose_route()) {
        xlog("L_ERR",
        "Attempt to route with preloaded Route's [$fu/$tu/$ru/$ci]");
        if (!is_method("ACK"))
            sl_send_reply("403","Preload Route denied");
        exit;
    }
    
    # record routing
    if (!is_method("REGISTER|MESSAGE"))
        record_route();

    if (is_method("INVITE")) {
        if ( !create_dialog("B") ) {
            send_reply("500","Internal Server Error");
            exit;
        }
        do_accounting("db");
    }
    
    if (!is_myself("$rd")) {
        append_hf("P-hint: outbound
");
        route(relay);
    }
    # requests for my domain
    
    if (is_method("PUBLISH|SUBSCRIBE"))
    {
        sl_send_reply("503", "Service Unavailable");
        exit;
    }

    if (is_method("REGISTER") )
    {    
        if (isflagset(NAT)) { 
            setbflag(SIP_PING_FLAG);
        }
        if (!save("location")){
            sl_reply_error();
        }

        if (!ds_select_dst("1", "0")) {
                send_reply("503","Service Unavailable");
                exit;
        } 
    }
    else if (is_method("INVITE")) {
        if (!load_balance("1","pstn")) {
            send_reply("503","Service Unavailable");
            exit;
        }
    } 
    if ($rU==NULL) { 
        send_reply("484","Address Incomplete");
        exit; 
    }
    alias_db_lookup("dbaliases");
    if (!lookup("location","m")) { 
        t_reply("404", "Not Found");
        exit;
    }
    if (isbflagset(NAT)) setflag(NAT);        
    do_accounting("db","missed");
    route(relay);
}
 
route[relay] {
    
    if (is_method("INVITE")) {
        if (isflagset(NAT)) {
            rtpproxy_offer("ro");
        }
        t_on_branch("per_branch_ops");
        t_on_reply("handle_nat");
        t_on_failure("missed_call");
    }
    if (isflagset(NAT)) {
        add_rr_param(";nat=yes");
    }
    if (!t_relay()) {
        send_reply("500","Internal Error");
    };
    exit;
}

branch_route[per_branch_ops] {
    xlog("new branch at $ru
");
}
 
 
onreply_route[handle_nat] { 
    if (nat_uac_test("1"))
        fix_nated_contact();
    if ( isflagset(NAT) )
        rtpproxy_answer("ro");
    xlog("incoming reply
");
}
 
 
failure_route[missed_call] {
    if (t_was_cancelled()) {
        exit;
    } 
}

local_route 
{    
     if (is_method("BYE") && $DLG_dir=="UPSTREAM") {
         acc_db_request("200 Dialog Timeout", "acc");
     }
}

二、数据库文件

# $Id$
#
# The OpenSIPS configuration file for the control tools.
#
# Here you can set variables used in the opensipsctl and opensipsdbctl setup
# scripts. Per default all variables here are commented out, the control tools
# will use their internal default values.

## your SIP domain
SIP_DOMAIN="zxr2016.com"

## chrooted directory
# $CHROOT_DIR="/path/to/chrooted/directory"

## database type: MYSQL, PGSQL, ORACLE, DB_BERKELEY, DBTEXT, or SQLITE
## by default none is loaded
# If you want to setup a database with opensipsdbctl, you must at least specify
# this parameter.
DBENGINE=MYSQL

## database port (PostgreSQL=5432 default; MYSQL=3306 default)
DBPORT=3306

## database host
DBHOST=localhost

## database name (for ORACLE this is TNS name)
DBNAME=opensips

# database path used by dbtext, db_berkeley, or sqlite
# DB_PATH="/usr/local/etc/opensips/dbtext"

## database read/write user
DBRWUSER=opensips

## password for database read/write user
DBRWPW="opensipsrw"

## engine type for the MySQL/MariaDB tabels (default InnoDB)
# MYSQL_ENGINE="MyISAM"

## database super user (for ORACLE this is 'scheme-creator' user
DBROOTUSER="root"

# user name column
# USERCOL="username"

## for testing / development
#ETCDIR=etc/

# SQL definitions
# If you change this definitions here, then you must change them 
# in db/schema/entities.xml too.
# FIXME

# FOREVER="2020-05-28 21:32:15"
# DEFAULT_ALIASES_EXPIRES=$FOREVER
# DEFAULT_Q="1.0"
# DEFAULT_CALLID="Default-Call-ID"
# DEFAULT_CSEQ="13"
# DEFAULT_LOCATION_EXPIRES=$FOREVER


# Program to calculate a message-digest fingerprint 
# MD5="md5sum"

# awk tool
# AWK="awk"

# gdb tool
# GDB="gdb"

# grep tool
# GREP="grep"

# sed tool
# SED="sed"


# Describe what additional tables to install. Valid values for the variables
# below are yes/no/ask. With ask (default) it will interactively ask the user
# for an answer, while yes/no allow for automated, unassisted installs.
#

# If to install tables for the modules in the EXTRA_MODULES variable.
# INSTALL_EXTRA_TABLES=ask

# If to install presence related tables.
# INSTALL_PRESENCE_TABLES=ask

# Define what module tables should be installed.
# If you use the postgres database and want to change the installed tables,
# then you must also adjust the STANDARD_TABLES or EXTRA_TABLES variable 
# accordingly in the opensipsdbctl.base script.

# opensips standard modules
# STANDARD_MODULES="standard acc domain group permissions usrloc 
#                   msilo alias_db uri_db speeddial avpops auth_db dialog 
#                   dispatcher dialplan drouting nathelper load_balancer"

# opensips extra modules
# EXTRA_MODULES="imc cpl siptrace domainpolicy carrierroute userblacklist 
#                 b2b registrant call_center fraud_detection cachedb_sql"


## type of aliases used: DB - database aliases
## - default: none
# ALIASES_TYPE="DB"

## control engine: FIFO or UNIXSOCK
## - default FIFO
CTLENGINE=FIFO

## path to FIFO file
OSIPS_FIFO="/tmp/opensips_fifo"

## MI_CONNECTOR control engine: FIFO, UNIXSOCK, UDP, XMLRPC
#MI_CONNECTOR=FIFO:/tmp/opensips_fifo
#MI_CONNECTOR=UNIXSOCK:/tmp/opensips.sock
#MI_CONNECTOR=UDP:192.168.67.253:8000
#MI_CONNECTOR=XMLRPC:192.168.67.253:8000

## check ACL names; default on (1); off (0)
# VERIFY_ACL=1

## ACL names - if VERIFY_ACL is set, only the ACL names from below list
## are accepted
# ACL_GROUPS="local ld int voicemail free-pstn"

## verbose - debug purposes - default '0'
# VERBOSE=1

## do (1) or don't (0) store plaintext passwords
## in the subscriber table - default '1'
# STORE_PLAINTEXT_PW=0

## do not display the output highlighted
# NOHLPRINT=1

## OPENSIPS START Options
## PID file path - default is: /var/run/opensips.pid
PID_FILE=/var/run/opensips.pid

## Extra start options - default is: not set
# example: start opensips with 64MB share memory: STARTOPTIONS="-m 64"
# STARTOPTIONS=

 三、参考脚本

原文地址:https://www.cnblogs.com/zhangxianrong/p/14325835.html