Asterisk

CentOS + Asterisk 15
# deploy asterisk ###################################
yum install dnsmasq tuned perf -y
yum install lame lame-libs -enablerepo=epel -y
yum install festival -y
tuned-adm profile latency-performance
tuned-adm active

yum-config-manager --add-repo https://ast.tucny.com/repo/tucny-asterisk.repo
yum-config-manager --enable asterisk-15

yum install asterisk asterisk-sip asterisk-dahdi asterisk-snmp asterisk-sounds-core-ru asterisk-voicemail asterisk-mp3 asterisk-festival

#cd /usr/share/asterisk/sounds/ 
#curl --location http://downloads.asterisk.org/pub/telephony/sounds/asterisk-core-sounds-ru-gsm-current.tar.gz | tar xzf -

# configure fail2ban #################################

yum install fail2ban -y

# add to /etc/asterisk/logger.conf line:
#   messages => notice,warning,error,security

asterisk -x "logger reload"

# Enable jail 'asterisk' in fail2ban by adding to /etc/fail2ban/jail.local
 [asterisk]
  enabled = true
  bantime = 86400

service fail2ban restart
fail2ban-client reload
fail2ban-client status asterisk
/etc/asterisk/extensions.conf
[general]
static=yes
writeprotect=no
autofallthrough=yes
clearglobalvars=no
;userscontext=default

[globals]
CONSOLE=Console/dsp                             ; Console interface for demo
;CONSOLE=DAHDI/1
;CONSOLE=Phone/phone0
;TRUNK=DAHDI/G2                                 ; Trunk interface
;TRUNKMSD=1                                     ; MSD digits to strip (usually 1 or 0)

; to allow options 200 answer
[default]
exten => s,1,Hangup

[sipnet-in]
exten => 5555555,1,Dial(SIP/101) ; все входящие звонки с транка sipnet -> 101

[zadarma-in]
exten => 6666666,1,Dial(SIP/101) ; все входящие звонки с транка 666666 (zadarma)

[internal-context]
; demo wav
exten => 999,1,Answer()
same => n,Playback(demo-congrats)
same => n,Hangup()

; echo test
exten => 888,1,Answer()
same => n,Playback(demo-echotest)
same => n,Echo()
same => n,Hangup()

; Voice Mail
;exten => 700,1,VoiceMailMain(300)
exten => 700,1,Log(NOTICE,Dialing out from ${CALLERID(all)} to VoiceMail (700))
;same => n,VoiceMailMain(@voicemailcontext) ;с авторизацией
same => n,VoiceMailMain(${CALLERID(num)}@voicemailcontext,s) ;без авторизации
same => n,Hangup

; demo menu
exten => 777,1,Goto(menu_777,s,1)

; time
exten => 100,1,Answer()
same => n,SayUnixTime(,,QdhAR)
same => n,Hangup()

; sipnet self addr
exten => 0042268047,1,Dial(SIP/101)
same  => n,Hangup()
; MeetMe (Conference)
exten => 1000,1,Answer()
same => 2,Playback(hello)
same => 3,Playback(conf-placeintoconf)
same => 4,Wait(2)
same => 5,MeetMe(1000,1pdMX)
same => 6,Playback(goodbye)
same => 7,Wait(2)
same => n,Hangup

; local extensions
exten => _XXX,1,Noop(internal call)
same => n,Set(FILE=${CALLERID(num)}-${STRFTIME(${EPOCH},,%Y%m%d-%H%M%S)}.wav)
same => n,MixMonitor(${FILE},ab,/var/spool/asterisk/transcode.sh "${FILE}")
same => n,DIAL(SIP/${EXTEN})
same => n,StopMixMonitor()
same => n,Set(CDR(accountcode)=voicemail)
same => n,GotoIf($["${DIALSTATUS}" = "BUSY"]?busy:unavail)
same => n(unavail),Voicemail(${EXTEN}@voicemailcontext,u)
same => n,Hangup()
same => n(busy),VoiceMail(${EXTEN}@voicemailcontext,b)
same => n,Hangup()

; EXTERNAL ROUTE > FreePBX
exten => _2XX,1,Noop(external call through FreePBX)
same => n,Dial(SIP/freepbx,60)
same => n,Hangup

