如何在 Freeswitch 中获取 SIP header 的值?

How to get value of SIP header in Freeswitch?

我需要获取 CALLED_DID header 的值并在拨号方案中做一些操作,但我不知道如何做。

我尝试使用 ${sip_h_CALLED_DID} 但它是空的,因为在 header 名称之前没有 X- 前缀。

是否有任何其他方法可以从 SIP 中提取价值header?

请帮帮我,我在网上翻遍了,但找不到答案。

INVITE sip:gw+zadarma-rbcrm@185.70.135.33:65000;transport=udp;gw=zadarma-rbcrm SIP/2.0
Record-Route: <sip:185.45.152.161;lr=on;ftag=as6a38207b>
Via: SIP/2.0/UDP 185.45.152.161;branch=z9hG4bK26d.6cf33cf5d2cdd6683e8de9503870f397.0
Via: SIP/2.0/UDP 185.45.152.148:5060;rport=5060;branch=z9hG4bK74d97ef6
Max-Forwards: 69
From: "+79630495339" <sip:+79630495339@sip.zadarma.com>;tag=as6a38207b
To: <sip:346127@185.45.152.161>
Contact: <sip:+79630495339@185.45.152.148:5060>
Call-ID: 401671d34e8247a9694c3da87c97fbbb@185.45.152.148:5060
CSeq: 102 INVITE
User-Agent: Zadarma Voip
Date: Thu, 07 Mar 2019 07:38:22 GMT
Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, SUBSCRIBE, NOTIFY, INFO, PUBLISH, MESSAGE
Supported: replaces
CALLED_DID: 73433023519
Content-Type: application/sdp
Content-Length: 415
hostname: sipbalancer-1
cc_num: 346127
cc_counter: 1

Sip 值不容易提取。 在 header 前加上 X-。其他无效。

您可以重新编译 mod_sofia - 添加一个额外的 header reader:

https://freeswitch.org/stash/projects/FS/repos/freeswitch/browse/src/mod/endpoints/mod_sofia/sofia.c 示例行 11297.

添加这个:

} else if (!strcasecmp(un->un_name, "CALLED_DID")) {
    switch_channel_set_variable(channel, "called_did", un->un_value);

之间:

} else if (!strcasecmp(un->un_name, "Geolocation")) {
    switch_channel_set_variable(channel, "sip_geolocation", un->un_value);

并且:

} else if (!strcasecmp(un->un_name, "Geolocation-Error")) {`
    switch_channel_set_variable(channel, "sip_user_location", un->un_value);

并像这样在拨号方案中检索此 header:

<action application="log" data="DEBUG Called Did -> ${called_did}"/> 

看了一大堆material,我得出的结论是:

不修改源代码就无法读取不带 X- 前缀的自定义 header。

但这对我来说是不可接受的方式。

可以通过设置 <param name="parse-all-invite-headers" value="true"/>Sofia SIP 配置文件 中。然后所有来自Invite的header都设置为sip_i_Header-Name channel variables.

在任何其他 SIP-Messages 中对特定 Headears 做出反应:

如果您想对其他消息的特定 header 作出反应,您可以通过设置变量 sip_watch_headers(需要导出并以 nolocal 为前缀,如果你只想要 B-leg)

如果检测到 header,您将获得子类“sofia::notify_watched_header”的自定义事件。

检测 原因 header 的示例 B-Leg:

<action application="export" data="_nolocal_sip_watch_headers=Reason"/>

这是在 B-Leg 上寻找原因 header 的事件示例,正在观看 Header“原因”:

  "Event-Name": "CUSTOM",
...
  "Event-Calling-File": "sofia.c",
  "Event-Calling-Function": "notify_watched_header",
  "Event-Calling-Line-Number": "1443",
  "Event-Sequence": "98672",
  "Event-Subclass": "sofia::notify_watched_header",
  "SIP-Message": "SIP/2.0 183 Session Progress",
  "Header-Name": "Reason",
  "Header-Value": "Q.850;cause=16",
  "Channel-State": "CS_CONSUME_MEDIA",
  "Channel-Call-State": "DOWN",
  "Channel-State-Number": "7",
...
"Call-Direction": "outbound",

可以通过在 Lua 配置中设置挂钩脚本或通过 AMQP / EventSocket 使用 Lua 来响应此事件。

如何使用 Lua

对这些事件做出反应

https://freeswitch.org/confluence/display/FREESWITCH/mod_lua#Event_Hooks

示例:autload_configs/lua.conf.xml:

<configuration name="lua.conf" description="LUA Configuration">
  <settings>
    <param name="module-directory" value="/etc/freeswitch/scripts/?.so"/>
    <param name="script-directory" value="/etc/freeswitch/scripts/?.lua"/>
    <!--<param name="startup-script" value="startup_script_1.lua"/>--> <!-- started at fs startup and maybe lives forever -->
    <hook event="CHANNEL_DESTROY" script="/etc/freeswitch/scripts/on_channel_destroy.lua"/>
    <hook event="CUSTOM" subclass="sofia::notify_watched_header" script="/etc/freeswitch/scripts/on_reason_header.lua"/>
    
  </settings>
</configuration>

示例Lua脚本

local uuid = event:getHeader("Unique-ID")
local shallHangup = event:getHeader("variable_HangupOnReasonInEarly")
local answerState = event:getHeader("Answer-State")

if (shallHangup ~= nil and shallHangup == "true" and answerState == "ringing") then
    local value = event:getHeader("Header-Value")
    local code = value:match(";cause=(%d*)")
    --local data = event:serialize("json")
    freeswitch.consoleLog("INFO","REASON DETECTED: for: " .. uuid .. "\n")
    api = freeswitch.API()
    api:executeString("uuid_kill " .. uuid .. " " .. code)
end

要在拨号方案中启用它:

<action application="export" data="_nolocal_sip_watch_headers=Reason"/>
<action application="export" data="_nolocal_HangupOnReasonInEarly=true"/>
<action application="bridge" data="..."/>