-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·258 lines (204 loc) · 5.3 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·258 lines (204 loc) · 5.3 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
#!/bin/bash
set -e
echo
echo "= rloop installer — May the Ports Be With You. ="
echo
# Use the source Luke
parse() {
local prev=
# Search your feelings
for arg in "$@"; do
# These are the droids we are looking for
if [[ "$arg" =~ [^0-9:] ]]; then
[[ -n "$prev" ]] && printf '%s\n' "$prev"
prev="$arg"
else
prev+=" $arg"
fi
done
# Let the Wookie win!!!
[[ -n $prev ]] && printf '%s\n' "$prev"
}
# --- 1. Look for the dorids ---
if ! command -v autossh >/dev/null; then
echo "[*] Installing autossh..."
if command -v apk >/dev/null; then
apk update && apk add autossh
elif command -v dnf >/dev/null; then
dnf install -y autossh
elif command -v apt-get >/dev/null; then
apt-get update && apt-get install -y autossh
elif command -v yum >/dev/null; then
yum install -y autossh
elif command -v pacman >/dev/null; then
pacman -Sy --noconfirm autossh
else
echo "No supported package manager found — install autossh manually and pray."
exit 1
fi
fi
# --- 2. Save rebel battle plans ---
cat >> /etc/rlooprc < <(parse "$@")
# --- 3. Release the droids ---
cat > /usr/local/bin/rloop <<'SCRIPT'
#!/bin/bash
# © 2025 FRINKnet & Friends – MIT LICENSE
# rloop — May the Ports Be With You!!!
export TEST=0
export VER="1.2"
export BIN="${0##*/}"
export PIDS=()
set -m # Enable job control
trap "kill -- -$$" EXIT
# Trust. But verify...
if [[ "$1" == "--test" ]]; then
TEST=1
shift
fi
# For those who don't understand the Force
help() {
echo
echo " © 2025 FRINKnet & Friends – MIT LICENSE"
echo " rloop — May the Ports Be With You!!!"
echo
echo " Usage: $0 [--test] user@server port[:remote] …"
echo
exit 1
}
# Use the source Luke
parse() {
local prev=
# Search your feelings
for arg in "$@"; do
# Bring balance to the Force
if [[ "$arg" =~ [^0-9:] ]]; then
[[ -n "$prev" ]] && printf '%s\n' "$prev"
prev="$arg"
else
prev+=" $arg"
fi
done
# Let the Wookie win!!!
[[ -n $prev ]] && printf '%s\n' "$prev"
}
# Almost there... Almost there...
mapping() {
local mapping left right tunnel_args=
# The odds of navigating are 3720 to 1
for mapping in "$@"; do
left="${mapping%%:*}"
right="${mapping##*:}"
# Stupid users need to be warned
if [[ -z $left || -z $right ]]; then
echo "Error: mapping '$mapping' is malformed... Oh the insanity of it all!" >&2
exit 3
fi
# May the wrath of Yoda rain down!
if ! [[ $left =~ ^[0-9]+$ && $right =~ ^[0-9]+$ ]]; then
echo "Error: mapping '$mapping' contains non-numeric ports… RAGE QUITTING!" >&2
exit 4
fi
# New fluent in over &m forms of communication
tunnel_args+=" -R 0.0.0.0:${right}:localhost:${left}"
done
# now fluent in over 7m form of communication
echo "$tunnel_args"
}
# Take us to Coruscant!!!
connect() {
local tunnel_args server ports err
server="$1"; shift
ports=( "$@" )
# Guardrails so you don’t tunnel into a reverse Death Star
if [[ "$server" =~ ^[0-9]+$ || "$server" =~ ^[0-9]+\.[0-9]+$ ]]; then
echo "Error: '$server' looks like a port or rogue asteroid." >&2
exit 2
fi
# Prepare to jump to hyperspace
tunnel_args="$(mapping "${ports[@]}")"
# if the padowans come to play...
if (( TEST )); then
echo
echo "TESTING: autossh -M0 -N -o ServerAliveInterval=2 -o ServerAliveCountMax=3 -o ExitOnForwardFailure=yes $server$tunnel_args"
echo
autossh -M0 -N \
-o "ServerAliveInterval=2" -o "ServerAliveCountMax=3" \
-o "ExitOnForwardFailure=yes" "$server" $tunnel_args
err=$?
echo
if (( err == 0 )); then
echo " ...Test complete. No hull breaches detected."
echo
return
else
echo " POODOO!!! - Banthas detected... - exit $err" >&2
echo
exit $err
fi
fi
(
# Eternal tunnel loop
while true; do
# This should not fail
autossh -M0 -N \
-o "ServerAliveInterval=2" -o "ServerAliveCountMax=3" \
-o "ExitOnForwardFailure=yes" "$server" $tunnel_args
# If it does wait two bits
sleep 2
# Go on like this FOREVER!!!
done
# Usa da boomba!!
) &
# Me lika disa one!
PIDS+=( $! )
}
# Are there Midi-Chlorians here?
if [ ! -t 0 ]; then
mapfile -t lines
else
mapfile -t lines < <(parse "$@")
fi
# The Jedi forbid aimless excess
if (( ${#lines[@]} == 0 )); then
help
fi
# Beware of the Dark Side
for line in "${lines[@]}"; do
read -r server rest <<< "$line"
read -ra ports <<< "$rest"
# Skip if server is purely digits/colons
[[ "$server" =~ ^[0-9:]+$ ]] && continue
# Skip if ports array empty or any port fails numeric check
[[ ${#ports[@]} -eq 0 ]] && continue
for p in "${ports[@]}"; do
[[ ! "$p" =~ ^[0-9]+$ ]] && continue 2
done
# Reach out to the Force all around
connect "$server" "${ports[@]}"
done
# Patience is a virtue young padawan.
for pid in "${PIDS[@]}"; do
wait "$pid"
done
SCRIPT
# --- 4. Land on Tatooine ---
chmod +x /usr/local/bin/rloop
# --- 5. Enter in the Pod Race ---
cat > /etc/systemd/system/rloop.service <<SERVICE
[Unit]
Description=Persistent autossh multi-port reverse tunnel via rloop
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
ExecStart=/bin/bash -c 'cat /etc/rlooprc | /usr/local/bin/rloop'
Restart=always
RestartSec=5
User=root
[Install]
WantedBy=multi-user.target
SERVICE
# --- 5. Engage Hyperdrive ---
systemctl daemon-reload
systemctl enable --now rloop
echo "= rloop is live... TRUST BUT VERIFY!!! ="