forked from h46incon/AliDDNSBash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathali_ddns.sh
executable file
·263 lines (213 loc) · 5.82 KB
/
ali_ddns.sh
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#!/usr/bin/env bash
#By h46incon, wuweixing
#Dependences: curl, openssl-util, tr, sort
#env: MY_ALIYUN_KEY, MY_ALIYUN_SECRET, DOMAIN, DomainRR
## ----- Log level -----
_DEBUG_=false
_LOG_=true
_ERR_=true
## ----- Setting -----
AccessKeyId=${MY_ALIYUN_KEY}
AccessKeySec=${MY_ALIYUN_SECRET}
# DomainRR, use "@" to set top level domain
DomainRR=${1:-${DomainRR}}
DomainName=${DOMAIN}
DomainType=${DOMAIN_TYPE:-A}
DomainRecordId=""
# The server address of ALi API
ALiServerAddr="alidns.aliyuncs.com"
# A url provided by a third-party to echo the public IP of host
MyIPEchoUrl="http://whatismyip.akamai.com/"
if [ "${DomainType}" == "AAAA" ]; then
MyIPEchoUrl="http://ipv6.whatismyip.akamai.com/"
fi
## ===== private =====
## ----- global var -----
# g_pkey_$i # param keys
# g_pval_$key # param values
g_pn=0 # number of params
_func_ret=""
## ----- Base Util -----
_debug() { ${_DEBUG_} && echo $(date "+%F %T") "DEBUG $*"; }
_log() { ${_LOG_} && echo $(date "+%F %T") "INFO $*"; }
_err() { ${_ERR_} && echo $(date "+%F %T") "ERROR $*" >&2; }
reset_func_ret() {
_func_ret=""
}
## ----- params -----
# @Param1: Key
# @Param2: Value
put_param() {
eval g_pkey_${g_pn}=$1
eval g_pval_$1=$2
g_pn=$((g_pn + 1))
}
reset_param() {
g_pn=0
}
# This function will init all public params EXCLUDE "Signature"
put_params_public() {
put_param "Format" "JSON"
put_param "Version" "2015-01-09"
put_param "AccessKeyId" "${AccessKeyId}"
put_param "SignatureMethod" "HMAC-SHA1"
put_param "SignatureVersion" "1.0"
rand_num=$(openssl rand -hex 16)
# time stamp
local time_utc=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
_debug time_stamp: ${time_utc}
put_param "Timestamp" "${time_utc}"
# random number
_debug rand_num: ${rand_num}
put_param "SignatureNonce" "${rand_num}"
}
# @Param1: New IP address
put_params_UpdateDomainRecord() {
put_param "Action" "UpdateDomainRecord"
put_param "RR" "${DomainRR}"
put_param "RecordId" "${DomainRecordId}"
put_param "Type" "${DomainType}"
put_param "Value" "${1}"
}
put_params_DescribeDomainRecordInfo() {
put_param "Action" "DescribeDomainRecords"
put_param "DomainName" "${DomainName}"
put_param "RRKeyWord" "${DomainRR}"
put_param "Type" "${DomainType}"
}
pack_params() {
reset_func_ret
local ret=""
local key key_enc val val_enc
local i=0
while [ $i -lt ${g_pn} ]; do
eval key="\$g_pkey_${i}"
eval val="\$g_pval_${key}"
rawurl_encode "${key}"
key_enc=${_func_ret}
rawurl_encode "${val}"
val_enc=${_func_ret}
ret="${ret}${key_enc}=${val_enc}&"
i=$((++i))
done
#delete last "&"
_func_ret=${ret%"&"}
}
# ----- Other utils -----
get_my_ip() {
reset_func_ret
local my_ip
if [ ${INTRANET} ]; then
my_ip=$(ip addr | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1')
else
my_ip=$(curl ${MyIPEchoUrl} --silent --connect-timeout 10)
fi
# local my_ip=$(ipconfig getifaddr en0)
# local my_ip=$(ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1')
#echo ${my_ip}
_func_ret=${my_ip}
}
get_domain_ip() {
put_params_public
put_params_DescribeDomainRecordInfo
send_request
local result=${_func_ret}
reset_param
reset_func_ret
_func_ret=$(echo ${result} |grep -Eo '"Value":"[0-9a-f:.]+"' |grep -Eo '[0-9a-f:.]{5,}')
DomainRecordId=$(echo ${result} |grep -Eo '"RecordId":"[0-9]+"' |grep -Eo '[0-9]{5,}')
if [ -z ${DomainRecordId} ]; then
_err get domain ip failed ${result}
exit 1
fi
_debug DomainRecordId: ${DomainRecordId}
}
# @Param1: Raw url to be encoded
rawurl_encode() {
reset_func_ret
local string="${1}"
local strlen=${#string}
local encoded=""
local pos c o
pos=0
while [ ${pos} -lt ${strlen} ]; do
c=${string:$pos:1}
case "$c" in
[-_.~a-zA-Z0-9] ) o="${c}" ;;
* ) o=$(printf "%%%02X" "'$c") ;;
esac
encoded="${encoded}${o}"
pos=$(($pos + 1))
done
_func_ret="${encoded}"
}
calc_signature() {
reset_func_ret
local sorted_key=$(
i=0
while [ $i -lt ${g_pn} ]; do
eval key="\$g_pkey_$i"
echo "${key}"
i=$((++i))
done | LC_ALL=C sort
)
local query_str=""
for key in ${sorted_key}; do
eval val="\$g_pval_${key}"
rawurl_encode "${key}"
key_enc=${_func_ret}
rawurl_encode "${val}"
val_enc=${_func_ret}
query_str="${query_str}${key_enc}=${val_enc}&"
done
# delete last "&"
query_str=${query_str%"&"}
_debug Query String: ${query_str}
# encode
rawurl_encode "${query_str}"
local encoded_str=${_func_ret}
local str_to_signed="GET&%2F&"${encoded_str}
_debug String to Signed: ${str_to_signed}
local key_sign="${AccessKeySec}&"
_func_ret=$(/bin/echo -n ${str_to_signed} | openssl dgst -binary -sha1 -hmac ${key_sign} | openssl enc -base64)
}
send_request() {
reset_func_ret
# put signature
calc_signature
local signature=${_func_ret}
put_param "Signature" "${signature}"
# pack all params
pack_params
local packed_params=${_func_ret}
local req_url="${ALiServerAddr}/?${packed_params}"
_debug Request addr: ${req_url}
local respond=$(curl -3 ${req_url} --silent --connect-timeout 10 -w "HttpCode:%{http_code}")
_debug ${respond}
_func_ret=${respond}
}
update_record() {
get_my_ip
local my_ip=${_func_ret}
# Check if need update
_debug My IP: ${my_ip}
if [ -z "${my_ip}" ]; then
_err Could not get my ip, exitting...
exit
fi
get_domain_ip
local domain_ip=${_func_ret}
_debug Current Domain IP: ${domain_ip}
if [ "${my_ip}" == "${domain_ip}" ]; then
_log Need not to update, current IP: ${my_ip}
else
put_params_public
put_params_UpdateDomainRecord ${my_ip}
send_request
_log updated ${my_ip}
fi
}
_log ${DomainRR}.${DomainName} ${DomainType}
_debug AccessKeyId: ${AccessKeyId}
_debug AccessKeySec: ${AccessKeySec}
update_record