mirror of
				https://github.com/DictXiong/dotfiles.git
				synced 2025-11-04 07:27:48 +08:00 
			
		
		
		
	Compare commits
	
		
			No commits in common. "cba76bd304587cdbff434cc1072eed69c88a8bf2" and "b33604ac147d18f80020d8fd860781ded792ea5a" have entirely different histories.
		
	
	
		
			cba76bd304
			...
			b33604ac14
		
	
		
@ -319,7 +319,9 @@ for i in ${GOT_OPTS[@]}; do
 | 
				
			|||||||
    case $i in
 | 
					    case $i in
 | 
				
			||||||
        -i ) FUNC=install ;;
 | 
					        -i ) FUNC=install ;;
 | 
				
			||||||
        -r ) FUNC=uninstall ;;
 | 
					        -r ) FUNC=uninstall ;;
 | 
				
			||||||
 | 
					        -d|--dev ) export DFS_DEV=1; set -x ;;
 | 
				
			||||||
        -a|--auto ) INSTALL_DEP=1 ;;
 | 
					        -a|--auto ) INSTALL_DEP=1 ;;
 | 
				
			||||||
 | 
					        -s|--secure ) export DFS_DEV=0 ;;
 | 
				
			||||||
        -H|--hist|--history ) store_hist=1 ;;
 | 
					        -H|--hist|--history ) store_hist=1 ;;
 | 
				
			||||||
        -x ) store_config=1 ;;
 | 
					        -x ) store_config=1 ;;
 | 
				
			||||||
        * ) fmt_fatal "unknown option \"$i\"" ;;
 | 
					        * ) fmt_fatal "unknown option \"$i\"" ;;
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										158
									
								
								scripts/riot
									
									
									
									
									
								
							
							
						
						
									
										158
									
								
								scripts/riot
									
									
									
									
									
								
							@ -5,105 +5,65 @@ source "$THIS_DIR/../tools/common.sh"
 | 
				
			|||||||
RIOT_TRUST_CLIENT=${RIOT_TRUST_CLIENT:-${DFS_TRUST:-0}}
 | 
					RIOT_TRUST_CLIENT=${RIOT_TRUST_CLIENT:-${DFS_TRUST:-0}}
 | 
				
			||||||
RIOT_TRUST_SERVER=${RIOT_TRUST_SERVER:-0}
 | 
					RIOT_TRUST_SERVER=${RIOT_TRUST_SERVER:-0}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# get single server setting
 | 
					# get target settings
 | 
				
			||||||
# 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#*@}
 | 
					 | 
				
			||||||
    fi
 | 
					 | 
				
			||||||
    # if in the form ...:22
 | 
					 | 
				
			||||||
    if [[ "$remote" == *:* ]]; then
 | 
					 | 
				
			||||||
        RET_PORT=${remote##*:}
 | 
					 | 
				
			||||||
        remote=${remote%:*}
 | 
					 | 
				
			||||||
    fi
 | 
					 | 
				
			||||||
    # presets -- match domain
 | 
					 | 
				
			||||||
    local domain=${remote##*.}
 | 
					 | 
				
			||||||
    local host=${remote%.*}
 | 
					 | 
				
			||||||
    # if ends with dot
 | 
					 | 
				
			||||||
    if [[ -z "$domain" ]]; then
 | 
					 | 
				
			||||||
        domain="ibd"
 | 
					 | 
				
			||||||
    fi
 | 
					 | 
				
			||||||
    # if there's no dot
 | 
					 | 
				
			||||||
    if [[ "$host" == "$domain" ]]; then
 | 
					 | 
				
			||||||
        domain="ibd"
 | 
					 | 
				
			||||||
    fi
 | 
					 | 
				
			||||||
    case $domain in
 | 
					 | 
				
			||||||
        ibd|ebd )
 | 
					 | 
				
			||||||
            RET_HOSTNAME=$host.$domain.ink
 | 
					 | 
				
			||||||
            RET_PORT=${RET_PORT:-12022}
 | 
					 | 
				
			||||||
            RET_USERNAME=${RET_USERNAME:-root}
 | 
					 | 
				
			||||||
            RET_TRUST_SERVER=1
 | 
					 | 
				
			||||||
            ;;
 | 
					 | 
				
			||||||
        nasp )
 | 
					 | 
				
			||||||
            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
 | 
					 | 
				
			||||||
            ;;
 | 
					 | 
				
			||||||
        proxied )
 | 
					 | 
				
			||||||
            RET_HOSTNAME=proxy.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
 | 
					 | 
				
			||||||
            ;;
 | 
					 | 
				
			||||||
        * )
 | 
					 | 
				
			||||||
            fmt_warning "unknown domain: $domain. will try as host name"
 | 
					 | 
				
			||||||
            RET_HOSTNAME="$remote"
 | 
					 | 
				
			||||||
    esac
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
# remote setting, including jump servers
 | 
					 | 
				
			||||||
