分享OpenSIPS 3.2.x for wss脚本

照一个官方的脚本(opensips2.4)改的

这几个是宏,需要修改

  • MY_SIP_LAN_PORT
  • MY_SIP_WAN_PORT
  • MY_IP4_ADDR
  • MY_IP4_PUBLIC_ADDR
  • MY_DOMAIN

也可以这样配置

modparam("tls_mgm", "match_sip_domain", "[dom1]*")

# opensips3.2.x, webrtc
# 参考文档:
# https://opensips.org/pub/docs/tutorials/websockets/opensips-late.cfg
# https://github.com/havfo/WEBRTC-to-SIP

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

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

log_level=2
xlog_level=3
log_stderror=yes
#log_facility=LOG_LOCAL0

udp_workers=4
tcp_workers=4

mhomed=1

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

# CUSTOMIZE ME
# wan
socket=udp:MY_IP4_ADDR:MY_SIP_WAN_PORT as MY_IP4_PUBLIC_ADDR:MY_SIP_WAN_PORT
socket=tcp:MY_IP4_ADDR:MY_SIP_WAN_PORT as MY_IP4_PUBLIC_ADDR:MY_SIP_WAN_PORT
socket=tls:MY_IP4_ADDR:MY_SIPS_PORT as MY_IP4_PUBLIC_ADDR:MY_SIPS_PORT
socket=ws:MY_IP4_ADDR:MY_SIP_WS_PORT as MY_IP4_PUBLIC_ADDR:MY_SIP_WS_PORT
socket=wss:MY_IP4_ADDR:MY_SIP_WSS_PORT as MY_IP4_PUBLIC_ADDR:MY_SIP_WSS_PORT
# lan
socket=udp:MY_IP4_ADDR:MY_SIP_LAN_PORT
socket=tcp:MY_IP4_ADDR:MY_SIP_LAN_PORT

# 域名,可以配置多个
alias=MY_DOMAIN

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

#set module path
mpath="/usr/lib/x86_64-linux-gnu/opensips/modules/"

loadmodule "proto_udp.so"
loadmodule "proto_tcp.so"
loadmodule "proto_tls.so"
loadmodule "proto_wss.so"
loadmodule "proto_ws.so"
loadmodule "tls_openssl.so"
loadmodule "tls_mgm.so"

modparam("tls_mgm", "server_domain", "dom1")
modparam("tls_mgm", "client_tls_domain_avp", "tls_match_dom")
modparam("tls_mgm", "client_sip_domain_avp", "sip_match_dom")
/* modparam("tls_mgm", "match_sip_domain", "[dom1]MY_DOMAIN") */
modparam("tls_mgm", "match_sip_domain", "[dom1]*")
modparam("tls_mgm", "tls_method", "[dom1]tlsv1_2")
modparam("tls_mgm", "verify_cert", "[dom1]0")
modparam("tls_mgm", "require_cert", "[dom1]0")
modparam("tls_mgm", "certificate", "[dom1]/etc/opensips/cert.pem")
modparam("tls_mgm", "private_key", "[dom1]/etc/opensips/privkey.pem")

modparam("proto_wss", "wss_max_msg_chunks", 8)
modparam("proto_wss", "wss_resource", "/")
modparam("proto_wss", "wss_handshake_timeout", 300)

#### 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)
#modparam("mi_fifo", "pretty_printing", 1)

#### MYSQL module
loadmodule "db_mysql.so"

#### HTTPD module
loadmodule "httpd.so"
modparam("httpd", "port", 8888)

#### USeR LOCation module
# loadmodule "usrloc.so"
# modparam("usrloc", "nat_bflag", "NAT")
# modparam("usrloc", "working_mode_preset", "single-instance-sql-write-back")
# modparam("usrloc", "db_url", DBURL) # 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)
modparam("acc", "db_url", DBURL) # 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", DBURL) # CUSTOMIZE ME
# modparam("auth_db", "load_credentials", "")

#### 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", DBURL) # CUSTOMIZE ME

####  NAT modules
loadmodule "nathelper.so"
modparam("nathelper", "natping_interval", 10)
modparam("nathelper", "ping_nated_only", 1)
modparam("nathelper", "sipping_bflag", "SIP_PING_FLAG")
modparam("nathelper", "sipping_from", "sip:pinger@127.0.0.1") # CUSTOMIZE ME
modparam("nathelper", "received_avp", "$avp(received_nh)")

