From e2f19675c0f031da2714e00c8ee0e5217169341f Mon Sep 17 00:00:00 2001 From: Luis Azedo Date: Sat, 8 Jun 2019 00:11:36 +0100 Subject: [PATCH 1/9] backport of #100 --- kamailio/auth.cfg | 44 ++++++++++++++++------- kamailio/default.cfg | 60 +++++++------------------------ kamailio/defs.cfg | 1 + kamailio/message-role.cfg | 51 ++++++++++++++++++++++++-- kamailio/nodes-role.cfg | 12 +++++-- kamailio/presence-fast-pickup.cfg | 6 +++- kamailio/presence-role.cfg | 14 ++++---- 7 files changed, 114 insertions(+), 74 deletions(-) diff --git a/kamailio/auth.cfg b/kamailio/auth.cfg index aca5bec..ffa5c32 100644 --- a/kamailio/auth.cfg +++ b/kamailio/auth.cfg @@ -30,6 +30,25 @@ route[AUTH_HEADERS] } +route[AUTH_HEADERS_JSON] +{ + xavp_params_implode("hf", "$var(outx)"); + $var(out) = $(var(outx){re.subst,/^(.*);$$/\1/}); + $var(c) = $(var(out){param.count}); + $var(headers_json) = ""; + $var(sep) = ""; + xlog("L_DEBUG", "$ci|auth|headers $var(c) => $var(out) => $var(outx)\n"); + while($var(c) > 0) { + $var(idx) = $var(c) - 1; + xlog("L_DEBUG", "$ci|auth|adding $(var(out){param.name,$var(idx)}): $(var(out){param.valueat,$var(idx)}{s.unescape.param})\n"); + append_hf("$(var(out){param.name,$var(idx)}): $(var(out){param.valueat,$var(idx)}{s.unescape.param})\r\n"); + $var(headers_json) = $_s($var(headers_json)$var(sep)"$(var(out){param.name,$var(idx)})" : "$(var(out){param.valueat,$var(idx)}{s.unescape.param})"); + $var(c) = $var(c) - 1; + $var(sep) = " , "; + } + $var(headers_json) = $_s({ $var(headers_json) }); +} + route[SETUP_AUTH_HEADERS] { @@ -37,11 +56,18 @@ route[SETUP_AUTH_HEADERS] $xavp(hf[0]=>X-AUTH-PORT) = $sp; #!ifdef REGISTRAR_ROLE + + $avp(is_registred) = "false"; + $xavp(regcfg=>match_received) = $su; + if (registered("location","$fu", 2, 1) != 1) return; + + $avp(is_registred) = "true"; #!ifdef WITH_AUTH_TOKEN route(AUTH_TOKEN); #!else route(AUTH_CCVS); #!endif + #!endif } @@ -50,23 +76,17 @@ route[SETUP_AUTH_HEADERS] route[AUTH_TOKEN] { - $xavp(regcfg=>match_received) = $su; - if (registered("location","$fu", 2, 1) == 1) { - if($(xavp(ulattrs=>token){s.len}) > 0) { - $xavp(hf[0]=>X-AUTH-Token) = $xavp(ulattrs=>token); - } else { - if($(xavp(ulattrs=>Authorizing-ID){s.len}) > 0 && $(xavp(ulattrs=>Account-ID){s.len})) { - $xavp(hf[0]=>X-AUTH-Token) = $_s($(xavp(ulattrs=>custom_channel_vars){kz.json,Authorizing-ID})@$(xavp(ulattrs=>custom_channel_vars){kz.json,Account-ID})); - } - } + if($(xavp(ulattrs=>token){s.len}) > 0) { + $xavp(hf[0]=>X-AUTH-Token) = $xavp(ulattrs=>token); + } else { + if($(xavp(ulattrs=>Authorizing-ID){s.len}) > 0 && $(xavp(ulattrs=>Account-ID){s.len})) { + $xavp(hf[0]=>X-AUTH-Token) = $_s($(xavp(ulattrs=>custom_channel_vars){kz.json,Authorizing-ID})@$(xavp(ulattrs=>custom_channel_vars){kz.json,Account-ID})); + } } } route[AUTH_CCVS] { - $xavp(regcfg=>match_received) = $su; - if (registered("location","$fu", 2, 1) != 1) return; - if($(xavp(ulattrs=>custom_channel_vars){kz.json,Account-ID}{s.len}) > 0) $xavp(hf[0]=>X-ecallmgr_Account-ID) = $(xavp(ulattrs=>custom_channel_vars){kz.json,Account-ID}); diff --git a/kamailio/default.cfg b/kamailio/default.cfg index 72b12a7..b1fb648 100644 --- a/kamailio/default.cfg +++ b/kamailio/default.cfg @@ -36,7 +36,7 @@ debug = KAZOO_LOG_LEVEL memdbg = 10 memlog = L_BUG corelog = L_ERR -mem_summary = 0 +mem_summary = KZ_MEM_SUMMARY log_stderror = no log_facility = LOG_LOCAL0 log_name="kamailio" @@ -327,13 +327,22 @@ route route(SIP_TRACE); #!endif + route_if_exists("CUSTOM_START_ROUTES"); + route(HANDLE_NOTIFY); #!ifdef AUTHORIZATION_ROLE route(AUTHORIZATION_CHECK); #!endif + #!ifdef MESSAGE_ROLE route(HANDLE_MESSAGE); + #!else + if (is_method("MESSAGE")) { + sl_send_reply("405", "Method Not Allowed"); + exit; + } + #!endif #!ifdef PRESENCE_ROLE route(HANDLE_SUBSCRIBE); @@ -486,48 +495,6 @@ route[HANDLE_NOTIFY] exit; } -route[HANDLE_MESSAGE] -{ - #!ifdef MESSAGE_ROLE - if (is_method("MESSAGE")) { - xlog("L_INFO", "$ci|MESSAGE from $fu to $tu\n"); - if (isflagset(FLAG_INTERNALLY_SOURCED) || src_ip == myself) { - xlog("L_INFO", "$ci| routing MESSAGE to external from $fu to $tu\n"); - if (registered("location")) { - lookup("location"); - xlog("L_INFO", "$ci|log|routing to $ruid\n"); - } - #!ifdef TRAFFIC_FILTER_ROLE - } else if (!isflagset(FLAG_TRUSTED_SOURCE) - && $rd =~ "[0-9]{1,3}\.[0-9]{1,3}.[0-9]{1,3}\.[0-9]{1,3}") { - xlog("L_WARN", "$ci|end|dropping MESSAGE request with IP domain\n"); - exit(); - #!endif - } else { - xlog("L_WARN", "$ci|end|MESSAGE $hdr(Content-Type)\n"); - if( $hdr(Content-Type) == "application/im-iscomposing+xml" ) { - xlog("L_WARN", "$ci|end|dropping MESSAGE application/im-iscomposing+xml\n"); - sl_send_reply("200", "OK"); - } else if( $hdr(Content-Type) == "message/imdn+xml" ) { - xlog("L_WARN", "$ci|end|dropping MESSAGE message/imdn+xml\n"); - sl_send_reply("202", "Accepted"); - } else { - route(DISPATCHER_FIND_ROUTES); - } - } - route(RELAY); - } - #!else - if (is_method("MESSAGE")) { - if (!isflagset(FLAG_INTERNALLY_SOURCED)) { - sl_send_reply("202", "delivered to /dev/null"); - exit(); - } - route(RELAY); - } - #!endif -} - route[HANDLE_REFER] { if (!is_method("REFER")) { @@ -691,7 +658,7 @@ branch_route[MANAGE_BRANCH] { #!endif route(BRANCH_HEADERS); - + route_if_exists("CUSTOM_BRANCH_HEADERS"); } route[RELAY] @@ -702,10 +669,6 @@ route[RELAY] if (isflagset(FLAG_INTERNALLY_SOURCED)) { route(INTERNAL_TO_EXTERNAL_RELAY); - #!ifdef MESSAGE_ROLE - } else if (is_method("MESSAGE") && src_ip == myself) { - route(INTERNAL_TO_EXTERNAL_RELAY); - #!endif } else { route(EXTERNAL_TO_INTERNAL_RELAY); } @@ -1000,5 +963,6 @@ route[LOCAL_REQUEST] exit; } +#!import_file "custom-routes.cfg" # vim: tabstop=4 softtabstop=4 shiftwidth=4 expandtab diff --git a/kamailio/defs.cfg b/kamailio/defs.cfg index 753828b..4a19595 100644 --- a/kamailio/defs.cfg +++ b/kamailio/defs.cfg @@ -27,6 +27,7 @@ #!trydef PV_BUFFER_SIZE 16384 #!trydef PV_BUFFER_SLOTS 30 +#!trydef KZ_MEM_SUMMARY 0 #!trydef KZ_DB_MODULE kazoo #!substdef "!KAMAILIO_DBMS!$def(KZ_DB_MODULE)!g" diff --git a/kamailio/message-role.cfg b/kamailio/message-role.cfg index 1122a1e..53e5cdb 100644 --- a/kamailio/message-role.cfg +++ b/kamailio/message-role.cfg @@ -1,5 +1,52 @@ modparam("htable", "htable", "msg=>size=32;autoexpire=60;") +route[HANDLE_MESSAGE] +{ + if (!is_method("MESSAGE")) return; + + xlog("L_INFO", "$ci|log|MESSAGE from $fu to $tu\n"); + if (isflagset(FLAG_INTERNALLY_SOURCED) || src_ip == myself) { + xlog("L_INFO", "$ci| routing MESSAGE to external from $fu to $tu\n"); + if (registered("location")) { + lookup("location"); + xlog("L_INFO", "$ci|log|routing to $ruid\n"); + } else { + xlog("L_INFO", "$ci|end|user is not registered\n"); + append_to_reply("$def(AOR_NOT_SUBSCRIBED_REASON)\r\n"); + sl_send_reply("410", "Not registered"); + exit; + } + } else { + route(AUTH); + $xavp(regcfg=>match_received) = $su; + if($avp(is_registred) != "true") { + sl_send_reply("403", "Forbidden"); + exit; + } else { + if($hdr(Content-Type) == "text/plain") { + route(MESSAGE_INBOUND); + } else { + xlog("L_WARN", "$ci|end|dropping MESSAGE $hdr(Content-Type)\n"); + sl_send_reply("200", "OK"); + exit; + } + } + } +} + +route[MESSAGE_INBOUND] +{ + route(AUTH_HEADERS_JSON); + $var(Payload) = $_s({"Event-Category" : "message", "Event-Name" : "inbound", "Call-ID" : "$ci", "Message-ID" : "$ci", "Route-Type" : "on-net", "Route-ID" : "sip", "From" : "$fU@$fd", "To" : "$tU@$td", "Request" : "$rU@$rd", "Caller-ID-Number" : "$fU", "Callee-ID-Number" : "$tU", "Body" : "$rb", "Custom-SIP-Headers" : $var(headers_json), "Msg-ID" : "$uuid(g)"}); + + $var(exchange) = "sms"; + $var(RoutingKey) = $_s(message.inbound.sip.$(ci{kz.encode})); + xlog("L_INFO", "$ci|msg|sending inbound message $var(RoutingKey) => $var(Payload)\n"); + kazoo_publish($var(exchange), $var(RoutingKey), $var(Payload)); + sl_send_reply("200", "OK"); + exit; + +} event_route[kazoo:consumer-event-message-route] { @@ -39,7 +86,7 @@ route[MESSAGE_REPLY] $var(Result) = "Success"; } - $var(Payload) = '{ "Event-Category" : "message", "Event-Name" : "delivery", "Call-ID" : "$(sht(msg=>$ci){kz.json,Call-ID})", "Message-ID" : "$(sht(msg=>$ci){kz.json,Message-ID})" , "Delivery-Result-Code" : "sip:$T_reply_code", "Msg-ID" : "$(sht(msg=>$ci){kz.json,Msg-ID})" , "Status" : "$var(Result)"}'; + $var(Payload) = $_s({ "Event-Category" : "message", "Event-Name" : "delivery", "Call-ID" : "$(sht(msg=>$ci){kz.json,Call-ID})", "Message-ID" : "$(sht(msg=>$ci){kz.json,Message-ID})" , "Delivery-Result-Code" : "sip:$T_reply_code", "Msg-ID" : "$(sht(msg=>$ci){kz.json,Msg-ID})" , "Status" : "$var(Result)"}); $var(RoutingKey) = $(sht(msg=>$ci){kz.json,Server-ID}); $var(exchange) = "targeted"; @@ -54,7 +101,7 @@ route[MESSAGE_REPLY] route[MESSAGE_BINDINGS] { $var(key) = "kamailio@MY_HOSTNAME"; - $var(payload) = $_s({"name": "sms", "exchange": "sms", "type": "topic", "queue": "MSG-QUEUE-MY_HOSTNAME", "routing": "message.route.$(var(key){kz.encode}).*", "no_ack": 0 }); + $var(payload) = $_s({"name": "sms", "exchange": "sms", "type": "topic", "queue": "MSG-QUEUE-MY_HOSTNAME", "routing": "message.route.$(var(key){kz.encode}).*", "no_ack": false }); kazoo_subscribe("$var(payload)"); } diff --git a/kamailio/nodes-role.cfg b/kamailio/nodes-role.cfg index cb985a2..f1a8fbd 100644 --- a/kamailio/nodes-role.cfg +++ b/kamailio/nodes-role.cfg @@ -136,10 +136,16 @@ route[CHECK_MEDIA_SERVERS] $var(ProfileIdx) = 0; while( $var(ProfileIdx) < $var(ProfileCount) ) { $var(MediaProfile) = $(avp(ProfileKeys)[$var(ProfileIdx)]); - $var(MediaRawUrl) = $(var(Payload){kz.json,Media-Servers.$var(MediaKey).Interfaces.$var(MediaProfile).URL}); + $var(MediaRawUrl) = $(var(Payload){kz.json,Media-Servers.$var(MediaKey).Interfaces.$var(MediaProfile).info.url}); + if($var(MediaRawUrl) == "") { + $var(MediaRawUrl) = $(var(Payload){kz.json,Media-Servers.$var(MediaKey).Interfaces.$var(MediaProfile).URL}); + } $var(MediaUrl) = $(var(MediaRawUrl){re.subst,/^sip:(.*)@(.*)/sip:\2/}); $var(MediaInstanceUUID) = $(var(Payload){kz.json,Media-Servers.$var(MediaKey).Instance-UUID}); - $var(MediaIP) = $(var(Payload){kz.json,Media-Servers.$var(MediaKey).Interfaces.$var(MediaProfile).SIP-IP}); + $var(MediaIP) = $(var(Payload){kz.json,Media-Servers.$var(MediaKey).Interfaces.$var(MediaProfile).info.sip-ip}); + if($var(MediaIP) == "") { + $var(MediaIP) = $(var(Payload){kz.json,Media-Servers.$var(MediaKey).Interfaces.$var(MediaProfile).SIP-IP}); + } if($shtinc(media=>$var(MediaUrl)::count) == 1) { $sht(media=>$var(MediaUrl)::zone) = $var(Zone); $shtex(media=>$var(MediaUrl)::zone) = 0; @@ -207,7 +213,7 @@ route[MEDIA_SERVER_RESTART] #!ifndef NODES_CUSTOM_BINDINGS route[NODES_BINDINGS] { - $var(payload) = $_s({"name": "nodes", "exchange" : "nodes" , "type" : "fanout", "queue" : "nodes-MY_HOSTNAME", "exclusive":0, "federate":1 }); + $var(payload) = $_s({"name": "nodes", "exchange" : "nodes" , "type" : "fanout", "queue" : "nodes-MY_HOSTNAME", "exclusive": false, "federate": true }); kazoo_subscribe("$var(payload)"); } #!endif diff --git a/kamailio/presence-fast-pickup.cfg b/kamailio/presence-fast-pickup.cfg index 692a32e..77669a6 100644 --- a/kamailio/presence-fast-pickup.cfg +++ b/kamailio/presence-fast-pickup.cfg @@ -60,11 +60,11 @@ route[PRESENCE_FAST_PICKUP_ATTEMPT] route(PRESENCE_FAST_PICKUP_OPTION); $du = $sht(fp=>$var(md5)); append_hf("Replaces: $var(replaced_call_id)$var(Pickup)\r\n"); + append_hf("k-cid: $var(replaced_call_id)\r\n"); xlog("L_INFO", "$ci|log|fast|found shortcut for call-id $var(replaced_call_id) , redirecting ($(ru{uri.user})) to $du\n"); route(RELAY); exit(); } else { - $var(replaced_call_id) = "none"; xlog("L_INFO", "$ci|log|fast|shortcut $var(md5) invalid in this server, using standard routing\n"); } } @@ -88,6 +88,7 @@ route[PRESENCE_FAST_PICKUP_ATTEMPT] } remove_hf_re("^Replaces"); append_hf("Replaces: $var(rep)\r\n"); + append_hf("k-cid: $var(replaced_call_id)\r\n"); xlog("L_INFO", "$ci|log|call-id $var(replaced_call_id) found, redirecting call ($(ru{uri.user})) to $du => $var(rep)\n"); route(RELAY); exit(); @@ -108,6 +109,7 @@ route[PRESENCE_FAST_PICKUP_ATTEMPT] $var(park) = $_s(*3$(ru{uri.user}{s.substr,2,0})@$(ru{uri.domain})); if($sht(park=>$var(park)) != $null) { $du = $sht(park=>$var(park)); + append_hf("k-cid: $sht(park=>$var(park)::callid)\r\n"); xlog("L_INFO", "$ci|log|redirecting park request to $du , callid : $sht(park=>$var(park)::callid)\n"); route(RELAY); exit(); @@ -117,6 +119,7 @@ route[PRESENCE_FAST_PICKUP_ATTEMPT] if($sht(park=>$(ru{uri.user})@$(ru{uri.domain})) != $null) { $du = $sht(park=>$(ru{uri.user})@$(ruri{uri.domain})); + append_hf("k-cid: $sht(park=>$(ru{uri.user})@$(ruri{uri.domain})::callid)\r\n"); xlog("L_INFO", "$ci|log|redirecting park request to $du, callid: $sht(park=>$(ru{uri.user})@$(ruri{uri.domain})::callid)\n"); route(RELAY); exit(); @@ -135,6 +138,7 @@ route[PRESENCE_FAST_PICKUP_ATTEMPT] $du = $(kzR{kz.json,Channels[0].switch_url}); if($du != $null) { xlog("L_INFO", "$ci|log|redirecting park request to $du from realtime query reply\n"); + append_hf("k-cid: $(kzR{kz.json,Channels[0].call_id})\r\n"); route(RELAY); exit(); } diff --git a/kamailio/presence-role.cfg b/kamailio/presence-role.cfg index e7e4815..0f73535 100644 --- a/kamailio/presence-role.cfg +++ b/kamailio/presence-role.cfg @@ -392,12 +392,10 @@ event_route[kazoo:consumer-event-presence-mwi-update] pres_refresh_watchers("$(kzE{kz.json,From})", "message-summary", 1); } else { #!ifdef PRESENCE_TRACK_ALL_PKG_MWI - if($(kzE{kz.json,Event-Package}) == "message-summary") { - $var(kz_presence_log_level) = $(sel(cfg_get.kazoo.presence_with_targets_log_level){s.int}); - xlog("L_DEBUG", "$(var(payload){kz.json,Call-ID})|log|saving $var(presentity) from mwi update => $var(payload)\n"); - if(kazoo_pua_publish_mwi($kzE) != 1) { - xlog("L_ERR", "$(var(payload){kz.json,Call-ID})|log|error publishing $var(presentity) mwi update => $var(payload)\n"); - } + $var(kz_presence_log_level) = $(sel(cfg_get.kazoo.presence_with_targets_log_level){s.int}); + xlog("L_DEBUG", "$(var(payload){kz.json,Call-ID})|log|saving $var(presentity) from mwi update => $var(payload)\n"); + if(kazoo_pua_publish_mwi($kzE) != 1) { + xlog("L_ERR", "$(var(payload){kz.json,Call-ID})|log|error publishing $var(presentity) mwi update => $var(payload)\n"); } #!else xlog("L_DEBUG", "$(var(payload){kz.json,Call-ID})|log|skip mwi update for $var(presentity)\n"); @@ -546,7 +544,7 @@ route[PRESENCE_BINDINGS] #!import_file "presence-custom-bindings.cfg" #!ifndef PRESENCE_CUSTOM_BINDINGS - $var(payload) = $_s({ "name" : "presence", "exchange" : "presence", "type" : "topic", "queue" : "presence-dialog-MY_HOSTNAME", "routing" : ["dialog.*.*", "update.*.*", "mwi_updates.*.*"], "exclusive" : 0, "federate" : 1 }); + $var(payload) = $_s({ "name" : "presence", "exchange" : "presence", "type" : "topic", "queue" : "presence-dialog-MY_HOSTNAME", "routing" : ["dialog.*.*", "update.*.*", "mwi_updates.*.*"], "exclusive" : false, "federate" : true }); kazoo_subscribe("$var(payload)"); #!endif @@ -561,7 +559,7 @@ route[PRESENCE_API_BINDINGS] #!import_file "presence-api-custom-bindings.cfg" #!ifndef PRESENCE_API_CUSTOM_BINDINGS - $var(payload) = $_s({"name": "presence-api", "exchange": "presence", "type": "topic", "queue": "presence-api-MY_HOSTNAME", "routing": ["presence.search_req.*", "presence.reset.*.*"], "exclusive": 0, "federate": 1 }); + $var(payload) = $_s({"name": "presence-api", "exchange": "presence", "type": "topic", "queue": "presence-api-MY_HOSTNAME", "routing": ["presence.search_req.*", "presence.reset.*.*"], "exclusive": false, "federate": true }); kazoo_subscribe("$var(payload)"); #!endif From 5671d7893f59b590783f679e9f6414e62540b284 Mon Sep 17 00:00:00 2001 From: lazedo Date: Mon, 10 Jun 2019 01:47:26 +0000 Subject: [PATCH 2/9] use internal routing for messages --- kamailio/default.cfg | 5 --- kamailio/message-role.cfg | 95 +++++++++++++++++++++------------------ 2 files changed, 52 insertions(+), 48 deletions(-) diff --git a/kamailio/default.cfg b/kamailio/default.cfg index b1fb648..05c8ec3 100644 --- a/kamailio/default.cfg +++ b/kamailio/default.cfg @@ -730,11 +730,6 @@ onreply_route[EXTERNAL_REPLY] route(DOS_PREVENTION); #!endif - #!ifdef MESSAGE_ROLE - if (is_method("MESSAGE")) { - route(MESSAGE_REPLY); - } - #!endif } onreply_route[INTERNAL_REPLY] diff --git a/kamailio/message-role.cfg b/kamailio/message-role.cfg index 53e5cdb..7b08b34 100644 --- a/kamailio/message-role.cfg +++ b/kamailio/message-role.cfg @@ -1,36 +1,24 @@ -modparam("htable", "htable", "msg=>size=32;autoexpire=60;") route[HANDLE_MESSAGE] { if (!is_method("MESSAGE")) return; + if (isflagset(FLAG_INTERNALLY_SOURCED) || src_ip == myself) return; + xlog("L_INFO", "$ci|log|MESSAGE from $fu to $tu\n"); - if (isflagset(FLAG_INTERNALLY_SOURCED) || src_ip == myself) { - xlog("L_INFO", "$ci| routing MESSAGE to external from $fu to $tu\n"); - if (registered("location")) { - lookup("location"); - xlog("L_INFO", "$ci|log|routing to $ruid\n"); - } else { - xlog("L_INFO", "$ci|end|user is not registered\n"); - append_to_reply("$def(AOR_NOT_SUBSCRIBED_REASON)\r\n"); - sl_send_reply("410", "Not registered"); - exit; - } + route(AUTH); + $xavp(regcfg=>match_received) = $su; + if($avp(is_registered) != "true") { + sl_send_reply("403", "Forbidden"); + exit; } else { - route(AUTH); - $xavp(regcfg=>match_received) = $su; - if($avp(is_registred) != "true") { - sl_send_reply("403", "Forbidden"); + if($hdr(Content-Type) == "text/plain") { + route(MESSAGE_INBOUND); + } else { + xlog("L_WARN", "$ci|end|dropping MESSAGE $hdr(Content-Type)\n"); + sl_send_reply("200", "OK"); exit; - } else { - if($hdr(Content-Type) == "text/plain") { - route(MESSAGE_INBOUND); - } else { - xlog("L_WARN", "$ci|end|dropping MESSAGE $hdr(Content-Type)\n"); - sl_send_reply("200", "OK"); - exit; - } - } + } } } @@ -53,7 +41,7 @@ event_route[kazoo:consumer-event-message-route] $var(uri_username) = ""; kazoo_json($kzE, "Endpoints[0].To-Username", "$var(uri_username)"); $var(x) = $(kzE{kz.json,Endpoints[0].To-Username}); - xlog("L_INFO", "received message route for $(kzE{kz.json,Endpoints[0].To-DID})\n"); + xlog("L_INFO", "$(kzE{kz.json,Call-ID})|log|received message route for $(kzE{kz.json,Endpoints[0].To-DID})\n"); if( $var(uri_username) != "" ) { $var(from_uri) = "sip:" + $(kzE{kz.json,Caller-ID-Number}) + "@" + $(kzE{kz.json,Endpoints[0].To-Realm}); $var(to_uri) = "sip:" + $(kzE{kz.json,Endpoints[0].To-Username}) + "@" + $(kzE{kz.json,Endpoints[0].To-Realm}); @@ -61,40 +49,61 @@ event_route[kazoo:consumer-event-message-route] $var(from_uri) = "sip:" + $(kzE{kz.json,Caller-ID-Number}) + $(kzE{kz.json,Endpoints[0].To-Realm}); $var(to_uri) = $(kzE{kz.json,Endpoints[0].Route}); } - $sht(msg=>$(kzE{kz.json,Call-ID})) = $kzE; $uac_req(method)="MESSAGE"; - $uac_req(body)= $(kzE{kz.json,Body}); - $uac_req(hdrs)="X-KAZOO-AOR: " + $var(to_uri)+ "\r\nContent-Type: text/plain\r\n"; + $uac_req(body)= $kzE; + $uac_req(hdrs)="X-TM-Local: MESSAGE_ROUTE\r\nX-KAZOO-AOR: " + $var(to_uri)+ "\r\nContent-Type: text/plain\r\n"; $uac_req(turi) = $var(to_uri); $uac_req(ruri) = $var(to_uri); $uac_req(furi) = $var(from_uri); - $uac_req(ouri) = "sip:MY_IP_ADDRESS:5060"; + $uac_req(ouri) = "sip:127.0.0.1:5090;transport=tcp"; $uac_req(callid) = $(kzE{kz.json,Call-ID}); - xlog("L_INFO", "sending message from $var(from_uri) to $var(to_uri) \n"); + xlog("L_INFO", "$(kzE{kz.json,Call-ID})|log|sending message from $var(from_uri) to $var(to_uri) \n"); uac_req_send(); } -route[MESSAGE_REPLY] +route[MESSAGE_ROUTE] { - if( $(sht(msg=>$ci)) == $null) { - exit(); - } + remove_hf_re("^X-"); - if($T_reply_code != 200 && $T_reply_code != 202) { - $var(Result) = "Failure"; - } else { - $var(Result) = "Success"; + route(ROUTE_TO_AOR); + $var(JObj) = $rb; + set_body("$(var(JObj){kz.json,Body})", "text/plain"); + $avp(message_id) = $(var(JObj){kz.json,Message-ID}); + $avp(msg_id) = $(var(JObj){kz.json,Msg-ID}); + $avp(server_id) = $(var(JObj){kz.json,Server-ID}); + t_on_reply("MESSAGE_REPLY"); + t_on_failure("MESSAGE_FAULT"); + + t_relay(); + +} + +onreply_route[MESSAGE_REPLY] +{ + $var(Payload) = $_s({ "Event-Category" : "message", "Event-Name" : "delivery", "Call-ID" : "$ci", "Message-ID" : "$avp(message_id)" , "Delivery-Result-Code" : "sip:$T_reply_code", "Msg-ID" : "$avp(msg_id)" , "Status" : "Success"}); + + $var(RoutingKey) = $avp(server_id); + $var(exchange) = "targeted"; + if($var(RoutingKey) == "") { + $var(exchange) = "sms"; + $var(RoutingKey) = "message.delivery." + $(ci{kz.encode}); } + xlog("L_INFO", "$ci|log|sending delivery message\n"); + kazoo_publish($var(exchange), $var(RoutingKey), $var(Payload)); +} + +failure_route[MESSAGE_FAULT] +{ - $var(Payload) = $_s({ "Event-Category" : "message", "Event-Name" : "delivery", "Call-ID" : "$(sht(msg=>$ci){kz.json,Call-ID})", "Message-ID" : "$(sht(msg=>$ci){kz.json,Message-ID})" , "Delivery-Result-Code" : "sip:$T_reply_code", "Msg-ID" : "$(sht(msg=>$ci){kz.json,Msg-ID})" , "Status" : "$var(Result)"}); + $var(Payload) = $_s({ "Event-Category" : "message", "Event-Name" : "delivery", "Call-ID" : "$ci", "Message-ID" : "$avp(message_id)" , "Delivery-Result-Code" : "sip:$T_reply_code", "Msg-ID" : "$avp(msg_id)" , "Status" : "Failure"}); - $var(RoutingKey) = $(sht(msg=>$ci){kz.json,Server-ID}); + $var(RoutingKey) = $avp(server_id); $var(exchange) = "targeted"; if($var(RoutingKey) == "") { $var(exchange) = "sms"; - $var(RoutingKey) = "message.delivery." + $(sht(msg=>$ci){kz.json,Call-ID}{kz.encode}); + $var(RoutingKey) = "message.delivery." + $(ci{kz.encode}); } - xlog("L_INFO", "sending delivery message for $ci\n"); + xlog("L_INFO", "$ci|log|sending delivery message\n"); kazoo_publish($var(exchange), $var(RoutingKey), $var(Payload)); } From fe1e4d0ede629612af48e3b6b5b936903bf5021c Mon Sep 17 00:00:00 2001 From: lazedo Date: Mon, 10 Jun 2019 01:48:20 +0000 Subject: [PATCH 3/9] fix spelling --- kamailio/auth.cfg | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kamailio/auth.cfg b/kamailio/auth.cfg index ffa5c32..e85e657 100644 --- a/kamailio/auth.cfg +++ b/kamailio/auth.cfg @@ -57,11 +57,11 @@ route[SETUP_AUTH_HEADERS] #!ifdef REGISTRAR_ROLE - $avp(is_registred) = "false"; + $avp(is_registered) = "false"; $xavp(regcfg=>match_received) = $su; if (registered("location","$fu", 2, 1) != 1) return; - $avp(is_registred) = "true"; + $avp(is_registered) = "true"; #!ifdef WITH_AUTH_TOKEN route(AUTH_TOKEN); #!else From 3e5b6dc898dc3093ab4f27f223788b695a99179a Mon Sep 17 00:00:00 2001 From: lazedo Date: Tue, 11 Jun 2019 16:08:33 +0000 Subject: [PATCH 4/9] add federation and simplify reply handling --- kamailio/message-role.cfg | 29 +++++++++-------------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/kamailio/message-role.cfg b/kamailio/message-role.cfg index 7b08b34..349073b 100644 --- a/kamailio/message-role.cfg +++ b/kamailio/message-role.cfg @@ -80,37 +80,26 @@ route[MESSAGE_ROUTE] onreply_route[MESSAGE_REPLY] { - $var(Payload) = $_s({ "Event-Category" : "message", "Event-Name" : "delivery", "Call-ID" : "$ci", "Message-ID" : "$avp(message_id)" , "Delivery-Result-Code" : "sip:$T_reply_code", "Msg-ID" : "$avp(msg_id)" , "Status" : "Success"}); - - $var(RoutingKey) = $avp(server_id); - $var(exchange) = "targeted"; - if($var(RoutingKey) == "") { - $var(exchange) = "sms"; - $var(RoutingKey) = "message.delivery." + $(ci{kz.encode}); + if($avp(server_id) == "") { + xlog("L_INFO", "$ci|log|sending success delivery message to $avp(server_id)\n"); + $var(Payload) = $_s({"Event-Category" : "message", "Event-Name" : "delivery", "Call-ID" : "$ci", "Message-ID" : "$avp(message_id)" , "Delivery-Result-Code" : "sip:$T_reply_code", "Msg-ID" : "$avp(msg_id)" , "Status" : "Success"}); + kazoo_publish("targeted", "$avp(server_id)", $var(Payload)); } - xlog("L_INFO", "$ci|log|sending delivery message\n"); - kazoo_publish($var(exchange), $var(RoutingKey), $var(Payload)); } failure_route[MESSAGE_FAULT] { - - $var(Payload) = $_s({ "Event-Category" : "message", "Event-Name" : "delivery", "Call-ID" : "$ci", "Message-ID" : "$avp(message_id)" , "Delivery-Result-Code" : "sip:$T_reply_code", "Msg-ID" : "$avp(msg_id)" , "Status" : "Failure"}); - - $var(RoutingKey) = $avp(server_id); - $var(exchange) = "targeted"; - if($var(RoutingKey) == "") { - $var(exchange) = "sms"; - $var(RoutingKey) = "message.delivery." + $(ci{kz.encode}); + if($avp(server_id) != "") { + xlog("L_INFO", "$ci|log|sending failure delivery message to $avp(server_id)\n"); + $var(Payload) = $_s({"Event-Category" : "message", "Event-Name" : "delivery", "Call-ID" : "$ci", "Message-ID" : "$avp(message_id)" , "Delivery-Result-Code" : "sip:$T_reply_code", "Msg-ID" : "$avp(msg_id)" , "Status" : "Failure"}); + kazoo_publish("targeted", "$avp(server_id)", $var(Payload)); } - xlog("L_INFO", "$ci|log|sending delivery message\n"); - kazoo_publish($var(exchange), $var(RoutingKey), $var(Payload)); } route[MESSAGE_BINDINGS] { $var(key) = "kamailio@MY_HOSTNAME"; - $var(payload) = $_s({"name": "sms", "exchange": "sms", "type": "topic", "queue": "MSG-QUEUE-MY_HOSTNAME", "routing": "message.route.$(var(key){kz.encode}).*", "no_ack": false }); + $var(payload) = $_s({"name": "sms", "exchange": "sms", "type": "topic", "queue": "MSG-QUEUE-MY_HOSTNAME", "routing": "message.route.$(var(key){kz.encode}).*", "no_ack": false, "federate": true }); kazoo_subscribe("$var(payload)"); } From aabec7673ea0e9cb8b3b0e0a23cc3468574c1811 Mon Sep 17 00:00:00 2001 From: lazedo Date: Tue, 11 Jun 2019 16:46:53 +0000 Subject: [PATCH 5/9] use sms exchange for delivery report --- kamailio/message-role.cfg | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/kamailio/message-role.cfg b/kamailio/message-role.cfg index 349073b..55c9766 100644 --- a/kamailio/message-role.cfg +++ b/kamailio/message-role.cfg @@ -80,20 +80,18 @@ route[MESSAGE_ROUTE] onreply_route[MESSAGE_REPLY] { - if($avp(server_id) == "") { - xlog("L_INFO", "$ci|log|sending success delivery message to $avp(server_id)\n"); - $var(Payload) = $_s({"Event-Category" : "message", "Event-Name" : "delivery", "Call-ID" : "$ci", "Message-ID" : "$avp(message_id)" , "Delivery-Result-Code" : "sip:$T_reply_code", "Msg-ID" : "$avp(msg_id)" , "Status" : "Success"}); - kazoo_publish("targeted", "$avp(server_id)", $var(Payload)); - } + xlog("L_INFO", "$ci|log|sending success delivery message to $avp(server_id)\n"); + $var(Payload) = $_s({"Event-Category" : "message", "Event-Name" : "delivery", "Call-ID" : "$ci", "Message-ID" : "$avp(message_id)" , "Delivery-Result-Code" : "sip:$T_reply_code", "Msg-ID" : "$avp(msg_id)" , "Status" : "Success"}); + $var(RK) = "message.delivery." + $(ci{kz.encode}); + kazoo_publish("sms", $var(RK), $var(Payload)); } failure_route[MESSAGE_FAULT] { - if($avp(server_id) != "") { - xlog("L_INFO", "$ci|log|sending failure delivery message to $avp(server_id)\n"); - $var(Payload) = $_s({"Event-Category" : "message", "Event-Name" : "delivery", "Call-ID" : "$ci", "Message-ID" : "$avp(message_id)" , "Delivery-Result-Code" : "sip:$T_reply_code", "Msg-ID" : "$avp(msg_id)" , "Status" : "Failure"}); - kazoo_publish("targeted", "$avp(server_id)", $var(Payload)); - } + xlog("L_INFO", "$ci|log|sending failure delivery message to $avp(server_id)\n"); + $var(Payload) = $_s({"Event-Category" : "message", "Event-Name" : "delivery", "Call-ID" : "$ci", "Message-ID" : "$avp(message_id)" , "Delivery-Result-Code" : "sip:$T_reply_code", "Msg-ID" : "$avp(msg_id)" , "Status" : "Failure"}); + $var(RK) = "message.delivery." + $(ci{kz.encode}); + kazoo_publish("sms", $var(RK), $var(Payload)); } route[MESSAGE_BINDINGS] From 784cb7056145fce5a4cf342c60d19b14a29b28ef Mon Sep 17 00:00:00 2001 From: lazedo Date: Tue, 11 Jun 2019 17:14:10 +0000 Subject: [PATCH 6/9] this is a targeted reply --- kamailio/message-role.cfg | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/kamailio/message-role.cfg b/kamailio/message-role.cfg index 55c9766..08cddc5 100644 --- a/kamailio/message-role.cfg +++ b/kamailio/message-role.cfg @@ -82,16 +82,18 @@ onreply_route[MESSAGE_REPLY] { xlog("L_INFO", "$ci|log|sending success delivery message to $avp(server_id)\n"); $var(Payload) = $_s({"Event-Category" : "message", "Event-Name" : "delivery", "Call-ID" : "$ci", "Message-ID" : "$avp(message_id)" , "Delivery-Result-Code" : "sip:$T_reply_code", "Msg-ID" : "$avp(msg_id)" , "Status" : "Success"}); - $var(RK) = "message.delivery." + $(ci{kz.encode}); - kazoo_publish("sms", $var(RK), $var(Payload)); + $var(exchange) = "targeted"; + $var(RK) = $avp(server_id); + kazoo_publish($var(exchange), $var(RK), $var(Payload)); } failure_route[MESSAGE_FAULT] { xlog("L_INFO", "$ci|log|sending failure delivery message to $avp(server_id)\n"); $var(Payload) = $_s({"Event-Category" : "message", "Event-Name" : "delivery", "Call-ID" : "$ci", "Message-ID" : "$avp(message_id)" , "Delivery-Result-Code" : "sip:$T_reply_code", "Msg-ID" : "$avp(msg_id)" , "Status" : "Failure"}); - $var(RK) = "message.delivery." + $(ci{kz.encode}); - kazoo_publish("sms", $var(RK), $var(Payload)); + $var(exchange) = "targeted"; + $var(RK) = $avp(server_id); + kazoo_publish($var(exchange), $var(RK), $var(Payload)); } route[MESSAGE_BINDINGS] From 55c794c00818ba9a17eecac5ced50ca3547f6599 Mon Sep 17 00:00:00 2001 From: lazedo Date: Wed, 12 Jun 2019 13:07:35 +0000 Subject: [PATCH 7/9] keep us in the loop for invite in-dialog requests --- kamailio/default.cfg | 1 + 1 file changed, 1 insertion(+) diff --git a/kamailio/default.cfg b/kamailio/default.cfg index 05c8ec3..3cf8c34 100644 --- a/kamailio/default.cfg +++ b/kamailio/default.cfg @@ -522,6 +522,7 @@ route[HANDLE_IN_DIALOG_REQUESTS] if (is_method("INVITE")) { setflag(FLAG_SESSION_PROGRESS); + record_route(); } if (loose_route()) { From dcb813d7f278f5a5c049709ae909eeaff3677d05 Mon Sep 17 00:00:00 2001 From: lazedo Date: Mon, 24 Jun 2019 16:16:03 +0000 Subject: [PATCH 8/9] add macro for presence_use_full_entity some UAs need the full entity user@domain enable it in local.cfg with #!trydef KZ_PUA_PRESENCE_USE_FULL_ENTITY 1 --- kamailio/kazoo-bindings.cfg | 3 +++ 1 file changed, 3 insertions(+) diff --git a/kamailio/kazoo-bindings.cfg b/kamailio/kazoo-bindings.cfg index d63bcf7..292dd80 100644 --- a/kamailio/kazoo-bindings.cfg +++ b/kamailio/kazoo-bindings.cfg @@ -2,6 +2,8 @@ ### ### +#!trydef KZ_PUA_PRESENCE_USE_FULL_ENTITY 0 + ####### Kazoo Integration module ########## loadmodule "kazoo.so" modparam("kazoo", "pua_mode", MY_AMQP_PUA_MODE) @@ -12,6 +14,7 @@ modparam("kazoo", "amqp_heartbeats", MY_AMQP_HEARTBEATS) modparam("kazoo", "amqp_max_channels", MY_AMQP_MAX_CHANNELS) modparam("kazoo", "amqp_consumer_processes", MY_AMQP_CONSUMER_PROCESSES) modparam("kazoo", "amqp_consumer_workers", MY_AMQP_CONSUMER_WORKERS) +modparam("kazoo", "presence_use_full_entity", KZ_PUA_PRESENCE_USE_FULL_ENTITY) ## amqp connections #!ifdef MY_AMQP_URL modparam("kazoo", "amqp_connection", "MY_AMQP_URL") From fee7835182726fde3f345a87e03f9b94fc2d32fa Mon Sep 17 00:00:00 2001 From: lazedo Date: Tue, 23 Jul 2019 10:59:40 +0000 Subject: [PATCH 9/9] remove double escape from json (cherry picked from commit 7a86589bd39e5a879c636154ef21615d4c3ab3c9) --- kamailio/presence-query.cfg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kamailio/presence-query.cfg b/kamailio/presence-query.cfg index 95fa96d..04bbb41 100644 --- a/kamailio/presence-query.cfg +++ b/kamailio/presence-query.cfg @@ -77,7 +77,7 @@ route[PRESENCE_SEARCH_DETAIL] while($xavp(ra) != $null) { $var(Event) = $xavp(ra=>event); while($xavp(ra) != $null && $var(Event) == $xavp(ra=>event)) { - $var(Sub) = $_s("$var(Username)" : {"$xavp(ra=>event)" : { "$xavp(ra=>watcher_username)" : {"kamailio@MY_HOSTNAME" : {"$xavp(ra=>callid)" : {"time" : $xavp(ra=>time), "result" : $xavp(ra=>result), "sent" : "$(xavp(ra=>sent_msg){s.escape.common}{s.replace,\','}{s.replace,$$,})", "received" : "$(xavp(ra=>received_msg){s.escape.common}{s.replace,\','}{s.replace,$$,})", "user_agent" : "$(xavp(ra=>user_agent){s.escape.common}{s.replace,\','}{s.replace,$$,})"}}}}}); + $var(Sub) = $_s("$var(Username)" : {"$xavp(ra=>event)" : { "$xavp(ra=>watcher_username)" : {"kamailio@MY_HOSTNAME" : {"$xavp(ra=>callid)" : {"time" : $xavp(ra=>time), "result" : $xavp(ra=>result), "sent" : "$(xavp(ra=>sent_msg){s.escape.common}{s.replace,\','}{s.replace,$$,}{re.subst,/\\\\\\"/\\"/g})", "received" : "$(xavp(ra=>received_msg){s.escape.common}{s.replace,\','}{s.replace,$$,})", "user_agent" : "$(xavp(ra=>user_agent){s.escape.common}{s.replace,\','}{s.replace,$$,})"}}}}}); xlog("L_DEBUG", "$ci| RESULT \"Subscriptions\" : { $var(Sub) }\n"); $var(amqp_payload_request) = '{"Event-Category" : "presence", "Event-Name" : "search_partial_resp", "Msg-ID" : "$var(Msg-ID)", "Subscriptions" : { $var(Sub) } }'; kazoo_publish("targeted", "$var(Queue)", $var(amqp_payload_request));