-
Notifications
You must be signed in to change notification settings - Fork 5
/
tproxy
165 lines (136 loc) · 2.81 KB
/
tproxy
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/bin/bash
PIDFILE="/home/provproxy/tinyproxy.pid"
TINYPROXYCMD="/usr/sbin/tinyproxy"
PIDVAL=0
WTIMEOUT=30
OK=0
CH_S[0]='-' #pseudographic items
CH_S[1]='/'
CH_S[2]='|'
CH_S[3]='\'
ITEM_ARR=0 #current item counter
process_status()
{
#poluchaem PID
if [ -e $PIDFILE ];then #esli file exist
PIDVAL=`cat $PIDFILE` #chitaem PID
TMPGREP=`ps -p $PIDVAL|grep "tinyproxy" -c` #esli process zapushen db 1 inache 0
if [ $TMPGREP -ge 1 ];then #process zapushen
return
else #pid-file levyi
rm $PIDFILE
fi
fi
PIDVAL=0
return
}
status_proxy()
{
process_status
if [ $PIDVAL -eq 0 ]; then
echo "Tinyproxy not running"
else
echo "Tinyproxy running [PID=$PIDVAL]"
fi
}
start_proxy()
{
process_status
if [ "$PIDVAL" -ne 0 ]; then
echo "Tinyproxy already work! [$PIDVAL]"
return 1
fi
$TINYPROXYCMD
RETCODE=$?
if [ "$RETCODE" -ne 0 ]; then
echo "Not starting (code $RETCODE)"
return 1
fi
echo -n "Starting proxy ("$WTIMEOUT" secounds): "
tput sc #save cursor position
while [ $WTIMEOUT -ge 0 ]; do
#print timeout and current pseudographic char
printf '%3s %s' $WTIMEOUT ${CH_S[ITEM_ARR]}
tput rc #restore cursor position
sleep 1
process_status
if [ $PIDVAL -ne 0 ]; then #zapustili
OK=1
break
fi
#decrease timeout and increase current item ctr.
let "WTIMEOUT=WTIMEOUT-1"
let "ITEM_ARR=ITEM_ARR+1"
if [ $ITEM_ARR -eq 4 ];then
#if items ctr > number of array items
#starting with 0 item
let "ITEM_ARR=0"
fi
done
#next message starting with new string
printf '\n'
if [ "$OK" -eq 1 ]; then
echo "Started [$PIDVAL]"
else
echo "Not started :("
fi
}
stop_proxy()
{
process_status
if [ $PIDVAL -eq 0 ]; then
echo "Tinyproxy not work!"
return 1
fi
echo -n "Stopping proxy [$PIDVAL] in $WTIMEOUT secounds: "
tput sc #save cursor position
while [ $WTIMEOUT -ge 0 ]; do
#print timeout and current pseudographic char
printf '%3s %s' $WTIMEOUT ${CH_S[ITEM_ARR]}
tput rc #restore cursor position
kill $PIDVAL
sleep 1
process_status
if [ $PIDVAL -eq 0 ]; then #ubit
OK=1
break
fi
#decrease timeout and increase current item ctr.
let "WTIMEOUT=WTIMEOUT-1"
let "ITEM_ARR=ITEM_ARR+1"
if [ $ITEM_ARR -eq 4 ];then
#if items ctr > number of array items
#starting with 0 item
let "ITEM_ARR=0"
fi
done
#next message starting with new string
printf '\n'
if [ "$OK" -eq 1 ]; then
echo "Stopped!"
else
echo "Not stopped :("
fi
}
restart_proxy()
{
stop_proxy
OK=0
start_proxy
}
case "$1" in
start)
start_proxy
;;
stop)
stop_proxy
;;
restart)
restart_proxy
;;
status)
status_proxy
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
esac