Asterisk dialplan to limit number of call in ‘x’ seconds
Here is a quick trick that you can use to limit the number of inbound calls from the same callerid in a certain time window.
It has the advantage to not use any database but a small file for each caller.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; With some adjustments:
;
; 1. adding a dir to the system /var/spool/asterisk/inbound_count
; 2. routing inbound channel into proper queue, menu, dialstatement or other destination
; 3. adjusting the number of attemps and the timeframe
; 4. adjusting GMT to your own situation, just to have the time right.; ;the script can be used.
;;;;;;;;;;;;;;;;;;;;;;;;;;;
; written by Erik de Wild
; Tripple-o
; Your Asterisk migration partner
; info at tripple-o.nl
;;;;;;;;;;;;;;;;;;;;;;;;;;;
; This is free software licensed under the GNU General Public
; License version 2; you are welcome to redistribute it under
; certain conditions.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; this script limmits the number of inbound call attempts within a certain timeframe.
; this script can help to prevents denial of service attacks or to prevent callers to spam you with incoming calls
;;;;;;;;;;;;;;;;;;;;;;;;;;;[max_inbount_trials]
exten => s,1,Set(FILE_EXISTS=0)
exten => s,n,SET(PAD=/var/spool/asterisk/inbound_count/${CALLERID(num)})
exten => s,n,SET(FILE_EXISTS=${STAT(e,${PAD})}) ; check if file already exists
exten => s,n,GotoIf($[ "${FILE_EXISTS}" : "1"]?check_last_read)
exten => s,n,System(echo -E “xx” > ${PAD}) ; create new file and add 1 line with xx
exten => s,n,MusicOnHold() ; just for now. This is the place to route call into queue, menu, dial statement or whatever
exten => s,n,Hangup()exten => s,n(check_last_read),Set(LAST_READ=${STAT(A,${PAD})}) ;EPOCH of last read
exten => s,n,NoOp(XXXXXXXXXXXXXXXXX ${LAST_READ})
exten => s,n,SET(SECONDS_AGO=$[${STRFTIME(${EPOCH},GMT-1,%s)} - ${LAST_READ}])exten => s,n,Set(TIME_WINDOW=3600) ; this is where the timefram for control the number is set in seconds
exten => s,n,GotoIf($[ ${SECONDS_AGO}<${TIME_WINDOW}]?add_line)
exten => s,n,NoOp( the last attemt was ${SECONDS_AGO} seconds ago)
exten => s,n,System(echo -E “xx” > ${PAD}) ; create new file, last attempt was more then 1 hour ago
exten => s,n,MusicOnHold() ; just for now. This is the place to route call into queue, menu, dial statement or whatever
exten => s,n,Hangup()exten => s,n(add_line),System(echo -E “xx” >> ${PAD}) ; add line to file
exten => s,n,Set(SIZE_FILE=${STAT(s,${PAD})}) ; check for number of bytes in file
exten => s,n,Set(MAX_SIZE=6) ; This is where the number of attempts within the timeframe is set.
; two attempts with in the timeframe, every attempt add 3 bytes to the file (xx and a line return)
exten => s,n,GotoIf($[ ${SIZE_FILE}>${MAX_SIZE}]?hang_up) ; Just two attempts within time frame
exten => s,n,MusicOnHold() ; just for now. This is the place to route call into queue, , menu, dial statement or whatever
exten => s,n,Hangup()
exten => s,n(hang_up),Playback(beep)
exten => s,n,Wait(2)
exten => s,n,Goto(hang_up)
Comments
Leave a Reply