diff --git a/README.md b/README.md index 45f7570..7aca990 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,11 @@ echo "arDdnsCheck test.org subdomain" >> ./ardnspod # 最近更新 +2022/12/24 + +- 支持指定网卡 +- 增加 arRequest 方法,实现 curl/wget 封装 + 2022/03/11 - 改用ddns接口更新数据 diff --git a/ardnspod b/ardnspod index c70d163..4378ca8 100644 --- a/ardnspod +++ b/ardnspod @@ -1,29 +1,22 @@ #!/bin/sh # -############################################################# -# AnripDdns v6.2.0 +################################################################### +# AnripDdns v6.3.0 # # Dynamic DNS using DNSPod API # # Author: Rehiy, https://github.com/rehiy -# https://www.anrip.com/?s=dnspod +# https://www.rehiy.com/?s=dnspod +# # Collaborators: ProfFan, https://github.com/ProfFan # # Usage: please refer to `ddnspod.sh` # -############################################################# +################################################################### params ## export arToken -# Define arLog to output to stderr, if not defined in ddnspod.sh - -if ! type arLog >/dev/null 2>&1; then - arLog() { - >&2 echo $@ - } -fi - # The url to be used for querying public ipv6 address. We set a default here. if [ -z "$arIp6QueryUrl" ]; then @@ -36,14 +29,82 @@ if [ -z "$arErrCodeUnchanged" ]; then arErrCodeUnchanged=0 # By default, report unchanged event as success fi -# Get IPv4 +# Define arLog to output to stderr, if not defined in ddnspod.sh + +if ! type arLog >/dev/null 2>&1; then + arLog() { + >&2 echo $@ + } +fi + +################################################################### http client ## + +# Use curl or wget open url +# Args: url postdata + +arRequest() { + + local url="$1" + local data="$2" + + local params="" + local agent="AnripDdns/6.3.0(wang@rehiy.com)" + + if type curl >/dev/null 2>&1; then + if echo $url | grep -q https; then + params="$params -k" + fi + if [ -n "$data" ]; then + params="$params -d $data" + fi + curl -s -A "$agent" $params $url + return $? + fi + + if type wget >/dev/null 2>&1; then + if echo $url | grep -q https; then + params="$params --no-check-certificate" + fi + if [ -n "$data" ]; then + params="$params --post-data $data" + fi + wget -qO- -U "$agent" $params $url + return $? + fi + + return 1 + +} + +################################################################### ipv4 util ## + +# Get regular expression for IPv4 LAN addresses + +arLanIp4() { + + 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}$)" + lanIps="$lanIps|(^100\.(6[4-9]|[7-9][0-9])\.[0-9]{1,3}\.[0-9]{1,3}$)" # 100.64.x.x - 100.99.x.x + lanIps="$lanIps|(^100\.1([0-1][0-9]|2[0-7])\.[0-9]{1,3}\.[0-9]{1,3}$)" # 100.100.x.x - 100.127.x.x + + echo $lanIps + +} + +# Get IPv4 by ip route or network arWanIp4() { local hostIp - local lanIps=$(arLanIp4) + local agent="AnripDdns/6.2.0(wang@rehiy.com)" + case $(uname) in 'Linux') hostIp=$(ip -o -4 route get 100.64.0.1 | grep -oE 'src [0-9\.]+' | awk '{print $2}' | grep -Ev "$lanIps") @@ -66,51 +127,12 @@ arWanIp4() { } -# Get IPv6 - -arWanIp6() { - - local hostIp - - local lanIps=$(arLanIp6) - - case $(uname) in - 'Linux') - hostIp=$(ip -o -6 route get 100::1 | grep -oE 'src [0-9a-fA-F:]+' | awk '{print $2}' | grep -Ev "$lanIps") - ;; - esac - - if [ -z "$hostIp" ]; then - if type curl >/dev/null 2>&1; then - hostIp=$(curl -6 -s $arIp6QueryUrl) - elif ! wget --help 2>&1 | grep -qs BusyBox; then - hostIp=$(wget -6 -q -O- $arIp6QueryUrl) - else - hostIp=$(wget -q -O- $arIp6QueryUrl) - fi - fi - - if [ -z "$hostIp" ]; then - arLog "> arWanIp6 - Can't get ip address" - return 1 - fi - - if [ -z "$(echo $hostIp | grep -E '^[0-9a-fA-F:]+$')" ]; then - arLog "> arWanIp6 - Invalid ip address" - return 1 - fi - - echo $hostIp - -} - # Get IPv4 from a specific interface # Args: interface arDevIp4() { local hostIp - local lanIps=$(arLanIp4) case $(uname) in @@ -133,13 +155,59 @@ arDevIp4() { } +################################################################### ipv6 util ## + +# Get regular expression for IPv6 LAN addresses + +arLanIp6() { + + local lanIps="(^$)" + + lanIps="$lanIps|(^::1$)" + lanIps="$lanIps|(^[fF][eE][8-9a-fA-F])" # Link-local addresses + lanIps="$lanIps|(^[fF][cdCD])" # Unique local addresses + + echo $lanIps + +} + +# Get IPv6 by ip route or network + +arWanIp6() { + + local hostIp + local lanIps=$(arLanIp6) + + case $(uname) in + 'Linux') + hostIp=$(ip -o -6 route get 100::1 | grep -oE 'src [0-9a-fA-F:]+' | awk '{print $2}' | grep -Ev "$lanIps") + ;; + esac + + if [ -z "$hostIp" ]; then + hostIp=$(arRequest $arIp6QueryUrl) + fi + + if [ -z "$hostIp" ]; then + arLog "> arWanIp6 - Can't get ip address" + return 1 + fi + + if [ -z "$(echo $hostIp | grep -E '^[0-9a-fA-F:]+$')" ]; then + arLog "> arWanIp6 - Invalid ip address" + return 1 + fi + + echo $hostIp + +} + # Get IPv6 from a specific interface # Args: interface arDevIp6() { local hostIp - local lanIps=$(arLanIp6) case $(uname) in @@ -176,51 +244,17 @@ arDevIp6() { } -# Get regular expression for IPv4 LAN addresses - -arLanIp4() { - 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}$)" - lanIps="$lanIps|(^100\.(6[4-9]|[7-9][0-9])\.[0-9]{1,3}\.[0-9]{1,3}$)" # 100.64.x.x - 100.99.x.x - lanIps="$lanIps|(^100\.1([0-1][0-9]|2[0-7])\.[0-9]{1,3}\.[0-9]{1,3}$)" # 100.100.x.x - 100.127.x.x - - echo $lanIps -} - -# Get regular expression for IPv6 LAN addresses - -arLanIp6() { - local lanIps="(^$)" - - lanIps="$lanIps|(^::1$)" - lanIps="$lanIps|(^[fF][eE][8-9a-fA-F])" # Link-local addresses - lanIps="$lanIps|(^[fF][cdCD])" # Unique local addresses - - echo $lanIps -} +################################################################### dnspod api ## # Dnspod Bridge # Args: interface data arDdnsApi() { - local agent="AnripDdns/6.2.0(wang@rehiy.com)" - local dnsapi="https://dnsapi.cn/${1:?'Info.Version'}" local params="login_token=$arToken&format=json&lang=en&$2" - if type curl >/dev/null 2>&1; then - curl -4 -s -A $agent -d $params $dnsapi - elif ! wget --help 2>&1 | grep -qs BusyBox; then - wget -4 -q -O- --no-check-certificate -U $agent --post-data $params $dnsapi - else - wget -q -O- --no-check-certificate -U $agent --post-data $params $dnsapi - fi + arRequest "$dnsapi" "$params" } @@ -253,11 +287,12 @@ arDdnsUpdate() { local errMsg - local lastRecordIp local recordRs local recordCd local recordIp + local lastRecordIp + # if code for unchanged event is specified to be different from updated event, we fetch the last record ip if [ $arErrCodeUnchanged -ne 0 ]; then recordRs=$(arDdnsApi "Record.Info" "domain=$1&record_id=$3") @@ -304,18 +339,20 @@ arDdnsUpdate() { } +################################################################### task hub ## + # DDNS Check # Args: domain subdomain [6|4] interface arDdnsCheck() { + local errCode + local hostIp local recordId local recordType - local errCode - arLog "=== Check $2.$1 ===" arLog "Fetching Host Ip" @@ -360,3 +397,5 @@ arDdnsCheck() { arDdnsUpdate "$1" "$2" "$recordId" "$recordType" "$hostIp" } + +################################################################### end ##