From 891bbcacfffa0df98a17f94115470f9ebe3859b9 Mon Sep 17 00:00:00 2001 From: Dict Xiong Date: Tue, 24 Jun 2025 19:56:04 +0800 Subject: [PATCH] [dev] refactor riot; `use` command; remove `log`; `sagt op` (#45) * feat(riot-config): default port 12022 * fix(ci) * feat(riot-config): remove tailing dot from .domain; remove j.remote and x.domain * feat(riot): *.batch * feat(riot): run command in tmux window(s) note that spaces in ssh commands are still not supported * feat(riot): -o RequestTTY=yes * fix(riot): SSH_OPTIONS * fix(riot): tmux use bash * feat(zshrc): use() * fix(riot): scp, and ci note that riot still has problems with spaces. we should use array to handle parameters. * fix(ci): update macos image * feat: update email * feat: revert git email * feat(ssh): update keys * feat(sagent): sagt op * fix(sagent): error note * fix(ci): test of auto-dep * feat(ssh): remove keys ltp1-bd and ltp1 * feat(riot): rm - remove host keys * build(ci): update to ubuntu-latest and Yikun/hub-mirror-action@v1.5 * feat: remove frigg-client.log * feat(riot): -t or --trust to set RIOT_TRUST_SERVER * feat(zshrc/alias): add sc and t, remove cps and mvs feat(zshrc/plugins): add man and web-search, remove ufw * feat(riot): refactor argparse feat(riot): add `--password` and `--` feat(common.sh): argparse supports `--` feat(riot): refactor ping to ping remote * feat(riot): sshd can specify the local port * feat(riot): print help when no argument or no remote * fix(riot): ci * feat(riot): better print_cmd with escape * feat(riot-config): support sed* * fix(frigg): hostname converted into lower case --- .github/workflows/gitee_sync.yml | 4 +- .github/workflows/gitee_sync_dependencies.yml | 32 +-- .github/workflows/test.yml | 4 +- .ssh/authorized_keys2 | 18 +- .update.sh | 3 +- .zshrc2 | 11 +- riot-config.sh | 52 ++-- scripts/riot | 236 +++++++++++++----- tools/common.sh | 18 +- tools/frigg-client.sh | 21 +- tools/sagent-op.sh | 9 + tools/sagent.sh | 9 + tools/test.zsh | 6 +- tools/to-install.sh | 2 +- 14 files changed, 265 insertions(+), 160 deletions(-) create mode 100755 tools/sagent-op.sh diff --git a/.github/workflows/gitee_sync.yml b/.github/workflows/gitee_sync.yml index 8d45a70..6ed909d 100644 --- a/.github/workflows/gitee_sync.yml +++ b/.github/workflows/gitee_sync.yml @@ -4,10 +4,10 @@ on: workflow_dispatch: ~ jobs: dotfiles: - runs-on: ubuntu-20.04 + runs-on: ubuntu-latest steps: - name: DictXiong/dotfiles - uses: Yikun/hub-mirror-action@v1.4 + uses: Yikun/hub-mirror-action@v1.5 with: src: github/DictXiong dst: gitee/dictxiong diff --git a/.github/workflows/gitee_sync_dependencies.yml b/.github/workflows/gitee_sync_dependencies.yml index 8c47530..ea7ef80 100644 --- a/.github/workflows/gitee_sync_dependencies.yml +++ b/.github/workflows/gitee_sync_dependencies.yml @@ -5,10 +5,10 @@ on: workflow_dispatch: ~ jobs: ohmyzsh: - runs-on: ubuntu-20.04 + runs-on: ubuntu-latest steps: - name: ohmyzsh repo - uses: Yikun/hub-mirror-action@v1.4 + uses: Yikun/hub-mirror-action@v1.5 with: src: github/ohmyzsh dst: gitee/dictxiong @@ -22,10 +22,10 @@ jobs: static_list: "ohmyzsh" timeout: '600s' zsh-users: - runs-on: ubuntu-20.04 + runs-on: ubuntu-latest steps: - name: zsh-users repos - uses: Yikun/hub-mirror-action@v1.4 + uses: Yikun/hub-mirror-action@v1.5 with: src: github/zsh-users dst: gitee/dictxiong @@ -39,10 +39,10 @@ jobs: static_list: "antigen,zsh-syntax-highlighting,zsh-autosuggestions,zsh-completions" timeout: '600s' zdharma-continuum: - runs-on: ubuntu-20.04 + runs-on: ubuntu-latest steps: - name: zdharma-continuum repos - uses: Yikun/hub-mirror-action@v1.4 + uses: Yikun/hub-mirror-action@v1.5 with: src: github/zdharma-continuum dst: gitee/dictxiong @@ -56,10 +56,10 @@ jobs: static_list: "fast-syntax-highlighting" timeout: '600s' tmux-mem-cpu-load: - runs-on: ubuntu-20.04 + runs-on: ubuntu-latest steps: - name: thewtex/tmux-mem-cpu-load - uses: Yikun/hub-mirror-action@v1.4 + uses: Yikun/hub-mirror-action@v1.5 with: src: github/thewtex dst: gitee/dictxiong @@ -73,10 +73,10 @@ jobs: static_list: "tmux-mem-cpu-load" timeout: '600s' tmux-plugins: - runs-on: ubuntu-20.04 + runs-on: ubuntu-latest steps: - name: tmux-plugins - uses: Yikun/hub-mirror-action@v1.4 + uses: Yikun/hub-mirror-action@v1.5 with: src: github/tmux-plugins dst: gitee/dictxiong @@ -90,10 +90,10 @@ jobs: static_list: "tpm,tmux-resurrect,tmux-yank" timeout: '600s' vundle-vim: - runs-on: ubuntu-20.04 + runs-on: ubuntu-latest steps: - name: VundleVim/Vundle-vim - uses: Yikun/hub-mirror-action@v1.4 + uses: Yikun/hub-mirror-action@v1.5 with: src: github/VundleVim dst: gitee/dictxiong @@ -107,10 +107,10 @@ jobs: static_list: "Vundle.vim" timeout: '600s' onedark-vim: - runs-on: ubuntu-20.04 + runs-on: ubuntu-latest steps: - name: joshdick/onedark-vim - uses: Yikun/hub-mirror-action@v1.4 + uses: Yikun/hub-mirror-action@v1.5 with: src: github/joshdick dst: gitee/dictxiong @@ -124,10 +124,10 @@ jobs: static_list: "onedark.vim" timeout: '600s' fzf: - runs-on: ubuntu-20.04 + runs-on: ubuntu-latest steps: - name: junegunn/fzf - uses: Yikun/hub-mirror-action@v1.4 + uses: Yikun/hub-mirror-action@v1.5 with: src: github/junegunn dst: gitee/dictxiong diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 57fd9f5..fe32f4a 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -46,7 +46,7 @@ jobs: test-macos: name: test on macos - runs-on: macos-12 + runs-on: macos-latest steps: - name: checkout repo uses: actions/checkout@v4 @@ -91,7 +91,7 @@ jobs: steps: - name: remove dependencies run: | - sudo apt-get -y remove curl vim python3 + sudo apt-get -y remove curl vim - name: checkout repo uses: actions/checkout@v4 diff --git a/.ssh/authorized_keys2 b/.ssh/authorized_keys2 index 6be10ef..c58401d 100644 --- a/.ssh/authorized_keys2 +++ b/.ssh/authorized_keys2 @@ -4,25 +4,17 @@ ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGakUcSuNhuH7tk+Nj9gDfRtxYfGPL9yb8toQ/EpSJAM ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCUN7IXF4nlFcVfgHesgik3LIAiXlVMYJPm3yD13EVarQx5jqdBgk8Dwgkgf4rPO6MFpvIpinOyEO8zOS6HHQrCLZUv5yTFaDkUuB7eQ0EmpicGbmk9bHqj1HkOZxaobkpEfQUmFKYvkp4EexVw66sO0qfXvjHZ4H6yCAJLK5aUnKfgrE8tODzP82sU/mpJjW+Pq3uanNq754gaHwhxCIXG143/zp8qzBAeKe38xVqqDq9fTkG4hvzFvkRdS88i6l1z++0P3n0HGdOjtSg7P7fO7+7ZyPYr0gO5vB720Om/zxqPrGd9cicWi4P+aVKa+0ujWH/pqufWG6uCjKWHnBs7 sk0/piv/9a -ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGV/6cLUGA5YWVI6uPhIBOBZvlNBlZLkehHQN0Gxnc+B ltp0/ubuntu - -ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILqRfdez8IAOf6m9Ysi3uSNKEG0Xi6GPuhA6R2fci2yB ltp0/windows - -ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFO4k0tJ+Bfu95Uavg/5P3EXMKNcq+bMqaTqzkvRZ7ji ltp1/ubuntu - -ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIN8g34WHLEix6Qt0J/ClYbZeb9wh+p1IOcZkz/vNbZrv ltp1/windows +ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMyZILj+GxTUhdCgz2w1TxQ+aTcggnOJIb84qA4u271S ltp0 ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIBN24pvN1RMN+iSZyHPdyExA1Rvt8pdr3e6ih6iX+KrQ ltp2 -ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGZFTyR+R86fUpZBDkDR9yYzJpmeNsIv2CMHTh6EuEeq pc0/windows +ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGZFTyR+R86fUpZBDkDR9yYzJpmeNsIv2CMHTh6EuEeq pc0 -ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIHLYgVj+NPino6sOmahULN7SbAMaVAgzqPfDjz2S8zDv pc1/windows +ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIHLYgVj+NPino6sOmahULN7SbAMaVAgzqPfDjz2S8zDv pc1 + +ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIF52mPmbLEriZ+DfmrMHFtvg2kiO2JpRpitvs7PbJowh home0 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBNvHya5i3GNniGww9Yx5ikos4Z5jevu8zz8hCQi05++SgejFPnbw9UjbHFjODK2M+ZHuhRF5mdf996r4/BxWyP8= pad0/sep ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBC5gu+U4picxVvCNYjLlmxGLpJl7svj3LklO7jSpwqdrNxlE+/xkx07PJx76AbA77dDM1Dxmm/VOvVQCKTRLTgk= ip14/sep -ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIF52mPmbLEriZ+DfmrMHFtvg2kiO2JpRpitvs7PbJowh home0 - -ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKhS4voo3K/Dvzqckr0bouO1WkCI5XxswstHWnuuyKBz ltp1-bd - diff --git a/.update.sh b/.update.sh index 1baa6c6..3ac0eef 100644 --- a/.update.sh +++ b/.update.sh @@ -39,8 +39,7 @@ case $DFS_UPDATE_CHANNEL in esac if [[ ${#DFS_COMMIT} != 40 ]]; then fmt_error "invalid commit id" - apost_beacon "dfs.invalid-commit" - apost_log "ERROR" "$THIS_FILE" "invalid commit id: ${DFS_COMMIT}" + apost_beacon "dfs.invalid-commit" "invalid commit id: ${DFS_COMMIT}" exit fi diff --git a/.zshrc2 b/.zshrc2 index 18e06df..5679b0f 100644 --- a/.zshrc2 +++ b/.zshrc2 @@ -69,6 +69,7 @@ ANTIGEN_PLUGINS+=( "fzf" "git" "magic-enter" + "man" "per-directory-history" "pip" "podman" @@ -78,7 +79,7 @@ ANTIGEN_PLUGINS+=( "systemd" "timer" "tmux" - "ufw" + "web-search" "z" ) for i in ${ANTIGEN_PLUGINS[*]}; do @@ -112,13 +113,11 @@ fi alias "pls"='sudo $(fc -ln -1)' alias "se"='sudo -sE' alias "sl"='sudo zsh -l' -alias "cps"='rsync -avh --info=progress2' -alias "mvs"='rsync -avh --info=progress2 --remove-source-files' if [[ "$DFS_OS_TYPE" == "linux" ]]; then alias "ping"='ping -n'; alias "ping6"='ping6 -n'; fi if [[ "$DFS_OS_TYPE" == "msys" ]]; then alias "tmux"='script -qO /dev/null -c "tmux -u"'; fi alias "pbd"='ping baidu.com' alias "p114"='ping 114.114.114.114' -alias "p666"='ping6 2001:da8::666' +alias "p666"='ping6 240c::6666' alias "cbd"='curl http://www.baidu.com' alias "cbds"='curl https://www.baidu.com' alias "gdebug"='git add -A; git commit --allow-empty -m "bug fix ($(date))"' @@ -129,8 +128,11 @@ alias "jce"='jc -e' alias "jceu"='jc -eu' alias "jcf"='jc -f' alias "jcfu"='jc -fu' +alias "sc"='systemctl' +alias "t"='tmux' gbes() { git for-each-ref --sort=-committerdate refs/heads refs/remotes --format="%(authordate:format:%y-%m-%d.%a %H:%M %z)|%(color:red)%(objectname:short)|%(color:yellow)%(refname:short)%(color:reset)|%(color:reset)%(authorname): %(color:green)%(subject)" --color=always | column -ts"|" | less -FX } sagt() { eval "$($DOTFILES/tools/sagent.sh $@)" } +use() { nix --experimental-features nix-command --extra-experimental-features flakes shell "${(*)@/#%(#b)([^#]#)/nixpkgs#$match}" } # key bindings bindkey "^b" beginning-of-line # ctrl+b @@ -186,7 +188,6 @@ dfs() "$DOTFILES/install.sh" -i echo 'Done. Please open a new shell to see the changes.' ;; - log ) "$DOTFILES/tools/common.sh" "post_log" "INFO" "dfs" "$2" ;; beacon ) "$DOTFILES/tools/common.sh" "post_beacon" "$2" "$3" ;; * ) echo "unknown command \"$1\". available: update, force-update, version, reset, cd, log" ;; esac diff --git a/riot-config.sh b/riot-config.sh index 23b1505..7c54e96 100644 --- a/riot-config.sh +++ b/riot-config.sh @@ -1,13 +1,29 @@ #!/bin/false -# remotes -j.remote() { - remote=ssh.beardic.cn - RET_PORT=${RET_PORT:-24022} - RET_USERNAME=${RET_USERNAME:-root} - RET_TRUST_SERVER=1 +# batches +nasps.batch() { + remotes+=( + g1.nasp + g2.nasp + g3.nasp + g4.nasp + g5.nasp + g6.nasp + g7.nasp + g8.nasp + g9.nasp + g10.nasp + g11.nasp + g12.nasp + g13.nasp + g14.nasp + dictxiong@g15.nasp + dictxiong@g16.nasp + g17.nasp + ) } +# remotes nasp.remote() { remote=nasp.fit RET_PORT=${RET_PORT:-36022} @@ -18,17 +34,16 @@ nasp.remote() { # domains .domain() { RET_USERNAME=${RET_USERNAME:-root} + RET_PORT=${RET_PORT:-12022} + RET_HOSTNAME=${remote%.} } dxng.domain() { - RET_HOSTNAME=$host.dxng.net - RET_PORT=${RET_PORT:-12022} - RET_USERNAME=${RET_USERNAME:-root} - RET_TRUST_SERVER=1 -} - -i.domain() { - RET_HOSTNAME=$host.ibd.ink + if [[ "$host" =~ ^sed([0-9]{1,2})$ ]]; then + RET_HOSTNAME=192.168.98.$((100+BASH_REMATCH[1])) + else + RET_HOSTNAME=$host.dxng.net + fi RET_PORT=${RET_PORT:-12022} RET_USERNAME=${RET_USERNAME:-root} RET_TRUST_SERVER=1 @@ -41,15 +56,6 @@ i.domain() { RET_TRUST_SERVER=1 } -x.domain() { - RET_HOSTNAME=ssh.beardic.cn - local tmp=$(sha256sum <<< "$host" | tr -cd "[:digit:]") - tmp=${tmp:0:4} - RET_PORT=$((10#$tmp+36000)) - RET_USERNAME=root - RET_TRUST_SERVER=1 -} - nasp.domain() { RET_HOSTNAME=$host RET_PORT=${RET_PORT:-12022} diff --git a/scripts/riot b/scripts/riot index db6e00a..6d029d1 100755 --- a/scripts/riot +++ b/scripts/riot @@ -4,7 +4,7 @@ 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="" +EXTRA_SSH_OPTIONS=() # config RIOT_CONFIG_FILES=( @@ -83,12 +83,12 @@ parse_remote() { TRUST_SERVER=1 PORT="" # optional USERNAME="" # optional - SSH_OPTIONS="" # optional + SSH_OPTIONS=("-o" "RequestTTY=yes") if [[ "$RIOT_TRUST_CLIENT" == "1" ]]; then - SSH_OPTIONS='-o PermitLocalCommand=yes' + 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" + SSH_OPTIONS+=("-o" "ControlMaster=auto" "-o" "ControlPath=~/.ssh/master-socket/%C") fi fi # handle input @@ -115,18 +115,41 @@ parse_remote() { done # construct cmd if [[ "$RIOT_TRUST_SERVER" == "1" || "$TRUST_SERVER" == "1" ]]; then - SSH_OPTIONS="$SSH_OPTIONS -o ForwardX11=yes -o ForwardAgent=yes" + SSH_OPTIONS+=("-o" "ForwardX11=yes" "-o" "ForwardAgent=yes") fi if [[ -n "$jump_servers" ]]; then - SSH_OPTIONS="$SSH_OPTIONS -o ProxyJump=$jump_servers" + SSH_OPTIONS+=("-o" "ProxyJump=$jump_servers") fi } +print_cmd() { + local output="" + for s in "${CMD[@]}"; do + if [[ "$s" =~ [\ \\\'\"] ]]; then + s="${s@Q}" + fi + output+="$s " + done + fmt_note "--> ${output% }" +} + eval_or_echo() { + local DO="" + local tmux_win=0 if [[ "$DFS_DRY_RUN" == "1" ]]; then - echo $@ + DO=echo + fi + if [[ "$USE_TMUX" == "1" ]]; then + if [[ -z "$TMUX_SESS" ]]; then + TMUX_SESS=riot-$(date +%s) + $DO tmux new-session -d -s $TMUX_SESS bash -l + else + tmux_win=$((tmux_win+1)) + $DO tmux new-window -t $TMUX_SESS:$tmux_win -d bash -l + fi + $DO tmux send-keys -t $TMUX_SESS:$tmux_win "${CMD[@]}" Enter else - eval $@ + $DO "${CMD[@]}" fi } @@ -138,15 +161,25 @@ prepare_ssh_cmd() { 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}" + CMD=( + "$ssh_bin" + "${PORT:+$port_param}" "$PORT" + "${SSH_OPTIONS[@]}" + "${EXTRA_SSH_OPTIONS[@]}" + "$SCP_SRC" + "$USERNAME${USERNAME:+@}$SERVER" + "$SCP_DST" + "${@:2}" + ) + for i in ${!CMD[@]}; do if [[ -z "${CMD[i]}" ]]; then unset CMD[i]; fi; done } # ssh run_ssh() { - local cmd="$(prepare_ssh_cmd $@)" - fmt_note "-->" $cmd - eval_or_echo $cmd + prepare_ssh_cmd "$@" + print_cmd + eval_or_echo } # sshl @@ -158,24 +191,22 @@ run_sshl() 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 + SSH_OPTIONS+=("-NC" "-L" "$port:$arg") + prepare_ssh_cmd ssh + print_cmd fmt_note " > please access localhost:$port" - eval_or_echo $cmd + eval_or_echo } # sshd run_sshd() { - local port=$(get_free_port) - - SSH_OPTIONS="$SSH_OPTIONS -NC -D $port" - local cmd="$(prepare_ssh_cmd ssh)" - fmt_note "-->" $cmd + local port=${1:-$(get_free_port)} + SSH_OPTIONS+=("-NC" "-D" "$port") + prepare_ssh_cmd ssh + print_cmd fmt_note " > please access localhost:$port" - eval_or_echo $cmd + eval_or_echo } # scp @@ -189,77 +220,158 @@ run_scp() { dst_is_remote=0 fi if [[ "$dst_is_remote" == "1" ]]; then - SCP_SRC=\""$src"\" - SERVER="$SERVER":\""$dst"\" + SCP_SRC="$src" + SERVER="$SERVER":"$dst" else - SERVER="$SERVER":\""$src"\" - SCP_DST=\""$dst"\" + 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 + SSH_OPTIONS+=("-r") + prepare_ssh_cmd scp + print_cmd + eval_or_echo +} + +# ping +run_ping() { + CMD=(ping) + if [[ "$1" == "ping4" ]]; then + CMD+=(-4) + elif [[ "$1" == "ping6" ]]; then + CMD+=(-6) + fi + CMD+=(-c 4 "$SERVER") + print_cmd + eval_or_echo +} + +# remove host keys +remove_hostkey() { + local key + if [[ -z "$PORT" || "$PORT" == "22" ]]; then + key=$SERVER + else + key="[$SERVER]:$PORT" + fi + ssh-keygen -R "$key" } # main print_help() { - fmt_info "usage: $0 [command] [options]" - echo "available commands: ssh (default), sshl (ssh -L), zssh, sftp" + fmt_info "usage: $0 [-Ddhlqt] [--dry-run] [--dev] [--help] [--lite] [--quite] [--trust] [--tmux] [--password] [[-o ssh-option]...] remote [command] [--] [ssh-command-args]" + cat < 0 ]]; do + case "$1" in + -h|--help ) + print_help + exit 0 + ;; + -t|--trust ) + RIOT_TRUST_SERVER=1 + ;; + --tmux ) + USE_TMUX=1 + ;; + --password ) + EXTRA_SSH_OPTIONS+=("-o" "PasswordAuthentication=yes" "-o" "PubkeyAuthentication=no") + ;; + -o ) + EXTRA_SSH_OPTIONS+=("-o" "$2") + shift + ;; + -- ) + shift + positional+=("$@") + break + ;; + -* ) + fmt_fatal "unknown option: $1" + ;; + * ) + positional+=("$1") + ;; + esac shift done - - IFS=',' read -ra remotes <<< "$1" - for remote in "${remotes[@]}"; do - if [[ -z "$remote" ]]; then + IFS=',' read -ra remotes <<< "${positional[0]}" + for i in ${!remotes[@]}; do if [[ -z "${remotes[i]}" ]]; then unset remotes[i]; fi; done + if [[ "${#positional[@]}" == "0" || "${#remotes[@]}" == "0" ]]; then + print_help + exit 1 + fi + for i in ${!remotes[@]}; do + remote="${remotes[i]}" + local batch_func="${remote}.batch" + if is_function "$batch_func"; then + "$batch_func" continue fi parse_remote "$remote" - case $2 in - ssh|"" ) - run_ssh ssh "${@:3}" + case "${positional[1]}" in + ssh|tmux|"" ) + [[ "${positional[1]}" == tmux ]] && USE_TMUX=1 + run_ssh ssh "${positional[@]:2}" ;; - ping|ping6 ) - run_ssh ssh "${@:2}" + ping|ping4|ping6 ) + test "${#positional[@]}" -eq 2 || fmt_fatal "ping requires no arguments" + run_ping "${positional[1]}" ;; zssh ) - run_ssh zssh + run_ssh zssh "${positional[@]:2}" ;; sftp ) - run_ssh sftp + run_ssh sftp "${positional[@]:2}" ;; sshl ) - test -n "$3" || fmt_fatal "no target address provided" - run_sshl "$3" + test -n "${positional[2]}" || fmt_fatal "no target address provided" + test "${#positional[@]}" -eq 3 || fmt_fatal "sshl requires exactly one argument" + run_sshl "${positional[2]}" ;; sshd ) - run_sshd + test "${#positional[@]}" -le 3 || fmt_fatal "sshd requires one or no arguments" + if [[ "${#positional[@]}" -eq 3 ]]; then + check_port "${positional[2]}" || fmt_fatal "invalid port number: ${positional[2]}" + run_sshd "${positional[2]}" + else + run_sshd + fi ;; scp ) - test -n "$3" || fmt_fatal "no source path specified" - test -n "$4" || fmt_fatal "no destination path specified" - run_scp "$3" "$4" + test "${#positional[@]}" -eq 4 || fmt_fatal "scp requires exactly two arguments: source and destination" + test -n "${positional[2]}" || fmt_fatal "no source path specified" + test -n "${positional[3]}" || fmt_fatal "no destination path specified" + run_scp "${positional[2]}" "${positional[3]}" + ;; + rm ) + test "${#positional[@]}" -eq 2 || fmt_fatal "rm requires no arguments" + remove_hostkey ;; * ) print_help - fmt_fatal "unknown command: $2" + fmt_fatal "unknown command: ${positional[1]}" ;; esac done + + if [[ -n "$TMUX_SESS" && "$DFS_DRY_RUN" != "1" ]]; then + tmux attach-session -t $TMUX_SESS + fi } router "${GOT_OPTS[@]}" diff --git a/tools/common.sh b/tools/common.sh index eafb2bd..86f9bd3 100755 --- a/tools/common.sh +++ b/tools/common.sh @@ -13,7 +13,10 @@ if [[ "${BASH_SOURCE[0]}" != "${0}" ]]; then ARG="" GOT_OPTS=() while [[ $# > 0 || -n "$ARG" ]]; do - if [[ -z "$ARG" ]]; then ARG=$1; shift; fi + if [[ -z "$ARG" ]]; then + if [[ "$1" == "--" ]]; then GOT_OPTS+=("$@"); break; fi + ARG="$1"; shift; + fi case $ARG in -q*|--quite ) export DFS_QUIET=1 ;; -l*|--lite ) export DFS_LITE=1 ;; @@ -162,19 +165,6 @@ ask_for_Yn() fi } -post_log() -{ - if [[ $# != 3 || -z "$1" || -z "$2" || -z "$3" ]]; then - fmt_fatal "usage: post_log
" - fi - "${DOTFILES}/tools/frigg-client.sh" "log" "[$1][$2] $3" -} - -apost_log() -{ - post_log "$@" 1>/dev/null & -} - post_beacon() { if [[ $# < 1 || -z "$1" ]]; then diff --git a/tools/frigg-client.sh b/tools/frigg-client.sh index 4d5d3da..f717f6b 100755 --- a/tools/frigg-client.sh +++ b/tools/frigg-client.sh @@ -25,6 +25,7 @@ elif [[ -f /etc/hostname ]]; then else fmt_fatal "unable to get hostname" fi +hostname=$(echo $hostname | tr '[:upper:]' '[:lower:]') init_uuid() { @@ -63,8 +64,8 @@ handle_resp() post_beacon() { - local beacon_type=$1 - local meta=$2 + local beacon_type="$1" + local meta="$2" if [[ -n "$CI" && "$beacon_type" != "gh.ci" && "$beacon_type" != "dfs.invalid-commit" && "$beacon_type" != "dfs.dirty" ]]; then return fi @@ -75,17 +76,6 @@ post_beacon() handle_resp "$resp" } -post_log() -{ - local log_content=$1 - if [[ -z "$log_content" ]]; then - fmt_fatal "log content is required" - fi - init_uuid - resp=$(curl $DFS_CURL_OPTIONS -sSL -X POST -H "Content-Type: text/plain" -d "$log_content" "https://api.beardic.cn/post-log?hostname=$hostname&uuid=$uuid") - handle_resp "$resp" -} - update_dns() { if [[ -z "$DFS_DDNS_IP4$DFS_DDNS_IP6" ]]; then @@ -138,7 +128,7 @@ update_dns() print_help() { - fmt_info "usage: $0 [beacon_type|log_content]" + fmt_info "usage: $0 [beacon_type] [beacon_meta]" } router() @@ -150,9 +140,6 @@ router() beacon) post_beacon "$2" "$3" ;; - log) - post_log "$2" - ;; ddns) update_dns ;; diff --git a/tools/sagent-op.sh b/tools/sagent-op.sh new file mode 100755 index 0000000..8fd8c5c --- /dev/null +++ b/tools/sagent-op.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash +set -e + +op=$(command -v op || command -v op.exe || true) +if [[ ! -x $op ]]; then + echo "1password cli not found" > /dev/stderr + exit -1 +fi +"$op" read "op://Personal/id25519-passphrase/$(hostname)" diff --git a/tools/sagent.sh b/tools/sagent.sh index 9b317a3..409a9ac 100755 --- a/tools/sagent.sh +++ b/tools/sagent.sh @@ -47,6 +47,12 @@ add_piv() list } +add_id25519_with_op() +{ + SSH_ASKPASS_REQUIRE=force SSH_ASKPASS="$THIS_DIR/sagent-op.sh" timeout 60s ssh-add ~/.ssh/id_ed25519 || fmt_fatal "timed out when adding the key. probably the passphrase is wrong or 1password-cli is not working" + list +} + list() { echo echo "available keys:" @@ -96,6 +102,9 @@ route() piv) add_piv ;; + op) + add_id25519_with_op + ;; reset) reset ;; diff --git a/tools/test.zsh b/tools/test.zsh index f8ac0b8..c555776 100644 --- a/tools/test.zsh +++ b/tools/test.zsh @@ -27,7 +27,6 @@ fi # check scripts and functions dfs version -dfs log 1 dfs beacon gh.ci $GITHUB_SHA z ~ test ~ -ef "$(pwd)" @@ -41,8 +40,9 @@ test $(echo n | tools/common.sh ask_for_yN "test") = "0" test $(echo | tools/common.sh ask_for_yN "test") = "0" test $(echo | tools/common.sh ask_for_Yn "test") = "1" test $(DFS_QUIET=1 tools/common.sh ask_for_Yn "test") = "1" -test "$(DFS_TRUST=1 riot time@is.impt:2222/yes@you-r.right/you@are.really.recht./ibd./try@it,another@host scp /tmp/ ./tmp -D 2>/dev/null)" = 'scp -P 12022 -o PermitLocalCommand=yes -o ControlMaster=auto -o ControlPath=~/.ssh/master-socket/%C -o ProxyJump=time@is.impt:2222,yes@you-r.right,you@are.really.recht.,root@ibd. -r try@it.dxng.net:"/tmp/" "./tmp" -scp -P 12022 -o PermitLocalCommand=yes -o ControlMaster=auto -o ControlPath=~/.ssh/master-socket/%C -o ForwardX11=yes -o ForwardAgent=yes -r another@host.dxng.net:"/tmp/" "./tmp"' +test "$(DFS_TRUST=1 riot time@is.impt:2222/yes@you-r.right/you@are.really.recht./ibd./try@it,another@host scp /tmp/ ./tmp -D 2>/dev/null)" = 'scp -P 12022 -o RequestTTY=yes -o PermitLocalCommand=yes -o ControlMaster=auto -o ControlPath=~/.ssh/master-socket/%C -o ProxyJump=time@is.impt:2222,yes@you-r.right:12022,you@are.really.recht:12022,root@ibd:12022 -r try@it.dxng.net:/tmp/ ./tmp +scp -P 12022 -o RequestTTY=yes -o PermitLocalCommand=yes -o ControlMaster=auto -o ControlPath=~/.ssh/master-socket/%C -o ForwardX11=yes -o ForwardAgent=yes -r another@host.dxng.net:/tmp/ ./tmp' +test "$(riot you@example.com:55 -tD ssh --password -- ping -c 1 2>/dev/null)" = 'ssh -p 55 -o RequestTTY=yes -o ForwardX11=yes -o ForwardAgent=yes -o PasswordAuthentication=yes -o PubkeyAuthentication=no you@example.com ping -c 1' # check alias alias p114 diff --git a/tools/to-install.sh b/tools/to-install.sh index 93f9d80..7af86fb 100755 --- a/tools/to-install.sh +++ b/tools/to-install.sh @@ -4,7 +4,7 @@ declare -A INSTALL_COMMANDS INSTALL_COMMANDS=(\ [git]="apt update && apt install git" \ [fzf]="git clone --depth 1 https://gitee.com/dictxiong/fzf.git ~/.fzf && ~/.fzf/install" \ - [acme.sh]="curl https://get.acme.sh | sh -s email=${EMAIL:-me@beardic.cn}" \ + [acme.sh]="curl https://get.acme.sh | sh -s email=${EMAIL:-acme@dxng.net}" \ [oh-my-zsh]='sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"' \ [oh-my-tuna]='wget https://tuna.moe/oh-my-tuna/oh-my-tuna.py && sudo python oh-my-tuna.py --global' \ [v2fly]="bash <(curl -L https://raw.githubusercontent.com/v2fly/fhs-install-v2ray/master/install-release.sh) #--remove" \