forked from rehiy/dnspod-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ardnspod
195 lines (152 loc) · 5.04 KB
/
ardnspod
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/bin/sh
#
#############################################################
# AnripDdns v6.0.0
#
# Dynamic DNS using DNSPod API
#
# Author: anrip<[email protected]>, www.anrip.com/post/872
# Collaborators: ProfFan, https://github.com/ProfFan
#
# Usage: please refer to `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")
# arm7l K3 官改获取外网IP方式
pppoename=`/sbin/ifconfig |grep ppp |awk '{print $1}'`
/sbin/ifconfig $pppoename | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}' >'/tmp/gge-get-newip'
hostIp=$(cat /tmp/gge-get-newip)
;;
'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([email protected])"
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")
#echo "arDdnsApi Domain.Info domain=$1 $domainId"
domainId=$(echo $domainId | sed 's/.*"id":"\([0-9]*\)".*/\1/')
#echo "domainId== $domainId"
# Get Record ID
# 20200410 增加 record_type=A 的过滤条件
recordId=$(arDdnsApi "Record.List" "domain_id=$domainId&sub_domain=$2&record_type=A")
recordId=$(echo $recordId | sed 's/.*"id":"\([0-9]*\)".*/\1/')
# echo "recordId== $recordId"
# Last IP
recordIp=$(arDdnsApi "Record.Info" "domain_id=$domainId&record_id=$recordId")
recordIp=$(echo $recordIp | sed 's/.*,"value":"\([0-9\.]*\)".*/\1/')
# echo "recordIp== $recordIp"
# Output IP
case "$recordIp" in
[1-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
# 20200410 增加 record_type=A 的过滤条件
recordId=$(arDdnsApi "Record.List" "domain_id=$domainId&sub_domain=$2&record_type=A")
recordId=$(echo $recordId | sed 's/.*"id":"\([0-9]*\)".*/\1/')
# Update DDNS IP
#recordRs=$(arDdnsApi "Record.Ddns" "domain_id=$domainId&record_id=$recordId&sub_domain=$2&record_type=A&value=$hostIp&record_line=默认")
# Update domain Ip
recordRs=$(arDdnsApi "Record.Modify" "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
}