freeswitch是一款強大的voip服務器,可以語音和視頻。但是它默認是采用/directory文件夾下的xml來配置用戶的,對於整合到現有系統或者使用數據庫保存用戶信息的系統都是非常不方便的,所以,本文主要描述一種方法,解決freeswitch用戶整合的問題。
完成這一任務需要三步,配置unixodbc,將用戶驗證轉接到lua腳本,修改撥號計划使得撥通所有號碼

開發環境:centos 6.2 64位, freeswitch, 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的部分,將驅動包指向正確,這要根據你本身的包安裝路徑配置
# Example driver definitions
# Driver from the postgresql-odbc package
# Setup from the unixODBC package
[PostgreSQL]
Description = ODBC for PostgreSQL
Driver = /usr/lib/psqlodbc.so
Setup = /usr/lib/libodbcpsqlS.so
Driver64 = /usr/lib64/psqlodbc.so
Setup64 = /usr/lib64/libodbcpsqlS.so
FileUsage = 1
# 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 -v freeswitch
如果出現
[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
腳本內容如下:
freeswitch.consoleLog("NOTICE","lua take the users...\n");
-- gen_dir_user_xml.lua
-- example script for generating user directory XML
-- comment the following line for production:
--freeswitch.consoleLog("notice", "Debug from gen_dir_user_xml.lua, provided params:\n" .. params:serialize() .. "\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")
local dbh = freeswitch.Dbh("freeswitch","user","password");
freeswitch.consoleLog("NOTICE","start connect DB...\r\n");
assert(dbh:connected());
dbh:query("select password from Users where id="..req_user,function(row)
freeswitch.consoleLog("NOTICE",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");
--assert (req_domain and req_key and req_user,
--"This example script only supports generating directory xml for a single user !\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
-- comment the following line for production:
freeswitch.consoleLog("notice", "Debug from gen_dir_user_xml.lua, generated XML:\n" .. XML_STRING .. "\n");
修改freeswitch/conf/directory中的一部分內容,使得通過xml驗證用戶的功能失效,這樣lua才能真正接管用戶注冊
刪除的內容如下:
<group name="default">
<users>
<X-PRE-PROCESS cmd="include" data="default/*.xml"/>
</users>
</group>
3 修改撥號計划
修改freeswitch/conf/dialplan/default.xml
修改如下內容,好讓所有的撥號通過以下的條件
<extension name="Local_Extension">
<!--<condition field="destination_number" expression="^(10[01][0-9])$">-->
<condition field="destination_number" expression="^(.*)$">
完成以上步驟,就基本完成了用戶整合的全部內容,但是要注意一點,第一步配置unixodbc之后,要重新編譯下freeswitch的源碼,即在/usr/local/src/freeswitch下執行 configure&&make install,然后重啟freeswitch。
還有就是每次修改xml后都需要重新在freeswitch控制台或者fs_cli下執行reloadxml,使得修改生效,修改lua腳本,則不需要如上操作。
來源:http://linyu19872008.iteye.com/blog/1736641