From c86cc85e2403f5b92d8bad768430e8fe53a818e4 Mon Sep 17 00:00:00 2001 From: Dict Xiong Date: Fri, 19 Dec 2025 21:21:22 +0800 Subject: [PATCH] feat(riot): sshr for ssh -R --- scripts/riot | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/scripts/riot b/scripts/riot index c33dcfa..5745e95 100755 --- a/scripts/riot +++ b/scripts/riot @@ -195,20 +195,26 @@ run_ssh() # sshl run_sshl() { - is_port() { - if [[ "$1" =~ ^[1-9][0-9]*$ && $1 -gt 1 && $1 -lt 65535 ]]; then return 0; else return 1; fi - } - res="${1//[^:]}" + local arg left right localsock access + local res="${1//[^:]}" + local lorr="-L" + if [[ "${FUNCNAME[1]}" == "run_sshr" ]]; then + lorr="-R" + fi if [[ ${#res} -eq 2 ]]; then arg="$1" elif [[ ${#res} -eq 0 ]]; then - arg="$(get_free_port):localhost:$1" + if [[ "$lorr" == "-R" ]]; then + arg="$1" + else + arg="$(get_free_port):localhost:$1" + fi else left=${1%%:*} right=${1##*:} - if is_port "$left"; then + if check_port "$left"; then arg="$1" - elif is_port "$right"; then + elif check_port "$right"; then arg="$(get_free_port):$1" else arg="$1" @@ -220,13 +226,19 @@ run_sshl() else access="localhost:${arg%%:*}" fi - SSH_OPTIONS+=("-NC" "-L" "$arg") + SSH_OPTIONS+=("-NC" "$lorr" "$arg") prepare_ssh_cmd ssh print_cmd fmt_note " > please access $access" eval_or_echo } +# sshr +run_sshr() +{ + run_sshl "$1" +} + # sshd run_sshd() { @@ -389,6 +401,11 @@ router() { test "${#positional[@]}" -eq 3 || fmt_fatal "sshl requires exactly one argument" run_sshl "${positional[2]}" ;; + sshr ) + test -n "${positional[2]}" || fmt_fatal "no target address provided" + test "${#positional[@]}" -eq 3 || fmt_fatal "sshr requires exactly one argument" + run_sshr "${positional[2]}" + ;; sshd ) test "${#positional[@]}" -le 3 || fmt_fatal "sshd requires one or no arguments" if [[ "${#positional[@]}" -eq 3 ]]; then