loadmodule "dispatcher.so"
modparam("dispatcher", "db_url", DBURL) # CUSTOMIZE ME
modparam("dispatcher", "ds_probing_mode", 1)
modparam("dispatcher", "ds_ping_interval", 10)
modparam("dispatcher", "ds_ping_from", "sip:opensips@MY_DOMAIN")
modparam("dispatcher", "hash_pvar", "$avp(hash)")
modparam("dispatcher", "attrs_avp", "$avp(attrs)")

####  MI_HTTP module
loadmodule "mi_http.so"

####  rtpengine module
loadmodule "rtpengine.so"
modparam("rtpengine", "rtpengine_sock", RTPENGINE_SOCK)  # CUSTOMIZE ME

####  topology_hiding module
loadmodule "topology_hiding.so"

####  mi_script module
loadmodule "mi_script.so"

####  cfgutils module
loadmodule "cfgutils.so"

####  json module
loadmodule "json.so"

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

# main request routing logic

route{
	xlog("start $rm|$ci|$fU|$tU|$socket_in(port) from $si:$sp\n");

	if (is_method("OPTIONS")) {
		send_reply(200, "Keepalive");
		exit;
	}

	if ($socket_in(proto) == "WS" || $socket_in(proto) == "WSS") {
		xlog("$rm|$ci|SRC_WS\n");
		setflag("SRC_WS");
	}

	if (ds_is_in_list($si, 0, 1)) {
		xlog("$rm|$ci|SRC_DISPATCHER\n");
		setflag("SRC_DISPATCHER");
	}

	# initial NAT handling; detect if the request comes from behind a NAT
	# and apply contact fixing
	force_rport();
	# if (nat_uac_test(23)) {
	if ($socket_in(port) == MY_SIP_WAN_PORT || isflagset("SRC_WS")) {
		if (is_method("REGISTER")) {
			fix_nated_register();
			setbflag("NAT");
			# vip
			if (isflagset("SRC_WS"))
				setbflag("DST_WS");
		} else {
			fix_nated_contact();
			setflag("NAT");
		}
	}

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

	if (has_totag()) {
		if (topology_hiding_match()) {
			# xlog("Callee side callid  is $TH_callee_callid\n");
			t_relay();
			exit;
		}

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

		# validate the sequential request against dialog
		if ( $DLG_status!=NULL && !validate_dialog() ) {
			xlog("$ci|In-Dialog $rm from $si (callid=$ci) is not valid according to dialog\n");
			## exit;
		}

		if (is_method("ACK")) {
			if (has_body("application/sdp")) {
				# check if destination is WS
				if ($du != NULL)
					$var(proto) = $dP;
				else
					$var(proto) = $rP;
				if ($var(proto) == "WS" || $var(proto) == "WSS")
					setbflag("DST_WS");

				route(rtpengine_answer);
			}
		}

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

		if (check_route_param("nat=yes"))
			setflag("NAT");
		# 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();

	# here no totag

	# preloaded route checking
	if (loose_route()) {
		xlog("L_ERR", "$ci|Attempt to route with preloaded Route's [$fu/$tu/$ru/$ci]");
		if (!is_method("ACK"))
			send_reply(403,"Preload Route denied");
		exit;
	}

	# account only INVITEs
	if (is_method("INVITE")) {
		# create dialog with timeout
		if ( !create_dialog("B") ) {
			send_reply(500,"Internal Server Error");
			exit;
		}

		do_accounting("db");
	}

	# 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")) {
		$var(sut) = "sip:" + $si  + ":" + $sp + ";transport=" + $socket_in(proto);
		append_hf("Path: <sip:MY_IP4_ADDR:MY_SIP_LAN_PORT;transport=udp;lr;received=$var(sut)>\r\n");
		route(dispatch);
		exit;
	}

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

	if (isflagset("SRC_DISPATCHER")) {
		if (is_method("INVITE")) {
			topology_hiding();
		}
		route(relay);
	} else {
		route(dispatch);
	}
	exit;
}

route[relay] {
	# for INVITEs enable some additional helper routes
	if (is_method("INVITE")) {
		if (!has_body("application/sdp")) {
			setflag("LATE_SDP_NEGOCIATION");
		}

		t_on_branch("per_branch_ops");
		t_on_reply("handle_nat");
		t_on_failure("missed_call");
	} else if (is_method("BYE|CANCEL")) {
		xlog("$ci|rtpengine_delete\n");
		rtpengine_delete();
	}

	if (isflagset("NAT")) {
		add_rr_param(";nat=yes");
	}

	if (!t_relay()) {
		send_reply(500, "Internal Error");
	}
	exit;
}

