From 1c661cd7d789e17184f8e283b8ad819d9928af7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=A5=E6=B5=B7?= Date: Thu, 2 Jan 2020 01:30:17 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=82=E9=85=8D=E6=96=B0=E7=89=88API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 51 ++++++------ ardnspod | 182 +++++++++++++++++++++++++++++++++++++++++ ddnspod.sh | 205 ++--------------------------------------------- dns.conf.example | 10 --- 4 files changed, 214 insertions(+), 234 deletions(-) create mode 100644 ardnspod delete mode 100644 dns.conf.example diff --git a/README.md b/README.md index e389271..69be9e6 100644 --- a/README.md +++ b/README.md @@ -1,46 +1,45 @@ # ArDNSPod -基于DNSPod用户API实现的纯Shell动态域名客户端,适配网卡地址。 +基于DNSPod用户API实现的纯Shell动态域名客户端,优先适配网卡地址,无法获得合法外网地址则使用外部接口获取IP地址 -# Usage +# 使用方法 -复制`dns.conf.example`到同一目录下的`dns.conf`并根据你的配置修改即可。 +- 修改`ddnspod.sh`中的`/your_real_path/ardnspod`为真实路径 -执行时直接运行`ddnspod.sh`,支持cron任务。 +- 运行`/your_real_path/ddnspod.sh`执行更新,_支持添加为cron任务_ -配置文件格式: -``` -# 安全起见,不推荐使用密码认证 -# arMail="test@gmail.com" -# arPass="123" +### 小提示 -# 推荐使用Token认证 -# 按`TokenID,Token`格式填写 -arToken="12345,7676f344eaeaea9074c123451234512d" - -# 每行一个域名 -arDdnsCheck "test.org" "subdomain" -``` +- 如需单文件运行,将`ddnspod.sh`中的配置项添加到`ardnspod`底部,直接运行`ardnspod`即可 # 最近更新 +2020/1/1 + +- 适配新版API(2019-11-26) +- 当`wget`不存在时,尝试使用`curl`提交 +- 由于`readlink`不可靠,更改为手动设置路径 +- 当无法从本地网卡获得外网ip时,尝试从外部api获取 + 2015/2/24 -- 增加token鉴权方式 (by wbchn) + +- 增加token鉴权方式 (by wbchn) 2015/7/7 -- 使用D+服务获取域名解析 + +- 使用D+服务获取域名解析 2016/2/25 -- 增加配置文件,分离脚本与配置,适配内网。 -- 加入Mac支持 -- sed脚本POSIX化,可跨平台 + +- 增加配置文件,分离脚本与配置,适配内网。 +- 加入Mac支持 +- sed脚本POSIX化,可跨平台 2016/3/23 -- 进一步POSIX化,支持Mac和大部分Linux发行版 -- 更改配置文件格式 -# Credit +- 进一步POSIX化,支持Mac和大部分Linux发行版 +- 更改配置文件格式 -Original: anrip +# 共同维护者 -This version maintained by ProfFan +ProfFan, diff --git a/ardnspod b/ardnspod new file mode 100644 index 0000000..7382b66 --- /dev/null +++ b/ardnspod @@ -0,0 +1,182 @@ +#!/bin/sh +# + +############################################################# +# AnripDdns v6.0.0 +# +# Dynamic DNS using DNSPod API +# +# Author: anrip, www.anrip.com/post/872 +# Collaborators: ProfFan, https://github.com/ProfFan +# +# Usage: /your_real_path/ddnspod.sh +# +############################################################# + +# TokenID,Token + +arToken="" + +# Get WAN-IP + +arWanIp() { + + local hostIp + + local lanIps="^$" + + lanIps="$lanIps|(^10\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$)" + lanIps="$lanIps|(^127\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$)" + lanIps="$lanIps|(^169\.254\.[0-9]{1,3}\.[0-9]{1,3}$)" + lanIps="$lanIps|(^172\.(1[6-9]|2[0-9]|3[0-1])\.[0-9]{1,3}\.[0-9]{1,3}$)" + lanIps="$lanIps|(^192\.168\.[0-9]{1,3}\.[0-9]{1,3}$)" + + case $(uname) in + 'Linux') + hostIp=$(ip -o -4 addr list | grep -Ev '\s(docker|lo)' | awk '{print $4}' | cut -d/ -f1 | grep -Ev "$lanIps") + ;; + 'Darwin') + hostIp=$(ifconfig | grep "inet " | grep -v 127.0.0.1 | awk '{print $2}' | grep -Ev "$lanIps") + ;; + esac + + if [ -z "$hostIp" ]; then + if type wget >/dev/null 2>&1; then + hostIp=$(wget --quiet --output-document=- http://members.3322.org/dyndns/getip) + else + hostIp=$(curl -s http://members.3322.org/dyndns/getip) + fi + fi + + echo $hostIp + +} + +# Dnspod Bridge +# Arg: type data + +arDdnsApi() { + + local agent="AnripDdns/6.0.0(mail@anrip.com)" + + local apiurl="https://dnsapi.cn/${1:?'Info.Version'}" + local params="login_token=$arToken&format=json&$2" + + if type wget >/dev/null 2>&1; then + wget --quiet --no-check-certificate --output-document=- --user-agent=$agent --post-data $params $apiurl + else + curl -s -A $agent -d $params $apiurl + fi + +} + +# Fetch Domain Ip +# Arg: domain + +arDdnsInfo() { + + local domainId + local recordId + local recordIp + + # Get domain ID + domainId=$(arDdnsApi "Domain.Info" "domain=$1") + domainId=$(echo $domainId | sed 's/.*"id":"\([0-9]*\)".*/\1/') + + # Get Record ID + recordId=$(arDdnsApi "Record.List" "domain_id=$domainId&sub_domain=$2") + recordId=$(echo $recordId | sed 's/.*"id":"\([0-9]*\)".*/\1/') + + # Last IP + recordIp=$(arDdnsApi "Record.Info" "domain_id=$domainId&record_id=$recordId") + recordIp=$(echo $recordIp | sed 's/.*,"value":"\([0-9\.]*\)".*/\1/') + + # Output IP + case "$recordIp" in + [1-9][0-9]*) + echo $recordIp + return 0 + ;; + *) + echo "Get Record Info Failed!" + return 1 + ;; + esac + +} + +# Update Domain Ip +# Arg: main-domain sub-domain + +arDdnsUpdate() { + + local domainId + local recordId + local recordRs + local recordIp + local recordCd + + local hostIp=$(arWanIp) + + # Get domain ID + domainId=$(arDdnsApi "Domain.Info" "domain=$1") + domainId=$(echo $domainId | sed 's/.*"id":"\([0-9]*\)".*/\1/') + + # Get Record ID + recordId=$(arDdnsApi "Record.List" "domain_id=$domainId&sub_domain=$2") + recordId=$(echo $recordId | sed 's/.*"id":"\([0-9]*\)".*/\1/') + + # Update IP + recordRs=$(arDdnsApi "Record.Ddns" "domain_id=$domainId&record_id=$recordId&sub_domain=$2&record_type=A&value=$hostIp&record_line=默认") + recordIp=$(echo $recordRs | sed 's/.*,"value":"\([0-9\.]*\)".*/\1/') + recordCd=$(echo $recordRs | sed 's/.*{"code":"\([0-9]*\)".*/\1/') + + # Output IP + if [ "$recordIp" = "$hostIp" ]; then + if [ "$recordCd" = "1" ]; then + echo $recordIp + return 0 + fi + # Echo error message + echo $recordRs | sed 's/.*,"message":"\([^"]*\)".*/\1/' + return 1 + else + echo "Update Failed! Please check your network." + return 1 + fi + +} + +# DDNS Check +# Arg: Main Sub +arDdnsCheck() { + + local postRs + local lastIP + + local hostIp=$(arWanIp) + + echo "Updating Domain: $2.$1" + echo "Host Ip: $hostIp" + + lastIP=$(arDdnsInfo "$1" "$2") + if [ $? -eq 0 ]; then + echo "lastIP: $lastIP" + if [ "$lastIP" != "$hostIp" ]; then + postRs=$(arDdnsUpdate "$1" "$2") + if [ $? -eq 0 ]; then + echo "postRs: $postRs" + return 0 + else + echo "$postRs" + return 1 + fi + fi + echo "Last IP is the same as current IP!" + return 1 + fi + + echo "$lastIP" + return 1 + +} diff --git a/ddnspod.sh b/ddnspod.sh index c9dedfb..21e8f0e 100755 --- a/ddnspod.sh +++ b/ddnspod.sh @@ -1,203 +1,12 @@ #!/bin/sh # -################################################# -# AnripDdns v5.09 -# Dynamic DNS using DNSPod API -# Original by anrip, www.anrip.com/post/872 -# Edited by ProfFan -################################################# +# Import ardnspod functions +. /your_real_path/ardnspod -# Global Variables +# Combine your token ID and token together as follows +arToken="12345,7676f344eaeaea9074c123451234512d" -# Token-based Authentication -arToken="" - -# Account-based Authentication -arMail="" -arPass="" - -# Get WAN IP - -case $(uname) in - 'Linux') - arIpAddress() { - local extip - extip=$(ip -o -4 addr list | grep -Ev '\s(docker|lo)' | awk '{print $4}' | cut -d/ -f1 | grep -Ev '(^127\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$)|(^10\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$)|(^172\.1[6-9]{1}[0-9]{0,1}\.[0-9]{1,3}\.[0-9]{1,3}$)|(^172\.2[0-9]{1}[0-9]{0,1}\.[0-9]{1,3}\.[0-9]{1,3}$)|(^172\.3[0-1]{1}[0-9]{0,1}\.[0-9]{1,3}\.[0-9]{1,3}$)|(^192\.168\.[0-9]{1,3}\.[0-9]{1,3}$)') - if [ -z "$extip" ]; then - extip=$(ip -o -4 addr list | grep -Ev '\s(docker|lo)' | awk '{print $4}' | cut -d/ -f1 ) - fi - echo $extip - } - ;; - 'Darwin') - arIpAddress() { - ifconfig | grep "inet " | grep -v 127.0.0.1 | awk '{print $2}' - } - ;; - *) - echo 'Unsupported OS' - exit 2 - ;; -esac - -# Get Domain IP -# Arg: domain - -arDdnsInfo() { - - local domainID recordID recordIP - - # Get domain ID - domainID=$(arApiPost "Domain.Info" "domain=$1") - domainID=$(echo $domainID | sed 's/.*"id":"\([0-9]*\)".*/\1/') - - # Get Record ID - recordID=$(arApiPost "Record.List" "domain_id=$domainID&sub_domain=$2") - recordID=$(echo $recordID | sed 's/.*"id":"\([0-9]*\)".*/\1/') - - # Last IP - recordIP=$(arApiPost "Record.Info" "domain_id=$domainID&record_id=$recordID") - recordIP=$(echo $recordIP | sed 's/.*,"value":"\([0-9\.]*\)".*/\1/') - - # Output IP - case "$recordIP" in - [1-9][0-9]*) - echo $recordIP - return 0 - ;; - *) - echo "Get Record Info Failed!" - return 1 - ;; - esac -} - -# Get data -# Arg: type data - -arApiPost() { - - local agent="AnripDdns/5.07(mail@anrip.com)" - local inter="https://dnsapi.cn/${1:?'Info.Version'}" - - if [ -n "$arToken" ]; then - local param="login_token=$arToken&format=json&$2" - else - local param="login_email=$arMail&login_password=$arPass&format=json&$2" - fi - - wget --quiet --no-check-certificate --output-document=- --user-agent=$agent --post-data $param $inter - -} - -# Update -# Arg: main domain sub domain - -arDdnsUpdate() { - local domainID recordID recordRS recordCD recordIP myIP - # Get domain ID - domainID=$(arApiPost "Domain.Info" "domain=$1") - domainID=$(echo $domainID | sed 's/.*"id":"\([0-9]*\)".*/\1/') - - # Get Record ID - recordID=$(arApiPost "Record.List" "domain_id=$domainID&sub_domain=$2") - recordID=$(echo $recordID | sed 's/.*"id":"\([0-9]*\)".*/\1/') - - # Update IP - myIP=$(arIpAddress) - recordRS=$(arApiPost "Record.Ddns" "domain_id=$domainID&record_id=$recordID&sub_domain=$2&record_type=A&value=$myIP&record_line=默认") - recordCD=$(echo $recordRS | sed 's/.*{"code":"\([0-9]*\)".*/\1/') - recordIP=$(echo $recordRS | sed 's/.*,"value":"\([0-9\.]*\)".*/\1/') - - # Output IP - if [ "$recordIP" = "$myIP" ]; then - if [ "$recordCD" = "1" ]; then - echo $recordIP - return 0 - fi - # Echo error message - echo $recordRS | sed 's/.*,"message":"\([^"]*\)".*/\1/' - return 1 - else - echo "Update Failed! Please check your network." - return 1 - fi -} - -# DDNS Check -# Arg: Main Sub -arDdnsCheck() { - local postRS - local lastIP - local hostIP=$(arIpAddress) - echo "Updating Domain: $2.$1" - echo "hostIP: $hostIP" - lastIP=$(arDdnsInfo "$1" "$2") - if [ $? -eq 0 ]; then - echo "lastIP: $lastIP" - if [ "$lastIP" != "$hostIP" ]; then - postRS=$(arDdnsUpdate "$1" "$2") - if [ $? -eq 0 ]; then - echo "postRS: $postRS" - return 0 - else - echo "$postRS" - return 1 - fi - fi - echo "Last IP is the same as current IP!" - return 1 - fi - echo "$lastIP" - return 1 -} - -# Get script dir -# See: http://stackoverflow.com/a/29835459/4449544 -# Execute the function in a *subshell* to localize variables and the effect of `cd`. - -rreadlink() { - - target=$1 fname= targetDir= CDPATH= - - { \unalias command; \unset -f command; } >/dev/null 2>&1 - [ -n "$ZSH_VERSION" ] && options[POSIX_BUILTINS]=on # make zsh find *builtins* with `command` too. - - while :; do # Resolve potential symlinks until the ultimate target is found. - [ -L "$target" ] || [ -e "$target" ] || { command printf '%s\n' "ERROR: '$target' does not exist." >&2; return 1; } - command cd "$(command dirname -- "$target")" # Change to target dir; necessary for correct resolution of target path. - fname=$(command basename -- "$target") # Extract filename. - [ "$fname" = '/' ] && fname='' # !! curiously, `basename /` returns '/' - if [ -L "$fname" ]; then - # Extract [next] target path, which may be defined - # *relative* to the symlink's own directory. - # Note: We parse `ls -l` output to find the symlink target - # which is the only POSIX-compliant, albeit somewhat fragile, way. - target=$(command ls -l "$fname") - target=${target#* -> } - continue # Resolve [next] symlink target. - fi - break # Ultimate target reached. - done - - targetDir=$(command pwd -P) # Get canonical dir. path - # Output the ultimate target's canonical path. - # Note that we manually resolve paths ending in /. and /.. to make sure we have a normalized path. - if [ "$fname" = '.' ]; then - command printf '%s\n' "${targetDir%/}" - elif [ "$fname" = '..' ]; then - # Caveat: something like /var/.. will resolve to /private (assuming /var@ -> /private/var), i.e. the '..' is applied - # AFTER canonicalization. - command printf '%s\n' "$(command dirname -- "${targetDir}")" - else - command printf '%s\n' "${targetDir%/}/$fname" - fi - -} - -# Load config file - -DIR=$(dirname -- "$(rreadlink "$0")") - -. $DIR/dns.conf +# Place each domain you want to check as follows +# you can have multiple arDdnsCheck blocks +arDdnsCheck "test.org" "subdomain" diff --git a/dns.conf.example b/dns.conf.example deleted file mode 100644 index 0164150..0000000 --- a/dns.conf.example +++ /dev/null @@ -1,10 +0,0 @@ -# For security reasons, it is recommended that you use token-based auth instead -# arMail="test@gmail.com" -# arPass="123" - -# Combine your token ID and token together as follows -arToken="12345,7676f344eaeaea9074c123451234512d" - -# Place each domain you want to check as follows -# you can have multiple arDdnsCheck blocks -arDdnsCheck "test.org" "subdomain"