Add profile support to BackTunnel with defaults, named remotes, and seamless integration across CLI, GUI, and documentation

This commit is contained in:
2025-09-14 19:44:37 +02:00
parent 905bc24f78
commit 0ad3041fce
6 changed files with 363 additions and 46 deletions

View File

@@ -10,6 +10,75 @@
set -euo pipefail
# Config search order: user → system → packaged example
CONFIG_USER="${XDG_CONFIG_HOME:-$HOME/.config}/backtunnel/profiles.ini"
CONFIG_SYS="/etc/backtunnel/profiles.ini"
CONFIG_PKG="/usr/share/backtunnel/profiles.ini"
# resolve CONFIG_FILE to first existing
if [[ -f "$CONFIG_USER" ]]; then
CONFIG_FILE="$CONFIG_USER"
elif [[ -f "$CONFIG_SYS" ]]; then
CONFIG_FILE="$CONFIG_SYS"
else
CONFIG_FILE="$CONFIG_PKG"
fi
# shellcheck disable=SC2317 # invoked later at runtime; ShellCheck can't see call path
ini_get() { # ini_get SECTION KEY -> value
local sec="$1" key="$2"
awk -v s="[""$sec""]" -v k="$key" '
$0==s {ok=1; next}
/^\[/ {ok=0}
ok && $0 ~ /^[[:alnum:]_.-]+[[:space:]]*=/ {
line=$0
sub(/[[:space:]]*=[[:space:]]*/, "=", line)
split(line,a,"=")
if (a[1]==k) {
val=substr(line, index(line,"=")+1)
gsub(/^[[:space:]]+|[[:space:]]+$/,"",val)
print val
exit
}
}' "${CONFIG_FILE}" 2>/dev/null || true
}
# shellcheck disable=SC2317
profile_expand_remote() { # "@name" -> user@host, otherwise pass through
local in="$1"
if [[ "$in" == @* ]]; then
local name="${in#@}" user host
user="$(ini_get "$name" user)"
host="$(ini_get "$name" host)"
if [[ -n "$user" && -n "$host" ]]; then
printf '%s@%s\n' "$user" "$host"
return 0
fi
fi
printf '%s\n' "$in"
}
# shellcheck disable=SC2317
profile_apply_defaults() { # set globals if unset; named overrides default
local name="$1" v
# defaults
v="$(ini_get default tunnel_port)"; [[ -n "$v" && "${TUNNEL_PORT}" == "2222" ]] && TUNNEL_PORT="$v"
v="$(ini_get default local_ssh_port)"; [[ -n "$v" && "${LOCAL_SSH_PORT}" == "22" ]] && LOCAL_SSH_PORT="$v"
v="$(ini_get default invite_mount)"; [[ -n "$v" && "${INVITE_MOUNT}" == "/mnt/remote-rssh" ]] && INVITE_MOUNT="$v"
v="$(ini_get default invite)"; [[ "${v,,}" == "true" ]] && INVITE=true
v="$(ini_get default qr)"; [[ "${v,,}" == "true" ]] && QR=true
if [[ -z "$DURATION" ]]; then
v="$(ini_get default duration)"; [[ -n "$v" ]] && DURATION="$v"
fi
# named section
if [[ -n "$name" ]]; then
v="$(ini_get "$name" tunnel_port)"; [[ -n "$v" ]] && TUNNEL_PORT="$v"
v="$(ini_get "$name" local_ssh_port)"; [[ -n "$v" ]] && LOCAL_SSH_PORT="$v"
v="$(ini_get "$name" invite_mount)"; [[ -n "$v" ]] && INVITE_MOUNT="$v"
fi
}
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
@@ -61,6 +130,15 @@ shift 5 || true
[[ "$KW1" != "with" ]] && usage
[[ "$KW2" != "for" ]] && usage
# Apply [default] + named profile (if REMOTE is @name) before flag overrides
profile_name=""
if [[ "$REMOTE" == @* ]]; then
profile_name="${REMOTE#@}"
fi
profile_apply_defaults "$profile_name"
# Expand @name -> user@host for actual ssh use
REMOTE="$(profile_expand_remote "$REMOTE")"
# --- optional flags ---
while [[ $# -gt 0 ]]; do
case "$1" in