Skip to content

Commit

Permalink
[dev] ddns; better ssh agent; riot sshl; sys.online; zsh hist import;…
Browse files Browse the repository at this point in the history
… DFS_DEBUG; cbd/cbds (#36)

* riot: bj1 -> proxy

* ci: test ask_for_yn

* --wip-- [skip ci]

* install.sh: will prompt version

* zshrc: piv-agent now can re-use agents

* zshrc: in docker use theme robbyrussell

* update.sh: will send online beacon

* beacon: support meta with limited len 64

* bug fix (Sat Jan 21 16:45:35 CST 2023)

* well, finally add ssh key for ltp1-bd

* set DFS_INITED and do not send login beacon if it was set

* rewrite piv-agent to sagent; fmt_note and fmt_into will write to stderr

* bug fix (Mon Jan 30 19:41:00 CST 2023)

* bug fix (Mon Jan 30 19:46:27 CST 2023)

* login beacon add details

* $DFS_ORPHAN turns logger off

* disable more beacons in CI

* install.sh: prepare config earlier

* init pbin

* install.sh: -H|--hist|--history

* install.sh: -H hint

* install.sh: -H support multiple keys

* fix zsh hist with no new line at the end

* remove ^M in hist

* bug fix (Wed Feb  1 21:11:41 CST 2023)

* bug fix (Wed Feb  1 21:16:29 CST 2023)

* bug fix (Wed Feb  1 21:20:21 CST 2023)

* ci will send gh.ci.fail

* show install opts

* DFS_DEBUG to set -x

* bug fix: install.sh re-start itself with no args incorrectly

* bug fix (Thu Feb  2 18:43:16 CST 2023)

* riot: ob->ebd, swap option $1 and $2 and $2 is default to ssh

* riot: introduce sshl

* logger.sh: support ddns

* bug fix (Wed Mar  1 16:37:42 CST 2023)

* dfs.ddns.failed -> dfs.ddns.fail

* login beacon 2>/dev/null

* logger.sh -> frigg-client.sh; curl add time limit 10s

* to-install.sh: improve docker-ce

* use $DFS_CURL_OPTIONS

* ci: add ddns

* bug fix (Wed Mar 15 19:37:21 CST 2023)

* Revert "bug fix (Wed Mar 15 19:37:21 CST 2023)"

This reverts commit 2df87ce.

* Revert "ci: add ddns"

This reverts commit 4fe8321.

* alias cbd and cbds, riot nasp port to 12022

* .zshrc: sagent -> sagt

* fix ci

* riot: bug fix when 'tmp' has leading zeros

* add ci for cbds

---------

Co-authored-by: xiongdian.me <[email protected]>
  • Loading branch information
DictXiong and xiongdian.me authored Apr 21, 2023
1 parent 9a4c955 commit 8fdd3c4
Show file tree
Hide file tree
Showing 15 changed files with 461 additions and 170 deletions.
2 changes: 2 additions & 0 deletions .ssh/authorized_keys2
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@ ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBNvHya5i
ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBC5gu+U4picxVvCNYjLlmxGLpJl7svj3LklO7jSpwqdrNxlE+/xkx07PJx76AbA77dDM1Dxmm/VOvVQCKTRLTgk= ip14/sep

ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIF52mPmbLEriZ+DfmrMHFtvg2kiO2JpRpitvs7PbJowh home0

ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKhS4voo3K/Dvzqckr0bouO1WkCI5XxswstHWnuuyKBz ltp1-bd
11 changes: 10 additions & 1 deletion .update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ source "$THIS_DIR/tools/common.sh"
DFS_UPDATED_RET=${DFS_UPDATED_RET:-0}
DFS_UPDATE_CHANNEL=${DFS_UPDATE_CHANNEL:-"main"}

# send beacon online
apost_beacon "sys.online"

# update dns
if [[ "$DFS_DDNS_ENABLE" == "1" ]]; then
fmt_info "updating dns ..."
"$THIS_DIR/tools/frigg-client.sh" ddns || (fmt_error "failed to update dns" && apost_beacon "dfs.ddns.fail")
fi

# fetch origin
cd $DOTFILES
git fetch --all --prune
Expand All @@ -18,7 +27,7 @@ fi

# get the specified commit id
case $DFS_UPDATE_CHANNEL in
"main" ) DFS_COMMIT=$(curl -fsSL https://api.beardic.cn/get-var/dfs-commit-id) ;;
"main" ) DFS_COMMIT=$(curl $DFS_CURL_OPTIONS -fsSL https://api.beardic.cn/get-var/dfs-commit-id) ;;
"dev" ) DFS_COMMIT=$(git rev-parse origin/dev 2> /dev/null) || DFS_COMMIT=$(git rev-parse origin/main) ;;
"latest" ) DFS_COMMIT=$(git for-each-ref --sort=-committerdate refs/heads refs/remotes --format='%(objectname)' | head -n 1) ;;
* ) fmt_fatal "invalid update channel: $DFS_UPDATE_CHANNEL" ;;
Expand Down
36 changes: 12 additions & 24 deletions .zshrc2
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ done
# select theme
TMP_THEME=ys
if [[ -e /.dockerenv ]]; then
TMP_THEME=blinks
TMP_THEME=robbyrussell
echo "==========================="
echo "This is in docker container"
echo "==========================="
Expand All @@ -90,10 +90,12 @@ antigen apply
# end of antigen config

