seminar-vibecoding/http2socks.sh
2026-08-06 23:08:41 +03:00

85 lines
2.2 KiB
Bash
Executable File

#!/usr/bin/env bash
# Start the HTTP-to-SOCKS5h bridge in the background.
#
# listens: http://127.0.0.1:18080
# upstream: socks5h://127.0.0.1:12026 (SSH -D, remote DNS)
#
# Tools that only understand HTTP proxies can now do:
# HTTP_PROXY=http://127.0.0.1:18080 HTTPS_PROXY=http://127.0.0.1:18080 ...
#
# Env overrides: BIND PORT UPSTREAM (see http2socks.py)
#
# ./http2socks.sh # start (idempotent -- won't double-start)
# ./http2socks.sh stop # kill running instance
# ./http2socks.sh status # show running pid + egress IP via the bridge
# ./http2socks.sh logs # tail the log
set -euo pipefail
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PY="$HERE/.venv/bin/python"
LOG="${LOG:-$HERE/.http2socks.log}"
PIDFILE="$HERE/.http2socks.pid"
BIND="${BIND:-127.0.0.1}"
PORT="${PORT:-18080}"
is_running() {
[[ -f "$PIDFILE" ]] && kill -0 "$(cat "$PIDFILE" 2>/dev/null)" 2>/dev/null
}
cmd_start() {
if is_running; then
echo "already running (pid $(cat "$PIDFILE")) on http://$BIND:$PORT"
return 0
fi
nohup "$PY" "$HERE/http2socks.py" --bind "$BIND" --port "$PORT" >"$LOG" 2>&1 &
echo $! > "$PIDFILE"
sleep 0.6
if is_running; then
echo "started (pid $(cat "$PIDFILE")) on http://$BIND:$PORT -> socks5h://127.0.0.1:12026"
echo "log: $LOG"
else
echo "failed to start; log:" >&2
tail -n 20 "$LOG" >&2 || true
rm -f "$PIDFILE"
exit 1
fi
}
cmd_stop() {
if is_running; then
pid="$(cat "$PIDFILE")"
kill "$pid" 2>/dev/null || true
sleep 0.5
kill -9 "$pid" 2>/dev/null || true
echo "stopped (pid $pid)"
else
echo "not running"
fi
rm -f "$PIDFILE"
}
cmd_status() {
if is_running; then
pid="$(cat "$PIDFILE")"
echo "running (pid $pid) on http://$BIND:$PORT -> socks5h://127.0.0.1:12026"
echo "egress via bridge:"
http_proxy="http://$BIND:$PORT" https_proxy="http://$BIND:$PORT" \
curl -s --max-time 15 https://api.ipify.org && echo || echo "(curl failed)"
else
echo "not running"
fi
}
cmd_logs() {
tail -n "${1:-50}" -f "$LOG"
}
case "${1:-start}" in
start) cmd_start ;;
stop) cmd_stop ;;
status) cmd_status ;;
logs) shift; cmd_logs "$@" ;;
*) echo "usage: $0 [start|stop|status|logs]" >&2; exit 2 ;;
esac