Skip to content

Instantly share code, notes, and snippets.

@lyc8503
Last active February 1, 2023 06:15
Show Gist options
  • Save lyc8503/ae3d0b1a394ef681cf412251a1504121 to your computer and use it in GitHub Desktop.
Save lyc8503/ae3d0b1a394ef681cf412251a1504121 to your computer and use it in GitHub Desktop.
aliddns_ak="xxx"
aliddns_sk="xxx"
domain="xxx.example.com"
timestamp=$(date -u "+%Y-%m-%dT%H%%3A%M%%3A%SZ")
ip=$(ip route get 2001::8888 | sed -n '/src/{s/.*src *\([^ ]*\).*/\1/p;q}')
urlencode() {
# urlencode <string>
out=""
while read -n1 c
do
case $c in
[a-zA-Z0-9._-]) out="$out$c" ;;
*) out="$out$(printf '%%%02X' "'$c")" ;;
esac
done
echo -n "$out"
}
enc() {
echo -n "$1" | urlencode
}
send_request() {
local hash=$(echo -n "GET&%2F&$(enc "$1")" | openssl dgst -sha1 -hmac "$aliddns_sk&" -binary | openssl base64)
curl -s "https://alidns.aliyuncs.com/?$1&Signature=$(enc "$hash")"
}
DescribeSubDomainRecords() {
send_request "AccessKeyId=$aliddns_ak&Action=DescribeSubDomainRecords&Format=json&SignatureMethod=HMAC-SHA1&SignatureNonce=$timestamp&SignatureVersion=1.0&SubDomain=$1&Timestamp=$timestamp&Version=2015-01-09"
}
UpdateDomainRecord() {
# args: RecordID RR TTL Type Value
send_request "AccessKeyId=$aliddns_ak&Action=UpdateDomainRecord&Format=json&RR=$2&RecordId=$1&SignatureMethod=HMAC-SHA1&SignatureNonce=$timestamp&SignatureVersion=1.0&TTL=$3&Timestamp=$timestamp&Type=$4&Value=$5&Version=2015-01-09"
}
PushMsg() {
echo "Push: $1"
curl -s --get --data-urlencode "msg=$1" "https://server.lyc8503.site/wepush?key=wepushkey"
}
records=$(DescribeSubDomainRecords "$domain")
echo "List subdomain:"
echo "$records" | jq
if [ $(echo "$records" | jq ".TotalCount") != "1" ] ; then
echo "Record count != 1, please make sure there's EXACTLY one record, exiting..."
exit 1
fi
remote_ip=$(echo "$records" | jq -r ".DomainRecords.Record[0].Value")
echo "Local ip: $ip"
echo "Remote ip: $remote_ip"
if [ "$remote_ip" == "$ip" ] ; then
echo "No ip change, pass."
exit 0
fi
target_record=$(echo "$records" | jq -r ".DomainRecords.Record[0]")
result=$(UpdateDomainRecord $(echo "$target_record" | jq -r ".RecordId") $(echo "$target_record" | jq -r ".RR") $(echo "$target_record" | jq -r ".TTL") $(echo "$target_record" | jq -r ".Type") $(enc "$ip"))
echo "Result:"
echo "$result" | jq
if [ $(echo "$result" | jq -r ".RecordId") != $(echo "$target_record" | jq -r ".RecordId") ] ; then
PushMsg "[AliDDNS] 域名 $domain IP 更新**失败**: $result"
exit 1
fi
PushMsg "[AliDDNS] 域名 $domain IP 更新成功: $ip"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment