-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAliyunDDNS.sh
executable file
·180 lines (164 loc) · 5.26 KB
/
AliyunDDNS.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
#!/bin/sh
set -e
export BASE_PWD="$(dirname "$0")"
Ali_API="https://alidns.aliyuncs.com/"
__check_tool() {
local tool="$1"
which "$tool" >/dev/null 2>&1 || { echo "$tool is not installed"; return 1; }
return 0
}
__ali_urlencode() {
local _str="$1"
local _str_len=${#_str}
local _u_i=1
while [ "$_u_i" -le "$_str_len" ]; do
local _str_c="$(printf "%s" "$_str" | cut -c "$_u_i")"
case $_str_c in [a-zA-Z0-9.~_-])
printf "%s" "$_str_c"
;;
*)
printf "%%%02X" "'$_str_c"
;;
esac
local _u_i="$(expr "${_u_i}" + 1)"
done
}
__ali_nonce() {
date +"%s%N"
}
__timestamp() {
date -u +"%Y-%m-%dT%H%%3A%M%%3A%SZ"
}
__ali_signature() {
local secret="$1"
echo -n "GET&%2F&$(__ali_urlencode "$2")" | openssl dgst -sha1 -hmac "$secret&" -binary | openssl base64 -A
}
__get_dns_record() {
local key_id="$1"
local secret="$2"
local dmn="$3"
local rr="$4"
local type="$5"
local query="AccessKeyId=${key_id}"
query=$query'&Action=DescribeDomainRecords'
query=$query'&DomainName='${dmn}
query=$query'&Format=json'
query=$query"&RRKeyWord=$(__ali_urlencode "$rr")"
query=$query'&SignatureMethod=HMAC-SHA1'
query=$query"&SignatureNonce=$(__ali_nonce)"
query=$query'&SignatureVersion=1.0'
query=$query'&Timestamp='$(__timestamp)
query=$query'&TypeKeyWord='${type}
query=$query'&Version=2015-01-09'
local signature="$(__ali_signature "$secret" "$query")"
local url="$Ali_API?$query&Signature=$(__ali_urlencode "$signature")"
lib_curl "$url"
return $?
}
__insert_dns_record() {
local key_id="$1"
local secret="$2"
local dmn="$3"
local rr="$4"
local type="$5"
local val="$6"
local query="AccessKeyId=${key_id}"
query=$query'&Action=AddDomainRecord'
query=$query'&DomainName='${dmn}
query=$query'&Format=json'
query=$query"&RR=$(__ali_urlencode "$rr")"
query=$query'&SignatureMethod=HMAC-SHA1'
query=$query"&SignatureNonce=$(__ali_nonce)"
query=$query'&SignatureVersion=1.0'
query=$query'&Timestamp='$(__timestamp)
query=$query'&Type='${type}
query=$query'&Value='$(__ali_urlencode "${val}")
query=$query'&Version=2015-01-09'
local signature="$(__ali_signature "$secret" "$query")"
local url="$Ali_API?$query&Signature=$(__ali_urlencode "$signature")"
lib_curl "$url"
return $?
}
__update_dns_record() {
local key_id="$1"
local secret="$2"
local dmn="$3"
local rr="$4"
local type="$5"
local val="$6"
local recid="$7"
local query="AccessKeyId=${key_id}"
query=$query'&Action=UpdateDomainRecord'
query=$query'&DomainName='${dmn}
query=$query'&Format=json'
query=$query"&RR=$(__ali_urlencode "$rr")"
query=$query'&RecordId='${recid}
query=$query'&SignatureMethod=HMAC-SHA1'
query=$query"&SignatureNonce=$(__ali_nonce)"
query=$query'&SignatureVersion=1.0'
query=$query'&Timestamp='$(__timestamp)
query=$query'&Type='${type}
query=$query'&Value='$(__ali_urlencode "${val}")
query=$query'&Version=2015-01-09'
local signature="$(__ali_signature "$secret" "$query")"
local url="$Ali_API?$query&Signature=$(__ali_urlencode "$signature")"
lib_curl "$url"
return $?
}
__exec_plugins() {
local plugin_file
for plugin_file in "${BASE_PWD}/plugins/"*.sh; do
if ! [ -e "$plugin_file" ]; then return 0; fi
local plugin_base_name="$(basename "$plugin_file")"
local plugin_name=${plugin_base_name%%.*}
if eval [ \"\$"p_${plugin_name}_enable"\" = \"1\" ]; then
"$plugin_file" "$@"
fi
done
}
. "${BASE_PWD}/AliyunDDNS.env"
. "${BASE_PWD}/lib/common.sh"
__check_tool "openssl"
__check_tool "curl"
lib_check_parm "access_key_id"
lib_check_parm "access_key_secret"
lib_check_parm "domain_name"
lib_check_parm "host_record"
lib_check_parm "ip_api_url"
dns_type="A"
if [ "$use_ipv6" = "1" ]; then
dns_type="$dns_type AAAA"
fi
for iptype in $dns_type; do
if [ "$iptype" = "A" ]; then
ip="$(lib_curl -4 "$ip_api_url")"
if [ $? != 0 ] || [ -z "$ip" ]; then
echo "get ipv4 address failed"
continue
fi
echo "handle ipv4..."
else
ip="$(lib_curl -6 "$ip_api_url")"
if [ $? != 0 ] || [ -z "$ip" ]; then
echo "get ipv6 address failed"
continue
fi
echo "handle ipv6..."
fi
respon="$(__get_dns_record "${access_key_id}" "${access_key_secret}" "${domain_name}" "${host_record}" "$iptype")"
dns_record_id="$(lib_json_value "$respon" "RecordId" "string")"
dns_value="$(lib_json_value "$respon" "Value" "string")"
if [ -z "$dns_record_id" ] || [ -z "$dns_value" ]; then
echo "insert dns record"
__insert_dns_record "${access_key_id}" "${access_key_secret}" "${domain_name}" "${host_record}" "$iptype" "$ip"
echo ""
__exec_plugins "1" "${iptype}" "${domain_name}" "${host_record}" "" "$ip"
else
if [ "$dns_value" != "$ip" ]; then
echo "update dns record"
__update_dns_record "${access_key_id}" "${access_key_secret}" "${domain_name}" "${host_record}" "$iptype" "$ip" "$dns_record_id"
echo ""
__exec_plugins "2" "${iptype}" "${domain_name}" "${host_record}" "$dns_value" "$ip"
fi
fi
done