-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.sh
More file actions
executable file
·394 lines (344 loc) · 10.9 KB
/
script.sh
File metadata and controls
executable file
·394 lines (344 loc) · 10.9 KB
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
388
389
390
391
392
393
394
#!/bin/bash
trap 'cleanup; exit 1' SIGINT SIGTERM
red="\033[0;31m"
green="\033[0;32m"
yellow="\033[0;33m"
light_green="\033[38;5;50m"
reset="\033[0m"
option=""
hostname=""
local_port=""
remote_port=""
subdomain=""
alias=""
show_help() {
echo "A Bash script to establish HTTP, TCP, and SSH tunnels using Serveo."
echo ""
echo "Usage: $0 [OPTIONS]"
echo ""
echo "Options:"
echo " -o, --option 'http' or 'tcp' or 'ssh' "
echo " -h, --hostname Specify the hostname"
echo " -lp, --local-port Set the local port number"
echo " -rp, --remote-port Set the remote port number"
echo " -s, --subdomain Coustom subdomain of http tunnel"
echo " -a, --alias Assign an alias for ssh"
echo ""
echo " -H, --help Show this help message "
exit 0
}
while [[ $# -gt 0 ]]; do
case "$1" in
-o|--option)
if [[ -z "$2" || "$2" == -* ]]; then
echo "Error: -o|--option requires a value."
exit 1
fi
option="$2"
shift 2
;;
-h|--hostname)
if [[ -z "$2" || "$2" == -* ]]; then
echo "Error: -h|--hostname requires a value."
exit 1
fi
hostname="$2"
shift 2
;;
-lp|-p|--local-port)
if [[ -z "$2" || "$2" == -* ]]; then
echo "Error: -lp|--local-port requires a value."
exit 1
fi
local_port="$2"
shift 2
;;
-rp|--remote-port)
if [[ -z "$2" || "$2" == -* ]]; then
echo "Error: -rp|--remote-port requires a value."
exit 1
fi
remote_port="$2"
shift 2
;;
-s|--subdomain)
if [[ -z "$2" || "$2" == -* ]]; then
echo "Error: -s|--subdomain requires a value."
exit 1
fi
subdomain="$2"
shift 2
;;
-a|--alias)
if [[ -z "$2" || "$2" == -* ]]; then
echo "Error: -a|--alias requires a value."
exit 1
fi
alias="$2"
shift 2
;;
-H|--help)
show_help
;;
*)
echo "Unknown option: $1"
exit 1
;;
esac
done
cleanup() {
kill "$req_pid" 2>/dev/null
kill "$tunnel_pid" 2>/dev/null
kill "$tcp_pid" 2>/dev/null
kill "$ssh_pid" 2>/dev/null
kill "$timer_pid" 2>/dev/null
sleep 1
kill $(pgrep -t $(tty | sed 's/\/dev\///') ssh) 2>/dev/null #just for a final command to ensure all process are killed
}
#function to monitor serveo status
serveo_stat(){
for i in {1..3}; do
if curl -sf -I --connect-timeout 5 "https://serveo.net" &>/dev/null; then
return 0 # Success
fi
done
return 1 # Failure
}
# function to check network connection
check_network() {
local url="https://www.google.com/generate_204"
curl -sf -I --connect-timeout 5 "$url" &>/dev/null || \
curl -sf -I --connect-timeout 5 "$url" &>/dev/null
}
# to keep the http tunnel alive by sending periodic requests
keepalive() {
local url=$(echo "$1" | tr -d '\n\r' | xargs)
while true; do
curl -Is $url | head -n 1 &>/dev/null
sleep 120
done
}
reg_sshkey() {
printf "${yellow}To request a particular subdomain, you first need to register your SSH public key.\n\
To register, visit one of the addresses below to log in with your Google or GitHub account.\n\
After registering, you'll be able to request your subdomain the next time you connect to Serveo.${reset}\n"
if ls ~/.ssh/id_rsa >/dev/null 2>&1; then
cd ~/.ssh || exit
fingerprint=$(ssh-keygen -lf id_rsa | awk '{print $2}')
encodedFingerprint=$(echo "$fingerprint" | sed \
-e 's/:/%3A/g' \
-e 's/\//%2F/g' \
-e 's/+/%2B/g' \
-e 's/=/ %3D/g')
echo ""
echo "register with google account: https://serveo.net/verify/google?fp=$encodedFingerprint"
echo "register with github account: https://serveo.net/verify/github?fp=$encodedFingerprint"
exit 1
else
echo ""
printf "A valid SSH key not found. do you want to create one (yes/no): "
read input
if [[ "${input,,}" == "yes" || "${input,,}" == "y" ]]; then
ssh-keygen -t rsa -b 4096
cd ~/.ssh || exit
fingerprint=$(ssh-keygen -lf id_rsa | awk '{print $2}')
encodedFingerprint=$(echo "$fingerprint" | sed \
-e 's/:/%3A/g' \
-e 's/\//%2F/g' \
-e 's/+/%2B/g' \
-e 's/=/ %3D/g')
echo ""
echo ""
echo "register with google account: https://serveo.net/verify/google?fp=$encodedFingerprint"
echo "register with github account: https://serveo.net/verify/github?fp=$encodedFingerprint"
exit 1
elif [[ "${input,,}" == "no" || "${input,,}" == "n" ]]; then
exit 1
else
echo Invalid input
exit 1
fi
fi
}
httptunnel() {
local port=80
local temp_file; temp_file=$(mktemp /tmp/tempfileXXXXXX.txt)
if [[ -z "$subdomain" ]]; then
printf "${green}Establishing HTTP tunnel with random subdomain...${reset}\n"
ssh -nT -R "$port:$hostname:$local_port" serveo.net | stdbuf -oL awk 'NR==1 {print $5}' > "$temp_file" &
tunnel_pid=$!
else
printf "${green}Checking availability of subdomain '$subdomain'...${reset}\n"
if wget -q --spider "https://$subdomain.serveo.net"; then
printf "${red}Subdomain '$subdomain' is not available. Exiting.${reset}\n"
rm "$temp_file"
exit 1
fi
printf "${green}Establishing HTTP tunnel with subdomain '$subdomain'...${reset}\n"
ssh -nT -R "$subdomain:$port:$hostname:$local_port" serveo.net | stdbuf -oL awk 'NR==1 {print $5}' > "$temp_file" &
tunnel_pid=$!
fi
while true; do #put this in a loop for a reson. there might be delay in writing
if [[ -n "$link" ]]; then
url=$link
break
else
link=$(<"$temp_file")
fi
done
if [[ $link == 'subdomain,' ]]; then
cleanup
reg_sshkey
exit 1
else
printf "${green}HTTP tunnel established: ${light_green}$link${reset}\n"
fi
rm "$temp_file"
keepalive "$url" &
req_pid=$!
}
tcptunnel() {
local port="${remote_port:-0}"
if [[ "$port" -ne 0 ]] && ncat -zv serveo.net "$remote_port" &>/dev/null; then
printf "${red}Remote port $port is in use. Try again with other port number or keep it '0' to get assigned a random one Exiting.${reset}\n"
exit 1
fi
printf "${green}Establishing TCP tunnel on random remote port...${reset}\n"
ssh -nT -R "$port:$hostname:$local_port" serveo.net &
tcp_pid=$!
sleep 5
if ! ps -p $tcp_pid &>/dev/null; then
printf "${red}Failed to establish TCP tunnel. Exiting.${reset}\n"
exit 1
fi
printf "${green}TCP tunnel established successfully.${reset}\n"
}
sshtunnel() {
local port=22
printf "${green}Establishing SSH tunnel...${reset}\n"
ssh -T -R "$alias:22:$hostname:22" serveo.net &
ssh_pid=$!
sleep 5
if ! ps -p $ssh_pid &>/dev/null; then
printf "${red}Failed to establish SSH tunnel. Exiting.${reset}\n"
exit 1
fi
printf "${green}SSH tunnel established successfully.${reset}\n"
}
#a timer function to keep track of time
timer() {
counter=0
while true; do
counter=$((counter + 1))
sleep 1
echo "$counter" > /tmp/counter_value
done
}
maintain_tunnel(){
while true; do
if [[ -z "$killed" ]]; then
killed=0
fi
if [[ -z "$prob_exp" ]]; then
prob_exp=0
fi
if ! check_network ; then
printf "${red}Network connection lost.${reset}\n"
printf "${red}Waiting for connection...${reset}\n"
until check_network ; do
timer &
timer_pid=$!
prob_exp=1
if [[ $(cat /tmp/counter_value 2>/dev/null) -gt $1 ]] ; then
cleanup
killed=1
else
killed=0
fi
sleep 3
done
if [[ $killed -ne 1 ]]; then
cleanup
killed=1
fi
fi
if ! serveo_stat ; then
printf "${red}Either serveo.net or Network is down${reset}\n"
prob_exp=1
if [[ $killed -ne 1 ]]; then
cleanup
killed=1
fi
until serveo_stat;do
if ! check_network; then
continue 2
fi
sleep 5
done
fi
if [[ $prob_exp -eq 1 ]] ; then #run if connection prob(serveo or net down) was there
printf "${green}Connection restroed.${reset}\n"
printf "${green}Restarting tunnel...${reset}\n"
restart_tunnel $option $host $local_port $parameter
prob_exp=0
killed=0
fi
done
}
restart_tunnel() {
case "$opt" in
1)
httptunnel "$@"
;;
2)
tcptunnel "$@"
;;
3)
sshtunnel "$@"
;;
*)
printf "${red}Unknown tunnel option during restart. Exiting.${reset}\n"
exit 1
;;
esac
}
main() {
# Check if internet connection is available
if ! check_network; then
printf "${red}No internet connection. Exiting.${reset}\n"
exit 1
fi
# Check if Serveo is up
if ! serveo_stat; then
printf "${red}SERVEO IS DOWN. Exiting.${reset}\n"
exit 1
fi
if [[ -z $option ]]; then
printf "${red}Protocol not specified. Exiting.${reset}\n"
exit 1
fi
# condition for each case
case "$option" in
http)
opt=1
[[ -z "$local_port" ]] && { printf "${red}-lp | Local port not specified. Exiting.${reset}\n"; exit 1; }
httptunnel "$@"
;;
tcp)
opt=2
[[ -z "$local_port" ]] && { printf "${red}-lp | Local port not specified. Exiting.${reset}\n"; exit 1; }
tcptunnel "$@"
;;
ssh)
opt=3
[[ -z "$alias" ]] && { printf "${red}-a | Public hostname alias not specified. Exiting.${reset}\n"; exit 1; }
sshtunnel "$@"
;;
*)
printf "${red}Invalid protocol specified. use --help for more info ${reset}\n"
exit 1
;;
esac
maintain_tunnel 600
}
main "$@"