文章目录
- 1. OpenSIPS 源码下载
- 2. 工具准备
- 3. 编译安装
- 4. opensips-cli 工具安装
- 5. 启动 OpenSIPS 实例
1. OpenSIPS 源码下载
使用以下命令即可下载 OpenSIPS 的源码,笔者下载的是比较稳定的 3.1 版本,读者有兴趣也可前往 官方传送门
sudo git clone https://github.com/OpenSIPS/opensips.git -b 3.1 opensips-3.1
2. 工具准备
在本地机器上编译 OpenSIPS 源码可能会有许多工具缺失,根据笔者的经验可以先执行以下命令下载相关工具。如果在编译过程中出现类似 make : flex: Command not found
这种报错,一般是本地缺少对应的软件或者依赖,直接按照提示使用 yum 命令下载相应工具即可,如本例可使用 yum install -y flex
命令
sudo yum install -y mysql-devel libmicrohttpd-devel bison flex
3. 编译安装
-
进入 OpenSIPS 源码根目录,执行
sudo make menuconfig
命令,则如下进入编译时配置界面,该界面使用右箭头进入下一级,左箭头返回上一级,空格选中 -
笔者进入菜单
Configure Compile Options
-->Configure Excluded Modules
空格勾选db_mysql
和httpd
,然后返回Configure Compile Options
-->Save Changes
回车保存配置。最后返回上一级菜单,回车选中Compile And Install OpenSIPS
开始编译安装 OpenSIPS -
编译完成后会回到菜单界面,回车选中
Exit & Save All Changes
即可完成安装。由于笔者指定了 mysql 作为 OpenSIPS 的数据库,则需要使用 opensips-cli 工具初始化数据库后才能启动 OpenSIPS 实例
4. opensips-cli 工具安装
-
该工具需要匹配 OpenSIPS 版本,读者可前往 官方传送门 选择相应的参数,最终确定一个命令并终端执行。笔者是 CentOS 7 系统的 3.1 版本 OpenSIPS,故执行以下命令即可
sudo yum install https://yum.opensips.org/3.1/releases/el/7/x86_64/opensips-yum-releases-3.1-6.el7.noarch.rpm
-
以上命令执行完成,执行下面的命令安装 opensips-cli 工具
sudo yum install -y opensips-cli
-
安装完成后,执行命令
vim ~/.opensips-cli.cfg
在当前用户目录下本地新增一个 opensips-cli 配置文件,读者可复制以下内容,但需要自行配置关键的数据库连接串信息[default] log_level: WARNING prompt_name: opensips-cli prompt_intro: Welcome to OpenSIPS Command Line Interface! prompt_emptyline_repeat_cmd: False history_file: ~/.opensips-cli.history history_file_size: 1000 output_type: pretty-print fifo_file: /tmp/opensips_fifo communication_type: http #database_modules: acc clusterer dialog dialplan dispatcher domain rtpproxy usrloc database_modules: ALL # 管理员账号连接串,自行修改 database_admin_url: mysql://root:123456@localhost:3306 # 普通账户连接串,自行修改 database_url: mysql://root:123456@localhost:3306 database_name: opensips # 修改成 opensips 源码路径下的scripts database_schema_path: /home/root/opensips-3.1/scripts
-
以上步骤执行完,执行命令
opensips-cli -x database create
创建 OpenSIPS 依赖的数据库表 -
OpenSIPS 实例启动后,可以通过执行
opensips-cli -i default
连接到 OpenSIPS,在命令界面查看 OpenSIPS 实例信息,读者如有兴趣可前往 官方传送门 了解支持的命令
5. 启动 OpenSIPS 实例
-
经过以上步骤,OpenSIPS 依赖的数据库表已经建好,接下来使用命令
vim ~/opensips.cfg
在当前用户目录创建 OpenSIPS 配置文件,读者可以复制以下内容,自行修改数据库连接及监听 IP 端口等内容####### Global Parameters ######### log_level=4 log_stderror=no log_facility=LOG_LOCAL0 /* 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 reverse DNS on IPs */ auto_aliases=no mhomed=yes #alias=35.187.154.207 listen=udp:127.0.0.1:8060 # CUSTOMIZE ME socket=ws:127.0.0.1:8090 # CUSTOMIZE ME ####### Modules Section ######## #set module path mpath="/usr/local//lib64/opensips/modules/" #### udp protocol module loadmodule "proto_udp.so" loadmodule "proto_ws.so" modparam("proto_ws", "ws_port", 8090) #### 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) modparam("rr", "enable_double_rr", 1) #### MAX ForWarD module loadmodule "maxfwd.so" #### SIP MSG OPerationS module loadmodule "sipmsgops.so" #### MYSQL module loadmodule "db_mysql.so" #### drouting module loadmodule "drouting.so" modparam("drouting", "db_url", "mysql://root:123456@localhost:3306/opensips") modparam("drouting", "default_group", 1) loadmodule "httpd.so" modparam("httpd", "port", 8888) loadmodule "mi_fifo.so" loadmodule "mi_http.so" loadmodule "acc.so" modparam("acc", "early_media", 0) modparam("acc", "report_cancels", 0) modparam("acc", "detect_direction", 0) modparam("acc", "db_url", "mysql://root:123456@localhost:3306/opensips") # CUSTOMIZE ME #### USeR LOCation module loadmodule "usrloc.so" modparam("usrloc", "nat_bflag", "NAT") modparam("usrloc", "db_mode", 2) modparam("usrloc", "db_url", "mysql://root:123456@localhost:3306/opensips") # CUSTOMIZE ME #### REGISTRAR module loadmodule "registrar.so" #### AUTHentication modules loadmodule "auth.so" loadmodule "auth_db.so" modparam("auth_db", "calculate_ha1", 1) modparam("auth_db|uri", "db_url", "mysql://root:123456@localhost:3306/opensips") # CUSTOMIZE ME modparam("auth_db", "load_credentials", "") #### Nathelper protocol loadmodule "nathelper.so" modparam("registrar|nathelper", "received_avp", "$avp(rcv)") #### DIALOG module loadmodule "dialog.so" modparam("dialog", "dlg_match_mode", 1) modparam("dialog", "default_timeout", 21600) # 6 hours timeout modparam("dialog", "db_mode", 1) modparam("dialog", "db_url", "mysql://root:123456@localhost:3306/opensips") # CUSTOMIZE ME # main request routing logic route{ if (!mf_process_maxfwd_header(10)) { send_reply(483,"Too Many Hops"); exit; } # 有 totag 意味着dialog已经建立 if (has_totag()) { # handle hop-by-hop ACK (no routing required) if ( is_method("ACK") && t_check_trans() ) { t_relay(); exit; } # sequential request within a dialog should # take the path determined by record-routing if ( !loose_route() ) { # we do record-routing for all our traffic, so we should not # receive any sequential requests without Route hdr. send_reply(404,"Not here"); exit; } #loose_route(); if (is_method("BYE")) { # do accounting even if the transaction failed do_accounting("db","failed"); } # 这里lookup是为了转成注册到opensips 的ua对应r-uri lookup("location"); # route it out to whatever destination was set by loose_route() # in $du (destination URI). route(relay); exit; } # handle cancel and re-transmissions if ( is_method("CANCEL") ) { if ( t_check_trans() ) t_relay(); exit; } # absorb retransmissions, but do not create transaction t_check_trans(); if ( !(is_method("REGISTER") ) ) { if (!is_from_gw(1)) { # authenticate if from local subscriber # authenticate all initial non-REGISTER request that pretend to be # generated by local subscriber (domain from FROM URI is local) if (!proxy_authorize("", "subscriber")) { proxy_challenge(""); exit; } consume_credentials(); # caller authenticated } } # preloaded route checking if (loose_route()) { xlog("L_ERR", "Attempt to route with preloaded Route's [$fu/$tu/$ru/$ci]"); if (!is_method("ACK")) send_reply(403,"Preload Route denied"); exit; } # record routing if (!is_method("REGISTER|MESSAGE")) record_route(); # account only INVITEs if (is_method("INVITE")) { # create dialog with timeout if ( !create_dialog("B") ) { send_reply(500,"Internal Server Error"); exit; } # 通过动态路由找到合适的du if (!do_routing()) { if (!lookup("location")) { send_reply(404,"Not Found"); exit; } } #$du = "sip:0.0.0.0:8060"; do_accounting("db"); } if (is_method("REGISTER")) { # authenticate the REGISTER requests if (!www_authorize("", "subscriber")) { www_challenge(""); exit; } fix_nated_register(); setbflag("NAT"); if (!save("location")) sl_reply_error(); exit; } # requests for my domain if (is_method("PUBLISH|SUBSCRIBE")) { send_reply(503, "Service Unavailable"); exit; } if ($rU==NULL) { # request with no Username in RURI send_reply(484,"Address Incomplete"); exit; } do_accounting("db","missed"); route(relay); } route[relay] { # for INVITEs enable some additional helper routes if (is_method("INVITE")) { rtpproxy_offer(); t_on_branch("per_branch_ops"); t_on_reply("handle_nat"); t_on_failure("missed_call"); } if (!t_relay()) { send_reply(500,"Internal Error"); } exit; } branch_route[per_branch_ops] { xlog("new branch at $ru\n"); } onreply_route[handle_nat] { rtpproxy_answer(); fix_nated_contact(); xlog("test"); } failure_route[missed_call] { if (t_was_cancelled()) { exit; } }
-
配置脚本准备好后,执行
opensips -f ~/opensips.cfg -D
命令启动OpenSIPS 实例,该命令中 -f 用于指定启动配置文件,-D 用于指定前台启动模式