FreeSWITCH使用mysql存储用户

一、配置mysql环境

vi /etc/odbc.ini
注意:如果没有内容,则直接添加,保存

[FREESWITCH]
Description=MySQL data_base_fs database
Driver          = MySQL
SERVER          = 192.168.0.25
PORT            = 3306
DATABASE        = data_base_fs
OPTION          = 67108864
PASSWORD        = abc
CHARSET         = UTF8

使用 isql -v FREESWITCH,检测环境是否ok。出现如下图所示,则标识ok
加粗样式

图二

二、准备数据

#创建用户表
CREATE TABLE `tb_sip_account` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `account` varchar(10) DEFAULT NULL,
  `password` varchar(30) DEFAULT NULL,
  'status' int(2)),
  PRIMARY KEY (`id`),
) ENGINE=InnoDB AUTO_INCREMENT=19 DEFAULT CHARSET=utf8;

#插入两个用户
insert into tb_sip_account (account,password,status) values('8001','123456', 0)
insert into tb_sip_account (account,password,status) values('8002','123456', 0)

三、将用户验证转接到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","abc");
assert(dbh:connected());
dbh:query("select password from tb_sip_account where status = 0 and account="..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="account" 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");

注意:上述内容里面的以下内容
local dbh = freeswitch.Dbh(“FREESWITCH”,“root”,“abc”);

FREESWITCH:此值对应/etc/odbc.ini文件中的[FREESWITCH]
root:数据库账号
abc:数据密码

四、修改default,是lua接管用户注册

修改freeswitch/conf/directory/default.xml中的一部分内容,使得通过xml验证用户的功能失效,这样lua才能真正接管用户注册,删除的内容如下:

<group name="default">
    <users>
      <X-PRE-PROCESS cmd="include" data="default/*.xml"/>
    </users>
 </group>

此部分注释不起作用,可以进行更改或者删除。如果是更改的话,可将"X-PRE-PROCESS"更改成格式如
“****X-PRE-PROCESS”

五、使用客户端进行测试

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值