opensips的成长之路--初识与搭建安装

  • opensips介绍
  • 编译安装
  • 安装依赖
  • 编译选择数据库
  • 修改安装路径
  • 安装
  • 重新生成配置文件
  • 方法1
  • 方法2
  • 修改配置
  • 创建opensips数据库
  • 添加域
  • 添加sip用户
  • 查看在线用户
  • 安装rtpproxy并配置
  • 安装并配置mediaproxy
  • 启动mediaproxy服务
  • 启动服务并检验
  • 测试
  • 安装配置opensips-cp
  • 结束


opensips介绍

编译安装

下载对应版本的源代码,现在最新稳定版是opensips-3.1.2(我用的是2.4.10)。
解压切换到代码根目录。

安装依赖

根据实际需求安装依赖。

apt-get install -y gcc bison flex make openssl libmysqlclient-dev perl libdbi-perl libdbd-mysql-perl libdbd-pg-perl libfrontier-rpc-perl libterm-readline-gnu-perl libberkeleydb-perl mysql-server ssh libxml2 libxml2-dev libxmlrpc-core-c3-dev libpcre3 libpcre3-dev subversion libncurses5-dev git ngrep libssl-dev python python-twisted python-cjson python-dev python-application python-gnutls iptables iptables-dev libnetfilter-conntrack-dev libnetfilter-conntrack3 libnetfilter-conntrack3-dbg

下载后上传至服务器,tar -zcvf opensips-2.4.10.tar.gz,解压并切换至目录下。

docker opengass 没有gsql命令_perl

如果希望使用默认编译选项编译,直接执行
make all 如果希望修改编译参数,执行
make menuconfig

编译选择数据库

等待执行完成,跳转到下图,依次按照下面选择,到最后选中db_mysql时,按空格选中。

Configure Compile Options-->Configure Excluded Modules-->db_mysql

docker opengass 没有gsql命令_perl_02


docker opengass 没有gsql命令_sed_03

修改安装路径

上一步执行完成后,按q返回上一层菜单,选择Configure Install Prefix 然后输入目录,回车,然后选择save changes最后再按q返回上一层

docker opengass 没有gsql命令_mysql_04

安装

选择Compile And Install OpenJIPS 回车安装。

docker opengass 没有gsql命令_perl_05

重新生成配置文件

方法1

安装完成后,回车返回菜单栏选择Generate OpenSIPS Script-->Residential Script-->Configure Residential Script

docker opengass 没有gsql命令_mysql_06

上下选择,空格选中,按q返回上一层。先保存save residential script,然后再生成文件Generate Residential Script

docker opengass 没有gsql命令_mysql_07

方法2

与方法1不同的是进入方式,找到安装目录下 sbin进入目录后执行./osipsconfig

docker opengass 没有gsql命令_perl_08


之后就同方法1相同。

docker opengass 没有gsql命令_sed_09


之后 到目录下,把新生成的文件替换一下原来文件(记得该名字哦)

修改配置

编辑脚本文件:opensips.cfg,大概在40行左右

#
# OpenSIPS residential configuration script
#     by OpenSIPS Solutions <team@opensips-solutions.com>
#
# This script was generated via "make menuconfig", from
#   the "Residential" scenario.
# You can enable / disable more features / functionalities by
#   re-generating the scenario with different options.#
#
# Please refer to the Core CookBook at:
#      https://opensips.org/Resources/DocsCookbooks
# for a explanation of possible statements, functions and parameters.
#


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

log_level=3
log_stderror=no
log_facility=LOG_LOCAL0

children=4

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


listen=udp:你的IP:你的端口   # CUSTOMIZE ME

listen=tcp:你的IP:你的端口    


####### 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)
#增加这一行,不然报错
modparam("uri", "db_url", "mysql://opensips:opensipsrw@localhost/opensips")

#### 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://opensips:opensipsrw@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 to enable "append_fromtag"
   in "rr" module */
   
modparam("acc", "detect_direction", 0)

/* account triggers (flags) */

modparam("acc", "db_url",
  "mysql://opensips:opensipsrw@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", "db_url",
  "mysql://opensips:opensipsrw@localhost/opensips") # CUSTOMIZE ME
modparam("auth_db", "load_credentials", "")  

#### ALIAS module
loadmodule "alias_db.so"
modparam("alias_db", "db_url",
  "mysql://opensips:opensipsrw@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://opensips:opensipsrw@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)")

loadmodule "rtpproxy.so"
modparam("rtpproxy", "rtpproxy_sock", "udp:localhost:2008") # 修改端口

#增加下边这一段
#### MediaProxy module
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")

loadmodule "proto_udp.so"
loadmodule "proto_tcp.so"
####### Routing Logic ########

