📜 ⬆️ ⬇️

HowTo: Skype gateway based on FreeSwitch

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

For the task was selected the following software:

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

How it all works


Functional diagram
The key element of the whole scheme is mod_skypiax, which plays the role of a certain layer between FreeSwitch and the Skype network. This module in the terminology of FreeSwitch is a channel driver or an endpoint (endpoint), the same as, for example, a regular IP phone.

Since the Skype protocol is closed, the only way to connect to the Skype network is the native client, with which mod_skypiax interacts via the Skype API. Each Skype launched is equivalent to one voice channel, i.e. if you have 30 Skypes running, 30 people can call you at the same time. It is worth noting that this article discusses the use of only one skype-account, which is simultaneously used by all running Skype.
')
To minimize server load, use the “fake” X-server Xvfb and snd_dummy driver. The use of the dummy driver is also justified by the fact that, as a rule, there is no real sound card on the servers.

The mod_sofia module is responsible for the SIP in FreeSwitch, which, as can be seen in the diagram, provides a SIP trunk up to Asterisk PBX.

The incoming call algorithm looks like this:
  1. The user initiates a call to your skype-account;
  2. mod_skypiax accepts the call using the first free copy of the skype client;
  3. mod_sofia initiates a SIP connection with an Asterisk PBX, which switches to 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 installation options: compile the sources from svn or compile the release sources. Of course, there is an option to build a deb-package from sources, and install from it (which would ideologically be the most correct solution), but in this case, a lot of modules that are not needed for the gateway will be compiled and installed. Therefore, consider the source version, namely svn.

First, we put everything that is needed for further assembly:

Further, all commands and actions are executed from under the root user, unless otherwise specified.

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


I’ll just make a reservation that Xvfb and everything related to the X server is needed to start the Skype client and build mod_skypiax

Download the source tree from svn:

cd /usr/src
svn co svn.freeswitch.org/svn/freeswitch/trunk freeswitch
cd freeswitch


We edit /usr/src/freeswitch/modules.conf for our needs. I he looked 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 assembly and installation:

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


The default installation is in the / usr / local / freeswitch directory. Copy the mod_skypiax config and the 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


We get the user under which FreeSwitch will work:
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


Install and configure Skype


The ready deb package can be downloaded from the official Skype site, so download and try to install:

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 the new beta version of Skype for Linux. I dare to assume that everything works in the new version too.

Most likely, dpkg will complain about the missing dependencies, put them using apt-get and try to install Skype again.

We get the user under which Skype will work:

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

Create a directory from which Skype will read its config:

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


Run Skype on the server to check its operation 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 a pre-registered Skype account
skype_secret - password from this account

You can check that Skype has started simply by searching it in the process list. Now you need to somehow get to the Skype GUI to make the necessary settings. To do this, use the VNC-server.

apt-get install x11vnc
x11vnc -display :101


Having connected via vnc to the server, you can see the Skype launched in the previous step, in the settings of which you need to remove all unnecessary (disable events, automatic status change, etc.) and select the dummy driver as all audio devices.

We now turn to the most important setting - Public API. Here you need to allow mod_skypiax to access Skype. Skype itself allows you to add programs to the “Allowed programs” list only after they have accessed Skype, but it is impractical to launch FreeSwitch and mod_skypiax just to make such a setting, so the creators of mod_skypiax wrote a small utility emulating the Skype mod_skypiax call. It is located in the FreeSwitch source tree and needs to 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 window - in the dialog box that appears, click "Yes" and tick "Remember this selection".

another program wants to use skype

This completes the Skype client setup. It remains only to clone the resulting config 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 is the required number of Skype channels.

To launch Skype, you can use the following 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 


This completes the Skype setup. It remains to connect FreeSwitch with Skype.

Config mod_skypiax


We edit the mod_skypiax config for the required number of channels:

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

Below is a piece of my config as an example:
 <configuration name = "skypiax.conf" description = "Skypiax Configuration">
   <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>
     <! - here, by analogy, add the required number of interfaces ->
   </ per_interface_settings>
 </ configuration>


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

Run 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 by running the sk list command, you can see a list of skypiax interfaces.

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


The important point is that skype-clients should be launched before loading the mod_skypiax module, i.e. before starting freeSwitch. Also, stopping Skype’s already involved mod_skypiax copies will cause FreeSwitch to crash.

As you probably remember, incoming Skype calls will be routed to extension 7777 , which still needs to be created. To do this in the directory / usr / local / freeswitch / conf / dialplan / default create a file 02_skype.xml with the following content:

 <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 to which the call will go (PBX based on Asterisk)
5555 - extension on this gateway (for testing it's best to just put Music On Hold there)

It remains to configure ...

SIP trunk between FreeSwitch and Asterisk


On the server with FreeSwitch in the / usr / local / freeswitch / conf / sip_profiles / external directory create the file asterisk.xml with the following content:

 <include> <gateway name="asterisk"> <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 (must match the one specified in the previous step);
asterisk.example.tld is the host name with Asterisk;
freeswitch - username to access the gateway;
supersecret - password.

On the server with Asterisk, add the following to sip.conf :
[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 previous step
Do not forget to also specify the context for the correct call routing.
It remains to re-read sip.conf:

rasterisk -x 'sip reload'

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

UPD: According to yitzhakv , now the skypiax module has been redesigned and renamed skypopen, which is less stable, in the Freeswitch svn trunk. Therefore, to squeeze the source you need to use the command svn co svn.freeswitch.org/svn/freeswitch/tags/1.0.4 freeswitch

Source: https://habr.com/ru/post/68399/


All Articles