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

This commit is contained in:
xiongdian.me 2023-03-01 20:03:32 +08:00
parent 334622aa9f
commit c06c2389c9
3 changed files with 45 additions and 54 deletions

View File

@ -13,7 +13,7 @@ apost_beacon "sys.online"
# update dns # update dns
if [[ "$DFS_DDNS_ENABLE" == "1" ]]; then if [[ "$DFS_DDNS_ENABLE" == "1" ]]; then
fmt_info "updating dns ..." fmt_info "updating dns ..."
"$THIS_DIR/tools/logger.sh" ddns || (fmt_error "failed to update dns" && apost_beacon "dfs.ddns.fail") "$THIS_DIR/tools/frigg-client.sh" ddns || (fmt_error "failed to update dns" && apost_beacon "dfs.ddns.fail")
fi fi
# fetch origin # fetch origin
@ -27,7 +27,7 @@ fi
# get the specified commit id # get the specified commit id
case $DFS_UPDATE_CHANNEL in case $DFS_UPDATE_CHANNEL in
"main" ) DFS_COMMIT=$(curl -fsSL https://api.beardic.cn/get-var/dfs-commit-id) ;; "main" ) DFS_COMMIT=$(curl -m 10 -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) ;; "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) ;; "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" ;; * ) fmt_fatal "invalid update channel: $DFS_UPDATE_CHANNEL" ;;

View File

@ -165,7 +165,7 @@ post_log()
if [[ $# != 3 || -z "$1" || -z "$2" || -z "$3" ]]; then if [[ $# != 3 || -z "$1" || -z "$2" || -z "$3" ]]; then
fmt_fatal "usage: post_log <level> <section> <content>" fmt_fatal "usage: post_log <level> <section> <content>"
fi fi
"${DOTFILES}/tools/logger.sh" "log" "[$1][$2] $3" "${DOTFILES}/tools/frigg-client.sh" "log" "[$1][$2] $3"
} }
apost_log() apost_log()
@ -178,7 +178,7 @@ post_beacon()
if [[ $# < 1 || -z "$1" ]]; then if [[ $# < 1 || -z "$1" ]]; then
fmt_fatal "usage: post_beacon <beacon>" fmt_fatal "usage: post_beacon <beacon>"
fi fi
"${DOTFILES}/tools/logger.sh" "beacon" "$1" "$2" "${DOTFILES}/tools/frigg-client.sh" "beacon" "$1" "$2"
} }
apost_beacon() apost_beacon()

View File

@ -43,6 +43,24 @@ init_uuid()
fi 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() post_beacon()
{ {
local beacon_type=$1 local beacon_type=$1
@ -53,13 +71,8 @@ post_beacon()
if [[ -z "$beacon_type" ]]; then if [[ -z "$beacon_type" ]]; then
fmt_fatal "beacon type is required" fmt_fatal "beacon type is required"
fi fi
resp=$(curl -sSL -X POST -H "Content-Type: text/plain" -d "$meta" "https://api.beardic.cn/post-beacon?hostname=$hostname&beacon=$beacon_type") resp=$(curl -m 10 -sSL -X POST -H "Content-Type: text/plain" -d "$meta" "https://api.beardic.cn/post-beacon?hostname=$hostname&beacon=$beacon_type")
if grep -q "200" <<< "$resp"; then handle_resp "$resp"
echo $resp
else
echo $resp >&2
fmt_fatal "error posting beacon"
fi
} }
post_log() post_log()
@ -69,19 +82,8 @@ post_log()
fmt_fatal "log content is required" fmt_fatal "log content is required"
fi fi
init_uuid init_uuid
resp=$(curl -sSL -X POST -H "Content-Type: text/plain" -d "$log_content" "https://api.beardic.cn/post-log?hostname=$hostname&uuid=$uuid") resp=$(curl -m 10 -sSL -X POST -H "Content-Type: text/plain" -d "$log_content" "https://api.beardic.cn/post-log?hostname=$hostname&uuid=$uuid")
if grep -q "200" <<< "$resp"; then handle_resp "$resp"
echo $resp
elif grep -q "403" <<< "$resp"; then
echo $resp >&2
fmt_error "error posting log: 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 "error posting log"
fi
} }
update_dns() update_dns()
@ -89,64 +91,53 @@ update_dns()
if [[ -z "$DFS_DDNS_IP4$DFS_DDNS_IP6" ]]; then if [[ -z "$DFS_DDNS_IP4$DFS_DDNS_IP6" ]]; then
fmt_fatal "neither DFS_DDNS_IP4 nor DFS_DDNS_IP6 is configured" fmt_fatal "neither DFS_DDNS_IP4 nor DFS_DDNS_IP6 is configured"
fi 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 init_uuid
local ip4 local ip4
local ip6 local ip6
local api_url="https://api.beardic.cn" local api_url="https://api.beardic.cn"
# get ip4
if [[ -z "$DFS_DDNS_IP4" ]]; then if [[ -z "$DFS_DDNS_IP4" ]]; then
ip4="" ip4=""
elif [[ "$DFS_DDNS_IP4" == "auto" ]]; then elif [[ "$DFS_DDNS_IP4" == "auto" ]]; then
ip4="auto" ip4="auto"
elif [[ "$DFS_DDNS_IP4" == "api" ]]; then elif [[ "$DFS_DDNS_IP4" == "api" ]]; then
ip4=$(curl -sSL "https://api.ipify.org") ip4=$(curl -m 10 -sSL "https://api.ipify.org")
elif [[ "$DFS_DDNS_IP4" == "http"* ]]; then elif [[ "$DFS_DDNS_IP4" == "http"* ]]; then
ip4=$(curl -sSL "$DFS_DDNS_IP4") ip4=$(curl -m 10 -sSL "$DFS_DDNS_IP4")
if [[ -z "$ip4" ]]; then
fmt_fatal "unable to get ip4 address from $DFS_DDNS_IP4"
fi
else else
ip4=$(ip a show $DFS_DDNS_IP4 | grep inet | grep global | awk '/inet / {print $2}' | awk -F'[/]' '{print $1}') ip4=$(ip a show $DFS_DDNS_IP4 | grep inet | grep global | awk '/inet / {print $2}' | awk -F'[/]' '{print $1}')
if [[ -z "$ip4" ]]; then
fmt_fatal "unable to get ip4 address from $DFS_DDNS_IP4"
fi fi
if [[ -n "$DFS_DDNS_IP4" && -z "$ip4" ]]; then
fmt_fatal "failed getting ip4 address"
fi fi
# get ip6
if [[ -z "$DFS_DDNS_IP6" ]]; then if [[ -z "$DFS_DDNS_IP6" ]]; then
ip6="" ip6=""
elif [[ "$DFS_DDNS_IP6" == "auto" ]]; then elif [[ "$DFS_DDNS_IP6" == "auto" ]]; then
ip6="auto" ip6="auto"
api_url="https://api6.beardic.cn" api_url="https://api6.beardic.cn"
elif [[ "$DFS_DDNS_IP6" == "api" ]]; then elif [[ "$DFS_DDNS_IP6" == "api" ]]; then
ip6=$(curl -sSL "https://api6.ipify.org") ip6=$(curl -m 10 -sSL "https://api6.ipify.org")
elif [[ "$DFS_DDNS_IP6" == "http"* ]]; then elif [[ "$DFS_DDNS_IP6" == "http"* ]]; then
ip6=$(curl -sSL "$DFS_DDNS_IP6") ip6=$(curl -m 10 -sSL "$DFS_DDNS_IP6")
if [[ -z "$ip6" ]]; then
fmt_fatal "unable to get ip6 address from $DFS_DDNS_IP6"
fi
else else
ip6=$(ip a show $DFS_DDNS_IP6 | grep inet6 | grep global | awk '/inet6 / {print $2}' | awk -F'[/]' '{print $1}') ip6=$(ip a show $DFS_DDNS_IP6 | grep inet6 | grep global | awk '/inet6 / {print $2}' | awk -F'[/]' '{print $1}')
if [[ -z "$ip6" ]]; then
fmt_fatal "unable to get ip6 address from $DFS_DDNS_IP6"
fi fi
if [[ -n "$DFS_DDNS_IP6" && -z "$ip6" ]]; then
fmt_fatal "failed getting ip6 address"
fi fi
# update dns
fmt_note "updating dns record for $hostname with ip4=$ip4 ip6=$ip6" fmt_note "updating dns record for $hostname with ip4=$ip4 ip6=$ip6"
resp=$(curl -sSL "$api_url/update-dns?hostname=$hostname&uuid=$uuid&ip4=$ip4&ip6=$ip6") resp=$(curl -m 20 -sSL "$api_url/update-dns?hostname=$hostname&uuid=$uuid&ip4=$ip4&ip6=$ip6")
if grep -q "200" <<< "$resp"; then handle_resp "$resp"
echo $resp
elif grep -q "403" <<< "$resp"; then
echo $resp >&2
fmt_error "error updating dns record: 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 "error updating dns record"
fi
} }
print_help() print_help()
{ {
fmt_info "usage: $0 <beacon|log|ddns> <beacon_type|log_content>" fmt_info "usage: $0 <beacon|log|ddns> [beacon_type|log_content]"
} }
router() router()