Advertisement
ergys_papa

Untitled

Mar 31st, 2020
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.54 KB | None | 0 0
  1. ###################################
  2. ####### M2 Kamailio proxy #########
  3. ###################################
  4.  
  5. #!define DBURL "sqlite:///etc/kamailio/kamailio.db"
  6. #!substdef "!RTPENGINE_ENGAGE!trust-address replace-origin replace-session-connection rtcp-mux-demux ICE=remove RTP/AVP!g"
  7. #!define FLT_DLGINFO 10
  8.  
  9. ####### Global Parameters #########
  10.  
  11. debug = 2
  12. log_stderror = no
  13. log_facility = LOG_LOCAL0
  14. fork = yes
  15. children = 4
  16. port = 5060
  17. listen = udp:136.243.43.23:5060
  18. disable_tcp = yes
  19. dns = no
  20. rev_dns = no
  21. mpath = "/usr/local/lib64/kamailio/modules/"
  22.  
  23. server_header = "Server: M2 Proxy server"
  24. user_agent_header = "User-Agent: M2 Proxy server"
  25.  
  26. ####### Modules Section ########
  27.  
  28. loadmodule "tm"
  29. loadmodule "kex"
  30. loadmodule "sl"
  31. loadmodule "rr"
  32. loadmodule "pv"
  33. loadmodule "maxfwd"
  34. loadmodule "textops"
  35. loadmodule "siputils"
  36. loadmodule "xlog"
  37. loadmodule "sanity"
  38. loadmodule "nat_traversal"
  39. loadmodule "dispatcher"
  40. loadmodule "ctl"
  41. loadmodule "jsonrpcs"
  42. loadmodule "db_mysql"
  43. loadmodule "db_sqlite"
  44. loadmodule "usrloc"
  45. loadmodule "registrar"
  46. loadmodule "auth"
  47. loadmodule "auth_db"
  48. loadmodule "nathelper"
  49. loadmodule "rtpengine"
  50. loadmodule "dialog"
  51.  
  52.  
  53. # ----------------- setting module-specific parameters ---------------
  54.  
  55. # ----- ctl -----------
  56.  
  57. modparam("ctl", "binrpc", "/var/run/kamailio/kamailio_ctl")
  58.  
  59. # ----- tm params -----
  60. modparam("tm", "fr_timer", 3000)
  61. modparam("tm", "fr_inv_timer", 40000)
  62. modparam("tm", "auto_inv_100_reason", "Trying")
  63.  
  64. # ----- rr params -----
  65. # add value to ;lr param to cope with most of the UAs
  66. modparam("rr", "enable_full_lr", 1)
  67. # do not append from tag to the RR (no need for this script)
  68. modparam("rr", "append_fromtag", 0)
  69.  
  70. # ----- dispatcher params -----
  71. modparam("dispatcher", "list_file", "/usr/local/etc/kamailio/dispatcher.list")
  72. modparam("dispatcher", "xavp_dst", "_dsdst_")
  73. modparam("dispatcher", "xavp_dst_mode", 1)
  74. modparam("dispatcher", "xavp_ctx", "_dsctx_")
  75. modparam("dispatcher", "xavp_ctx_mode", 1)
  76. modparam("dispatcher", "ds_ping_method", "OPTIONS")
  77. modparam("dispatcher", "ds_ping_interval", 30)
  78. modparam("dispatcher", "ds_probing_mode", 1)
  79. modparam("dispatcher", "ds_inactive_threshold", 1)
  80. modparam("dispatcher", "ds_probing_threshold", 1)
  81. modparam("dispatcher", "ds_hash_size", 12)
  82. modparam("dispatcher", "ds_ping_reply_codes", "code=404")
  83.  
  84. # ----- nathelper -----
  85. modparam("nathelper", "natping_interval", 10)
  86. modparam("nathelper", "received_avp", "$avp(received_avp)")
  87.  
  88. # ----- registrar -----
  89. modparam("registrar", "default_expires", 1800)
  90. modparam("registrar", "min_expires", 60)
  91. modparam("registrar", "max_expires", 180)
  92. modparam("registrar", "received_avp", "$avp(received_avp)")
  93.  
  94. # ----- usrloc -----
  95. modparam("usrloc", "version_table", 0)
  96. modparam("usrloc", "db_mode", 3)
  97. modparam("usrloc", "db_url", "mysql://kamailio:kamailio@159.69.65.156/m2")
  98.  
  99. # ----- auth db -----
  100. modparam("auth_db", "version_table", 0)
  101. modparam("auth_db", "calculate_ha1", yes)
  102. modparam("auth_db", "user_column", "op_username")
  103. modparam("auth_db", "password_column", "op_password")
  104. modparam("auth_db", "db_url", "mysql://kamailio:kamailio@159.69.65.156/m2")
  105.  
  106. # ----- rtpengine -----
  107. modparam("rtpengine", "db_url", DBURL)
  108. #modparam("rtpengine", "rtpengine_allow_op", 1)
  109.  
  110. # ----- dialog params -----
  111. modparam("dialog", "db_url", DBURL)
  112. modparam("dialog", "enable_stats", 1)
  113. modparam("dialog", "db_mode", 1)
  114. modparam("dialog", "dlg_flag", FLT_DLG)
  115.  
  116. ####### Routing Logic ########
  117.  
  118. # main request routing logic
  119.  
  120. route {
  121.  
  122. if(is_method("INVITE|BYE|UPDATE|CANCEL|ACK")) {
  123. setflag(FLT_DLGINFO);
  124. dlg_manage();
  125. }
  126.  
  127. # delete request in rtpenngine
  128. if (is_method("BYE")) {
  129. rtpengine_delete("via-branch=auto");
  130. xlog("L_NOTICE", "DELETE COMMAND - Bye Request");
  131. }
  132. if (is_method("CANCEL")) {
  133. rtpengine_delete("via-branch=auto");
  134. xlog("L_NOTICE", "DELETE COMMAND - Cancel Request");
  135. }
  136.  
  137. # per request initial checks
  138. route(REQINIT);
  139.  
  140. # handle OPTIONS
  141. if (is_method("OPTIONS")) {
  142. sl_send_reply("200", "OK");
  143. exit;
  144. }
  145.  
  146. # check NAT
  147. if (client_nat_test("3")) {
  148. fix_contact();
  149. }
  150.  
  151. # handle not implemented request
  152. if (!ds_is_from_list() && is_method("SUBSCRIBE")) {
  153. xlog("L_NOTICE", "Rejecting $rm from $si:$sp\n");
  154. sl_send_reply("501", "Not Implemented");
  155. exit;
  156. }
  157.  
  158.  
  159. # handle requests within SIP dialogs
  160. route(WITHINDLG);
  161.  
  162.  
  163.  
  164. # CANCEL processing
  165. if (is_method("CANCEL")) {
  166. if (t_check_trans()) {
  167. t_relay();
  168. }
  169. exit;
  170. }
  171. t_check_trans();
  172.  
  173. # record routing for dialog forming requests (in case they are routed)
  174. if (is_method("INVITE")) {
  175. record_route();
  176. # dispatch destinations
  177. if (!ds_is_from_list()) {
  178. route(DISPATCH);
  179. }
  180. }
  181.  
  182. if (is_method("REGISTER")) {
  183. route(REGISTER);
  184. }
  185.  
  186. route(RELAY);
  187. }
  188.  
  189.  
  190.  
  191. route[RELAY] {
  192. if (is_method("INVITE")) {
  193. if(!t_is_set("failure_route")) {
  194. t_on_failure("MANAGE_FAILURE");
  195. }
  196. }
  197.  
  198. if (!t_relay()) {
  199. sl_reply_error();
  200. }
  201. exit;
  202. }
  203.  
  204. # Per SIP request initial checks
  205. route[REQINIT] {
  206. if (!mf_process_maxfwd_header("10")) {
  207. sl_send_reply("483", "Too Many Hops");
  208. exit;
  209. }
  210.  
  211. if(!sanity_check("1511", "7")) {
  212. xlog("Malformed SIP message from $si:$sp\n");
  213. exit;
  214. }
  215. }
  216.  
  217. # Handle requests within SIP dialogs
  218. route[WITHINDLG] {
  219. if (has_totag()) {
  220. # sequential request withing a dialog should
  221. # take the path determined by record-routing
  222. if (loose_route()) {
  223.  
  224. # if (is_method("ACK") && has_body("application/sdp")) {
  225. # rtpengine_answer("trust-address replace-origin replace-session-connection rtcp-mux-demux ICE=remove RTP/AVP");
  226. # }
  227. route(RELAY);
  228. } else {
  229. if (is_method("ACK")) {
  230. if (t_check_trans()) {
  231. xlog("L_NOTICE", "ACK COMMAND - NOT Sent to RTP");
  232. # non loose-route, but stateful ACK;
  233. # must be ACK after a 487 or e.g. 404 from upstream server
  234. t_relay();
  235. exit;
  236. } else {
  237. # ACK without matching transaction ... ignore and discard.
  238. exit;
  239. }
  240. }
  241. sl_send_reply("404", "Not here");
  242. }
  243. exit;
  244. }
  245. }
  246.  
  247. # Manage failure routing cases
  248. failure_route[MANAGE_FAILURE] {
  249. #if (!is_method("CANCEL|BYE")) {
  250. rtpengine_delete("via-branch=auto");
  251. xlog("L_NOTICE", "CLEAN UP PORTS - RtpEngine!");
  252. #}
  253. if (t_is_canceled()) {
  254. exit;
  255. }
  256. }
  257.  
  258. onreply_route[1] {
  259. if(is_method("INVITE|UPDATE") && t_check_status("200|18[0-3]"")) {
  260. if (has_body("application/sdp")) {
  261. rtpengine_answer("trust-address replace-origin replace-session-connection rtcp-mux-demux ICE=remove via-branch=auto RTP/AVP");
  262. xlog("L_NOTICE", "ANSWER COMMAND - Sent to RTP!");
  263. }
  264. }
  265. }
  266.  
  267. onreply_route[2] {
  268. if (has_body("application/sdp")) {
  269. rtpengine_offer("trust-address replace-origin replace-session-connection rtcp-mux-demux ICE=remove via-branch=auto RTP/AVP");
  270. xlog("L_NOTICE", "OFFER COMMAND - Sent to RTP!");
  271. }
  272. }
  273.  
  274. # Dispatch requests
  275. route[DISPATCH] {
  276.  
  277. if (is_method("INVITE")) {
  278. # record_route();
  279. if (has_body("application/sdp")) {
  280. if (rtpengine_offer("trust-address replace-origin replace-session-connection rtcp-mux-demux ICE=remove via-branch=auto RTP/AVP")) {
  281. t_on_reply("1");
  282. }
  283.  
  284. } else {
  285. t_on_reply("2");
  286. }
  287. }
  288.  
  289. # if (is_method("ACK") && has_body("application/sdp")) {
  290. # rtpengine_answer("trust-address replace-origin replace-session-connection rtcp-mux-demux ICE=remove via-branch=auto RTP/AVP");
  291. # xlog("L_NOTICE", "ACK COMMAND - Sent to RTP");
  292. # }
  293.  
  294.  
  295. # round-robin dispatching on gateways group '1'
  296. if (!ds_select_dst("1", "4")) {
  297. send_reply("404", "No destination");
  298. exit;
  299. }
  300.  
  301. # remove special M2 proxy headers
  302. # only M2 proxy is allowed to set these headers
  303. remove_hf("x-M2-Originator-ip");
  304. remove_hf("x-M2-Originator-port");
  305.  
  306. # set specials M2 proxy headers
  307. append_hf("x-M2-Originator-ip: $si\r\n");
  308. append_hf("x-M2-Originator-port: $sp\r\n");
  309.  
  310. xlog("L_NOTICE", "$rm from $si:$sp going to <$ru> dispatched to <$du> server\n");
  311. return;
  312. #route(RELAY);
  313. }
  314.  
  315. # Handle registrations
  316. route[REGISTER] {
  317. if (nat_uac_test("23")) {
  318. fix_nated_register();
  319. }
  320.  
  321. if ($expires(max) == 0) {
  322. xlog("L_NOTICE", "Unregistration attempt from $ct\n");
  323. } else {
  324. xlog("L_NOTICE", "Registration attempt from $ct\n");
  325. }
  326.  
  327. $var(auth_result) = www_authorize("$td", "devices");
  328.  
  329. if ($var(auth_result) < 0) {
  330. switch($var(auth_result)) {
  331. case -1:
  332. xlog("L_NOTICE", "Generic authentication error (Bad credentials, couldn't parse credentials, no memory left, couldn't fetch from table, ...)\n");
  333. break;
  334. case -2:
  335. xlog("L_NOTICE", "Registration error - wrong password for user '$au'\n");
  336. break;
  337. case -3:
  338. xlog("L_NOTICE", "Registration error - authentication user '$au' does not exist\n");
  339. break;
  340. case -4:
  341. xlog("L_NOTICE", "Registration error - the nonce has expired for user '$au'\n");
  342. break;
  343. case -5:
  344. xlog("L_NOTICE", "Challenging registration attempt from $ct (asking for credentials)\n");
  345. break;
  346. case -6:
  347. xlog("L_NOTICE", "Registration error - the nonce has already been used to authenticate a previous request for user '$au'\n");
  348. break;
  349. case -8:
  350. xlog("L_NOTICE", "Registration error - authuser mismatch for '$au' (From/To/RURI user does not match the authentication user)\n");
  351. break;
  352. default:
  353. xlog("L_NOTICE", "Unknown registration error\n");
  354. }
  355.  
  356. www_challenge("$td", "1");
  357. exit;
  358. };
  359.  
  360. if (!save("location")) {
  361. xlog("L_NOTICE", "Cannot save registration information for user '$au'\n");
  362. sl_reply_error();
  363. }
  364.  
  365. if ($expires(max) == 0) {
  366. xlog("L_NOTICE", "User '$au' is successfully unregistered\n");
  367. } else {
  368. xlog("L_NOTICE", "User '$au' is successfully registered\n");
  369. }
  370.  
  371. exit;
  372. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement