zoukankan      html  css  js  c++  java
  • FreeSWITCH 使用 lua 脚本 接管 分机注册,鉴权等

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

    1. 更改lua.conf.xml配置

    <configuration name="lua.conf" description="LUA Configuration">
      <settings>
    
        <!--
        Specify local directories that will be searched for LUA modules
        These entries will be pre-pended to the LUA_CPATH environment variable
        -->
        <!-- <param name="module-directory" value="/usr/lib/lua/5.1/?.so"/> -->
        <!-- <param name="module-directory" value="/usr/local/lib/lua/5.1/?.so"/> -->
    
        <!--
        Specify local directories that will be searched for LUA scripts
        These entries will be pre-pended to the LUA_PATH environment variable
        -->
        <!-- <param name="script-directory" value="/usr/local/lua/?.lua"/> -->
        <!-- <param name="script-directory" value="$${script_dir}/?.lua"/> -->
        <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" />
    
        <!--<param name="xml-handler-bindings" value="dialplan"/>-->
        <!--
        The following options identifies a lua script that is launched
        at startup and may live forever in the background.
        You can define multiple lines, one for each script you
        need to run.
        -->
        <!--<param name="startup-script" value="startup_script_1.lua"/>-->
        <!--<param name="startup-script" value="startup_script_2.lua"/>-->
    
        <!--<hook event="CUSTOM" subclass="conference::maintenance" script="catch-event.lua"/>-->
      </settings>
    </configuration>

    其中

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

     指定使用 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:
    " .. params:serialize() .. "
    ")
     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.."
    ");
    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...
    ");
    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
    ",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.."
    ");
    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 !
    ")
    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     <document type="freeswitch/xml">
    43       <section name="directory">
    44         <domain name="]]..req_domain..[[">
    45           <params>
    46             <param name="dial-string" value="{^^:sip_invite_domain=${dialed_domain}:presence_id=${dialed_user}@${dialed_domain}}${sofia_contact(*/${dialed_user}@${dialed_domain})},${verto_contact(${dialed_user}@${dialed_domain})}"/>
    47             <!-- These are required for Verto to function properly -->
    48             <param name="jsonrpc-allowed-methods" value="verto"/>
    49             <param name="jsonrpc-allowed-event-channels" value="demo,conference,presence"/>
    50           </params>
    51           <groups>
    52             <group name="default">
    53               <users>
    54                 <user id="]] ..req_user..[[">
    55                   <params>
    56                     <param name="password" value="]]..req_password..[["/>
    57                     <param name="vm-password" value="]]..req_password..[["/>
    58                   </params>
    59                   <variables>
    60                     <variable name="toll_allow" value="domestic,international,local"/>
    61                     <variable name="accountcode" value="]] ..req_user..[["/>
    62                     <variable name="user_context" value="default"/>
    63                     <variable name="directory-visible" value="true"/>
    64                     <variable name="directory-exten-visible" value="true"/>
    65                     <variable name="limit_max" value="15"/>
    66                     <variable name="effective_caller_id_name" value="Extension ]] ..req_user..[["/>
    67                     <variable name="effective_caller_id_number" value="]] ..req_user..[["/>
    68                     <variable name="callgroup" value="chezai"/>
    69                   </variables>
    70                 </user>
    71               </users>
    72             </group>
    73           </groups>
    74         </domain>
    75       </section>
    76     </document>]]
    77 else
    78     XML_STRING = nil
    79     --[[<?xml version="1.0" encoding="UTF-8" standalone="no"?>
    80     <document type="freeswitch/xml">
    81       <section name="directory">
    82       </section>
    83     </document>]]
    84 end
    85 
    86 -- comment the following line for production:
    87 -- freeswitch.consoleLog("notice", "Debug from gen_dir_user_xml.lua, generated XML:
    " .. XML_STRING .. "
    ");

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

    当前使用的是 从 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配置

  • 相关阅读:
    快速生成移动设备应用图标的在线工具 makeappicon
    绝对令人的惊叹的CSS3折叠效果
    GBin1专题之Web热点秀#14
    对一个正整数n,求出n!中末尾0的个数。
    大整数加法
    HDOJ2013
    HDOJ1203 I NEED A OFFER!
    HDOJ1005【苏哥的解法】
    HDOJ1297
    HDOJ1004
  • 原文地址:https://www.cnblogs.com/lzpong/p/13024577.html
Copyright © 2011-2022 走看看