# beacon
if [[ "$EUID" == "0" ]]; then
"$DOTFILES/tools/common.sh" apost_beacon "sys.login.root"
else
"$DOTFILES/tools/common.sh" apost_beacon "sys.login.user"
if [[ -z "$DFS_INITED" && -z "$CI" ]]; then
if [[ "$EUID" == "0" ]]; then
"$DOTFILES/tools/common.sh" apost_beacon "sys.login.root" "$(who am i | sed -E 's/ +/ /g' | awk '$1=$1')" 2>/dev/null
else
"$DOTFILES/tools/common.sh" apost_beacon "sys.login.user" "$(who am i | sed -E 's/ +/ /g' | awk '$1=$1')" 2>/dev/null
fi
fi

# alias
Expand All @@ -102,6 +104,8 @@ alias "se"='sudo -sE'
alias "pbd"='ping baidu.com'
alias "p114"='ping 114.114.114.114'
alias "p666"='ping6 2001:da8::666'
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))"'
case $(bash "$DOTFILES/tools/common.sh" get_os_type) in
macos ) alias l='ls -lAGh -D "%y-%m-%d %H:%M"' ;;
Expand All @@ -111,24 +115,7 @@ if [[ -x $(command -v trash) ]]; then
alias "rm"="echo use the full path i.e. '/bin/rm'\; consider using trash"
fi
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 }
piv-agent()
{
eval $(ssh-agent -k)
# they are: ubuntu, macos, respectively
local SO_PATHS=( "/usr/lib64/opensc-pkcs11.so" "/usr/local/lib/opensc-pkcs11.so" )
for i in ${SO_PATHS[*]}; do
if [[ -f "$i" ]]; then
echo "using: $i"
eval $(ssh-agent -P "$i")
ssh-add -s "$i"
echo "now available keys:"
ssh-add -l
return 0
fi
done
echo "opensc-pkcs11.so not found"
return 1
}
sagt() { eval "$($DOTFILES/tools/sagent.sh $@)" }

# key bindings
bindkey "^b" beginning-of-line # ctrl+b
Expand Down Expand Up @@ -182,10 +169,11 @@ dfs()
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" ;;
beacon ) "$DOTFILES/tools/common.sh" "post_beacon" "$2" "$3" ;;
* ) echo "unknown command \"$1\". available: update, force-update, version, reset, cd, log" ;;
esac
}

# clean
unset i
export DFS_INITED=1
20 changes: 18 additions & 2 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ prepare_config()
echo
}

append_hist()
{
fmt_note "appending zsh history ..."
"$DOTFILES/tools/append_zsh_hist.sh" "$@"
}

install_file_content()
{
fmt_note "installing file content ..."
Expand Down Expand Up @@ -249,7 +255,7 @@ install_update()
RET=$?
if [[ $RET == 85 ]]; then
fmt_note "dfs updated. re-running install.sh ..."
"${DOTFILES}/install.sh" "$@" && exit
"${DOTFILES}/install.sh" "${ORIGIN_ARGS[@]}" && exit
elif [[ $RET != 0 ]]; then
fmt_fatal "update.sh failed with exit code $RET"
fi
Expand All @@ -265,16 +271,17 @@ uninstall_update()
install()
{
if [[ "$INSTALL_DEP" == "1" ]]; then install_dependencies; fi
prepare_config
install_update
preinstall_check
prepare_config
install_crontab
install_file_content
install_symlink
apost_beacon "dfs.installed"
# those that won't be uninstalled in the future
install_tmux_tpm
install_vim_vundle
if [[ -n "$DFS_HIST" ]]; then append_hist "$DFS_HIST"; fi
fmt_note "done installing!"
}

Expand All @@ -292,21 +299,30 @@ uninstall()
fmt_note "done uninstalling!"
}

