lede/package/lean/luci-app-aliddns/root/usr/sbin/aliddns
2017-09-06 20:18:34 +08:00

126 lines
3.9 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/sh
aliddns_ak=`uci get aliddns.base.app_key 2>/dev/null`
aliddns_sk=`uci get aliddns.base.app_secret 2>/dev/null`
aliddns_record_id=`uci get aliddns.base.record_id 2>/dev/null`
time=`uci get aliddns.base.time 2>/dev/null`
aliddns_enable=`uci get aliddns.base.enable`
aliddns_domain=`uci get aliddns.base.main_domain 2>/dev/null`
aliddns_name=`uci get aliddns.base.sub_domain 2>/dev/null`
interface=`uci get aliddns.base.interface 2>/dev/null`
DATE=$(date +'%Y-%m-%d %H:%M:%S')
timestamp=`date -u "+%Y-%m-%dT%H%%3A%M%%3A%SZ"`
ip=$(ifconfig $interface 2> /dev/null | grep 'inet addr' | awk '{print $2}' | cut -d: -f2 2>/dev/null)
check_aliddns() {
#ip=`wget -qO- http://whatismyip.akamai.com/ 2>/dev/null`
current_ip=`nslookup $aliddns_name.$aliddns_domain | grep "Address 1"|tail -n1|cut -d' ' -f3 2>/dev/null`
echo "$DATE 当前路由IP: ${ip}" >> /var/log/aliddns.log
echo "$DATE 远程解析IP: ${current_ip}" >> /var/log/aliddns.log
if [ "$ip" = "$current_ip" ]
then
echo "$DATE IP未改变无需更新" >> /var/log/aliddns.log
exit 0
else
echo "$DATE 更新中..." >> /var/log/aliddns.log
fi
}
urlencode() {
# urlencode <string>
local length="${#1}"
i=0
out=""
for i in $(awk "BEGIN { for ( i=0; i<$length; i++ ) { print i; } }")
do
local c="${1:$i:1}"
case $c in
[a-zA-Z0-9._-]) out="$out$c" ;;
*) out="$out`printf '%%%02X' "'$c"`" ;;
esac
i=$(($i + 1))
done
echo -n $out
}
send_request() {
local args="AccessKeyId=$aliddns_ak&Action=$1&Format=json&$2&Version=2015-01-09"
local hash=$(urlencode $(echo -n "GET&%2F&$(urlencode $args)" | openssl dgst -sha1 -hmac "$aliddns_sk&" -binary | openssl base64))
wget -qO- "http://alidns.aliyuncs.com/?$args&Signature=$hash" 2> /dev/null
}
get_recordid() {
grep -Eo '"RecordId":"[0-9]+"' | cut -d':' -f2 | tr -d '"'
}
query_recordid() {
send_request "DescribeSubDomainRecords" "SignatureMethod=HMAC-SHA1&SignatureNonce=$timestamp&SignatureVersion=1.0&SubDomain=$aliddns_name.$aliddns_domain&Timestamp=$timestamp"
}
update_record() {
send_request "UpdateDomainRecord" "RR=$aliddns_name&RecordId=$1&SignatureMethod=HMAC-SHA1&SignatureNonce=$timestamp&SignatureVersion=1.0&Timestamp=$timestamp&Type=A&Value=$ip"
}
add_record() {
send_request "AddDomainRecord&DomainName=$aliddns_domain" "RR=$aliddns_name&SignatureMethod=HMAC-SHA1&SignatureNonce=$timestamp&SignatureVersion=1.0&Timestamp=$timestamp&Type=A&Value=$ip"
}
go_record() {
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`
echo "$DATE 添加 record $aliddns_record_id" >> /var/log/aliddns.log
else
update_record $aliddns_record_id
echo "$DATE 更新 record $aliddns_record_id" >> /var/log/aliddns.log
fi
# save to file
if [ "$aliddns_record_id" = "" ]; then
# failed
echo "$DATE : 更新出错,请检查设置!" >> /var/log/aliddns.log
exit 0
else
uci set aliddns.base.record_id=$aliddns_record_id
uci commit
echo "$DATE : 更新成功!($ip)" >> /var/log/aliddns.log
fi
}
#将执行脚本写入crontab定时运行
add_aliddns_cru(){
wirtecron=$(cat /etc/crontabs/root | grep "$time * * * *" | grep aliddns)
if [ -z "$wirtecron" ];then
sed -i '/aliddns/d' /etc/crontabs/root >/dev/null 2>&1
echo "*/$time * * * * /usr/sbin/aliddns" >> /etc/crontabs/root
fi
}
#清除过多记录
clean_log(){
logrow=$(grep -c "" /var/log/aliddns.log)
if [ $logrow -ge 15 ];then
cat /dev/null > /var/log/aliddns.log
echo "$DATE Log条数超限清空处理" >> /var/log/aliddns.log
fi
}
#停止服务
stop_aliddns(){
#停掉cru里的任务
sed -i '/aliddns/d' /etc/crontabs/root >/dev/null 2>&1
}
if [ "$aliddns_enable" != "1" ]; then
stop_aliddns
echo "$DATE : aliddns没有开启" >> /var/log/aliddns.log
else
clean_log
check_aliddns
go_record
add_aliddns_cru
fi