Tuesday, December 6, 2011

Skype-based gateway FreeSwitch


HowTo: Skype-based gateway FreeSwitch

In this article I would like to consider the creation of SIP <-> Skype gateway for forwarding incoming skype-calls to a corporate IP PBX (in this case it will Asterisk).  Since Skype is currently perhaps the most popular VoIP client among users, the ability to call in your company via Skype will be an additional plus for customers.


For this task has been selected the following software:


In principle mod_skypiax can work with Asterisk, but I was curious to touch it FreeSwitch, yes, and this module was written originally for FreeSwitch.

How it works


Functional diagram
A key element of the whole scheme is mod_skypiax, who plays the role of a layer between the FreeSwitch and network Skype.This module is in the terminology FreeSwitch is a channel driver or the endpoint (endpoint), such as, for example, conventional IP-phone.

 Since the Skype protocol is closed, then the only way to connect to the Skype network remains the native client, which interacts mod_skypiax througf Skype API. т.е. Each running Skype is equivalent to one voice channel, If you are running 30 Skype'ov you can simultaneously call 30 people. It is worth noting that this article discusses the use of only one skype-account's, which also is used by all running Skype'ami.

To minimize the load on the server used «fake» X-server and Xvfb snd_dummy driver. Using dummy-driver is also justified by the fact that the servers are usually not a real sound card.

For SIP to FreeSwitch responsible module mod_sofia, which, as seen in the diagram, provides SIP-trunk to the Asterisk PBX.



Algorithm for an incoming call looks like this:
  1. The user initiates a call to your skype-account as a gift;
  2. mod_skypiax принимает вызов используя первую свободную копию skype-клиента; mod_skypiax accepts the challenge using the first free copy of the skype-client;
  3. mod_sofia инициирует SIP-соединение с Asterisk PBX, которое коммутируется с mod_skypiax. mod_sofia initiates SIP-connection to the Asterisk PBX, which commutes with mod_skypiax.


Consider the process of installing and configuring software.

Installing FreeSwitch and mod_skypiax

 In debian there is no ready-made package for FreeSwitch, so we are left with two options for installation: compile the source code from svn and compile the source code release.  There is certainly an option to collect from source deb-package, and put out of it (that would be the most ideologically right decision), but in this case, will be built and installed a whole bunch of unnecessary gateway modules. Therefore, consider the source, namely from svn.

First, put everything needed for further assembly:

Then all commands and actions are carried out under the user root, unless otherwise

apt-get -y install build-essential subversion automake autoconf wget libtool \
libncurses5-dev xvfb libx11-dev libasound2-dev xfs xfonts-100dpi xfonts-75dpi xfonts-scalable
Just specify that Xvfb and everything associated with the X-server, you need to launch Skype-client and build mod_skypiax

Download source tree from svn:
cd /usr/src
svn co svn.freeswitch.org/svn/freeswitch/trunk freeswitch
cd freeswitch

Edit /usr/src/freeswitch/modules.conf for our needs.I have it look like this:
loggers/mod_console
loggers/mod_logfile
loggers/mod_syslog
applications/mod_commands
applications/mod_dptools
applications/mod_fifo
applications/mod_limit
applications/mod_expr
applications/mod_esf
codecs/mod_g723_1
codecs/mod_amr
codecs/mod_g729
codecs/mod_voipcodecs
codecs/mod_ilbc
codecs/mod_speex
dialplans/mod_dialplan_xml
endpoints/mod_sofia
endpoints/mod_skypiax
event_handlers/mod_event_socket
event_handlers/mod_cdr_csv
formats/mod_native_file
formats/mod_sndfile
formats/mod_local_stream
formats/mod_tone_stream
formats/mod_file_string
xml_int/mod_xml_cdr


Actually build and install:

cd /usr/src/freeswitch; ./bootstrap.sh ; ./configure
make && make install


By default, the installation is done in the directory / usr / local / freeswitch. Copy the config mod_skypiax and init-script to run FreeSwitch:

cp /usr/src/freeswitch/src/mod/endpoints/mod_skypiax/configs/skypiax.conf.xml \
/usr/local/freeswitch/conf/autoload_configs/

cp /usr/src/freeswitch/debian/freeswitch.init /etc/init.d/freeswitch
sed -i 's/opt/usr\/local/g' /etc/init.d/freeswitch

