Enhance backtunnel-share with improved desktop integration, port collision checks, and pre-flight validations
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# Copyright (c) 2025. LUXIM d.o.o., Slovenia - Matjaž Mozetič.
|
||||
# Licensed under the GNU GPL v3.0
|
||||
|
||||
# backtunnel-share: Share a folder using reverse SSH for a limited duration
|
||||
# Syntax: backtunnel-share /path/to/folder with remoteuser:remotehost for 2h
|
||||
@@ -163,8 +164,23 @@ fi
|
||||
|
||||
echo "Tip: On the remote side, mount with:"
|
||||
echo " backtunnel-access '${FOLDER}' from ${REMOTE_USER}@${REMOTE_HOST} -p ${TUNNEL_PORT}"
|
||||
echo "To stop sharing early: press Ctrl+C here."
|
||||
|
||||
# --- optional pre-flight: warn if remote loopback port is already in use (best-effort) ---
|
||||
if ssh -o BatchMode=yes -o ConnectTimeout=5 "${REMOTE_USER}@${REMOTE_HOST}" \
|
||||
"command -v nc >/dev/null 2>&1 && nc -z 127.0.0.1 ${TUNNEL_PORT}"; then
|
||||
echo "⚠️ Port ${TUNNEL_PORT} on remote 127.0.0.1 appears in use; choose another with -p." >&2
|
||||
# Uncomment the next line to make it a hard failure:
|
||||
# exit 1
|
||||
fi
|
||||
|
||||
# Bind reverse to 127.0.0.1 on the REMOTE host (private-by-default).
|
||||
# Keepalive so the tunnel drops on dead links.
|
||||
# Exit fast if the reverse bind fails (e.g., port in use).
|
||||
timeout "$DURATION" ssh -N \
|
||||
-o ExitOnForwardFailure=yes \
|
||||
-o ServerAliveInterval=15 \
|
||||
-o ServerAliveCountMax=3 \
|
||||
-R "${TUNNEL_PORT}:localhost:${LOCAL_SSH_PORT}" \
|
||||
-- "${REMOTE_USER}@${REMOTE_HOST}"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user