-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathssh-proxy.sh
More file actions
executable file
·285 lines (241 loc) · 9.7 KB
/
ssh-proxy.sh
File metadata and controls
executable file
·285 lines (241 loc) · 9.7 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
#!/usr/bin/env bash
# ssh-proxy.sh – auto‑installer & manager for an SSH→SOCKS→HTTP proxy
# ------------------------------------------------------------------
# * Creates an SSH key, copies it to the server
# * Adds an alias to ~/.ssh/config
# * Generates a systemd‑user service (autossh + gost)
# * Provides install | reconfigure | uninstall | status commands
# * Optionally installs missing dependencies via the host package manager
# ------------------------------------------------------------------
set -euo pipefail
# ---- meta -----------------------------------------------------
SCRIPT_NAME="ssh-proxy"
AUTHOR="Arshia Moghaddam"
GITHUB_URL="https://github.com/ars2062"
VERSION="1.0.0"
# ---- paths & constants ---------------------------------------
APP_NAME="$SCRIPT_NAME" # keep for backward compat if used elsewhere
CONFIG_DIR="$HOME/.config/$APP_NAME"
CONFIG_FILE="$CONFIG_DIR/config"
SERVICE_FILE="$HOME/.config/systemd/user/$APP_NAME.service"
SSH_KEY="$HOME/.ssh/id_ed25519_${APP_NAME}"
SSH_ALIAS="$APP_NAME"
SOCKS_PORT=10808
HTTP_PORT=9001
AUTOSSH_POLL=30
# ---- pretty printing helpers ---------------------------------
BOLD=$(tput bold || true)
GREEN=$(tput setaf 2 || true)
RED=$(tput setaf 1 || true)
YELLOW=$(tput setaf 3 || true)
RESET=$(tput sgr0 || true)
info() { printf "%s%s%s\n" "$GREEN" "$*" "$RESET"; }
warn() { printf "%s%s%s\n" "$YELLOW" "$*" "$RESET"; }
error() { printf "%s%s%s\n" "$RED" "$*" "$RESET" >&2; }
banner() {
local inner_width=78
local credit_line="$SCRIPT_NAME v$VERSION – by $AUTHOR | $GITHUB_URL"
local credit_len=${#credit_line}
if [[ $credit_len -gt $inner_width ]]; then
credit_line="${credit_line:0:$inner_width}"
credit_len=${#credit_line}
fi
local pad_left=$(( (inner_width - credit_len) / 2 ))
local pad_right=$(( inner_width - credit_len - pad_left ))
cat <<'ART'
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ ┃
┃ ┃
┃ ███████╗███████╗██╗ ██╗ ██████╗ ██████╗ ██████╗ ██╗ ██╗██╗ ██╗ ┃
┃ ██╔════╝██╔════╝██║ ██║ ██╔══██╗██╔══██╗██╔═══██╗╚██╗██╔╝╚██╗ ██╔╝ ┃
┃ ███████╗███████╗███████║█████╗██████╔╝██████╔╝██║ ██║ ╚███╔╝ ╚████╔╝ ┃
┃ ╚════██║╚════██║██╔══██║╚════╝██╔═══╝ ██╔══██╗██║ ██║ ██╔██╗ ╚██╔╝ ┃
┃ ███████║███████║██║ ██║ ██║ ██║ ██║╚██████╔╝██╔╝ ██╗ ██║ ┃
┃ ╚══════╝╚══════╝╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝ ┃
┃ ┃
ART
printf '┃%*s%s%*s┃\n' "$pad_left" '' "$credit_line" "$pad_right" ''
cat <<'ART'
┃ ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
ART
}
require_cmd() { command -v "$1" >/dev/null 2>&1 || { error "Missing dependency: $1"; exit 1; }; }
# ---- dependency installer ------------------------------------
install_dependencies() {
local need_install=()
for cmd in autossh gost ssh ssh-keygen ssh-copy-id; do
command -v "$cmd" >/dev/null 2>&1 || need_install+=("$cmd")
done
[[ ${#need_install[@]} -eq 0 ]] && return
warn "Installing missing dependencies: ${need_install[*]}"
if command -v apt-get >/dev/null 2>&1; then
sudo apt-get update -qq
if [[ " ${need_install[*]} " =~ " autossh " ]]; then sudo apt-get install -y autossh; fi
elif command -v dnf >/dev/null 2>&1; then
sudo dnf install -y autossh
elif command -v pacman >/dev/null 2>&1; then
sudo pacman -Sy --noconfirm autossh
else
warn "Unsupported package manager – please install autossh and gost manually."
fi
if ! command -v gost >/dev/null 2>&1; then
warn "Installing gost from GitHub releases..."
warn "Installing gost from GitHub releases..."
local tmp
tmp=$(mktemp -d)
curl -sSL -o "$tmp/gost.tgz" "https://github.com/go-gost/gost/releases/latest/download/gost-linux-amd64.tgz"
tar -C "$tmp" -xzf "$tmp/gost.tgz"
sudo /usr/bin/install -m 755 "$tmp/gost" /usr/local/bin/gost
rm -rf "$tmp"
fi
}
# ---- config helpers ------------------------------------------
generate_config() {
mkdir -p "$CONFIG_DIR"
cat >"$CONFIG_FILE" <<EOF
REMOTE_HOST="$REMOTE_HOST"
REMOTE_USER="$REMOTE_USER"
REMOTE_PORT="$REMOTE_PORT"
SOCKS_PORT="$SOCKS_PORT"
HTTP_PORT="$HTTP_PORT"
SSH_KEY="$SSH_KEY"
SSH_ALIAS="$SSH_ALIAS"
EOF
}
load_config() {
[[ -f $CONFIG_FILE ]] || { error "Config not found. Run '$0 install' first."; exit 1; }
# shellcheck source=/dev/null
source "$CONFIG_FILE"
}
generate_ssh_config_block() {
cat <<EOF
Host $SSH_ALIAS
HostName $REMOTE_HOST
User $REMOTE_USER
Port $REMOTE_PORT
IdentityFile $SSH_KEY
ServerAliveInterval 60
ServerAliveCountMax 3
EOF
}
# ---- core actions --------------------------------------------
install() {
install_dependencies
require_cmd autossh
require_cmd gost
require_cmd ssh
require_cmd ssh-keygen
[[ -f $CONFIG_FILE ]] && { warn "Existing configuration found."; reconfigure_prompt; return; }
read -rp "Remote SSH host (IP or DNS): " REMOTE_HOST
read -rp "Remote SSH user [$(whoami)]: " REMOTE_USER
REMOTE_USER=${REMOTE_USER:-$(whoami)}
read -rp "Remote SSH port [22]: " REMOTE_PORT
REMOTE_PORT=${REMOTE_PORT:-22}
info "Generating SSH key $SSH_KEY (if not present)…"
[[ -f $SSH_KEY ]] || ssh-keygen -t ed25519 -f "$SSH_KEY" -N "" -C "$APP_NAME"
info "Copying public key to $REMOTE_USER@$REMOTE_HOST…"
ssh-copy-id -i "${SSH_KEY}.pub" -p "$REMOTE_PORT" "$REMOTE_USER@$REMOTE_HOST"
mkdir -p "$HOME/.ssh"
touch "$HOME/.ssh/config"
if ! grep -q "Host $SSH_ALIAS" "$HOME/.ssh/config"; then
generate_ssh_config_block >>"$HOME/.ssh/config"
info "Added entry '$SSH_ALIAS' to ~/.ssh/config"
else
warn "An entry named '$SSH_ALIAS' already exists in ~/.ssh/config (skipped)"
fi
generate_config
info "Config saved to $CONFIG_FILE"
create_service
info "Installation complete. Use 'systemctl --user status $APP_NAME' to see logs."
}
create_service() {
mkdir -p "$(dirname "$SERVICE_FILE")"
# Absolute path so systemd doesn’t need PATH to find it
GOST_BIN=$(command -v gost)
cat >"$SERVICE_FILE" <<EOF
[Unit]
Description=SSH → SOCKS → HTTP proxy tunnel (autossh + gost)
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
# autossh housekeeping
Environment="AUTOSSH_GATETIME=0" "AUTOSSH_POLL=$AUTOSSH_POLL"
# add /usr/local/bin in case you put other stuff there later
Environment="PATH=/usr/local/sbin:/usr/local/bin:/usr/bin:/bin"
Restart=on-failure
RestartSec=5
ExecStart=/usr/bin/env bash -c 'autossh -M 0 -N -D 127.0.0.1:$SOCKS_PORT $SSH_ALIAS & exec "$GOST_BIN" -L=http://:$HTTP_PORT -F=socks5://127.0.0.1:$SOCKS_PORT'
KillMode=control-group
[Install]
WantedBy=default.target
EOF
systemctl --user daemon-reload
systemctl --user enable --now "$APP_NAME.service"
}
uninstall() {
load_config
info "Stopping and disabling service…"
systemctl --user disable --now "$APP_NAME.service" || true
rm -f "$SERVICE_FILE"
if grep -q "Host $SSH_ALIAS" "$HOME/.ssh/config"; then
info "Removing SSH config entry '$SSH_ALIAS'…"
sed -i.bak "/Host $SSH_ALIAS/,+5d" "$HOME/.ssh/config"
fi
info "Removing config directory…"
rm -rf "$CONFIG_DIR"
read -rp "Delete SSH key pair $SSH_KEY? [y/N]: " ans
if [[ $ans =~ ^[Yy]$ ]]; then
rm -f "$SSH_KEY" "$SSH_KEY.pub"
info "Key pair deleted."
fi
info "Uninstall completed."
}
status() {
load_config
systemctl --user status "$APP_NAME.service"
}
# ---- runtime controls ---------------------------------------
stop() {
load_config
info "Stopping service…"
systemctl --user stop "$APP_NAME.service"
}
start() {
load_config
info "Starting service…"
systemctl --user start "$APP_NAME.service"
}
reconfigure_prompt() {
read -rp "Reinstall? This will overwrite configuration. [y/N]: " ans
[[ $ans =~ ^[Yy]$ ]] || { info "Aborted."; exit 0; }
uninstall
install
}
usage() {
banner
cat <<EOF
${BOLD}Usage:${RESET} $0 <command>
Commands:
install Install and start the proxy tunnel
reconfigure Run the installer again and overwrite existing config
uninstall Remove the service, config, and optionally the key
status Show service status
start Start the proxy service
stop Stop the proxy service
help Show this message
EOF
}
# ---- main -----------------------------------------------------
banner
case "${1:-install}" in
install) install ;;
reconfigure) reconfigure_prompt ;;
uninstall) uninstall ;;
status) status ;;
start) start ;;
stop) stop ;;
help|-h|--help) usage ;;
*) usage ;;
esac