# will be called only once
 | 
					 | 
				
			||||||
# provides:
 | 
					# provides:
 | 
				
			||||||
SERVER=""
 | 
					SERVER=""
 | 
				
			||||||
TRUST_SERVER=1
 | 
					 | 
				
			||||||
PORT=""  # optional
 | 
					PORT=""  # optional
 | 
				
			||||||
USERNAME=""  # optional
 | 
					USERNAME=""  # optional
 | 
				
			||||||
SSH_OPTIONS=""  # optional
 | 
					SSH_OPTIONS=""
 | 
				
			||||||
if [[ "$RIOT_TRUST_CLIENT" == "1" ]]; then
 | 
					if [[ "$RIOT_TRUST_CLIENT" == "1" ]]; then
 | 
				
			||||||
    SSH_OPTIONS='-o ControlMaster=auto -o ControlPath=/tmp/sshcm-%C -o PermitLocalCommand=yes'
 | 
					    SSH_OPTIONS='-o ControlMaster=auto -o ControlPath=/tmp/sshcm-%C -o PermitLocalCommand=yes'
 | 
				
			||||||
fi
 | 
					fi
 | 
				
			||||||
parse_remote() {
 | 
					get_server_meta()
 | 
				
			||||||
    local remote="$1"
 | 
					{
 | 
				
			||||||
    local jump_servers=""
 | 
					    local trust_server="$RIOT_TRUST_SERVER"
 | 
				
			||||||
    # loop for jump servers
 | 
					    local arg="$1"
 | 
				
			||||||
    while [[ -n $remote ]]; do 
 | 
					    # overwrite
 | 
				
			||||||
        local server=${remote%%,*}
 | 
					    if [[ "$arg" == *@* ]]; then
 | 
				
			||||||
        remote=${remote#*,}
 | 
					        USERNAME=${arg%%@*}
 | 
				
			||||||
        get_server_meta "$server"
 | 
					        arg=${arg#*@}
 | 
				
			||||||
        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
 | 
					    fi
 | 
				
			||||||
    if [[ -n "$jump_servers" ]]; then
 | 
					    if [[ "$arg" == *:* ]]; then
 | 
				
			||||||
        SSH_OPTIONS="$SSH_OPTIONS -o ProxyJump=$jump_servers"
 | 
					        PORT=${arg##*:}
 | 
				
			||||||
 | 
					        arg=${arg%:*}
 | 
				
			||||||
 | 
					    fi
 | 
				
			||||||
 | 
					    # presets
 | 
				
			||||||
 | 
					    local domain=${arg##*.}
 | 
				
			||||||
 | 
					    local host=${arg%.*}
 | 
				
			||||||
 | 
					    if [[ -z "$domain" ]]; then
 | 
				
			||||||
 | 
					        domain="ibd"
 | 
				
			||||||
 | 
					    fi
 | 
				
			||||||
 | 
					    if [[ "$host" == "$domain" ]]; then
 | 
				
			||||||
 | 
					        domain="proxied"
 | 
				
			||||||
 | 
					    fi
 | 
				
			||||||
 | 
					    case $domain in
 | 
				
			||||||
 | 
					        ibd|ebd )
 | 
				
			||||||
 | 
					            SERVER=$host.$domain.ink
 | 
				
			||||||
 | 
					            PORT=${PORT:-12022}
 | 
				
			||||||
 | 
					            USERNAME=${USERNAME:-root}
 | 
				
			||||||
 | 
					            trust_server=1
 | 
				
			||||||
 | 
					            ;;
 | 
				
			||||||
 | 
					        nasp )
 | 
				
			||||||
 | 
					            SERVER=$host
 | 
				
			||||||
 | 
					            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))
 | 
				
			||||||
 | 
					            USERNAME=root
 | 
				
			||||||
 | 
					            trust_server=1
 | 
				
			||||||
 | 
					            ;;
 | 
				
			||||||
 | 
					        * )
 | 
				
			||||||
 | 
					            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
 | 
					    fi
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -115,7 +75,7 @@ eval_or_echo() {
 | 
				
			|||||||
    fi
 | 
					    fi
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# ssh series
 | 
					# ssh
 | 
				
			||||||
prepare_ssh_cmd() {
 | 
					prepare_ssh_cmd() {
 | 
				
			||||||
    local ssh_bin="${1:-ssh}"
 | 
					    local ssh_bin="${1:-ssh}"
 | 
				
			||||||
    if [[ "$ssh_bin" == "scp" || "$ssh_bin" == "sftp" ]]; then
 | 
					    if [[ "$ssh_bin" == "scp" || "$ssh_bin" == "sftp" ]]; then
 | 
				
			||||||
@ -126,7 +86,6 @@ prepare_ssh_cmd() {
 | 
				
			|||||||
    echo "$ssh_bin ${PORT:+$port_param} $PORT $SSH_OPTIONS $SCP_SRC $USERNAME${USERNAME:+@}$SERVER $SCP_DST"
 | 
					    echo "$ssh_bin ${PORT:+$port_param} $PORT $SSH_OPTIONS $SCP_SRC $USERNAME${USERNAME:+@}$SERVER $SCP_DST"
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# ssh
 | 
					 | 
				
			||||||
run_ssh()
 | 
					run_ssh()
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    local cmd="$(prepare_ssh_cmd $1)"
 | 
					    local cmd="$(prepare_ssh_cmd $1)"
 | 
				
			||||||
@ -137,7 +96,10 @@ run_ssh()
 | 
				
			|||||||
# sshl
 | 
					# sshl
 | 
				
			||||||
run_sshl()
 | 
					run_sshl()
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    local arg="$1"
 | 
					    if [[ -z "$1" ]]; then
 | 
				
			||||||
 | 
					        fmt_fatal "invalid remote address: $1"
 | 
				
			||||||
 | 
					    fi
 | 
				
			||||||
 | 
					    arg="$1"
 | 
				
			||||||
    if [[ "$arg" != *":"* ]]; then
 | 
					    if [[ "$arg" != *":"* ]]; then
 | 
				
			||||||
        # treat as a port number
 | 
					        # treat as a port number
 | 
				
			||||||
        arg=localhost:$arg
 | 
					        arg=localhost:$arg
 | 
				
			||||||
@ -186,12 +148,13 @@ print_help()
 | 
				
			|||||||
    echo "available commands: ssh (default), sshl (ssh -L), zssh, sftp"
 | 
					    echo "available commands: ssh (default), sshl (ssh -L), zssh, sftp"
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
router() {
 | 
					router()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
    if [[ -z "$1" || "$1" == "-h" || "$1" == "--help" ]]; then
 | 
					    if [[ -z "$1" || "$1" == "-h" || "$1" == "--help" ]]; then
 | 
				
			||||||
        print_help
 | 
					        print_help
 | 
				
			||||||
        exit
 | 
					        exit
 | 
				
			||||||
    fi
 | 
					    fi
 | 
				
			||||||
    parse_remote "$1"
 | 
					    get_server_meta "$1"
 | 
				
			||||||
    case $2 in
 | 
					    case $2 in
 | 
				
			||||||
        -h|--help)
 | 
					        -h|--help)
 | 
				
			||||||
            print_help
 | 
					            print_help
 | 
				
			||||||
@ -207,12 +170,9 @@ router() {
 | 
				
			|||||||
            run_ssh sftp
 | 
					            run_ssh sftp
 | 
				
			||||||
            ;;
 | 
					            ;;
 | 
				
			||||||
        sshl )
 | 
					        sshl )
 | 
				
			||||||
            test -n "$3" || fmt_fatal "no target address provided"
 | 
					 | 
				
			||||||
            run_sshl "$3"
 | 
					            run_sshl "$3"
 | 
				
			||||||
            ;;
 | 
					            ;;
 | 
				
			||||||
        scp )
 | 
					        scp )
 | 
				
			||||||
            test -n "$3" || fmt_fatal "no source path specified"
 | 
					 | 
				
			||||||
            test -n "$4" || fmt_fatal "no destination path specified"
 | 
					 | 
				
			||||||
            run_scp "$3" "$4"
 | 
					            run_scp "$3" "$4"
 | 
				
			||||||
            ;;
 | 
					            ;;
 | 
				
			||||||
        * )
 | 
					        * )
 | 
				
			||||||
 | 
				
			|||||||
