改用ddns接口更新数据,更改IP获取策略

- ipv4 先从本机获取,若没有合法地址,则使用dnspod接口填充IP
- ipv6 本机获取难度太大,且错误率高,改为直接从外部接口获取地址
This commit is contained in:
rehiy authored and GitHub committed 2022-03-11 09:54:06 +08:00
1 parent 7fb644430f
commit 909ca43215
1 file changed
+51 -123
+51 -123
View File
@@ -2,7 +2,7 @@
# #
############################################################# #############################################################
# AnripDdns v6.1.1 # AnripDdns v6.2.0
# #
# Dynamic DNS using DNSPod API # Dynamic DNS using DNSPod API
# #
@@ -14,9 +14,7 @@
# #
############################################################# #############################################################
# TokenID,Token export arToken
export arToken=""
# Get WAN IPv4 # Get WAN IPv4
@@ -42,20 +40,10 @@ arWanIp4() {
esac esac
if [ -z "$hostIp" ]; then if [ -z "$hostIp" ]; then
if type wget >/dev/null 2>&1; then
hostIp=$(wget -q -O- https://v4.myip.la)
else
hostIp=$(curl -s https://v4.myip.la)
fi
fi
if [ -z "$hostIp" ]; then
echo "arWanIp4 - Can't get ip address"
return 1 return 1
fi fi
if [ -z "$(echo $hostIp | grep -E '^[0-9\.]+$')" ]; then if [ -z "$(echo $hostIp | grep -E '^[0-9\.]+$')" ]; then
echo "arWanIp4 - Invalid ip address $hostIp"
return 1 return 1
fi fi
@@ -69,23 +57,10 @@ arWanIp6() {
local hostIp local hostIp
local lanIps="(^$)|(^::1$)|(^[fF][eE][8-9a-fA-F])" if type curl >/dev/null 2>&1; then
hostIp=$(curl -s https://v6.myip.la)
case $(uname) in else
'Linux') hostIp=$(wget -q -O- https://v6.myip.la)
hostIp=$(ip -o -6 addr list | grep -Ev '\s(docker|lo)' | awk '{print $4,substr($NF,0,length($NF)-3)}' | sed 's/fore/2592000/g' | sort -k 2 -n | cut -d/ -f1 | grep -Ev "$lanIps" | head -n 1)
;;
Darwin|FreeBSD)
hostIp=$(ifconfig | grep "inet6 " | awk '{print $2}' | grep -Ev "$lanIps" | head -n 1)
;;
esac
if [ -z "$hostIp" ]; then
if type wget >/dev/null 2>&1; then
hostIp=$(wget -q -O- https://v6.myip.la)
else
hostIp=$(curl -s https://v6.myip.la)
fi
fi fi
if [ -z "$hostIp" ]; then if [ -z "$hostIp" ]; then
@@ -107,82 +82,42 @@ arWanIp6() {
arDdnsApi() { arDdnsApi() {
local agent="AnripDdns/6.1.0(wang@rehiy.com)" local agent="AnripDdns/6.2.0(wang@rehiy.com)"
local apiurl="https://dnsapi.cn/${1:?'Info.Version'}" local dnsapi="https://dnsapi.cn/${1:?'Info.Version'}"
local params="login_token=$arToken&format=json&$2" local params="login_token=$arToken&format=json&lang=en&$2"
if type wget >/dev/null 2>&1; then if type curl >/dev/null 2>&1; then
wget -q -O- --no-check-certificate -U $agent --post-data $params $apiurl curl -s -A $agent -d $params $dnsapi
else else
curl -s -A $agent -d $params $apiurl wget -q -O- --no-check-certificate -U $agent --post-data $params $dnsapi
fi fi
} }
# Fetch Ids of Domain and Record # Fetch recordId
# Args: recordType domain subdomain # Args: domain subdomain recordType
arDdnsIds() { arDdnsLookup() {
local errMsg local errMsg
local domainId
local recordId local recordId
# Get Domain Id
domainId=$(arDdnsApi "Domain.Info" "domain=$2")
domainId=$(echo $domainId | sed 's/.*"id":"\([0-9]*\)".*/\1/')
if ! [ "$domainId" -gt 0 ] 2>/dev/null ;then
errMsg=$(echo $domainId | sed 's/.*"message":"\([^\"]*\)".*/\1/')
echo "arDdnsIds - $errMsg"
return 1
fi
# Get Record Id # Get Record Id
recordId=$(arDdnsApi "Record.List" "domain_id=$domainId&sub_domain=$3&record_type=$1") recordId=$(arDdnsApi "Record.List" "domain=$1&sub_domain=$2&record_type=$3")
recordId=$(echo $recordId | sed 's/.*"id":"\([0-9]*\)".*/\1/') recordId=$(echo $recordId | sed 's/.*"id":"\([0-9]*\)".*/\1/')
if ! [ "$recordId" -gt 0 ] 2>/dev/null ;then if ! [ "$recordId" -gt 0 ] 2>/dev/null ;then
errMsg=$(echo $recordId | sed 's/.*"message":"\([^\"]*\)".*/\1/') errMsg=$(echo $recordId | sed 's/.*"message":"\([^\"]*\)".*/\1/')
echo "arDdnsIds - $errMsg" echo "arDdnsLookup - $errMsg"
return 1 return 1
fi fi
echo $domainId $recordId echo $recordId
}
# Fetch Record Ip
# Args: domainId recordId
arDdnsRecordIp() {
local errMsg
local recordIp
# Get Record Ip
recordIp=$(arDdnsApi "Record.Info" "domain_id=$1&record_id=$2")
recordIp=$(echo $recordIp | sed 's/.*,"value":"\([0-9a-fA-F\.\:]*\)".*/\1/')
# Output Record Ip
case "$recordIp" in
[0-9a-fA-F]*)
echo $recordIp
return 0
;;
*)
errMsg=$(echo $recordIp | sed 's/.*"message":"\([^\"]*\)".*/\1/')
echo "arDdnsRecordIp - $errMsg"
return 1
;;
esac
} }
# Update Record Ip # Update Record Ip
# Args: domainId recordId subdomain hostIp recordType # Args: domain subdomain recordId recordType hostIp
arDdnsUpdate() { arDdnsUpdate() {
@@ -192,19 +127,19 @@ arDdnsUpdate() {
local recordIp local recordIp
local recordCd local recordCd
# update ip
if [ -z "$5" ]; then if [ -z "$5" ]; then
echo "arDdnsUpdate - Args number error" recordRs=$(arDdnsApi "Record.Ddns" "domain=$1&sub_domain=$2&record_id=$3&record_type=$4&record_line=%e9%bb%98%e8%ae%a4")
return 1 else
recordRs=$(arDdnsApi "Record.Ddns" "domain=$1&sub_domain=$2&record_id=$3&record_type=$4&value=$5&record_line=%e9%bb%98%e8%ae%a4")
fi fi
# Update Ip # parse result
recordRs=$(arDdnsApi "Record.Modify" "domain_id=$1&record_id=$2&sub_domain=$3&record_type=$5&value=$4&record_line=%e9%bb%98%e8%ae%a4")
recordIp=$(echo $recordRs | sed 's/.*,"value":"\([0-9a-fA-F\.\:]*\)".*/\1/')
recordCd=$(echo $recordRs | sed 's/.*{"code":"\([0-9]*\)".*/\1/') recordCd=$(echo $recordRs | sed 's/.*{"code":"\([0-9]*\)".*/\1/')
recordIp=$(echo $recordRs | sed 's/.*,"value":"\([0-9a-fA-F\.\:]*\)".*/\1/')
# Output Result if [ "$recordCd" = "1" ]; then
if [ "$recordIp" = "$4" ] && [ "$recordCd" = "1" ]; then echo "arDdnsUpdate - successful"
echo "arDdnsUpdate - success"
return 0 return 0
else else
errMsg=$(echo $recordRs | sed 's/.*,"message":"\([^"]*\)".*/\1/') errMsg=$(echo $recordRs | sed 's/.*,"message":"\([^"]*\)".*/\1/')
@@ -215,62 +150,55 @@ arDdnsUpdate() {
} }
# DDNS Check # DDNS Check
# Args: Main Sub # Args: domain subdomain [6|4]
arDdnsCheck() { arDdnsCheck() {
local errCode local errCode
local recordId
local recordType local recordType
local hostIp local hostIp
local ddnsIds local updateResult
local lastIp
local postRs
echo "Fetching Host Ip" echo "Fetching Host Ip"
if [ "$3" = "6" ]; then if [ "$3" = "6" ]; then
recordType=AAAA recordType=AAAA
hostIp=$(arWanIp6) hostIp=$(arWanIp6)
errCode=$?
echo "> Host Ip: $hostIp"
echo "> Record Type: $recordType"
if [ $errCode -ne 0 ]; then
return 1
fi
else else
recordType=A recordType=A
hostIp=$(arWanIp4) hostIp=$(arWanIp4)
if [ $? -ne 0 ]; then
echo "> Host Ip: Auto"
echo "> Record Type: $recordType"
else
echo "> Host Ip: $hostIp"
echo "> Record Type: $recordType"
fi
fi fi
echo "Fetching RecordId of $2.$1"
recordId=$(arDdnsLookup "$1" "$2" "$recordType")
errCode=$? errCode=$?
echo "> Host Ip: $hostIp" echo "> Record Id: $recordId"
echo "> Record Type: $recordType"
if [ $errCode -ne 0 ]; then if [ $errCode -ne 0 ]; then
return 1 return 1
fi fi
echo "Fetching Ids of $2.$1"
ddnsIds=$(arDdnsIds "$recordType" "$1" "$2")
errCode=$?
echo "> Domain Ids: $ddnsIds"
if [ $errCode -ne 0 ]; then
return 1
fi
echo "Checking Record for $2.$1"
lastIp=$(arDdnsRecordIp $ddnsIds)
errCode=$?
echo "> Last Ip: $lastIp"
if [ $errCode -ne 0 ]; then
return 1
fi
if [ "$lastIp" = "$hostIp" ]; then
echo "> Last Ip is the same as host Ip"
return 0
fi
echo "Updating Record for $2.$1" echo "Updating Record for $2.$1"
postRs=$(arDdnsUpdate $ddnsIds "$2" "$hostIp" "$recordType") updateResult=$(arDdnsUpdate "$1" "$2" "$recordId" "$recordType" "$hostIp")
errCode=$? errCode=$?
echo "> $postRs" echo "> $updateResult"
if [ $errCode -ne 0 ]; then if [ $errCode -ne 0 ]; then
return 1 return 1
fi fi