dotfiles/tools/frigg-client.sh
Dict Xiong 3bd0121786
[dev] GPG agent forwarding; improve riot, sagt, tmux and zsh (#46)
* feat(riot): support literal ipv6 addresses (with or without ports given)

feat(riot): -v, -4, and -6

fix(riot): do not call external binaries if possible

* feat(riot): command git

* build(riot-config): nasp.fit -> jump.nasp.fit

* feat: add bnd and nor and completions

* fix(zshrc): bnd exit when build error

* fix(tmux): press ^a twice to send ^a

* feat(riot): sshl supports specifying local port and unix sock

* feat(riot): sshr for ssh -R

* feat(riot): -o ServerAliveInterval=60

* feat(riot): conditionally RequestTTY

* feat: zsh completion for beam and beaml

* fix(frigg): ddns get ip

* fix(riot): tmux error with window number and spaces

The fix to spaces is just a workaround. Issue may still exist when
command contains spaces.

* feat(riot): ControlPersist=60s

* fix(ci): riot

* feat(tmux): prevent tmux from exiting copy mode after selection with mouse

see:
- https://www.reddit.com/r/tmux/comments/v73005/how_to_prevent_tmux_from_exiting_copy_mode_after/
- https://www.reddit.com/r/tmux/comments/1ltms6a/how_to_select_text_in_tmux_without_having_it_jump/

* feat(tmux): F12 to passthru

* fix(riot): ControlPersist 60s -> 5s

* feat: sne: search and edit

* feat(ssh): add key ip17/sep

* feat(tmux): press ESC to exit copy mode

* feat(ssh): add key sk1/piv/9a

* feat(ssh): add key sk1/fido2

* feat(ssh): add key openpgp:0x8774BF70 and remove key ip14/sep and ltp2 (asu127)

* feat(sagent): sagt gpg; improve code

* feat(sagent): check pinentry and give notes

* feat(riot): add GPG agent forwarding

Signed-off-by: Dict Xiong <me@beardic.cn>

* feat: gitconf add gpg signature; remove macos from ci

* feat(eid): add sk1 9a cert

* feat(riot): better help and fix option quite->quiet

* feat(riot): separate command options from riot options

* feat(sagt): gpg-pin and associated tests

* feat(riot): zsh completions

* fix: Apply suggestions from code review

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* fix(riot): simplify and secure

* fix: syntax

---------

Signed-off-by: Dict Xiong <me@beardic.cn>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
2026-08-13 20:10:55 +08:00

154 lines
4.4 KiB
Bash
Executable File

#!/usr/bin/env bash
set -e
THIS_DIR=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd )
source "$THIS_DIR/common.sh"
if [[ "$DFS_ORPHAN" == "1" ]]; then
exit 0
fi
if [[ -n "$DFS_HOSTNAME" ]]; then
hostname=$DFS_HOSTNAME
elif [[ -x $(command -v hostname) ]]; then
hostname=$(hostname)
elif [[ -x $(command -v uname) ]]; then
hostname=$(uname -n)
elif [[ -x $(command -v hostnamectl) ]]; then
hostname=$(hostnamectl --static)
elif [[ -n "$HOSTNAME" ]]; then
hostname=$HOSTNAME
elif [[ -f /proc/sys/kernel/hostname ]]; then
hostname=$(cat /proc/sys/kernel/hostname)
elif [[ -f /etc/hostname ]]; then
hostname=$(cat /etc/hostname)
else
fmt_fatal "unable to get hostname"
fi
hostname=$(echo $hostname | tr '[:upper:]' '[:lower:]')
init_uuid()
{
if [[ -f ~/.config/dotfiles/uuid ]]; then
uuid=$(cat ~/.config/dotfiles/uuid)
else
if [[ -x $(command -v uuidgen) ]]; then
uuid=$(uuidgen)
elif [[ -f /proc/sys/kernel/random/uuid ]]; then
uuid=$(cat /proc/sys/kernel/random/uuid)
else
fmt_fatal "unable to generate uuid"
fi
mkdir -p ~/.config/dotfiles
echo "$uuid" > ~/.config/dotfiles/uuid
fi
}
handle_resp()
{
local resp="$1"
if grep -q "200" <<< "$resp"; then
echo $resp
elif grep -q "403" <<< "$resp"; then
echo $resp >&2
fmt_error "error accessing api: authentification failed"
fmt_info "try to register you hostname and uuid"
fmt_info "hostname: $hostname"
fmt_info "uuid: $uuid"
else
echo $resp >&2
fmt_fatal "server returned an error"
# here return 1 because this is not expected
fi
}
post_beacon()
{
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
if [[ -z "$beacon_type" ]]; then
fmt_fatal "beacon type is required"
fi
resp=$(curl $DFS_CURL_OPTIONS -sSL -X POST -H "Content-Type: text/plain" -d "$meta" "https://api.beardic.cn/post-beacon?hostname=$hostname&beacon=$beacon_type")
handle_resp "$resp"
}
update_dns()
{
if [[ -z "$DFS_DDNS_IP4$DFS_DDNS_IP6" ]]; then
fmt_fatal "neither DFS_DDNS_IP4 nor DFS_DDNS_IP6 is configured"
fi
if [[ "$DFS_DDNS_IP4$DFS_DDNS_IP6" == "autoauto" ]]; then
fmt_fatal "DFS_DDNS_IP4 and DFS_DDNS_IP6 cannot both be auto"
fi
init_uuid
local ip4
local ip6
local api_url="https://api.beardic.cn"
# get ip4
if [[ -z "$DFS_DDNS_IP4" ]]; then
ip4=""
elif [[ "$DFS_DDNS_IP4" == "auto" ]]; then
ip4="auto"
api_url="https://api4.beardic.cn"
elif [[ "$DFS_DDNS_IP4" == "api" ]]; then
ip4=$(curl $DFS_CURL_OPTIONS -sSL "https://api.ipify.org")
elif [[ "$DFS_DDNS_IP4" == "http"* ]]; then
ip4=$(curl $DFS_CURL_OPTIONS -sSL "$DFS_DDNS_IP4")
else
ip4=$(ip -4 --brief addr show $DFS_DDNS_IP4 scope global primary | awk '{split($3,a,"/");print a[1]}' | head -n 1)
fi
if [[ -n "$DFS_DDNS_IP4" && -z "$ip4" ]]; then
fmt_fatal "failed getting ip4 address"
fi
# get ip6
if [[ -z "$DFS_DDNS_IP6" ]]; then
ip6=""
elif [[ "$DFS_DDNS_IP6" == "auto" ]]; then
ip6="auto"
api_url="https://api6.beardic.cn"
elif [[ "$DFS_DDNS_IP6" == "api" ]]; then
ip6=$(curl $DFS_CURL_OPTIONS -sSL "https://api6.ipify.org")
elif [[ "$DFS_DDNS_IP6" == "http"* ]]; then
ip6=$(curl $DFS_CURL_OPTIONS -sSL "$DFS_DDNS_IP6")
else
ip6=$(ip -6 --brief addr show $DFS_DDNS_IP6 scope global primary | awk '{split($3,a,"/");print a[1]}' | head -n 1)
fi
if [[ -n "$DFS_DDNS_IP6" && -z "$ip6" ]]; then
fmt_fatal "failed getting ip6 address"
fi
# update dns
fmt_note "updating dns record for $hostname with ip4=$ip4 ip6=$ip6"
resp=$(curl $DFS_CURL_OPTIONS -sSL "$api_url/update-dns?hostname=$hostname&uuid=$uuid&ip4=$ip4&ip6=$ip6")
handle_resp "$resp"
}
print_help()
{
fmt_info "usage: $0 <beacon|ddns> [beacon_type] [beacon_meta]"
}
router()
{
case "$1" in
-h|--help)
print_help
;;
beacon)
post_beacon "$2" "$3"
;;
ddns)
update_dns
;;
*)
print_help
fmt_fatal "invalid argument"
;;
esac
}
router "${GOT_OPTS[@]}"