6 Commits

2 changed files with 110 additions and 21 deletions

View File

@@ -13,6 +13,11 @@ apost_beacon "sys.online"
# update dns
if [[ "$DFS_DDNS_ENABLE" == "1" ]]; then
fmt_info "updating dns ..."
if ! is_tty; then
time_to_sleep=$((RANDOM%600))
fmt_note "sleep for $time_to_sleep seconds"
sleep $time_to_sleep
fi
"$THIS_DIR/tools/frigg-client.sh" ddns || (fmt_error "failed to update dns" && apost_beacon "dfs.ddns.fail")
fi

View File

@@ -2,75 +2,150 @@
# connect to iot services
THIS_DIR=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd )
source "$THIS_DIR/../tools/common.sh"
RIOT_TRUST_CLIENT=${RIOT_TRUST_CLIENT:-${DFS_TRUST:-0}}
RIOT_TRUST_SERVER=${RIOT_TRUST_SERVER:-0}
# get target settings
# provides:
SERVER=""
PORT=""
SSH_USERNAME=""
PORT="" # optional
USERNAME="" # optional
SSH_OPTIONS=""
if [[ "$RIOT_TRUST_CLIENT" == "1" ]]; then
SSH_OPTIONS='-o ControlMaster=auto -o ControlPath=/tmp/sshcm-%C -o PermitLocalCommand=yes'
fi
get_server_meta()
{
local domain=${1##*.}
local host=${1%.*}
local trust_server="$RIOT_TRUST_SERVER"
local arg="$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
domain="ibd"
fi
if [[ "$host" == "$domain" ]]; then
domain=""
domain="proxied"
fi
case $domain in
ibd|ebd )
SERVER=$host.$domain.ink
PORT=12022
SSH_USERNAME=root
PORT=${PORT:-12022}
USERNAME=${USERNAME:-root}
trust_server=1
;;
nasp )
SERVER=$host
PORT=12022
SSH_USERNAME=dictxiong
SSH_OPTIONS='-o ProxyJump="ssh@nasp.ob.ac.cn:36022"'
PORT=${PORT:-12022}
USERNAME=${USERNAME:-dictxiong}
SSH_OPTIONS="$SSH_OPTIONS -o ProxyJump=ssh@nasp.ob.ac.cn:36022"
trust_server=1
;;
"" )
proxied )
SERVER=proxy.beardic.cn
local tmp=$(sha256sum <<< "$host" | tr -cd "[:digit:]")
tmp=${tmp:0:4}
PORT=$((10#$tmp+36000))
SSH_USERNAME=root
USERNAME=root
trust_server=1
;;
* )
fmt_fatal "unknown domain: $domain"
fmt_warning "unknown domain: $domain. will try as server name"
SERVER="$arg"
esac
if [[ "$trust_server" == "1" ]]; then
SSH_OPTIONS="$SSH_OPTIONS -o ForwardX11=yes -o ForwardAgent=yes"
fi
}
eval_or_echo() {
if [[ "$DFS_DRY_RUN" == "1" ]]; then
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'
else
local port_param='-p'
fi
echo "$ssh_bin ${PORT:+$port_param} $PORT $SSH_OPTIONS $SCP_SRC $USERNAME${USERNAME:+@}$SERVER $SCP_DST"
}
run_ssh()
{
CMD="ssh -p $PORT $SSH_OPTIONS $SSH_USERNAME@$SERVER"
fmt_note "-->" $CMD
eval $CMD
local cmd="$(prepare_ssh_cmd $1)"
fmt_note "-->" $cmd
eval_or_echo $cmd
}
# sshl
run_sshl()
{
if [[ -z "$1" || "$1" != *":"* ]]; then
if [[ -z "$1" ]]; then
fmt_fatal "invalid remote address: $1"
fi
arg="$1"
if [[ "$arg" != *":"* ]]; then
# treat as a port number
arg=localhost:$arg
fi
while
local port=$(shuf -n 1 -i 49152-65535)
netstat -atun | grep -q "$port"
do
continue
done
CMD="ssh -p $PORT $SSH_OPTIONS -NC -L $port:$1 $SSH_USERNAME@$SERVER"
fmt_note "-->" $CMD
SSH_OPTIONS="$SSH_OPTIONS -NC -L $port:$arg"
local cmd="$(prepare_ssh_cmd ssh)"
fmt_note "-->" $cmd
fmt_note " > please access localhost:$port"
eval $CMD
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"\"
else
SERVER="$SERVER":\""$src"\"
SCP_DST=\""$dst"\"
fi
SSH_OPTIONS="$SSH_OPTIONS -r"
local cmd="$(prepare_ssh_cmd scp)"
fmt_note "-->" $cmd
eval_or_echo $cmd
}
# main
print_help()
{
fmt_info "usage: $0 <service> [command] [options]"
echo "available commands: ssh (default), sshl (ssh -L)"
echo "available commands: ssh (default), sshl (ssh -L), zssh, sftp"
}
router()
@@ -88,9 +163,18 @@ router()
ssh|"" )
run_ssh
;;
zssh )
run_ssh zssh
;;
sftp )
run_ssh sftp
;;
sshl )
run_sshl "$3"
;;
scp )
run_scp "$3" "$4"
;;
* )
print_help
fmt_fatal "unknown command: $2"