-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.sh
203 lines (170 loc) · 5.22 KB
/
main.sh
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
#!/bin/bash
trap 'cleanup; exit' SIGINT SIGTERM
red="\033[0;31m"
green="\033[0;32m"
yellow="\033[0;33m"
light_green="\033[38;5;50m"
reset="\033[0m"
cleanup() {
printf "${yellow}Cleaning up processes...${reset}\n"
kill "$req_pid" 2>/dev/null
kill "$tcp_pid" 2>/dev/null
kill "$ssh_pid" 2>/dev/null
sleep 1
kill $(pgrep -t $(tty | sed 's/\/dev\///') ssh) 2>/dev/null
}
check_network() {
local url="https://www.google.com/generate_204"
curl -sf --connect-timeout 5 "$url" &>/dev/null
}
keepalive() {
local url=$(echo "$1" | tr -d '\n\r' | xargs)
while true; do
curl -Is $url | head -n 1 &>/dev/null
sleep 120
done
}
httptunnel() {
local remote_port=80
local subdomain="$4"
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 "$remote_port:$host:$3" 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 --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:$remote_port:$host:$3" serveo.net | stdbuf -oL awk 'NR==1 {print $5}' > "$temp_file" &
tunnel_pid=$!
fi
while true; do
if [[ -n "$link" ]]; then
printf "${green}HTTP tunnel established: ${light_green}$link${reset}\n"
url=$link
link=""
break
else
link=$(<"$temp_file")
fi
sleep 1
done
sleep 2
rm "$temp_file"
keepalive "$url" &
req_pid=$!
}
tcptunnel() {
local remote_port="${4:-0}"
if [[ "$remote_port" -ne 0 ]] && ncat -zv serveo.net "$remote_port" &>/dev/null; then
printf "${red}Remote port $remote_port is in use. Exiting.${reset}\n"
exit 1
fi
printf "${green}Establishing TCP tunnel on port $remote_port...${reset}\n"
ssh -nT -R "$remote_port:$host:$3" 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() {
printf "${green}Establishing SSH tunnel...${reset}\n"
ssh -T -R "$3:22:$host: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"
}
restart_tunnel() {
case "$opt" in
1)
httptunnel "$@"
;;
2)
tcptunnel "$@"
;;
3)
sshtunnel "$@"
;;
*)
printf "${red}Unknown tunnel option during restart. Exiting.${reset}\n"
exit 1
;;
esac
}
monitor_tunnels() {
local first_warning=true
while true; do
if ! check_network; then
printf "${yellow}Internet connection lost.${reset}\n"
if $first_warning; then
printf "${yellow}Waiting for internet connection...${reset}\n"
first_warning=false
fi
until check_network; do
sleep 5
done
printf "${green}Internet connection restored. Restarting tunnels...${reset}\n"
first_warning=true
sleep 10
cleanup
restart_tunnel "$@"
fi
if { [[ "$opt" -eq 1 ]] && ! ps -p "$tunnel_pid" &>/dev/null; } || \
{ [[ "$opt" -eq 2 ]] && ! ps -p "$tcp_pid" &>/dev/null; } || \
{ [[ "$opt" -eq 3 ]] && ! ps -p "$ssh_pid" &>/dev/null; }; then
printf "${yellow}Tunnel process has stopped unexpectedly. Restarting...${reset}\n"
cleanup
restart_tunnel "$@"
fi
sleep 10
done
}
main() {
if ! check_network; then
printf "${red}No internet connection. Exiting.${reset}\n"
exit 1
fi
if [[ "$2" == "lh" ]]; then
host="localhost"
elif [[ -z $2 ]]; then
printf "${red}Hostname not specified. Exiting.${reset}\n"
exit 1
else
host="$2"
fi
case "$1" in
http)
opt=1
[[ -z "$3" ]] && { printf "${red}Local port not specified. Exiting.${reset}\n"; exit 1; }
httptunnel "$@"
;;
tcp)
opt=2
[[ -z "$3" ]] && { printf "${red}Local port not specified. Exiting.${reset}\n"; exit 1; }
tcptunnel "$@"
;;
ssh)
opt=3
[[ -z "$3" ]] && { printf "${red}Public hostname alias not specified. Exiting.${reset}\n"; exit 1; }
sshtunnel "$@"
;;
*)
printf "${red}Invalid protocol specified. Exiting.${reset}\n"
exit 1
;;
esac
monitor_tunnels "$@"
}
main "$@"