Set up user that will work FreeSwitch:

adduser --disabled-password --quiet --system --home /usr/local/freeswitch \
--gecos "FreeSwitch Voice Platform" --ingroup daemon freeswitch
adduser freeswitch audio
chown -R freeswitch.daemon /usr/local/freeswitch

Installing and configuring Skype

Ready-deb-package can be downloaded from the official website of Skype, so that the swing and try to set:

wget www.skype.com/go/getskype-linux-deb
dpkg -i skype-debian_2.0.0.72-1_i386.deb

The original article was written before the release of a new beta version of Skype for Linux. I will assume that the new version, all works too.


Rather dpkg rugnetsya the missing dependency, put them througf apt-get and try to install Skype again.




Set up user that will operate Skype:

adduser --home /home/skype --ingroup audio --disabled-password skype





Create a directory from which Skype will read your configuration:

mkdir -p /home/skype/multi/interface01
chown -R skype.audio /home/skype/multi


Running Skype on the server to verify that it works and configure:

/usr/bin/Xvfb :101 -ac &
su skype -c "/bin/echo ' skype_user skype_secret '| DISPLAY=:101 /usr/bin/skype \
--dbpath=/home/skype/multi/interface01 --pipelogin &"

where
skype_user - the name of pre-registered Skype-account's
skype_secret - this account's password
Check that Skype will launch you can just search it in the list of processes.  Now we have to somehow get to the GUI Skype, For this we use VNC-server.

apt-get install x11vnc
x11vnc -display :101
When you connect to vnc to the server, can be seen running in the previous step, Skype, in the settings that you want to remove all superfluous (or disable events, automatic change of status, etc.) and select the dummy-driver as all the audio devices.


— Public API.
Now comes the most important settings - Public API. Here it is necessary to allow mod_skypiax contact Skype. Sam Skype allows you to add programs to the list «Allowed programs» only on the fact of their conversion to Skype, but only in order to produce such a setting, it is impractical to run FreeSwitch and mod_skypiax, so the creators have written a small utility mod_skypiax, emulating treatment mod_skypiax to Skype. It is located in the source tree FreeSwitch and it must be compiled separately:

cd /usr/src/freeswitch/src/mod/endpoints/mod_skypiax/configs
gcc -Wall -ggdb skypiax_auth.c -o skypiax_auth -lX11
./skypiax_auth :101
Return to the vnc-connection - in the dialog box should press «Yes» and tick «Remember this selection».

another program wants to use skype


What remains now is to clone the resulting configuration for the desired number of Skype-channels.

cd /home/skype/multi
for i in $(seq 2 N ); do i=$(printf "%02d" $i); cp -a interface01 interface$i; done


where N - the right amount of Skype-channels.


To start Skype'ov can use this script:


#!/bin/sh
SKYPE_SYSTEM_USER=skype
SKYPE_HOME=/home/skype/multi
SKYPE_USER=skype_user
SKYPE_PASSWORD=skype_secret
SKYPE_INSTANCES=N
XVFB=/usr/bin/Xvfb

module_reload() {
  rmmod snd-dummy
  modprobe snd-dummy
}

skype_start() {
  for i in $(seq 1 $SKYPE_INSTANCES); do
    i=`printf "%02d" $i`
    $XVFB :1$i -ac >> /dev/null 2>&1 &
    sleep 3
    su $SKYPE_SYSTEM_USER -c "/bin/echo '$SKYPE_USER $SKYPE_PASSWORD'| DISPLAY=:1$i  /usr/bin/skype \
    --dbpath=$SKYPE_HOME/interface$i --pipelogin >> /dev/null 2>&1 &"
    echo "Skype $i started"
  done
}

skype_stop() {
kill -TERM `ps -u $SKYPE_SYSTEM_USER -o pid=` >> /dev/null 2>&1
sleep 3
kill -TERM `ps -C Xvfb -o pid=` >> /dev/null 2>&1
}
case "$1" in
  start)
        module_reload
        sleep 3
        skype_start
        ;;
  stop)
        skype_stop
        ;;
  restart)
        skype_stop
        sleep 3
        skype_start
        ;;
  *)
        echo $"Usage: $0 {start|stop|restart}"
        exit 1
