บทความนี้แปลมาจาก https://www.vitalpbx.org/blog/asterisk-webrtc-from-scratch

เนื้อหาหลักๆ คือ ติดตั้ง asterisk 18 + PJSIP บน CentOS 7 คอนฟิก PJSIP ให้รองรับ WebRTC

คือ ใช้ web browser (chrome, firefox, Opera, etc.) ทำหน้าเป็นเครื่องโทรศัพท์ แทนที่ IP phone 

หรือ softphone นั่นเอง

 

1. ติดตั้ง CentOS 7 minimal ตาม link

http://www.asterisk.in.th/voip/index.php/centos-linux/45-centos-7-minimal-install

 

2. ติดตั้ง software เพิ่มเติม

#yum install wget nano git epel-release

ยกเลิก SELINUX

#sed -i s/^SELINUX=.*$/SELINUX=disabled/ /etc/selinux/config

#reboot

 

3. ติดตั้ง Asterisk 18

#cd /usr/src

#wget http://downloads.astrerisk.org/pub/telephony/asterisk/asterisk-18-current.tar.gz

#tar -zxvf asterisk-18-current.tar.gz

#cd asterisk-18.*

#yum install svn

#./contrib/scripts/get_mp3_source.sh

#contrib/scripts/install_prereq install

#./config --libdir=/urs/lib64 --with-jansson-bundled --with-pjproject-bundled

#make menuselect

อย่าลืมเลือก codec_opus ในหัวข้อ Codec Translators

#make && make install

#make samples

#make config

#groupadd asterisk

#useradd -r -d /var/lib/asterisk -g asterisk asterisk

#usermod -aG audio, dialout asterisk

#chown asterisk. -R /etc/asterisk

#chown asterisk. -R /var/{lib, log, spool}/asterisk

#chown -R asterisk.asterisk /usr/lib64/asterisk

#vi /etc/sysconfig/asterisk

AST_USER = "asterisk"

AST_GROUP = "asterisk"

#chkconfig asterisk on

#systemctl restart asterisk

#asterisk -rvvv

ถ้าได้ prompt asterisk ดังรูปก็ถือว่า OK

 

 

4. ติดตั้ง Apache

#yum install httpd

#systemctl enable httpd

#systemctl start httpd

#mkdir -p /var/www/html/ws.adventek.net/{public_html,logs}

*** ws.adventek.net คือ server ตัวที่เราติดตั้ง (อยู่บน cloud)

edit config file server ของเรา ดังนี้ 

#vi /etc/httpd/conf.d/ws.adventek.net.conf

 

NameVirtualHost *:80
<VirtualHost *:80>
       ServerAdmin This email address is being protected from spambots. You need JavaScript enabled to view it.adventek.net      

       ServerName ws.adventek.net
       ServerAlias ws.adventek.net

       DocumentRoot /var/www/html/ws.adventek.net/public_html/
       ErrorLog /var/www/html/ws.adventek.net/logs/error.log
       CustomLog /var/www/html/ws.adventek.net/logs/access.log combined
</VirtualHost>

 

#system restart httpd

 

5. Create certificate

#yum install certbot python2-certbot-apache mod_ssl

#certbot --apache -d ws.adventek.net

แก้ไขไฟล์ /etc/httpd/conf.d/ssl.conf

#mv /etc/httpd/conf.d/ssl.conf /etc/httpd/conf.d/ssl.conf.bak
#vi /etc/httpd/conf.d/ssl.conf


Listen 443 https
SSLPassPhraseDialog exec:/usr/libexec/httpd-ssl-pass-dialog
SSLSessionCache shmcb:/run/httpd/sslcache(512000)
SSLSessionCacheTimeout 300
SSLRandomSeed startup file:/dev/urandom 256
SSLRandomSeed connect builtin

 

