riot: 'user@..' to overwrite username, '...:22' to overwrite port, treat the arg as server name if it doesn't match any preset, sshl can accept only a port number and default to use localhost

This commit is contained in:
xiongdian.me 2023-05-09 14:59:15 +08:00
parent d6d3167de3
commit 8eb2088331

View File

@ -6,13 +6,24 @@ source "$THIS_DIR/../tools/common.sh"
# get target settings # get target settings
# provides: # provides:
SERVER="" SERVER=""
PORT="" PORT="" # optional
SSH_USERNAME="" USERNAME="" # optional
SSH_OPTIONS="" SSH_OPTIONS="" # optional
get_server_meta() get_server_meta()
{ {
local domain=${1##*.} arg="$1"
local host=${1%.*} # overwrite
if [[ "$arg" == *@* ]]; then
USERNAME=${arg%%@*}
arg=${arg#*@}
fi
if [[ "$arg" == *:* ]]; then
PORT=${arg##*:}
arg=${arg%:*}
fi
# presets
local domain=${arg##*.}
local host=${arg%.*}
if [[ -z "$domain" ]]; then if [[ -z "$domain" ]]; then
domain="ibd" domain="ibd"
fi fi
@ -22,13 +33,13 @@ get_server_meta()
case $domain in case $domain in
ibd|ebd ) ibd|ebd )
SERVER=$host.$domain.ink SERVER=$host.$domain.ink
PORT=12022 PORT=${PORT:-12022}
SSH_USERNAME=root USERNAME=${USERNAME:-root}
;; ;;
nasp ) nasp )
SERVER=$host SERVER=$host
PORT=12022 PORT=${PORT:-12022}
SSH_USERNAME=dictxiong USERNAME=${USERNAME:-dictxiong}
SSH_OPTIONS='-o ProxyJump="ssh@nasp.ob.ac.cn:36022"' SSH_OPTIONS='-o ProxyJump="ssh@nasp.ob.ac.cn:36022"'
;; ;;
proxied ) proxied )
@ -36,37 +47,51 @@ get_server_meta()
local tmp=$(sha256sum <<< "$host" | tr -cd "[:digit:]") local tmp=$(sha256sum <<< "$host" | tr -cd "[:digit:]")
tmp=${tmp:0:4} tmp=${tmp:0:4}
PORT=$((10#$tmp+36000)) PORT=$((10#$tmp+36000))
SSH_USERNAME=root USERNAME=root
;; ;;
* ) * )
fmt_fatal "unknown domain: $domain" fmt_warning "unknown domain: $domain. will try as server name"
SERVER="$arg"
esac esac
} }
# ssh # ssh
run_ssh() run_ssh()
{ {
CMD="ssh -p $PORT $SSH_OPTIONS $SSH_USERNAME@$SERVER" CMD="ssh ${PORT:+-p} $PORT $SSH_OPTIONS $USERNAME${USERNAME:+@}$SERVER"
fmt_note "-->" $CMD fmt_note "-->" $CMD
eval $CMD if [[ "$DFS_DRY_RUN" == "1" ]]; then
echo $CMD
else
eval $CMD
fi
} }
# sshl # sshl
run_sshl() run_sshl()
{ {
if [[ -z "$1" || "$1" != *":"* ]]; then if [[ -z "$1" ]]; then
fmt_fatal "invalid remote address: $1" fmt_fatal "invalid remote address: $1"
fi fi
arg="$1"
if [[ "$arg" != *":"* ]]; then
# treat as a port number
arg=localhost:$arg
fi
while while
local port=$(shuf -n 1 -i 49152-65535) local port=$(shuf -n 1 -i 49152-65535)
netstat -atun | grep -q "$port" netstat -atun | grep -q "$port"
do do
continue continue
done done
CMD="ssh -p $PORT $SSH_OPTIONS -NC -L $port:$1 $SSH_USERNAME@$SERVER" CMD="ssh ${PORT:+-p} $PORT $SSH_OPTIONS -NC -L $port:$arg $USERNAME${USERNAME:+@}$SERVER"
fmt_note "-->" $CMD fmt_note "-->" $CMD
fmt_note " > please access localhost:$port" fmt_note " > please access localhost:$port"
eval $CMD if [[ "$DFS_DRY_RUN" == "1" ]]; then
echo $CMD
else
eval $CMD
fi
} }
# main # main