Add invite and QR code features to backtunnel-share

This commit is contained in:
2025-09-14 12:54:06 +02:00
parent d510d777ca
commit 07dbd79aa2
3 changed files with 134 additions and 4 deletions

View File

@@ -4,7 +4,7 @@
# backtunnel-share: Share a folder using reverse SSH for a limited duration
# Syntax: backtunnel-share /path/to/folder with remoteuser:remotehost for 2h
# Options: -p|--tunnel-port <PORT> -l|--local-ssh-port <PORT> -h|--help
# Options: -p|--tunnel-port <PORT> -l|--local-ssh-port <PORT> -i|--invite [--invite-mount <PATH>] [--invite-file <FILE>] [--qr] -h|--help
set -euo pipefail
@@ -12,6 +12,11 @@ TUNNEL_PORT=2222 # remote-side port exposed via -R
LOCAL_SSH_PORT=22 # local sshd port to forward to
DURATION="" # required: e.g. 30m, 2h, 1d
INVITE=false # print a ready-to-copy access command
INVITE_MOUNT="/mnt/remote-rssh"
INVITE_FILE=""
QR=false # render invite as terminal QR (requires qrencode)
usage() {
cat >&2 <<EOF
Usage:
@@ -27,11 +32,16 @@ Positional (required, in order):
Options:
-p, --tunnel-port N Remote tunnel port (default: ${TUNNEL_PORT})
-l, --local-ssh-port N Local sshd port to expose (default: ${LOCAL_SSH_PORT})
-i, --invite Print a ready-to-copy access command for the remote side
--invite-mount PATH Mount point suggested in invite (default: ${INVITE_MOUNT})
--invite-file FILE Also write the invite text (with unmount hint) to FILE
--qr Also print a QR code (requires 'qrencode')
-h, --help Show this help
Examples:
$(basename "$0") ~/projects with alice:vps.example.com for 2h
$(basename "$0") ~/projects with alice@vps.example.com for 1d -p 4422 -l 2222
$(basename "$0") ~/projects with alice@vps.example.com for 2h -i --qr
EOF
exit 1
}
@@ -62,6 +72,24 @@ while [[ $# -gt 0 ]]; do
LOCAL_SSH_PORT=$2
shift 2
;;
-i|--invite)
INVITE=true
shift
;;
--invite-mount)
[[ $# -lt 2 ]] && usage
INVITE_MOUNT=$2
shift 2
;;
--invite-file)
[[ $# -lt 2 ]] && usage
INVITE_FILE=$2
shift 2
;;
--qr)
QR=true
shift
;;
-h|--help)
usage
;;
@@ -101,6 +129,38 @@ echo " remote bind port : ${TUNNEL_PORT} (on ${REMOTE_HOST})"
echo " remote user : ${REMOTE_USER}"
echo " duration : ${DURATION}"
echo
# --- print invite (optional) ---
if $INVITE; then
INVITE_CMD="backtunnel-access '${FOLDER}' from ${REMOTE_USER}@${REMOTE_HOST} -p ${TUNNEL_PORT} -m '${INVITE_MOUNT}'"
INVITE_TEXT=$(
cat <<EOT
# Paste this on the REMOTE host (or SSH there first, then paste):
${INVITE_CMD}
# Unmount when done:
fusermount -u '${INVITE_MOUNT}'
EOT
)
echo "🔗 Invite (copy to chat):"
echo "------------------------------------------------------------"
echo "${INVITE_CMD}"
echo "------------------------------------------------------------"
if [[ -n "${INVITE_FILE}" ]]; then
printf "%s\n" "${INVITE_TEXT}" > "${INVITE_FILE}"
echo "Saved invite to: ${INVITE_FILE}"
fi
if $QR; then
if command -v qrencode >/dev/null 2>&1; then
echo
echo "📱 QR (scan to copy the command):"
printf "%s" "${INVITE_CMD}" | qrencode -t ansiutf8
else
echo "⚠️ 'qrencode' not installed; skipping QR."
fi
fi
echo
fi
echo "Tip: On the remote side, mount with:"
echo " backtunnel-access '${FOLDER}' from ${REMOTE_USER}@${REMOTE_HOST} -p ${TUNNEL_PORT}"