branch_route[per_branch_ops] {
	xlog("$ci|new branch at ru = $ru, du = $du, branch = $T_branch_idx, branch flags = $bf, socket = $(branch(socket)[$T_branch_idx])\n");
	route(rtpengine_offer);
}

onreply_route[handle_nat] {
	xlog("$ci|incoming reply|$rs\n");

	fix_nated_contact();

	if (!has_body("application/sdp")) {
		return;
	}

	if (isflagset("LATE_SDP_NEGOCIATION")) {
		route(rtpengine_offer);
	} else {
		route(rtpengine_answer);
	}
}

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

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

route[rtpengine_offer] {
	$var(rtpengine_flags) = "replace-origin replace-session-connection";

	if (isflagset("SRC_WS") && isbflagset("DST_WS")) { # ws->ws
		$var(rtpengine_flags) = $var(rtpengine_flags) + " ICE=force-relay DTLS=passive";
		xlog("$ci|rtpengine_offer|run line: $cfg_line\n");
	}
	else if (isflagset("SRC_WS") && !isbflagset("DST_WS")) { # ws->sip
		$var(rtpengine_flags) = $var(rtpengine_flags) + " rtcp-mux-demux DTLS=off SDES-off ICE=remove RTP/AVP";
	}
	else if (!isflagset("SRC_WS") && isbflagset("DST_WS")) { # sip->ws
		$var(rtpengine_flags) = $var(rtpengine_flags) + " rtcp-mux-offer generate-mid DTLS=passive SDES-off ICE=force RTP/SAVPF";
		xlog("$ci|rtpengine_offer|run line: $cfg_line\n");
	}
	else if (!isflagset("SRC_WS") && !isbflagset("DST_WS")) { # sip->sip
		$var(rtpengine_flags) = $var(rtpengine_flags) + " DTLS=off SDES-off ICE=remove RTP/AVP";
		xlog("$ci|rtpengine_offer|run line: $cfg_line\n");
	}

	if (!has_totag()) {
		if (isflagset("NAT")) {
			$var(in) = "pub";
		} else {
			$var(in) = "priv";
		}

		if (isbflagset("NAT")) {
			$var(out) = "pub";
		} else {
			$var(out) = "priv";
		}

		$var(rtpengine_flags) = $var(rtpengine_flags) + " in-iface=" + $var(in) + " out-iface=" + $var(out);
	}

	rtpengine_offer("$var(rtpengine_flags)");
	xlog("$ci|rtpengine_offer|$var(rtpengine_flags)\n");
}

route[rtpengine_answer] {
	$var(rtpengine_flags) = "replace-origin replace-session-connection";

	if (isflagset("SRC_WS") && isbflagset("DST_WS")) { # ws->ws
		$var(rtpengine_flags) = $var(rtpengine_flags) + " ICE=force-relay DTLS=passive";
		xlog("$ci|rtpengine_answer|run line: $cfg_line\n");
	}
	else if (isflagset("SRC_WS") && !isbflagset("DST_WS")) { # ws->sip
		$var(rtpengine_flags) = $var(rtpengine_flags) + " rtcp-mux-demux DTLS=off SDES-off ICE=force UDP/TLS/RTP/SAVPF";
		xlog("$ci|rtpengine_answer|run line: $cfg_line\n");
	}
	else if (!isflagset("SRC_WS") && isbflagset("DST_WS")) { # sip->ws
		$var(rtpengine_flags) = $var(rtpengine_flags) + " rtcp-mux-offer generate-mid DTLS=passive SDES-off ICE=remove RTP/AVP";
		xlog("$ci|rtpengine_answer|run line: $cfg_line\n");
	}
	else if (!isflagset("SRC_WS") && !isbflagset("DST_WS")) { # sip->sip
		$var(rtpengine_flags) = $var(rtpengine_flags) + " DTLS=passive SDES-off ICE=remove RTP/AVP";
		xlog("$ci|rtpengine_answer|run line: $cfg_line\n");
	}

	xlog("$ci|rtpengine_answer|$var(rtpengine_flags)\n");
	rtpengine_answer("$var(rtpengine_flags)");
}

route[dispatch] {
	# xlog("$ci|$rm|dispatch tU = $tU\n");
	$avp(hash) = $tU;
	if (!ds_select_dst(1, 7)) {
		send_reply(404, "No destination");
		exit;
	}
	xlog("$ci|$rm|dispatch|ru:<$ru> via du:<$du> (attrs: $avp(attrs))\n");
	if (is_method("INVITE")) {
		topology_hiding();
	}

	if (!is_method("REGISTER|MESSAGE")) {
		record_route();
	}
	route(relay);
	exit;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值