-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtbcli
executable file
·387 lines (322 loc) · 13 KB
/
tbcli
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#!/usr/bin/env bash
path_config="/etc/tbcli/tbcli.conf"
path_program="$(which tbcli)"
# Add config file
source "${path_config}" 2>/dev/null
if [[ $? -ne 0 ]] ; then
echo -e "\e[1;31mconfig file \e[1;32m${path_config} \e[1;31mnot exist"
exit 1
fi
# check tor file is exist
if [ ! -e "$tor_config_file" ]; then
echo -e "${red}tor config file $tor_config_file doesn't exist"
exit 1
fi
# requirements for run this script
if [[ ! $(which feh) || ! $(which tor) || ! $(which proxychains4) || ! $(which obfs4proxy) ]]; then
echo -e "${light_red}check that programs are installed: "
echo -e "${light_cyan}proxychains \nfeh \ntor \nobfs4proxy${nc}"
exit 1
fi
# usage
function usage(){
echo -e "${light_green}Usage : ${light_yellow}sudo tbcli [options]\n"
echo -e "${nc}a simple script for get tor bridge from ${light_magenta}${url_bridges}${nc}"
echo -e "and manage tor service\n\n"
echo -e "${light_green} -a, -A, --add-bridges ${yellow}add bridges into ${tor_config_file} and print bridges"
echo -e "${light_green} -p, -P, --print-only-bridges ${yellow}just print bridges"
echo -e "${light_green} -d, -D, --disable-broken-bridges ${yellow}disable broken bridges in this network connection"
echo -e "${light_green} -c, -C, --clear-broken-bridges ${yellow}remove all broken bridges from config file ${tor_config_file}"
echo -e "${light_green} -e, -E, --enable-all-bridges ${yellow}enable all broken bridges"
echo -e "${light_green} -r, -R, --reset-tor ${yellow}restart tor service${red}(${light_yellow}you can use for start tor btw${red})"
echo -e "${light_green} -s, -S, --status-tor [length] ${yellow}status tor service${red}(${light_yellow}no need to root permission,default length is 10${red})"
echo -e "${light_green} -o, -O, --off-tor ${yellow}stop tor service"
echo -e "${light_green} -u, -U, --uninstall ${yellow}uninstall Script"
echo -e "${light_green} -h, -H, --help ${yellow}show help${red}(${light_yellow}no need to root permission${red})"
echo -e "${nc}"
}
# delete tmp files
function ClearTmpFiles() {
for file in "${HTML_FILE}" "${IMAGE_CAPTCHA_FILE}" "${BridgeFile}"; do
if [[ -e "$file" ]]; then
rm $file
fi
done
}
# get tor bridges from
function get_tor_bridges(){
# get Captcha
proxychains4 -q curl -s "$url_bridges" -o "$HTML_FILE"
# net test
[[ -z $(cat "$HTML_FILE" 2>/dev/null) ]] && echo "Error TOR" && exit 0
# cut base64 Image challenge and convert to image
base64 -i -d <<< $(grep -Eo "/9j/[^\"]*" "$HTML_FILE") > $IMAGE_CAPTCHA_FILE
# show image captcha security code
feh "$IMAGE_CAPTCHA_FILE" &
FEH_PID=$!
# Captcha challenge field ( captcha serial )
capChallenge="$(grep -Eo 'value="[^"]*"' img.html|cut -d\" -f2)"
# captcha security code
while [[ -z $captcha_security_code ]]; do
# show your ip
echo -ne "${light_magenta}[ $(proxychains4 -q curl -s "$IpSite") ] ${light_blue}"
# get code from user
read -p "enter code (enter 'r' for reset captcha): " captcha_security_code
# press 'r' for reset captcha
if [[ $captcha_security_code == "r" ]]; then
# close image captcha security code
kill $FEH_PID
# delete tmp files
ClearTmpFiles
# unset captcha_security_code
unset captcha_security_code
# reset captcha
get_tor_bridges
return
fi
# slove captcha and get Bridges
proxychains4 -q curl -s "$url_bridges" \
--data "captcha_challenge_field=${capChallenge}&captcha_response_field=${captcha_security_code}" -o "$BridgeFile"
# cut bridges from html file(if code is incorrect bridges file is empty)
RES=$(grep -Eo 'obfs4 [0-9][^<]*' "$BridgeFile")
# if captcha_security_code is correct code save bridges into Variable BRIDGES
# if incorrect print error
if [[ $(tr -d '\n' <<< "$RES") ]]; then
BRIDGES="$(sed 's/^/Bridge /g' <<< "$RES")"
else
echo -e "${light_yellow}the code entered is incorrect! try again ..${nc}"
# for continue while and try again . unset captcha_security_code
unset captcha_security_code
fi
done
# close image captcha security code
kill $FEH_PID
}
# add bridges into tor config file
function save_and_print_bridges(){
# add 'UseBridges 1' in to $tor_config_file if not exist
if ! grep -q "^UseBridges 1" $tor_config_file ; then
echo "UseBridges 1" >> $tor_config_file
fi
# add 'ClientTransportPlugin obfs4 exec /usr/bin/obfs4proxy' into $tor_config_file after 'UseBridges 1' if not exist
if ! grep -q "^ClientTransportPlugin obfs4 exec" $tor_config_file ; then
sed -i "/UseBridges 1/a ClientTransportPlugin obfs4 exec $(which obfs4proxy)" $tor_config_file
fi
# add bridges
sed -i "/ClientTransportPlugin obfs4 exec/a $(sed ':a;N;$!ba;s/\n/\\n/g' <<< $BRIDGES)" "$tor_config_file"
# show bridges
echo -e "${light_magenta}bridges added into ${magenta}${tor_config_file}: \n"
echo -e "${cyan}${BRIDGES}"
}
# enable all broken bridge ( uncomment commented bridges )
function enable_all_bridges(){
broken_bridges=$(grep "^# Bridge obfs4" $tor_config_file)
if [[ ! -z "$broken_bridges" ]]; then
echo -e "${yellow}broken bridges : ${light_green}"
# display broken bridge
for bridge in $(echo "$broken_bridges"| cut -d " " -f 3); do
echo -e "\t${light_magenta}[${light_yellow}✓${light_magenta}] ${cyan}${bridge}"
done
# enable all broken bridges
sed -i "s/^# Bridge obfs4/Bridge obfs4/g" "$tor_config_file" # enable all broken bridges
echo -e "${light_green}broken bridges Enabled."
# restart tor in end program
bridges_manager="$bridges_manager -r"
else
echo -e "${light_yellow}all bridges are Enable."
fi
# display number of active bridge
echo -e "${cyan}active bridges : $(active_bridges)${nc}"
}
# read 'unable connetion bridges' from status tor and comment broken bridges
function disable_broken_bridges(){
echo -ne "${light_yellow}waiting for find broken bridges:\n\t"
# broken bridges
unable_to_connect_bridges=$(tor_status |grep "unable" |grep -E -o "([0-9]{1,3}.){3}[0-9]{1,3}:[0-9]{2,8}"|sort -u)
# just sellect realy broken bridge not old broken commented bridges
for bridge in ${unable_to_connect_bridges[@]};do
line_bridge=$(grep "$bridge" $tor_config_file)
if [[ ! -z $line_bridge ]] ; then
if ! grep -q "^# " <<< $line_bridge ; then
broken_bridges="$broken_bridges $bridge"
fi
fi
done
# check bridges exist
if [[ ! -z $(echo $broken_bridges|tr -d '\n') ]];then
echo -e "${light_red}Broken Bridges:"
# comment broken bridges
for bridge in ${broken_bridges[@]};do
echo -e "\t${light_magenta}[${light_red}X${light_magenta}] ${cyan}${bridge}"
sed -i "s/^Bridge.*${bridge}/# &/g" $tor_config_file
done
echo -e "${light_yellow}broken bridges successfully disable.${nc}"
# restart tor in end program
bridges_manager="$bridges_manager -r"
else
echo -e "${magenta}All bridges are healthy${nc}"
fi
echo -e "${cyan}active bridges : $(active_bridges)${nc}"
}
# remove all broken bridge
function clear_broken_bridges(){
broken_bridges=$(grep "^# Bridge obfs4" $tor_config_file)
if [[ ! -z "$broken_bridges" ]]; then
echo -e "${light_blue}broken bridges : ${light_red}"
# colored logo [X] ip:port < bridge
logo="\\$nc\\$light_blue[\\${light_red}X\\$light_blue] \\$clear_text"
# change format text to list
echo -e "$(cut -d' ' -f 2-5 <<< "$broken_bridges"|sed "s/^/\t$logo/g"))"
# do u want to remove disabled bridges ?
echo -ne "\n${light_magenta}do you want to delete broken bridges (y/n): "
read delete
if [[ "$delete" == [yY]* ]]; then
# remove all disabled bridges
sed -i "/^# Bridge obfs4/ d" "$tor_config_file"
echo -e "${light_green}broken bridges successfully removed."
else
echo -e "${light_yellow}remove broken bridges cancelled."
fi
else
echo -e "${light_yellow}you dont have broken bridges"
fi
}
# status tor service
function status_tor(){
echo -e "${nc}Tor Status :"
# start separator
echo -e "\n\t${status_sep}${status_color}"
# status tor text to menu list + colored important sections + numbered list
echo -e "$(tor_status "$1" |
sed "s/\(Bootstrapped[^%]*%\)./\\${status_important}\1\\${status_important_line}/g" |
# bad line colored
sed "s/\(^.*\)\(unable [^(]\+\)\(.*$\)/\\${status_bad_bridge_line}\1\\${status_bad_bridge}\2\\${status_bad_bridge_line}\3/g" |
nl | sed "s/^ \+[0-9]\+/\\${status_num_color}&\\${status_color}/g" |
sed "s/^/\n/g" |
sed 's/\.\s\+/\.\n\t\t/g')"
# end separator
echo -e "\n\t${status_sep}"
exit 0
}
# stop tor service
function stop_tor(){
# just stop tor > function > /etc/tbcli/tbcli.conf
tor_stop
echo -e "${light_cyan}tor Stoped!"
}
# restart tor service with display bar
function reset_tor(){
# restart tor. function in config file
echo -e "${magenta}wait for restart tor service .."
tor_restart
# my tor status bar
res=0
status=" "
sep="${reset_sep}"
while [[ "$res" -lt "100" ]];do
sleep 0.1
res=$(tor_status |grep -E -o "Bootstrapped[^%]*" |tail -n 1 |cut -d' ' -f2)
[[ -z $(grep "$res" <<< "$status") ]] && status="${res}${status}" && echo -ne "${sep}${res}"
done
echo
}
# uninstall script
function UninstallTBCLI(){
echo -ne "${light_blue}"
read -p "Do you want to delete this Script (y/n): " req
if [[ "$req" == [yY]* ]] ;then
echo -e "${light_green}Wait for uninstall ${light_red}...${light_yellow}"
directory_conf=$(grep -o "^.*/" <<< "$path_config")
# /usr/bin/tbcli, /etc/tbcli
PRJ_FILES=("$path_program" "$directory_conf")
for path in ${PRJ_FILES[@]} ;do
if [[ -e "$path" ]] ;then
echo -e "${light_yellow}program : ${light_magenta}${path}"
rm -rf "${path}"
else
echo -e "${light_red}can't remove this path : ${path} ."
exit 1
fi
done
echo -e "${light_magenta}Thank you for using."
fi
echo -ne "${nc}"
}
# reset terminal
#tput reset
# without args print help
if [ $# -lt 1 ] ;then
usage && exit 0
fi
while [ "$1" != "" ]; do
case $1 in
-a | -A | --add-bridges ) add_bridges="True" ;;
-p | -P | --print-only-bridges ) print_bridges="True" ;;
-d | -D | --disable-broken-bridges ) bridges_manager="$bridges_manager -d" ;;
-c | -C | --clear-broken-bridges ) bridges_manager="$bridges_manager -c" ;;
-e | -E | --enable-all-bridges ) bridges_manager="$bridges_manager -e" ;;
-r | -R | --reset-tor ) bridges_manager="$bridges_manager -r" ;;
-s | -S | --status-tor ) status_tor "$2";;
-o | -O | --off-tor ) bridges_manager="$bridges_manager -o" ;;
-u | -U | --uninstall ) uninstall="True" ;;
-h | -H | --help | * ) usage && exit 0;;
esac
shift
done
# This script need to run as root
if [[ $UID -ne 0 ]]; then
echo -e "${light_red}this script must be run as root!${nc}"
exit 1
fi
echo -e "$sections_sep" # separator
# uninstall program
if [[ ! -z $uninstall ]] ; then
UninstallTBCLI
echo -e "$sections_sep" # separator
exit 0
fi
# delete my tmp files pls :)
trap ClearTmpFiles EXIT
# get tor bridges
if [[ ! -z $print_bridges || ! -z $add_bridges ]] ; then
BRIDGES=""
get_tor_bridges
bridges_manager="$bridges_manager -r" # restart tor in end
echo -e "$sections_sep" # separator
fi
# print only bridges ( not add )
if [[ ! -z $print_bridges && -z $add_bridges ]] ; then
echo -e "${cyan}${BRIDGES}"
echo -e "$sections_sep" # separator
fi
# add bridges into tor config file and print bridges
if [[ ! -z $add_bridges ]] ; then
save_and_print_bridges
echo -e "$sections_sep" # separator
fi
# enable all bridges
if grep -q "\-e" <<< $bridges_manager ;then
enable_all_bridges
echo -e "$sections_sep" # separator
fi
# disable broken bridges
if grep -q "\-d" <<< $bridges_manager ;then
disable_broken_bridges
echo -e "$sections_sep" # separator
fi
# clear all broken bridges
if grep -q "\-c" <<< $bridges_manager ;then
clear_broken_bridges
echo -e "$sections_sep" # separator
fi
# stop tor service
if grep -q "\-o" <<< $bridges_manager ;then
stop_tor
echo -e "$sections_sep" # separator
exit 0
fi
# reset tor service
if grep -q "\-r" <<< $bridges_manager ;then
reset_tor
echo -e "$sections_sep" # separator
fi