#mv /etc/httpd/conf.d/welcome.conf /etc/httpd/conf.d/welcome.conf.bak
# mv /etc/httpd/conf.d/ws.adventek.net.conf /etc/httpd/conf.d/ws.adventek.net.bak

 

#systemctl restart httpd

#crontab -e

45 3 24 5,8,11,2 * /usr/bin/certbot renew && systemctl reload httpd

*** renew certificates ทุกๆ 3 เดือน ***

6. Config Asterisk

เช็คว่า Asterisk มี module เหล่านี้พร้อมหรือไม่

- res_crypto

- res_http_websocket

- res_pjsip_transport_websocket

- codec_opus

(ใช้คำสั่ง #asterisk -rx "module show")

#vi /etc/asterisk/http.conf

[general]

Servername = Asterisk

tlsbindaddr = 0.0.0.0:8089

bindaddr = 0.0.0.0

bindport = 8088

enabled = yes

tlsenable = yes

tlscertfile=/etc/letsencrypt/live/mydomian.com/fullchain.pem
tlsprivatekey=/etc/letsencrypt/live/mydomain.com/privkey.pem

แก้ไขไฟล์ /etc/asterisk/pjsip.conf ดังนี้

#mv /etc/asterisk/pjsip.conf /etc/asterisk/pjsip.conf.bak

#vi pjsip.conf

[system]

type=system

timer_t1=500

timer_b=32000

disable_tcp_switch=yes

 

[global]

type=global

max_initial_qualify_time=0

keep_alive_interval=90

contact_expiration_check_interval=30

default_voicemail_extension=*97

unidentified_request_count=3

unidentified_request_period=5

unidentified_request_prune_interval=30

mwi_tps_queue_high=500

mwi_tps_queue_low=-1

mwi_disable_initial_unsolicited=yes

send_contact_status_on_update_registration=yes

 

[transport-wss]

type=transport

protocol=wss

bind=0.0.0.0:8089

allow_reload=yes

 

[transport-udp]

type=transport

protocol=udp

bind=0.0.0.0

 

[webrtc-phones](!)

context=main-context

transport=transport-wss

allow=!all,opus,ulaw,alaw,vp8,vp9

webrtc=yes

 

[User1](webrtc-phones)

type=endpoint

callerid=”User One” <100>

auth=User1

aors=User1

 

[User1]

type=aor

max_contacts=3

 

[User1]

type=auth

auth_type=userpass

username=User1

 

password=1234

 

 

[User2](webrtc-phones)

type=endpoint

callerid=”User Two” <101>

auth=User2

aors=User2

 

[User2]

type=aor

max_contacts=3

 

[User2]

type=auth

auth_type=userpass

username=User2

password=1234

 

[User3](webrtc-phones)

type=endpoint

callerid=”User Three” <102>

auth=User3

aors=User3

 

[User3]

type=aor

max_contacts=3

 

[User3]

type=auth

auth_type=userpass

username=User3

password=1234

 

[User4]

type=endpoint

callerid="User Four" <714>

context=main-context

disallow=all

allow=ulaw

transport=transport-udp

auth=User4

aors=User4

 

[User4]

type=auth

auth_type=userpass

password=1234

username=User4

 

[User4]

type=aor

max_contacts=1

 

[User5]

type=endpoint

callerid="User Five" <715>

context=main-context

disallow=all

allow=ulaw

transport=transport-udp

auth=User5

aors=User5

 

[User5]

type=auth

auth_type=userpass

password=1234

username=User5

 

[User5]

type=aor

 

max_contacts=1

;;; end of file ;;;

User1 - User3 สำหรับ WebRTC

User4 - User5 สำหรับ IP phone หรือ softphone

 

เปลี่ยน file owner

#chown asterisk. /etc/asterisk/pjsip.conf

 

แก้ไขไฟล์ /etc/asterisk/extensions.conf

#mv /etc/asterisk/extensions.conf /etc/asterisk/extensions.conf.bak

#vi /etc/asterisk/extensions.conf

[general]

static=yes

writeprotect=yes

priorityjumping=no

autofallthrough=no

 

[globals]

ATTENDED_TRANSFER_COMPLETE_SOUND=beep

 

[main-context]

include => from-extensions

include => subscriptions

include => textmessages

include => echo-test

include => speak-exte-nnum

 

[echo-test]

exten => 777,1,NoOp(FEATURE: ECHO TEST)

 same => n,Answer

 same => n,Wait(1)

 same => n,Playback(demo-echotest)

 same => n,Echo()

 same => n,Playback(demo-echodone)

 same => n,Hangup()

;END of [echo-test]

[textmessages]

exten => 100,1,Gosub(send-text,s,1,(User1))

exten => 101,1,Gosub(send-text,s,1,(User2))

exten => 102,1,Gosub(send-text,s,1,(User3))

exten => 714,1,Gosub(send-text,s,1,(User4))

exten => 715,2,Gosub(send-text,s,1,(User5))

 

[subscriptions]

exten => 100,hint,PJSIP/User1

exten => 101,hint,PJSIP/User2

exten => 102,hint,PJSIP/User3

exten => 714,hint,PJSIP/User4

exten => 715,hint,PJSIP/User5

 

[from-extensions]

; Feature Codes:

exten => *65,1,Gosub(moh,s,1)

; Extensions

exten => 100,1,Gosub(dial-extension,s,1,(User1))

exten => 101,1,Gosub(dial-extension,s,1,(User2))

exten => 102,1,Gosub(dial-extension,s,1,(User3))

exten => 714,1,Gosub(dial-extension,s,1,(User4))

exten => 715,1,Gosub(dial-extension,s,1,(User5))

 

exten => e,1,Hangup()

 

 

[moh]

exten => s,1,NoOp(Music On Hold)

exten => s,n,Ringing()

exten => s,n,Wait(2)

exten => s,n,Answer()

exten => s,n,Wait(1)

exten => s,n,MusicOnHold()

 

[dial-extension]

exten => s,1,NoOp(Calling: ${ARG1})

exten => s,n,Set(JITTERBUFFER(adaptive)=default)

exten => s,n,Dial(PJSIP/${ARG1},30)

exten => s,n,Hangup()

 

exten => e,1,Hangup()

 

[send-text]

exten => s,1,NoOp(Sending Text To: ${ARG1})

exten => s,n,Set(PEER=${CUT(CUT(CUT(MESSAGE(from),@,1),<,2),:,2)})

exten => s,n,Set(FROM=${SHELL(asterisk -rx ‘pjsip show endpoint ${PEER}’ | grep ‘callerid ‘ | cut -d’:’ -f2- | sed ‘s/^ *//’ | tr -d ‘‘)})

exten => s,n,Set(CALLERID_NUM=${CUT(CUT(FROM,>,1),<,2)})

exten => s,n,Set(FROM_SIP=${STRREPLACE(MESSAGE(from),

exten => s,n,MessageSend(pjsip:${ARG1},${FROM_SIP})

 

exten => s,n,Hangup()

;;; end of file ;;;

เปลี่ยน file owner

#chown asterisk. /etc/asterisk/extensions.conf

reload asterisk

#asterisk -rvvv
*CLI> module reload res_pjsip.so
*CLI> dialplan reload

 

7. ติดตั้ง WebRTC Client

#cd /var/www/html/ws.adventek.net/public_html

#git clone http://github.com/InnovateAsterisk/Browser-Phone.git

#chown -R apache:apache /var/www/html/ws.adventek.net/public_html/

 

8. ทดสอบ

ใช้ PC หรือ Notebook 2 เครื่อง เปิด web browser แล้ว connect ไปที่ ws.adventek.net แล้ว คอนฟิก account ดังนี้

- User3 (extension 102)

 

 

User1 (extension 100)

 

 

ลองโทรหากัน