@ -3,7 +3,7 @@ set -e
 | 
				
			|||||||
THIS_DIR_COMMON_SH=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd )
 | 
					THIS_DIR_COMMON_SH=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd )
 | 
				
			||||||
export DOTFILES=$( cd "$THIS_DIR_COMMON_SH/.." && pwd )
 | 
					export DOTFILES=$( cd "$THIS_DIR_COMMON_SH/.." && pwd )
 | 
				
			||||||
if [[ -f ~/.config/dotfiles/env ]]; then set -a; source ~/.config/dotfiles/env; set +a; fi
 | 
					if [[ -f ~/.config/dotfiles/env ]]; then set -a; source ~/.config/dotfiles/env; set +a; fi
 | 
				
			||||||
if [[ "$DFS_DEV" == "1" ]]; then set -x; fi
 | 
					if [[ "$DFS_DEBUG" == "1" ]]; then set -x; fi
 | 
				
			||||||
DFS_CURL_OPTIONS="--retry 2 --max-time 20"
 | 
					DFS_CURL_OPTIONS="--retry 2 --max-time 20"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# parse args and set env, when it is sourced
 | 
					# parse args and set env, when it is sourced
 | 
				
			||||||
@ -17,9 +17,8 @@ if [[ "${BASH_SOURCE[0]}" != "${0}" ]]; then
 | 
				
			|||||||
        case $ARG in
 | 
					        case $ARG in
 | 
				
			||||||
            -q*|--quite ) export DFS_QUIET=1 ;;
 | 
					            -q*|--quite ) export DFS_QUIET=1 ;;
 | 
				
			||||||
            -l*|--lite ) export DFS_LITE=1 ;;
 | 
					            -l*|--lite ) export DFS_LITE=1 ;;
 | 
				
			||||||
            -d*|--dev ) export DFS_DEV=1; set -x ;;
 | 
					 | 
				
			||||||
            -D*|--dry-run ) export DFS_DRY_RUN=1 ;;
 | 
					 | 
				
			||||||
            --color ) export DFS_COLOR=1 ;;
 | 
					            --color ) export DFS_COLOR=1 ;;
 | 
				
			||||||
 | 
					            --dry-run ) export DFS_DRY_RUN=1 ;;  # TODO!!!
 | 
				
			||||||
            --*=* ) GOT_OPTS+=("${ARG%%=*}" "${ARG#*=}") ;;
 | 
					            --*=* ) GOT_OPTS+=("${ARG%%=*}" "${ARG#*=}") ;;
 | 
				
			||||||
            --* ) GOT_OPTS+=("$ARG") ;;
 | 
					            --* ) GOT_OPTS+=("$ARG") ;;
 | 
				
			||||||
            -* ) GOT_OPTS+=("${ARG:0:2}") ;;
 | 
					            -* ) GOT_OPTS+=("${ARG:0:2}") ;;
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user