# main request routing logic

route{

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

	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;
		}

		if (is_method("BYE")) {
			# do accounting even if the transaction fails
			do_accounting("log","failed");
		}

		# route it out to whatever destination was set by loose_route()
		# in $du (destination URI).
		route(relay);
		exit;
	}

	# CANCEL processing
	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_myself("$fd")) {
					
		} else {
			# if caller is not local, then called number must be local
			
			if (!is_myself("$rd")) {
				send_reply("403","Relay Forbidden");
				exit;
			}
		}

	}

	# 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")) {
		
		do_accounting("log");
	}

	
	if (!is_myself("$rd")) {
		append_hf("P-hint: outbound\r\n"); 
		
		route(relay);
	}

	# requests for my domain
	
	if (is_method("PUBLISH|SUBSCRIBE")) {
		send_reply("503", "Service Unavailable");
		exit;
	}

	if (is_method("REGISTER")) {
		# store the registration and generate a SIP reply
		if (!save("location"))
			xlog("failed to register AoR $tu\n");

		exit;
	}

	if ($rU==NULL) {
		# request with no Username in RURI
		send_reply("484","Address Incomplete");
		exit;
	}

	# do lookup with method filtering
	if (!lookup("location","m")) {
		t_reply("404", "Not Found");
		exit;
	}

	# when routing via usrloc, log the missed calls also
	do_accounting("log","missed");
	route(relay);
}


