-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkamailio.cfg
More file actions
279 lines (235 loc) · 7.51 KB
/
kamailio.cfg
File metadata and controls
279 lines (235 loc) · 7.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!KAMAILIO
#!define DS_LIST "/etc/kamailio/dispatcher.list"
##!define LISTEN_UDP_PUBLIC udp:<external_ip_addr>:5060
#!define LISTEN_UDP_PRIVATE udp:<internal_ip_addr>:5060
/* set paths to location of modules */
# mpath="/usr/lib/x86_64-linux-gnu/kamailio/modules/"
####### Global Parameters #########
#mhomed=1 # doesn't work with virtual IPs
#!ifdef WITH_DEBUG
debug=4
#!else
debug=2
#!endif
log_stderror=no
memdbg=5
memlog=5
log_facility=LOG_LOCAL6
fork=yes
children=4
auto_aliases=no
#listen=LISTEN_UDP_PUBLIC
listen=LISTEN_UDP_PRIVATE
####### FLAGS ######
#!define FLAG_FROM_FREESWITCH 10
#!define FLAG_FROM_PEER 11
sip_warning=no
####### Modules Section ########
loadmodule "tm.so"
loadmodule "sl.so"
loadmodule "rr.so"
loadmodule "pv.so"
loadmodule "maxfwd.so"
loadmodule "textops.so"
loadmodule "siputils.so"
loadmodule "xlog.so"
loadmodule "sanity.so"
loadmodule "jsonrpcs.so"
loadmodule "dispatcher.so"
loadmodule "nathelper.so"
loadmodule "rtpengine.so"
loadmodule "stun.so"
loadmodule "uac.so"
loadmodule "ipops.so"
# ----------------- setting module-specific parameters ---------------
# ----- dispatcher params -----
modparam("dispatcher", "list_file", DS_LIST)
modparam("dispatcher", "flags", 2)
#modparam("dispatcher", "dst_avp", "$avp(AVP_DST)")
#modparam("dispatcher", "grp_avp", "$avp(AVP_GRP)")
#modparam("dispatcher", "cnt_avp", "$avp(AVP_CNT)")
#modparam("dispatcher", "sock_avp", "$avp(AVP_SOCK)")
#modparam("rr", "enable_full_lr", 1)
#modparam("rr", "append_fromtag", 1)
# ----- rtpproxy params -----
modparam("rtpengine", "rtpengine_sock", "udp:127.0.0.1:2222")
modparam("nathelper", "received_avp", "$avp(s:rcv)")
####### Routing Logic ########
# main request routing logic
route {
# per request initial checks
route(SANITY_CHECK);
# CANCEL processing
if (is_method("CANCEL")) {
if (t_check_trans()) {
t_relay();
}
exit;
}
# check src ip and set flag
route(CHECK_SOURCE_IP);
# always add record_route when forwarding SUBSCRIBEs
if (is_method("SUBSCRIBE")) {
exit;
}
# handle requests within SIP dialogs
route(WITHINDLG);
### only initial requests (no To tag)
t_check_trans();
# dispatch destinations
route(DISPATCH);
}
route[SANITY_CHECK] {
# 校验当前请求
# Notsupport PUBLISH METHOD
if (is_method("PUBLISH")) {
send_reply("403", "Forbidden");
exit;
}
if (!mf_process_maxfwd_header("10")) {
xlog("L_WARN", "$ci|end|too much hops, not enough barley");
send_reply("483", "Too Many Hops");
exit;
}
if (!sanity_check()) {
xlog("L_WARN", "$ci|end|message is insane");
exit;
}
if ($ua == "friendly-scanner" ||
$ua == "sundayddr" ||
$ua =~ "sipcli" ) {
xlog("L_WARN", "$ci|end|dropping message with user-agent $ua");
exit;
}
}
route[CHECK_SOURCE_IP] {
# 通过$hdr或者指定报文头
#if ($hdr(From) == "sip:user@example.com") {
# From 报文头的值为 "sip:user@example.com"
#}
# 判断是否时某一个代理请求
if(ds_is_from_list('2')) {
setflag(FLAG_FROM_FREESWITCH);
} else {
setflag(FLAG_FROM_PEER);
}
}
route[RELAY] {
if (is_method("INVITE")) {
if(!t_is_set("failure_route")) {
t_on_failure("MANAGE_FAILURE");
}
}
if (is_method("BYE")) {
xlog("L_WARN", "$rm $rmid delete rtpengine \n ");
rtpengine_delete();
}
if (isflagset(FLAG_FROM_PEER)) {
xlog("L_WARN", "$rm $rmid commented out using USP private \n ");
# 强制使用私有端口发送
force_send_socket(LISTEN_UDP_PRIVATE);
} else {
xlog("L_WARN", " commented out using USP public \n ");
# force_send_socket(LISTEN_UDP_PUBLIC);
}
if (!t_relay()) {
sl_reply_error();
}
#exit;
}
# Handle requests within SIP dialogs
route[WITHINDLG] {
if (has_totag()) {
# sequential request withing a dialog should take the path determined by record-routing
# 如果已经进入了路由状态,则走以下的路径
if (loose_route()) {
xlog("L_WARN", "$rm $rmid receive xxxxxxxx \n ");
route(RELAY);
} else {
if (is_method("NOTIFY")) {
route(RELAY);
}
if (is_method("SUBSCRIBE") && uri == myself) {
# in-dialog subscribe requests
exit;
}
if (is_method("ACK")) {
if (t_check_trans()) {
# non loose-route, but stateful ACK;
# must be ACK after a 487 or e.g. 404 from upstream server
t_relay();
exit;
} else {
# ACK without matching transaction ... ignore and discard.
exit;
}
}
sl_send_reply("404","Not here");
}
exit;
}
}
# Manage failure routing cases
failure_route[MANAGE_FAILURE] {
if (t_is_canceled()) {
exit;
}
}
onreply_route[1] {
if (has_body("application/sdp")) {
rtpengine_answer();
}
}
# onreply_route[2] {
# if (has_body("application/sdp")) {
# rtpengine_offer();
# }
# }
# Dispatch requests
route[DISPATCH] {
# round robin dispatching on gateways group '1'
# record routing for dialog forming requests (in case they are routed)
# - remove preloaded route headers
remove_hf("Route");
if (is_method("INVITE|REFER")) {
if (!has_body("application/sdp")) {
send_reply("400","Missing SDP body");
exit;
}
record_route();
if (has_body("application/sdp")) {
if (rtpengine_offer("codec-mask=all codec-transcode=PCMA codec-transcode=PCMU")) {
xlog("L_WARN", "$rm $rmid receive INVITE \n ");
t_on_reply("1");
}
}
if (isflagset(FLAG_FROM_PEER)) {
# ds_select_dst 第一个参数标识路由的组,第二个参数标识路由策略,路由策略见https://kamailio.org/docs/modules/4.3.x/modules/dispatcher.html#dispatcher.f.ds_select_dst
if(!ds_select_dst("2", "4")) {
send_reply("404", "No destination");
exit;
}
} else if (isflagset(FLAG_FROM_FREESWITCH)){
if(!ds_select_dst("1", "4")) {
send_reply("404", "No destination");
exit;
}
}
}
if(is_method("REGISTER")){
if (isflagset(FLAG_FROM_PEER)) {
# 路由至内部freeswitch,当做代理使用
if(!ds_select_dst("2", "4")) {
send_reply("404", "No destination");
exit;
}
} else if (isflagset(FLAG_FROM_FREESWITCH)){
# 路由至gateway网关
if(!ds_select_dst("1", "4")) {
send_reply("404", "No destination");
exit;
}
}
}
route(RELAY);
}