; EXTERNAL ROUTE > sipnet balance
exten => 111,1,Noop(external call through Sipnet - 111)
same => n,Dial(SIP/sipnet/2252623,60)
same => n,Hangup
; sipnet echo
exten => 444,1,Noop(external call through Sipnet - 444)
same => n,Dial(SIP/sipnet/00000,60)
same => n,Hangup

; EXTERNAL ROUTE > zadarma balance\echo
exten => 1111,1,Noop(external call through Zadarma - 1111)
same => n,Dial(SIP/zadarma/1111,60)
same => n,Hangup
exten => 4444,1,Noop(external call through Zadarma - 4444)
same => n,Dial(SIP/zadarma/4444,60)
same => n,Hangup

; EXTERNAL ROUTE > zadarma -> moscow
exten => _XXXX.,1,Noop(external all calls through Zadarma)
;exten => _7XXXXXXXXXX,1,Noop(external call through Zadarma)
same => n,Set(CALLERID(num)=15555555555)
same => n,Dial(SIP/zadarma/${EXTEN},60)
same => n,Hangup

[menu_777]
exten => s,1,Answer()
exten => s,2,Background(dir-instr)
exten => s,3,WaitExten(7)

exten => 1,1,Background(demo-abouttotry)
exten => 1,2,Goto(s,1)

exten => 2,1,Playback(demo-instruct)
exten => 2,2,Goto(s,1)

exten => 3,1,Playback(demo-thanks)
exten => 3,2,Goto(s,1)

exten => 4,1,Answer()
exten => 4,2,MixMonitor(${UNIQUEID}.wav,ab)
exten => 4,3,Playback(demo-thanks)
exten => 4,4,StopMixMonitor()
exten => 4,5,Hangup()

exten => 5,1,Answer()
exten => 5,2,Playback(conf-now-recording)
exten => 5,3,Voicemail(${EXTEN},u)
exten => 5,4,Hangup()

;exten => 2,1,Dial(SIP/operator)

;Тут мы принимаем факсы, первым делом устанавливаем факсимильному файлу - время во сколько пришел факс и с какого номера он пришел
exten => 9,1,Set(FAXFILE=/tmp/fax/${STRFTIME(${EPOCH},,%Y%m%d_%H_%M_%S)}-from-${CALLERID(num)})
exten => 9,2,ReceiveFax(${FAXFILE}.tif)
; Функция приема факса с именем который мы указали выше
exten => 9,3,System(sendEmail -f [email protected] -t [email protected] -u "Входящий факс." -m "Вам пришел факс с номера ${CALLERID(num)} в ${STRFTIME(${EPOCH},,%H:%M:%S)}. Факс во вложении." -a ${FAXFILE}.tif -o message-charset=UTF-8)

; redirect to local ext
exten => _XXX,1,Dial(SIP/${EXTEN})
exten => t,1,Playback(time)
exten => t,2,Goto(s,1)
exten => i,1,Playback(please-try-again)
exten => i,2,Goto(s,1)
/etc/asterisk/sip.conf
[general]
context=default
useragent=Mediant 4000/v.7.20A.002
;useragent=CommuniGatePro/6.1.13
srvlookup=yes

udpbindaddr=0.0.0.0:5060
tcpenable=yes
tcpbindaddr=0.0.0.0

language=ru
;tonezone=ru
videosupport=yes
subscribemwi = no ;voice mail led
pedantic=no

;security
alwaysauthreject = yes
allowguest=no
allowoverlap=no
;match_auth_username=yes

localnet=192.168.0.0/255.255.0.0
localnet=10.0.0.0/255.0.0.0
localnet=172.16.0.0/255.240.0.0
localnet=169.254.0.0/255.255.0.0

externaddr = your.external.ip:5060
externhost = your.hostname:9999
externrefresh=180

; fix nat
nat=force_rport,comedia
icesupport=yes
canreinvite=no
directmedia=no
;media_address = your.external.ip.address
;icesupport=no

;multifon
;register => <номер>@multifon.ru:<пароль>:<номер>@sbc.megafon.ru:5060/<номер>

;youmagic
;register => 883140xxxxxxxxx:[email protected]@voip.mtt.ru/883140xxxxxxxxx

[mtt]
username=883140xxxxxxxxx
fromuser=883140xxxxxxxxx
[email protected]
type=friend
host=voip.mtt.ru
fromdomain=voip.mtt.ru
insecure=port,invite
qualify=300