echo "this is the dotfiles installer, version $(cd "$DOTFILES" && git describe --tags --always --dirty)"
echo "install options:" "${GOT_OPTS[@]}"
FUNC=install
INSTALL_DEP=0
store_config=0
store_hist=0
for i in ${GOT_OPTS[@]}; do
if [[ "$store_config" == "1" ]]; then
store_config=0
DFS_CONFIGS+=("$i")
continue
fi
if [[ "$store_hist" == "1" ]]; then
store_hist=0
DFS_HIST=$i
continue
fi
case $i in
-i ) FUNC=install ;;
-r ) FUNC=uninstall ;;
-d|--dev ) export DFS_DEV=1; set -x ;;
-a|--auto ) INSTALL_DEP=1 ;;
-s|--secure ) export DFS_DEV=0 ;;
-H|--hist|--history ) store_hist=1 ;;
-x ) store_config=1 ;;
* ) fmt_fatal "unknown option \"$i\"" ;;
esac
Expand Down
44 changes: 44 additions & 0 deletions scripts/pbin
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/bin/bash
set -e
THIS_DIR=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd )
source "$THIS_DIR/../tools/common.sh"

get()
{
local key=$1
if [[ -z "$key" ]]; then
fmt_fatal "missing key"
fi
local output=${2:-/dev/stdout}
local url="https://pastebin.com/raw/$key"
curl -fsSL "$url" > "$output"
}

put()
{
local input=${1:-/dev/stdin}
(echo -n api_paste_code=; cat "$input") | curl -fsSL -X POST -d 'api_dev_key=SKZLfq9y_zW2hkgQOKZz1b5rw8hGiqxZ' -d 'api_option=paste' --data-binary @- "https://pastebin.com/api/api_post.php"
}

route()
{
local cmd=$1
if [[ -z "$cmd" ]]; then
put
exit
else
shift
fi
case $cmd in
get )
get "$@"
;;
put )
put "$@"
;;
* )
fmt_fatal "unknown command: $cmd"
esac
}

route "${GOT_OPTS[@]}"
63 changes: 42 additions & 21 deletions scripts/riot
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,22 @@ get_server_meta()
domain=""
fi
case $domain in
ibd )
SERVER=$host.ibd.ink
ibd|ebd )
SERVER=$host.$domain.ink
PORT=12022
SSH_USERNAME=root
;;
ob )
SERVER=$host.ob.ac.cn
PORT=24022
SSH_USERNAME=root
;;
nasp )
SERVER=$host
PORT=22
PORT=12022
SSH_USERNAME=dictxiong
SSH_OPTIONS='-o ProxyJump="[email protected]:36022"'
;;
"" )
SERVER=bj1.ob.ac.cn
SERVER=proxy.beardic.cn
local tmp=$(sha256sum <<< "$host" | tr -cd "[:digit:]")
tmp=${tmp:0:4}
PORT=$((tmp+36000))
PORT=$((10#$tmp+36000))
SSH_USERNAME=root
;;
* )
Expand All @@ -46,33 +41,59 @@ get_server_meta()
}

# ssh
_ssh()
run_ssh()
{
get_server_meta "$1"
fmt_note "--> ssh to $SERVER:$PORT"
eval ssh -p $PORT $SSH_OPTIONS $SSH_USERNAME@$SERVER
CMD="ssh -p $PORT $SSH_OPTIONS $SSH_USERNAME@$SERVER"
fmt_note "-->" $CMD
eval $CMD
}

# sshl
run_sshl()
{
if [[ -z "$1" || "$1" != *":"* ]]; then
fmt_fatal "invalid remote address: $1"
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
fmt_note " > please access localhost:$port"
eval $CMD
}

# main
print_help()
{
fmt_info "usage: $0 <command> <service> [options]"
echo "available commands: ssh"
fmt_info "usage: $0 <service> [command] [options]"
echo "available commands: ssh (default), sshl (ssh -L)"
}

router()
{
test $# -eq 2 || (print_help && fmt_fatal "invalid arguments")
case $1 in
if [[ -z "$1" || "$1" == "-h" || "$1" == "--help" ]]; then
print_help
exit
fi
get_server_meta "$1"
case $2 in
-h|--help)
print_help
exit
;;
ssh|"" )
run_ssh
;;
ssh )
_ssh "$2"
sshl )
run_sshl "$3"
;;
* )
print_help
fmt_fatal "unknown command: $1"
fmt_fatal "unknown command: $2"
;;
esac
}
Expand Down
33 changes: 33 additions & 0 deletions tools/append_zsh_hist.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/bin/bash
set -e
THIS_DIR=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd )
source "$THIS_DIR/common.sh"

zsh_hist_file="$HOME/.zsh_history"

do_append()
{
timestamp=$(date +%s)
while read -r line; do
if [[ -n "$line" ]]; then
echo ": $timestamp:0;$line" >> "$zsh_hist_file"
fi
done
}

main()
{
key=$1
if [[ -z "$key" ]]; then
fmt_fatal "missing key"
fi
IFS=',' read -r -a keys<<<"$key"
for k in "${keys[@]}";do
if [[ -z "$k" ]]; then
continue
fi
(curl -fsSL "https://pastebin.com/raw/$k" && echo) | sed 's/\r//' | do_append
done
}

main "${GOT_OPTS[@]}"
Loading

0 comments on commit 8fdd3c4

Please sign in to comment.