freeswitch用户用mysql数据库管理

窃破天道 2015-07-08

开发环境:centos6.464位,freeswitch1.4,linphone,mysql

1首先安装unixodbc

unixODBC是一个可以让你在Unix/Linux系统下使用ODBC来连接数据库的组件,就像java中的mysql-connector-java-5.1.6-bin.jar一样,负责连接数据库的。

yum install unixODBC-devel.x86_64

yum install mysql-connector-odbc.x86_64

安装后修改两个文件:/etc/odbc.ini,/etc/odbcinst.ini

/etc/odbc.ini配置要连接的数据库信息

[freeswitch]

Driver          = /usr/lib64/libmyodbc5.so

SERVER       = ip

PORT           = 3306

DATABASE    = database

USER            = user

PASSWORD   = password

/etc/odbcinst.ini修改mysq的部分,将驱动包指向正确,这要根据你本身的包安装路径配置

# Driver from the mysql-connector-odbc package

# Setup from the unixODBC package

[MySQL]

Description     = ODBC for MySQL

Driver          = /usr/lib64/libmyodbc5.so

Setup           = /usr/lib64/libodbcmyS.so

Driver64        = /usr/lib64/libmyodbc5.so

Setup64         = /usr/lib64/libodbcmyS.so

FileUsage       = 1

修改之后,执行isql-vfreeswitch

如果出现

[root@test etc]# isql -v freeswitch
+---------------------------------------+
| Connected!                            |
|                                       |
| sql-statement                         |
| help [tablename]                      |
| quit                                  |
|                                       |
+---------------------------------------+
SQL>

则代表你的unixodbc配置成功了

2修改用户注册部分,转接到lua脚本进行注册验证

修改freeswitch/conf/autoload_configs/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="$${base_dir}/scripts/?.lua"/> -->

    <!--<param name="xml-handler-script" value="/dp.lua"/>-->
    <!--<param name="xml-handler-bindings" value="dialplan"/>-->
    <param name="xml-handler-script" value="gen_dir_user_xml.lua" />
    <param name="xml-handler-bindings" value="directory" />
    <!--
        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"/>-->
  </settings>
</configuration>

让lua脚本接管用户注册验证,这里默认调用的脚本是freeswitch/script/gen_dir_user_xml.lua

脚本内容如下:

local req_domain = params:getHeader("domain")
local req_key    = params:getHeader("key")
local req_user   = params:getHeader("user")
local dbh = freeswitch.Dbh("freeswitch","root","root123")
if dbh:connected() == false then
  freeswitch.consoleLog("notice", "mysql.lua cannot connect to database" .. dsn .. "\n")
  return
end
if req_user==nil then
	req_user="0000"
end
 XML_STRING =
[[<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type=" freeswitch/xml">
  <section name="directory">
    <domain name="]] .. req_domain .. [[">
      <user id="]] .. req_user  .. [[">
        <params>
          <param name="password" value=""/>
           <param name="dial-string" value="{sip_invite_domain=${dialed_domain},presence_id=${dialed_user}@${dialed_domain}}${sofia_contact(${dialed_user}@${dialed_domain})}"/>
        </params>
        <variables>
          <variable name="user_context" value="default"/>
        </variables>
      </user>
    </domain>
  </section>
</document>]]

local pass="43211234567890"
if req_domain==nil then
	req_domain="192.168.20.133"
end
if req_user==nil then
	req_user="1000"
end
 
pass="4321_"..req_user;
  
local my_query = string.format("select password from userinfo where username='%s' limit 1", req_user)
assert (dbh:query(my_query, function(u) -- there will be only 0 or 1 iteration (limit 1)
  XML_STRING =
[[<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type=" freeswitch/xml">
  <section name="directory">
    <domain name="]] .. req_domain .. [[">
      <user id="]] .. req_user  .. [[">
        <params>
          <param name="password" value="]] .. u.password .. [["/>
           <param name="dial-string" value="{sip_invite_domain=${dialed_domain},presence_id=${dialed_user}@${dialed_domain}}${sofia_contact(${dialed_user}@${dialed_domain})}"/>
        </params>
        <variables>
          <variable name="user_context" value="default"/>
        </variables>
      </user>
    </domain>
  </section>
</document>]]
end)) 	 
--freeswitch.consoleLog("warning", "Debug from user.lua, generated XML:\n" .. XML_STRING .. "\n")