0%

All credits goes to kyrios@Koolshare


之前一直在使用NameCheap的dns服务,丫自带ddns
这周历尽千辛万苦的备案终于搞定了,就想把解析放到国内来实现隐形跳转…
但是这就没办法使用原来的ddns了…
在Koolshare梅林的软件中心中发现了aliddns的插件…
但是我觉得ddns脚本这种东西,每次wan状态发生变化的时候运行一下就够了,没有必要隔几分钟跑一次…
所以就将kyrios的脚本稍作改写
自己加了@和*的处理,用于根域名解析和泛解析…虽然很少有人这么用…

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
#!/bin/sh

# configuration
AccessKeyID=XXXXXXXXXXX
AccessKeySecret=XXXXXXXXXXX
Host=ddns
Domain=example.tld

# Should be no need to modify anything beyond this point
timestamp=`date -u "+%Y-%m-%dT%H%%3A%M%%3A%SZ"`
ip=$(curl -s whatismyip.akamai.com)

urlencode() {
out=""
while read -n1 c
do
case $c in
[a-zA-Z0-9._-]) out="$out$c" ;;
*) out="$out`printf '%%%02X' "$c"`" ;;
# 此行正确应为 *) out="$out`printf '%%%02X' "'$c"`" ;;
# 但是如果加上那个单引号,博客的语法高亮会崩...蛋疼
esac
done
echo -n $out
}

enc() {
echo -n "$1" | urlencode
}

send_request() {
local args="AccessKeyId=$AccessKeyID&Action=$1&Format=json&$2&Version=2015-01-09"
local hash=$(echo -n "GET&%2F&$(enc "$args")" | openssl dgst -sha1 -hmac "$AccessKeySecret&" -binary | openssl base64)
curl -s "http://alidns.aliyuncs.com/?$args&Signature=$(enc "$hash")"
}

get_recordid() {
grep -Eo '"RecordId":"[0-9]+"' | cut -d':' -f2 | tr -d '"'
}

query_recordid() {
if [ "$Host" = "@" ]
then
send_request "DescribeSubDomainRecords" "SignatureMethod=HMAC-SHA1&SignatureNonce=$timestamp&SignatureVersion=1.0&SubDomain=$Domain&Timestamp=$timestamp&Type=A"
elif [ "$Host" = "*" ]
then
send_request "DescribeSubDomainRecords" "SignatureMethod=HMAC-SHA1&SignatureNonce=$timestamp&SignatureVersion=1.0&SubDomain=%2A.$Domain&Timestamp=$timestamp&Type=A"
else
send_request "DescribeSubDomainRecords" "SignatureMethod=HMAC-SHA1&SignatureNonce=$timestamp&SignatureVersion=1.0&SubDomain=$Host.$Domain&Timestamp=$timestamp&Type=A"
fi
}

update_record() {
if [ "$Host" = "@" ]
then
send_request "UpdateDomainRecord" "RR=%40&RecordId=$1&SignatureMethod=HMAC-SHA1&SignatureNonce=$timestamp&SignatureVersion=1.0&TTL=600&Timestamp=$timestamp&Type=A&Value=$ip"
elif [ "$Host" = "*" ]
then
send_request "UpdateDomainRecord" "RR=%2A&RecordId=$1&SignatureMethod=HMAC-SHA1&SignatureNonce=$timestamp&SignatureVersion=1.0&TTL=600&Timestamp=$timestamp&Type=A&Value=$ip"
else
send_request "UpdateDomainRecord" "RR=$Host&RecordId=$1&SignatureMethod=HMAC-SHA1&SignatureNonce=$timestamp&SignatureVersion=1.0&TTL=600&Timestamp=$timestamp&Type=A&Value=$ip"
fi
}

add_record() {
if [ "$Host" = "@" ]
then
send_request "AddDomainRecord&DomainName=$Domain" "RR=%40&SignatureMethod=HMAC-SHA1&SignatureNonce=$timestamp&SignatureVersion=1.0&TTL=600&Timestamp=$timestamp&Type=A&Value=$ip"
elif [ "$Host" = "*" ]
then
send_request "AddDomainRecord&DomainName=$Domain" "RR=%2A&SignatureMethod=HMAC-SHA1&SignatureNonce=$timestamp&SignatureVersion=1.0&TTL=600&Timestamp=$timestamp&Type=A&Value=$ip"
else
send_request "AddDomainRecord&DomainName=$Domain" "RR=$Host&SignatureMethod=HMAC-SHA1&SignatureNonce=$timestamp&SignatureVersion=1.0&TTL=600&Timestamp=$timestamp&Type=A&Value=$ip"
fi
}

if [ "$aliddns_record_id" = "" ]
then
aliddns_record_id=`query_recordid | get_recordid`
fi
if [ "$aliddns_record_id" = "" ]
then
aliddns_record_id=`add_record | get_recordid`
else
update_record $aliddns_record_id
fi

if [ $? -eq 0 ]; then
/sbin/ddns_custom_updated 1
else
/sbin/ddns_custom_updated 0
fi

将configuration部分填好,保存为/jffs/scripts/ddns-start,给0755权限
在路由web界面的外部网络-DDNS设置页中,选择Custom
应用即可