route[relay] {
	# for INVITEs enable some additional helper routes
	if (is_method("INVITE")) {
		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] {
	xlog("incoming reply\n");
}


failure_route[missed_call] {
	if (t_was_cancelled()) {
		exit;
	}

	# uncomment the following lines if you want to block client 
	# redirect based on 3xx replies.
	##if (t_check_status("3[0-9][0-9]")) {
	##t_reply("404","Not found");
	##	exit;
	##}

	
}

修改opensipsctlrc文件
修改SIP_DOMAIN为服务器的ip地址:

# $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=ip

## 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=xmlrpc

## 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.2.133:8000
# MI_CONNECTOR=XMLRPC:192.168.2.133: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=

创建opensips数据库

切换到安装目录下sbin 检测配置文件语法

./opensips -C

创建数据库

./opensipsdbctl create

记得要输入数据库的密码哦!

添加域

./opensipsctl domain add dasha500 #如果没有域名可不用添加

docker opengass 没有gsql命令_perl_10

添加sip用户

./opensipsctl add username password #添加用户

docker opengass 没有gsql命令_sed_11

./opensipsctl rm username #删除用户

查看在线用户

./opensipsctl ul show

./opensipsctl online

安装rtpproxy并配置

sudo apt-get install rtpproxy
sudo vi /etc/default/rtpproxy
# Defaults for rtpproxy

# The control socket.
#CONTROL_SOCK="unix:/var/run/rtpproxy/rtpproxy.sock"
# To listen on an UDP socket, uncomment this line:
CONTROL_SOCK=udp:127.0.0.1:22222

# Additional options that are passed to the daemon.
EXTRA_OPTS=""
LISTEN_ADDR=你的ip
EXTRA_OPTS="-l ${LISTEN_ADDR}"

docker opengass 没有gsql命令_mysql_12


启动rtpproxy

sudo killall rtpproxy
sudo /etc/init.d/rtpproxy start

安装并配置mediaproxy

官方安装

wget http://download.ag-projects.com/agp-debian-gpg.key
sudo apt-key add agp-debian-gpg.key
sudo vi /etc/apt/sources.list

添加

deb     http://ag-projects.com/ubuntu bionic main 
deb-src http://ag-projects.com/ubuntu bionic main

安装mediaproxy

sudo apt-get update
sudo apt-get install mediaproxy-dispatcher mediaproxy-relay mediaproxy-web-sessions

拷本密钥文件,修改配置文件

docker opengass 没有gsql命令_perl_13

cd /etc/mediaproxy/tls/
sudo cp /usr/share/doc/mediaproxy-common/tls/* .
cd ..
sudo vi config.ini

修改如下

[Relay]

dispatchers = 114.55.75.238

passport = None

relay_ip = 114.55.75.238

;advertised_ip =

port_range = 50000:60000

; Logging level (one of CRITICAL, ERROR, WARNING, INFO or DEBUG)
log_level = DEBUG

stream_timeout = 90

on_hold_timeout = 7200

;dns_check_interval = 60

reconnect_delay = 10

traffic_sampling_period = 15


[Dispatcher]
socket_path = dispatcher.sock

listen = 114.55.75.238

listen_management = 114.55.75.238

management_use_tls = yes

passport = None

management_passport = None

log_level = DEBUG

relay_timeout = 5

;accounting =

[TLS]
certs_path = tls

verify_interval = 300


[Database]
; This section needs to be configured if database accounting is enabled

; Database URI in the form: scheme://user:password@host/database
;dburi = mysql://mediaproxy:CHANGEME@localhost/mediaproxy

; Name for the table.
;sessions_table = media_sessions

; Column names. Columns are strings except for info which is a BLOB
;
;callid_column = call_id
;fromtag_column = from_tag
;totag_column = to_tag
;info_column = info

[Radius]
; This section needs to be configured if radius accounting is enabled

; OpenSIPS RADIUS configuration file. All RADIUS configuration parameters
; will be read from this file, including dictionary files.
;
;config_file = /etc/opensips/radius/client.conf

; Additional dictionary file with MediaProxy specific attributes.
;additional_dictionary = radius/dictionary

[OpenSIPS]

socket_path = /run/opensips/socket

max_connections =10

启动mediaproxy服务

sudo service mediaproxy-dispatcher restart
sudo service mediaproxy-relay restart
ps -ef |grep media

docker opengass 没有gsql命令_perl_14


则说明已经正常启动。如果没有正常启动可以在/var/log/syslog中查看原因

启动服务并检验

修改日志文件配置,是opensips的日志保存在/var/log/opensips.log

sudo touch /var/log/opensips.log
sudo chmod 777 /var/log/opensips.log
sudo vi /etc/rsyslog.d/opensips.conf

增加如下内容
local0.* /var/log/opensips.log 重启syslogservice rsyslog restart

cd /usr/local/sbin

./opensipsctl start #开启opensips服务

docker opengass 没有gsql命令_perl_15


./opensipsctl stop #关闭opensips服务

./opensipsctl restart #重启opensips服务

测试

下载官方测试程序 pc版的,手机版与之差不多。

docker opengass 没有gsql命令_perl_16


docker opengass 没有gsql命令_mysql_17

docker opengass 没有gsql命令_perl_18

安装配置opensips-cp

先说坑先看自己 的opensips版本安装指南 一些依赖

apt-get install -y apache2;
apt-get install -y php php-curl php-gd php-pear php-cli php-mysql php-apcu;
apt-get install libapache2-mod-php  unzip;

下载最新版本的opensips-cp,位置在http://sourceforge.net/projects/opensips-cp/

cd /var/www/html;
wget https://github.com/OpenSIPS/opensips-cp/archive/master.zip;
unzip master.zip;
mv opensips-cp-master opensips-cp
chown -R www-data:www-data /var/www/html/opensips-cp/
mysql -Dopensips < /var/www/html/opensips-cp/config/db_schema.mysql
cp /var/www/html/opensips-cp/config/tools/system/smonitor/opensips_stats_cron /etc/cron.d/
cd /etc/apache2/sites-available/
ls
 vim 000-default.conf

参照

<VirtualHost *:28000>
	# The ServerName directive sets the request scheme, hostname and port that
	# the server uses to identify itself. This is used when creating
	# redirection URLs. In the context of virtual hosts, the ServerName
	# specifies what hostname must appear in the request's Host: header to
	# match this virtual host. For the default virtual host (this file) this
	# value is not decisive as it is used as a last resort host regardless.
	# However, you must set it for any further virtual host explicitly.
	#ServerName www.example.com

	ServerAdmin webmaster@localhost
	DocumentRoot /opt/www/html

	# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
	# error, crit, alert, emerg.
	# It is also possible to configure the loglevel for particular
	# modules, e.g.
	#LogLevel info ssl:warn

	ErrorLog ${APACHE_LOG_DIR}/error.log
	
	CustomLog ${APACHE_LOG_DIR}/access.log combined
	
	<Directory /var/www/html/opensips-cp/web>
		Options Indexes FollowSymLinks MultiViews
		AllowOverride None
		Require all granted
	</Directory>
	<Directory /var/www/html/opensips-cp>
		Options Indexes FollowSymLinks MultiViews
		AllowOverride None
		Require all denied
	</Directory>
	Alias /cp /var/www/html/opensips-cp/web

	<DirectoryMatch "/var/www/html/opensips-cp/web/tools/.*/.*/(template|custom_actions|lib)/">
		Require all denied
	</DirectoryMatch>

	# For most configuration files from conf-available/, which are
	# enabled or disabled at a global level, it is possible to
	# include a line for only one particular virtual host. For example the
	# following line enables the CGI configuration for this host only
	# after it has been globally disabled with "a2disconf".
	#Include conf-available/serve-cgi-bin.conf
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

重启服务器

su - 
reboot

然后就可以访问了 http://你的ip :你的端口/cp/

docker opengass 没有gsql命令_mysql_19

结束