mirror of
https://github.com/DictXiong/dotfiles.git
synced 2024-11-24 06:07:02 +08:00
Dict Xiong
92aa5fa920
* riot: remove unknown domain warn * install.sh: --no-ssh -> --no-auth-info * doll: --restart=unless-stopped * zshrc: alias cps and mvs * riot: proxy -> ssh * zshrc: not alias rm to trash * (trial) riot devel: separate preset to config dir riot.d * riot: support extra options and extra -o options * (experimental) riot config in a single file * riot config: add nasp remote and null domain * gitconf: pull.ff = only * fix ci * riot: dynamic port forwarding * riot: only one domain func will be exec * to-install: update lemonbench * to-install: alist * [exp] riot inferred ssh: ping ping6 * sagt: fix nixos * riot config: domain 42 * ubuntu.sh: DEBIAN_FRONTEND=noninteractive * zshrc: ping -n * zshrc: alias ping -n * riot-config: jumpserver from sir0 to ssh.beardic.cn * zshrc: alias ping before checking os type * frigg: support api4.beardic.cn * fix(install.sh): crontab fails on a new server * fix(riot-conf): nasp.ob.ac.cn -> nasp.fit * fix(install.sh): install crontab (exp) * feat(test.zsh): test crontab * fix(riot): secure control master * fix(ci): riot control master * fix(riot): not mkdir if dry-run * feat(vimrc): set shiftwidth=4 * feat(ci): sync tmux-yank * feat(tmux): set-clipboard on and mouse on (experimental) * feat(zshrc): alias ping6 * build(ci): hub mirror 1.3->1.4 * fix(zshrc): tmux on msys; feat(common): better perf getting os type and linux dist * fix(common.sh): get_os_type and get_linux_dist * feat(zshrc): add plugin {magic-enter,per-directory-history,pip,podman,python,rsync,systemd,timer} * feat(zshrc): journalctl alias * feat(vimrc): tab=2 for c,cpp,nix,yaml * build(ci): checkout v3 -> v4 --------- Co-authored-by: xiongdian.me <xiongdian.me@bytedance.com>
266 lines
6.9 KiB
Bash
Executable File
266 lines
6.9 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# 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}
|
|
RIOT_EXTRA_OPTIONS=""
|
|
|
|
# config
|
|
RIOT_CONFIG_FILES=(
|
|
"$DOTFILES/riot-config.sh"
|
|
"$HOME/.config/riot-config.sh"
|
|
"riot-config.sh"
|
|
)
|
|
for file in "${RIOT_CONFIG_FILES[@]}"; do
|
|
if [[ -f "$file" ]]; then
|
|
source "$file"
|
|
fi
|
|
done
|
|
|
|
# check if port number valid
|
|
check_port() {
|
|
( echo $1 | grep -qxE "[1-9][0-9]{0,4}" ) || return 1
|
|
test $1 -lt 65536 -a $1 -gt 0 || return 1
|
|
return 0
|
|
}
|
|
|
|
# check if username valid
|
|
check_username() {
|
|
( echo $1 | grep -qxE "^[a-z][-a-z0-9_]*\$" ) || return 1
|
|
return 0
|
|
}
|
|
|
|
# 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#*@}
|
|
check_username $RET_USERNAME || fmt_warning \"$RET_USERNAME\" is not a valid unix username
|
|
fi
|
|
# if in the form ...:22
|
|
if [[ "$remote" == "["*"]":* || ( "$remote" != "["*"]" && "$remote" == *:* ) ]]; then
|
|
RET_PORT=${remote##*:}
|
|
remote=${remote%:*}
|
|
check_port $RET_PORT || fmt_fatal invalid port number \"$RET_PORT\"
|
|
fi
|
|
# presets -- match remote
|
|
local remote_func="$remote.remote"
|
|
if is_function "$remote_func"; then
|
|
"$remote_func"
|
|
fi
|
|
# presets -- match domain
|
|
RET_HOSTNAME=${remote}
|
|
local domain=${remote##*.}
|
|
local host=${remote%.*}
|
|
# if there's no dot
|
|
if [[ "$host" == "$domain" && "$host" != "["*"]" ]]; then
|
|
domain="default"
|
|
fi
|
|
local domain_func="$domain.domain"
|
|
if is_function "$domain_func"; then
|
|
"$domain_func"
|
|
elif is_function ".domain"; then
|
|
".domain"
|
|
fi
|
|
}
|
|
|
|
parse_remote() {
|
|
# remote setting, including jump servers
|
|
# called for every remote
|
|
# provides:
|
|
SERVER=""
|
|
TRUST_SERVER=1
|
|
PORT="" # optional
|
|
USERNAME="" # optional
|
|
SSH_OPTIONS="" # optional
|
|
if [[ "$RIOT_TRUST_CLIENT" == "1" ]]; then
|
|
SSH_OPTIONS='-o PermitLocalCommand=yes'
|
|
if [[ "$(get_os_type)" != "msys" ]]; then
|
|
test "$DFS_DRY_RUN" = "1" || mkdir -p ~/.ssh/master-socket
|
|
SSH_OPTIONS="$SSH_OPTIONS -o ControlMaster=auto -o ControlPath=~/.ssh/master-socket/%C"
|
|
fi
|
|
fi
|
|
# handle input
|
|
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
|
|
SSH_OPTIONS="$SSH_OPTIONS -o ForwardX11=yes -o ForwardAgent=yes"
|
|
fi
|
|
if [[ -n "$jump_servers" ]]; then
|
|
SSH_OPTIONS="$SSH_OPTIONS -o ProxyJump=$jump_servers"
|
|
fi
|
|
}
|
|
|
|
eval_or_echo() {
|
|
if [[ "$DFS_DRY_RUN" == "1" ]]; then
|
|
echo $@
|
|
else
|
|
eval $@
|
|
fi
|
|
}
|
|
|
|
# ssh series
|
|
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 $RIOT_EXTRA_OPTIONS $SCP_SRC $USERNAME${USERNAME:+@}$SERVER $SCP_DST ${@:2}"
|
|
}
|
|
|
|
# ssh
|
|
run_ssh()
|
|
{
|
|
local cmd="$(prepare_ssh_cmd $@)"
|
|
fmt_note "-->" $cmd
|
|
eval_or_echo $cmd
|
|
}
|
|
|
|
# sshl
|
|
run_sshl()
|
|
{
|
|
local arg="$1"
|
|
if [[ "$arg" != *":"* ]]; then
|
|
# treat as a port number
|
|
arg=localhost:$arg
|
|
fi
|
|
local port=$(get_free_port)
|
|
|
|
SSH_OPTIONS="$SSH_OPTIONS -NC -L $port:$arg"
|
|
local cmd="$(prepare_ssh_cmd ssh)"
|
|
fmt_note "-->" $cmd
|
|
fmt_note " > please access localhost:$port"
|
|
eval_or_echo $cmd
|
|
}
|
|
|
|
# sshd
|
|
run_sshd()
|
|
{
|
|
local port=$(get_free_port)
|
|
|
|
SSH_OPTIONS="$SSH_OPTIONS -NC -D $port"
|
|
local cmd="$(prepare_ssh_cmd ssh)"
|
|
fmt_note "-->" $cmd
|
|
fmt_note " > please access localhost:$port"
|
|
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), zssh, sftp"
|
|
}
|
|
|
|
router() {
|
|
if [[ -z "$1" || "$1" == "-h" || "$1" == "--help" ]]; then
|
|
print_help
|
|
exit
|
|
fi
|
|
|
|
while [[ "$1" == -* ]]; do
|
|
RIOT_EXTRA_OPTIONS="$RIOT_EXTRA_OPTIONS $1"
|
|
if [[ "$1" == "-o" ]]; then
|
|
RIOT_EXTRA_OPTIONS="$RIOT_EXTRA_OPTIONS $2"
|
|
shift
|
|
fi
|
|
shift
|
|
done
|
|
|
|
IFS=',' read -ra remotes <<< "$1"
|
|
for remote in "${remotes[@]}"; do
|
|
if [[ -z "$remote" ]]; then
|
|
continue
|
|
fi
|
|
parse_remote "$remote"
|
|
case $2 in
|
|
ssh|"" )
|
|
run_ssh ssh "${@:3}"
|
|
;;
|
|
ping|ping6 )
|
|
run_ssh ssh "${@:2}"
|
|
;;
|
|
zssh )
|
|
run_ssh zssh
|
|
;;
|
|
sftp )
|
|
run_ssh sftp
|
|
;;
|
|
sshl )
|
|
test -n "$3" || fmt_fatal "no target address provided"
|
|
run_sshl "$3"
|
|
;;
|
|
sshd )
|
|
run_sshd
|
|
;;
|
|
scp )
|
|
test -n "$3" || fmt_fatal "no source path specified"
|
|
test -n "$4" || fmt_fatal "no destination path specified"
|
|
run_scp "$3" "$4"
|
|
;;
|
|
* )
|
|
print_help
|
|
fmt_fatal "unknown command: $2"
|
|
;;
|
|
esac
|
|
done
|
|
}
|
|
|
|
router "${GOT_OPTS[@]}"
|