2023-01-06 16:14:41 +08:00
|
|
|
#!/bin/bash
|
|
|
|
# connect to iot services
|
|
|
|
THIS_DIR=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd )
|
|
|
|
source "$THIS_DIR/../tools/common.sh"
|
2023-05-22 19:05:24 +08:00
|
|
|
RIOT_TRUST_CLIENT=${RIOT_TRUST_CLIENT:-${DFS_TRUST:-0}}
|
|
|
|
RIOT_TRUST_SERVER=${RIOT_TRUST_SERVER:-0}
|
2023-01-06 16:14:41 +08:00
|
|
|
|
2023-05-24 13:59:57 +08:00
|
|
|
# get single server setting
|
|
|
|
# may be called more than once
|
|
|
|
get_server_meta() {
|
|
|
|
# returns:
|
|
|
|
RET_HOSTNAME=""
|
|
|
|
RET_TRUST_SERVER=0
|
|
|
|
RET_PORT="" # optional
|
|
|
|
RET_USERNAME="" # optional
|
|
|
|
RET_JUMP_SERVER="" # optional
|
|
|
|
# body
|
|
|
|
local remote="$1"
|
|
|
|
# if in the form user@...
|
|
|
|
if [[ "$remote" == *@* ]]; then
|
|
|
|
RET_USERNAME=${remote%%@*}
|
|
|
|
remote=${remote#*@}
|
2023-05-09 14:59:15 +08:00
|
|
|
fi
|
2023-05-24 13:59:57 +08:00
|
|
|
# if in the form ...:22
|
|
|
|
if [[ "$remote" == *:* ]]; then
|
|
|
|
RET_PORT=${remote##*:}
|
|
|
|
remote=${remote%:*}
|
2023-05-09 14:59:15 +08:00
|
|
|
fi
|
2023-05-24 13:59:57 +08:00
|
|
|
# presets -- match domain
|
|
|
|
local domain=${remote##*.}
|
|
|
|
local host=${remote%.*}
|
|
|
|
# if ends with dot
|
2023-05-09 12:47:52 +08:00
|
|
|
if [[ -z "$domain" ]]; then
|
|
|
|
domain="ibd"
|
|
|
|
fi
|
2023-05-24 13:59:57 +08:00
|
|
|
# if there's no dot
|
2023-01-06 16:14:41 +08:00
|
|
|
if [[ "$host" == "$domain" ]]; then
|
2023-05-24 13:59:57 +08:00
|
|
|
domain="ibd"
|
2023-01-06 16:14:41 +08:00
|
|
|
fi
|
|
|
|
case $domain in
|
2023-04-21 12:37:05 +08:00
|
|
|
ibd|ebd )
|
2023-05-24 13:59:57 +08:00
|
|
|
RET_HOSTNAME=$host.$domain.ink
|
|
|
|
RET_PORT=${RET_PORT:-12022}
|
|
|
|
RET_USERNAME=${RET_USERNAME:-root}
|
|
|
|
RET_TRUST_SERVER=1
|
2023-01-06 16:14:41 +08:00
|
|
|
;;
|
|
|
|
nasp )
|
2023-05-24 13:59:57 +08:00
|
|
|
RET_HOSTNAME=$host
|
|
|
|
RET_PORT=${RET_PORT:-12022}
|
|
|
|
RET_USERNAME=${RET_USERNAME:-dictxiong}
|
|
|
|
RET_JUMP_SERVER="ssh@nasp.ob.ac.cn:36022"
|
|
|
|
RET_TRUST_SERVER=1
|
2023-01-06 16:14:41 +08:00
|
|
|
;;
|
2023-05-09 12:47:52 +08:00
|
|
|
proxied )
|
2023-05-24 13:59:57 +08:00
|
|
|
RET_HOSTNAME=proxy.beardic.cn
|
2023-01-06 16:14:41 +08:00
|
|
|
local tmp=$(sha256sum <<< "$host" | tr -cd "[:digit:]")
|
|
|
|
tmp=${tmp:0:4}
|
2023-05-24 13:59:57 +08:00
|
|
|
RET_PORT=$((10#$tmp+36000))
|
|
|
|
RET_USERNAME=root
|
|
|
|
RET_TRUST_SERVER=1
|
2023-01-06 16:14:41 +08:00
|
|
|
;;
|
|
|
|
* )
|
2023-05-24 13:59:57 +08:00
|
|
|
fmt_warning "unknown domain: $domain. will try as host name"
|
|
|
|
RET_HOSTNAME="$remote"
|
2023-01-06 16:14:41 +08:00
|
|
|
esac
|
2023-05-24 13:59:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
# remote setting, including jump servers
|
|
|
|
# will be called only once
|
|
|
|
# provides:
|
|
|
|
SERVER=""
|
|
|
|
TRUST_SERVER=1
|
|
|
|
PORT="" # optional
|
|
|
|
USERNAME="" # optional
|
|
|
|
SSH_OPTIONS="" # optional
|
|
|
|
if [[ "$RIOT_TRUST_CLIENT" == "1" ]]; then
|
|
|
|
SSH_OPTIONS='-o ControlMaster=auto -o ControlPath=/tmp/sshcm-%C -o PermitLocalCommand=yes'
|
|
|
|
fi
|
|
|
|
parse_remote() {
|
|
|
|
local remote="$1"
|
|
|
|
local jump_servers=""
|
|
|
|
# loop for jump servers
|
|
|
|
while [[ -n $remote ]]; do
|
|
|
|
local server=${remote%%,*}
|
|
|
|
remote=${remote#*,}
|
|
|
|
get_server_meta "$server"
|
|
|
|
if [[ -n "$RET_JUMP_SERVER" ]]; then
|
|
|
|
jump_servers="$jump_servers${jump_servers:+,}$RET_JUMP_SERVER"
|
|
|
|
fi
|
|
|
|
# only if all servers are trusted
|
|
|
|
TRUST_SERVER=$((TRUST_SERVER*RET_TRUST_SERVER))
|
|
|
|
if [[ "$server" == "$remote" || -z "$remote" ]]; then
|
|
|
|
SERVER="$RET_HOSTNAME"
|
|
|
|
PORT="$RET_PORT"
|
|
|
|
USERNAME="$RET_USERNAME"
|
|
|
|
remote=""
|
|
|
|
else
|
|
|
|
jump_servers="$jump_servers${jump_servers:+,}$RET_USERNAME${RET_USERNAME:+@}$RET_HOSTNAME${RET_PORT:+:}$RET_PORT"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
# construct cmd
|
|
|
|
if [[ "$RIOT_TRUST_SERVER" == "1" || "$TRUST_SERVER" == "1" ]]; then
|
2023-05-23 20:32:43 +08:00
|
|
|
SSH_OPTIONS="$SSH_OPTIONS -o ForwardX11=yes -o ForwardAgent=yes"
|
2023-05-22 19:05:24 +08:00
|
|
|
fi
|
2023-05-24 13:59:57 +08:00
|
|
|
if [[ -n "$jump_servers" ]]; then
|
|
|
|
SSH_OPTIONS="$SSH_OPTIONS -o ProxyJump=$jump_servers"
|
|
|
|
fi
|
2023-01-06 16:14:41 +08:00
|
|
|
}
|
|
|
|
|
2023-05-23 20:32:43 +08:00
|
|
|
eval_or_echo() {
|
2023-05-09 14:59:15 +08:00
|
|
|
if [[ "$DFS_DRY_RUN" == "1" ]]; then
|
2023-05-23 20:32:43 +08:00
|
|
|
echo $@
|
|
|
|
else
|
|
|
|
eval $@
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# ssh
|
|
|
|
prepare_ssh_cmd() {
|
|
|
|
local ssh_bin="${1:-ssh}"
|
|
|
|
if [[ "$ssh_bin" == "scp" || "$ssh_bin" == "sftp" ]]; then
|
|
|
|
local port_param='-P'
|
2023-05-09 14:59:15 +08:00
|
|
|
else
|
2023-05-23 20:32:43 +08:00
|
|
|
local port_param='-p'
|
2023-05-09 14:59:15 +08:00
|
|
|
fi
|
2023-05-23 20:32:43 +08:00
|
|
|
echo "$ssh_bin ${PORT:+$port_param} $PORT $SSH_OPTIONS $SCP_SRC $USERNAME${USERNAME:+@}$SERVER $SCP_DST"
|
|
|
|
}
|
|
|
|
|
|
|
|
run_ssh()
|
|
|
|
{
|
|
|
|
local cmd="$(prepare_ssh_cmd $1)"
|
|
|
|
fmt_note "-->" $cmd
|
|
|
|
eval_or_echo $cmd
|
2023-04-21 12:37:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
# sshl
|
|
|
|
run_sshl()
|
|
|
|
{
|
2023-05-24 13:59:57 +08:00
|
|
|
local arg="$1"
|
2023-05-09 14:59:15 +08:00
|
|
|
if [[ "$arg" != *":"* ]]; then
|
|
|
|
# treat as a port number
|
|
|
|
arg=localhost:$arg
|
|
|
|
fi
|
2023-04-21 12:37:05 +08:00
|
|
|
while
|
|
|
|
local port=$(shuf -n 1 -i 49152-65535)
|
|
|
|
netstat -atun | grep -q "$port"
|
|
|
|
do
|
|
|
|
continue
|
|
|
|
done
|
2023-05-23 20:32:43 +08:00
|
|
|
|
|
|
|
SSH_OPTIONS="$SSH_OPTIONS -NC -L $port:$arg"
|
|
|
|
local cmd="$(prepare_ssh_cmd ssh)"
|
|
|
|
fmt_note "-->" $cmd
|
2023-04-21 12:37:05 +08:00
|
|
|
fmt_note " > please access localhost:$port"
|
2023-05-23 20:32:43 +08:00
|
|
|
eval_or_echo $cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
# scp
|
|
|
|
run_scp() {
|
|
|
|
local src="$1"
|
|
|
|
local dst="$2"
|
|
|
|
local dst_is_remote=1
|
|
|
|
# whoever is ./*, it can't be the remote; whoever not exists on local, it's possible the remote.
|
|
|
|
# it is suggested to use ./* for local files.
|
|
|
|
if [[ "$src" != "./"* && ( "$dst" == "./"* || ( ! -e "$src" && -e "$dst" ) ) ]]; then
|
|
|
|
dst_is_remote=0
|
|
|
|
fi
|
|
|
|
if [[ "$dst_is_remote" == "1" ]]; then
|
|
|
|
SCP_SRC=\""$src"\"
|
|
|
|
SERVER="$SERVER":\""$dst"\"
|
2023-05-09 14:59:15 +08:00
|
|
|
else
|
2023-05-23 20:32:43 +08:00
|
|
|
SERVER="$SERVER":\""$src"\"
|
|
|
|
SCP_DST=\""$dst"\"
|
2023-05-09 14:59:15 +08:00
|
|
|
fi
|
2023-05-23 20:32:43 +08:00
|
|
|
SSH_OPTIONS="$SSH_OPTIONS -r"
|
|
|
|
local cmd="$(prepare_ssh_cmd scp)"
|
|
|
|
fmt_note "-->" $cmd
|
|
|
|
eval_or_echo $cmd
|
2023-01-06 16:14:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
# main
|
|
|
|
print_help()
|
|
|
|
{
|
2023-04-21 12:37:05 +08:00
|
|
|
fmt_info "usage: $0 <service> [command] [options]"
|
2023-05-23 20:32:43 +08:00
|
|
|
echo "available commands: ssh (default), sshl (ssh -L), zssh, sftp"
|
2023-01-06 16:14:41 +08:00
|
|
|
}
|
|
|
|
|
2023-05-24 13:59:57 +08:00
|
|
|
router() {
|
2023-04-21 12:37:05 +08:00
|
|
|
if [[ -z "$1" || "$1" == "-h" || "$1" == "--help" ]]; then
|
|
|
|
print_help
|
|
|
|
exit
|
|
|
|
fi
|
2023-05-24 13:59:57 +08:00
|
|
|
parse_remote "$1"
|
2023-04-21 12:37:05 +08:00
|
|
|
case $2 in
|
2023-01-06 16:14:41 +08:00
|
|
|
-h|--help)
|
|
|
|
print_help
|
2023-04-21 12:37:05 +08:00
|
|
|
exit
|
|
|
|
;;
|
|
|
|
ssh|"" )
|
|
|
|
run_ssh
|
2023-01-06 16:14:41 +08:00
|
|
|
;;
|
2023-05-22 19:05:24 +08:00
|
|
|
zssh )
|
2023-05-23 20:32:43 +08:00
|
|
|
run_ssh zssh
|
2023-05-22 19:05:24 +08:00
|
|
|
;;
|
|
|
|
sftp )
|
2023-05-23 20:32:43 +08:00
|
|
|
run_ssh sftp
|
2023-05-22 19:05:24 +08:00
|
|
|
;;
|
2023-04-21 12:37:05 +08:00
|
|
|
sshl )
|
2023-05-24 13:59:57 +08:00
|
|
|
test -n "$3" || fmt_fatal "no target address provided"
|
2023-04-21 12:37:05 +08:00
|
|
|
run_sshl "$3"
|
2023-01-06 16:14:41 +08:00
|
|
|
;;
|
2023-05-23 20:32:43 +08:00
|
|
|
scp )
|
2023-05-24 13:59:57 +08:00
|
|
|
test -n "$3" || fmt_fatal "no source path specified"
|
|
|
|
test -n "$4" || fmt_fatal "no destination path specified"
|
2023-05-23 20:32:43 +08:00
|
|
|
run_scp "$3" "$4"
|
|
|
|
;;
|
2023-01-06 16:14:41 +08:00
|
|
|
* )
|
|
|
|
print_help
|
2023-04-21 12:37:05 +08:00
|
|
|
fmt_fatal "unknown command: $2"
|
2023-01-06 16:14:41 +08:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
|
|
|
router "${GOT_OPTS[@]}"
|