esac



In this setting Skype completed.  It remains to link FreeSwitch with Skype'ami.

Setting mod_skypiax


Edit the configuration mod_skypiax for the desired number of channels:


vim /usr/local/freeswitch/conf/autoload_configs/skypiax.conf.xml




The following quote a piece of my config as an example:

<global_settings>
    <param name="debug" value="8"/>
    <param name="codec-master" value="us"/>
    <param name="dialplan" value="XML"/>
    <param name="context" value="default"/>
    <param name="codec-prefs" value="gsm,ulaw"/>
    <param name="codec-rates" value="8000,16000"/>
    <param name="hold-music" value="$${moh_uri}"/>
    <param name="destination" value="7777"/>
  </global_settings>
  <!-- one entry here per skypiax interface -->
  <per_interface_settings>
    <interface id="1" name="skypiax1">
    <param name="hold-music" value="$${moh_uri}"/>
    <param name="dialplan" value="XML"/>
    <param name="context" value="default"/>
    <param name="X11-display" value=":101"/>
    <param name="tcp_cli_port" value="15556"/>
    <param name="tcp_srv_port" value="15557"/>
    <param name="skype_user" value="skype_user"/>
    <param name="destination" value="7777"/>
    </interface>
    <!-- тут по аналогии добавляем нужное кол-во интерфейсов -->
  </per_interface_settings>
</configuration>



Here
7777 - extension, which will be routed incoming Skype-call;
: 101 - display X-server (for the second channel would be: 102 and so on)
skype_user - name Skype-account's

Starting FreeSwitch and try to download mod_skypiax:

/etc/init.d/freeswitch start
/usr/local/freeswitch/bin/fs_cli
freeswitch@internal> load mod_skypiax



If all is well, then running sk list, you can see a list of skypiax-interfaces.


Add to the list mod_skypiax modules to be loaded during start FreeSwitch. To do this in the file /usr/local/freeswitch/conf/autoload_configs/modules.conf.xml need to uncomment the line <load module="mod_skypiax"/>

т.е. The important point is that the skype-client should be run before loading the module mod_skypiax, before the start FreeSwitch. Also, stop already involved mod_skypiax copies of Skype will lead to a fall FreeSwitch.


As you probably remember incoming Skype-calls will be routed to extension 7777, which has yet to be created.

 To do this in the directory /usr/local/freeswitch/conf/dialplan/default create a file 02_skype.xml read:

<include>
  <extension name="skype_incoming">
    <condition field="destination_number" expression="^7777$">
      <action application="set" data="hangup_after_bridge=true"/>
      <action application="set" data="effective_caller_id_name=Skype"/>
      <action application="bridge" data="sofia/gateway/asterisk/5555"/>
      <action application="hangup"/>
    </condition>
  </extension>
</include>

where
asterisk - the name of the gateway, which will leave the call (based PBX Asterisk)
5555 - extension on this gateway (for testing are best to just put it Music On Hold)

It remains to adjust ...

SIP-Trunk between Asterisk and FreeSwitch

On the server c FreeSwitch in the directory / usr / local / freeswitch / conf / sip_profiles / external    asterisk.xml create a file to read: ( create a asterisk.xml file to read: )

<include>
  <gateway name="asterisk_pbx">
  <param name="username" value="freeswitch"/>
  <param name="realm" value="asterisk.example.tld"/>
  <param name="password" value="supersecret"/>
  <param name="register" value="false"/>
  </gateway>
</include>

where
asterisk - the name of the gateway (this should coincide with that in the previous step);
asterisk.example.tld - hostname with Asterisk;
freeswitch - user name for access to the gateway;
supersecret - password.


 On the server with Asterisk sip.conf to add the following:


[freeswitch]
type=peer
host= 1.1.1.1
username= freeswitch
port=5080
fromdomain=1.1.1.1
secret= supersecret
where
1.1.1.1 - server address with FreeSwitch
freeswitch, supersecret - see the previous step
Do not forget to also specify the context for the correct routing of the call.
It remains to reread sip.conf:

rasterisk -x 'sip reload'




Now when you receive a call on skype_user FreeSwitch will connect it with the extensions 5555 on a host with Asterisk.

No comments:

Post a Comment