[zadarma]
host=sip.zadarma.com
defaultuser=6666666
[email protected]
insecure=invite,port
type=friend
fromdomain=sip.zadarma.com
disallow=all
allow=alaw
allow=g729
allow=ulaw
allow=gsm
dtmfmode=auto
trunkname=6666666
fromuser=666666
callbackextension=666666
context=zadarma-in
qualify=400
directmedia=no
;register => [email protected]

[sipnet]
host=sipnet.ru
defaultuser=login
[email protected]
type=friend
insecure=invite
qualify=yes
nat=force_rport,comedia
dtmfmode=rfc2833
context=sipnet-in
canreinvite=yes
disallow=all
allow=alaw
allow=g729
allow=ulaw
allow=gsm
registerattempts=0
rtpkeepalive=10
callerid=SIPID
fromdomain=sip.your.domain

[skype](!)
username=9905xxxxxxxxxx
fromuser=9905xxxxxxxxxx
[email protected]
type=friend
fromdomain=sip.skype.com
insecure=port,invite
qualify=300

[skype-1](skype)
host=5.sip.skype.com

[skype-2](skype)
host=3.sip.skype.com

[pbx](!)
host=dynamic
transport=udp,tcp
context=internal-context
type=friend
trustrpid=yes
sendrpid=no
qualifyfreq=60
qualify=120000
qualify=yes
dtmfmode=auto
avpf=no
force_avp=yes
directmedia=no
disallow=all
qualify=yes
allow=gsm,g722,g729,ulaw,alaw,vp8,h264,h263p,mpeg4
encryption=no
permit=0.0.0.0/0.0.0.0

[101](pbx)
callerid="PHONE-101" <101>
defaultusername=101
[email protected]
call-limit=2
callgroup=1
pickupgroup=1
[email protected]

[102](pbx)
callerid="PHONE-102" <102>
defaultusername=102
[email protected]
call-limit=2
callgroup=1
pickupgroup=1
[email protected]

[103](pbx)
defaultusername=103
[email protected]
call-limit=2
[email protected]
/etc/asterisk/voicemail.conf
[general]
tz=ru
format=wav49
serveremail=asterisk
attach=yes
maxmsg=100
maxsecs=180
minsecs=3
skipms=3000
maxsilence=10
silencethreshold=128
maxlogins=3
moveheard=yes
charset=UTF-8
pbxskip=yes ; Skip the "[PBX]:" string from the message title
fromstring=Asterisk VoiceMail
emailsubject=Новое голосовое сообщение ${VM_MSGNUM} в ящике ${VM_MAILBOX}  ; можете поменять Subject
emailbody=Уважаемый ${VM_NAME}:\n\nХотим сообщить, что Вам пришло новое голосовое сообщение длиной ${VM_DUR} под номером (number ${VM_MSGNUM})\nв ящик ${VM_MAILBOX} от ${VM_CALLERID}, в ${VM_DATE}. \nКак будет время, проверьте его!  Спасибо!\n\n\t\t ; и поменять сообщение
emaildateformat=%A, %B %d, %Y at %r
pagerdateformat=%A, %B %d, %Y at %r
;delete=yes              ; Сообщение будет удалено из системы как только будет отправлено
sendvoicemail=yes ; Allow the user to compose and send a voicemail while inside

[zonemessages]
ru=Europe/Moscow|'vm-received' q 'digits/at' H 'hours' M 'minutes'

[voicemailcontext]
101 => 101,User1,[email protected],,attach=yes|tz=ru
102 => 102,User2,[email protected],,attach=yes|tz=ru
103 => 103,User3,[email protected],,attach=yes|tz=ru
/etc/asterisk/meetme.conf
[general]
audiobuffers=32

[rooms]
; Usage is conf => confno[,pin][,adminpin]
conf => 1000
;conf => 2345,9938
/var/spool/asterisk/monitor/transcode.sh
#!/bin/bash
FILE=$1
NEWFILE="${FILE%.*}.mp3"
lame -h -b 128 "$FILE" "$NEWFILE"
sleep 1
if [ -f $NEWFILE ];
then
    rm "$FILE"
fi
exit 0
# cli
asterisk -rx "core show channels"
asterisk -rx "core show peers"

# perf
perf stat -p $(pidof asterisk)
perf top -p $(pidof asterisk)