FreeSWITCH 使用 lua 脚本 接管 分机注册,鉴权等


FreeSWITCH 使用 lua 脚本 接管 分机注册,鉴权等

1. 更改lua.conf.xml配置

<configuration name="lua.conf" description="LUA Configuration">
  <settings>

    
    
    

    
    
    
    <param name="script-directory" value="$${script_dir}/"/>
    <param name="script-directory" value="$${script_dir}/?.lua"/>

    <param name="xml-handler-script" value="user_auth_xml.lua" />
    <param name="xml-handler-bindings" value="directory" />

    
    
    
    

    
  settings>
configuration>

其中


 指定使用 user_auth_xml.lua 优先获取分机配置.

包括分机注册,拨打鉴权验证等获取分分机配置信息.

2. user_auth_xml.lua 脚本

 1 -- user_auth_xml.lua
 2 -- example script for generating user directory XML
 3 -- comment the following line for production:
 4 --freeswitch.consoleLog("notice", "Debug from user_auth_xml.lua, provided params:\n" .. params:serialize() .. "\n")
 5 require ("config")
 6 
 7 local req_domain = params:getHeader("domain")
 8 local req_key    = params:getHeader("key")
 9 local req_user   = params:getHeader("user")
10 local req_password   = nil -- params:getHeader("pass")
11 freeswitch.consoleLog("NOTICE","lua take the key="..req_key..", user="..req_user..", domain="..req_domain.."\n");
12 
13 --[[
14 local dbh = freeswitch.Dbh("freeswitch","dbuser","dbpswd");
15 local my_query = string.format("select password from userinfo where username='%s' limit 1", req_user)
16 --freeswitch.consoleLog("NOTICE","start connect DB...\r\n");
17 assert(dbh:connected());
18 freeswitch.consoleLog("notice", "the query string is:"..my_query)
19 dbh:query(my_query,function(row)
20   --freeswitch.consoleLog("NOTICE",string.format("%s\n",row.password))
21   req_password=string.format("%s",row.password)
22 end);
23 dbh:release();
24 freeswitch.consoleLog("NOTICE","info:"..req_domain.."--"..req_key.."--"..req_user.."--"..req_password.."\n");
25 ]]--
26 for line in io.lines("scripts/users.txt") do
27   --print(line)
28   if line~=nil then
29     local arr=Split(line,' ')
30     --print( arr[1].." "..arr[2])
31     if arr[1] == req_user and arr[2]~=nil then
32       req_password=arr[2]
33       break
34     end
35   end
36 end
37 --assert (req_domain and req_key and req_user,
38 --"This example script only supports generating directory xml for a single user !\n")
39 if req_domain ~= nil and req_key=="id" and req_user~=nil and req_password~=nil  then
40     XML_STRING =
41     [[<?xml version="1.0" encoding="UTF-8" standalone="no"?>
42     
43       
44 ]]..req_domain..[["> 45 46 47 48 49 50 51 52 53 54 ]] ..req_user..[["> 55 56 ]]..req_password..[["/> 57 ]]..req_password..[["/> 58 59 60 61 ]] ..req_user..[["/> 62 63 64 65 66 ]] ..req_user..[["/> 67 ]] ..req_user..[["/> 68 69 70 71 72 73 74 75
76 ]] 77 else 78 XML_STRING = nil 79 --[[<?xml version="1.0" encoding="UTF-8" standalone="no"?> 80 81
82
83 ]] 84 end 85 86 -- comment the following line for production: 87 -- freeswitch.consoleLog("notice", "Debug from gen_dir_user_xml.lua, generated XML:\n" .. XML_STRING .. "\n");

如果要使用数据库, 自行取消注释并修改.

当前使用的是 从 scripts/users.txt 文件中 读取 分机号码 和 密码(空格分隔,每行一个分机).

scripts/users.txt 文件中没有配置 分机号和密码时,将会自动到xml 配置中去查找.

即: 此 lua 脚本 优先于 xml 配置文件执行, XML_STRING 变量为 nil 时就去查找默认的xml配置.

 当前使用的是 1.10.3 版本测试的, 较低的版本可能要执行第三步,如下

3. 更改 conf/directory/default.xml 使 其配置的分机配置失效 

红色 框里的 注释或者删掉

我用 1.10.3 版本, 不删也可以, 其他版本未测试, 更高的版本 应该也不用删掉的

不删的话, lua 脚本中 XML_STRING 变量为 nil 时就去查找默认的xml配置