-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStunScript
102 lines (79 loc) · 2.66 KB
/
StunScript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/bin/bash
function valid_ip()
{
local ip=$1
local stat=1
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
OIFS=$IFS
IFS='.'
ip=($ip)
IFS=$OIFS
[[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \
&& ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
stat=$?
fi
return $stat
}
#########################################################################################
function port_is_ok() {
local port="$1"
local -i port_num=$(to_int "${port}" 2>/dev/null)
if (( $port_num < 1 || $port_num > 65535 )) ; then
return 1
else
return 0
fi
}
function to_int {
local -i num="10#${1}"
echo "${num}"
}
#########################################################################################
AllFile=true
SERVICE="/usr/bin/turnserver"
if pgrep "$SERVICE" >/dev/null 2>&1 ; then
echo "$SERVICE is running"
fi
FILE=/etc/turnserver.conf
if ! [ -f "$FILE" ]; then
echo "$FILE does not exist , STUN is not installed correctly !!"
AllFile=false
fi
##############################################################################################
if [ "$AllFile" = true ] ; then
echo "Enter the IP address of STUN server - or enter q to skip :"
read srvip
if ! [ "$srvip" = 'q' ] ; then
while ! valid_ip $srvip
do
echo "Bad IP Address"
echo "Please Re-Enter the IP address of STUN server - or enter q to skip : :"
read srvip
done
if valid_ip $srvip
then
sed -i "s+#stun-only+stun-only+g" "/etc/turnserver.conf"
sed -i "0,/#listening-ip=/{/#listening-ip=.*/s//listening-ip=$srvip/}" "/etc/turnserver.conf"
sed -i "s+#listening-ip=.*+###+" "/etc/turnserver.conf"
sed -i "0,/listening-ip=/{/listening-ip=.*/s//listening-ip=$srvip/}" "/etc/turnserver.conf"
#sed -i "s+#listening-ip=.*+listening-ip=$srvip+" "/etc/turnserver.conf"
#sed -i "s+listening-ip=.*+listening-ip=$srvip+" "/etc/turnserver.conf"
else echo "Bad IP Address"
fi
fi
############################################################################################
echo "Enter the STUN Port number - or enter q to skip :"
read stunprt
if ! [ "$stunprt" = 'q' ] ; then
while ! port_is_ok $stunprt ; do
echo "STUN Port number is not Valid !!"
echo "Enter a Valid STUN Port number:"
read stunprt
done
sed -i "s+#listening-port=.*+listening-port=$stunprt+g" "/etc/turnserver.conf"
sed -i "s+listening-port=.*+listening-port=$stunprt+g" "/etc/turnserver.conf"
fi
##############################################################################################
ps -ef | grep turnserver | grep -v grep | awk '{print $2}' | xargs kill
/usr/bin/turnserver --daemon -c /etc/turnserver.conf --pidfile /run/turnserver/turnserver.pid
fi