FreeSWITCH默认是采用/directory文件夹下的xml来配置用户的,对于后期开发来说是非常不方便的,所以本文来实现使用mysql来存储用户,并用lua脚本来接管用户验证,步骤如下
一、配置ODBC
参考我的另外一篇文章:https://my.oschina.net/stache/blog/1542926
二、创建数据库
#创建用户表
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user` varchar(10) DEFAULT NULL,
`password` varchar(30) DEFAULT NULL,
PRIMARY KEY (`id`),
) ENGINE=InnoDB AUTO_INCREMENT=19 DEFAULT CHARSET=utf8;
#插入两个用户
insert into user (user,password) values('8001','123456')
insert into user (user,password) values('8002','123456')
三、将用户验证转接到lua脚本
1、将freeswitch/conf/autoload_configs/lua.conf.xml中的两行代码修改为如下所示
<param name="xml-handler-script" value="gen_dir_user_xml.lua" />
<param name="xml-handler-bindings" value="directory" />
2、让lua脚本接管用户注册验证,这里调用的脚本是freeswitch/script/gen_dir_user_xml.lua内容如下,默认无该脚本,需自行创建
freeswitch.consoleLog("NOTICE","lua take the users...\n");
local req_domain = params:getHeader("domain")
local req_key = params:getHeader("key")
local req_user = params:getHeader("user")
local req_password = params:getHeader("pass")
freeswitch.consoleLog("NOTICE","start connect DB...\r\n");
local dbh = freeswitch.Dbh("freeswitch","root","123456");
assert(dbh:connected());
dbh:query("select password from user where user="..req_user,function(row)
freeswitch.consoleLog("NOTICE","DB select password="..string.format("%s\n",row.password))
req_password=string.format("%s",row.password)
end);
dbh:release();
freeswitch.consoleLog("NOTICE","info:"..req_domain.."--"..req_key.."--"..req_user.."--"..req_password.."\n");
if req_domain ~= nil and req_key ~= nil and req_user ~= nil then
XML_STRING =
[[<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="freeswitch/xml">
<section name="directory">
<domain name="]]..req_domain..[[">
<params>
<param name="dial-string"
value="{presence_id=${dialed_user}@${dialed_domain}}${sofia_contact(${dialed_user}@${dialed_domain})}"/>
</params>
<groups>
<group name="default">
<users>
<user id="]]..req_user..[[">
<params>
<param name="password" value="]]..req_password..[["/>
<param name="vm-password" value="]]..req_password..[["/>
</params>
<variables>
<variable name="toll_allow" value="domestic,international,local"/>
<variable name="accountcode" value="]]..req_user..[["/>
<variable name="user_context" value="default"/>
<variable name="directory-visible" value="true"/>
<variable name="directory-exten-visible" value="true"/>
<variable name="limit_max" value="15"/>
<variable name="effective_caller_id_name" value="Extension ]]..req_user..[["/>
<variable name="effective_caller_id_number" value="]]..req_user..[["/>
<variable name="outbound_caller_id_name" value="${outbound_caller_name}"/>
<variable name="outbound_caller_id_number" value="${outbound_caller_id}"/>
<variable name="callgroup" value="techsupport"/>
</variables>
</user>
</users>
</group>
</groups>
</domain>
</section>
</document>]]
else
XML_STRING =
[[<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="freeswitch/xml">
<section name="directory">
</section>
</document>]]
end
freeswitch.consoleLog("NOTICE", "debug from gen_dir_user_xml.lua, generated XML:\n" .. XML_STRING .. "\n");
3、修改freeswitch/conf/directory/default.xml中的一部分内容,使得通过xml验证用户的功能失效,这样lua才能真正接管用户注册,删除的内容如下:
<group name="default">
<users>
<X-PRE-PROCESS cmd="include" data="default/*.xml"/>
</users>
</group>
四、测试
使用SIP软电话(zoiper、eyeBeam等)来测试8001、或8002是否能正常登陆,可登陆即为配置成功