mirror of
https://github.com/coolsnowwolf/lede.git
synced 2025-07-26 06:46:59 +08:00
support dnspod api by token
This commit is contained in:
parent
87208cf99c
commit
15d60da9bc
@ -1,149 +1,119 @@
|
|||||||
#!/bin/sh
|
#!/bin/bash
|
||||||
#written by benson huang
|
#written by Michael Qu
|
||||||
#admin@zhetenger.com
|
#greennyreborn@gmail.com
|
||||||
#http://www.terryche.me/openwrt-dnspod-ddns.html
|
|
||||||
|
|
||||||
#dnspod帐号名
|
#dnspod api token
|
||||||
email=`uci get dnspod.base_arg.login_email`
|
token=`uci get dnspod.base_arg.login_token`
|
||||||
|
|
||||||
#dnspod密码
|
|
||||||
password=`uci get dnspod.base_arg.login_password 2>/dev/null`
|
|
||||||
|
|
||||||
#域名
|
#域名
|
||||||
domainname=`uci get dnspod.base_arg.main_domain 2>/dev/null`
|
domain=`uci get dnspod.base_arg.main_domain 2>/dev/null`
|
||||||
|
|
||||||
#需要更新的子域名列表,多个的话,以空格分割。
|
|
||||||
#例如:
|
#需要更新的子域名,只允许一个
|
||||||
# sub_domains='www home'
|
#由于*linux会被脚本解析为当前目录下的所有文件,所以请填写\*
|
||||||
sub_domains=`uci get dnspod.base_arg.sub_domain 2>/dev/null`
|
# sub_domain='\*'
|
||||||
|
sub_domain=`uci get dnspod.base_arg.sub_domain 2>/dev/null`
|
||||||
|
if [ $sub_domain = "\*" ];then
|
||||||
|
sub_domain="*"
|
||||||
|
fi
|
||||||
|
|
||||||
#check for changed ip every 300 seconds
|
#check for changed ip every 300 seconds
|
||||||
wait=300
|
wait=300
|
||||||
|
wait_second=`uci get dnspod.base_arg.wait_second 2>/dev/null`
|
||||||
|
if [[ $wait_second =~ ^[1-9][0-9]*$ ]]; then
|
||||||
|
wait=$wait_second
|
||||||
|
fi
|
||||||
|
|
||||||
#检查是否安装curl
|
#检查curl是否安装
|
||||||
curl_status=`which curl 2>/dev/null`
|
curl_status=`which curl 2>/dev/null`
|
||||||
[ -n "$curl_status" ] || { echo "curl is not installed";exit 3; }
|
[ -n "$curl_status" ] || { echo "curl is not installed";exit 3; }
|
||||||
|
|
||||||
os=$(uname -a | egrep -io 'openwrt' | tr [A-Z] [a-z])
|
os=$(uname -a | egrep -io 'openwrt' | tr [A-Z] [a-z])
|
||||||
|
|
||||||
API_url="https://dnsapi.cn"
|
API_url="https://dnsapi.cn"
|
||||||
|
LOG_FILE="/tmp/dnspod.log"
|
||||||
|
MAX_LOG_SIZE=1*1024*1024
|
||||||
|
PROGRAM=$(basename $0)
|
||||||
|
|
||||||
format='json'
|
format='json'
|
||||||
lang='en'
|
lang='en'
|
||||||
record_type='A'
|
record_type='A'
|
||||||
offset="2"
|
common_options="login_token=$token&format=${format}&lang=${lang}"
|
||||||
length=""
|
|
||||||
common_options="--data-urlencode \"login_email=${email}\"\
|
|
||||||
--data-urlencode \"login_password=${password}\"\
|
|
||||||
--data-urlencode \"format=${format}\"\
|
|
||||||
--data-urlencode \"lang=${lang}\""
|
|
||||||
PROGRAM=$(basename $0)
|
|
||||||
is_svc=0
|
is_svc=0
|
||||||
|
last_modified_ip=
|
||||||
|
|
||||||
|
getFileSize() {
|
||||||
|
local file=$1
|
||||||
|
wc -c $file
|
||||||
|
}
|
||||||
|
|
||||||
|
clearLog() {
|
||||||
|
local file_size=$(getFileSize $LOG_FILE)
|
||||||
|
if [ $file_size -gt $MAX_LOG_SIZE ]; then
|
||||||
|
echo "" > $LOG_FILE
|
||||||
|
fi
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
printMsg() {
|
printMsg() {
|
||||||
|
local time=$(date "+%Y-%m-%d %H:%M:%S")
|
||||||
local msg="$1"
|
local msg="$1"
|
||||||
if [ $is_svc -eq 1 ];then
|
if [ $is_svc -eq 1 ];then
|
||||||
logger -t ${PROGRAM} "${msg}"
|
echo "$time: $msg" >> $LOG_FILE
|
||||||
|
# logger -t ${PROGRAM} "${msg}"
|
||||||
else
|
else
|
||||||
echo $msg
|
echo "$time: $msg"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
getIp() {
|
getIp() {
|
||||||
# curl -s http://checkip.dyndns.com | sed -n 's/.*: \([0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\).*/\1/p'
|
# curl -s http://checkip.dyndns.com | sed -n 's/.*: \([0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\).*/\1/p'
|
||||||
curl -s http://members.3322.org/dyndns/getip
|
curl -s whatismyip.akamai.com
|
||||||
}
|
|
||||||
|
|
||||||
getJsonValue(){
|
|
||||||
local params="$1"
|
|
||||||
echo $json_data | sed 's/\\\\\//\//g' | sed 's/[{}]//g;s/\(\[\|\]\)//g' |\
|
|
||||||
awk -F ',' '{ for (i=1;i<=NF;i++) { print $i }}' |\
|
|
||||||
sed 's/":/\|/g;s/"//g' |\
|
|
||||||
awk -v k="$params" -F'|' '{ if ($(NF - 1) == k ) print $NF }'
|
|
||||||
}
|
}
|
||||||
|
|
||||||
execAPI() {
|
execAPI() {
|
||||||
local action="$1"
|
local action="$1"
|
||||||
local extra_options="$2"
|
local extra_options="$2"
|
||||||
eval "curl -k -A 'xddns' ${API_url}/${action} ${common_options} ${extra_options}"
|
|
||||||
|
eval "curl -k -X POST \"${API_url}/${action}\" -d \"${common_options}&${extra_options}\""
|
||||||
}
|
}
|
||||||
|
|
||||||
getDomainId() {
|
getRecordList() {
|
||||||
local extra_options="--data-urlencode \"domain=${domainname}\""
|
local extra_options="domain=$domain"
|
||||||
json_data=$(execAPI "Domain.info" "${extra_options}")
|
echo $(execAPI "Record.List" "$extra_options")
|
||||||
getJsonValue id
|
|
||||||
}
|
|
||||||
|
|
||||||
getRecordId() {
|
|
||||||
local extra_options
|
|
||||||
for sub_domain in $sub_domains;do
|
|
||||||
extra_options="--data-urlencode \"record_type=${record_type}\"\
|
|
||||||
--data-urlencode \"domain_id=${domain_id}\"\
|
|
||||||
--data-urlencode \"sub_domain=${sub_domain}\"\
|
|
||||||
--data-urlencode \"offset=${offset}\"\
|
|
||||||
--data-urlencode \"length=${length}\""
|
|
||||||
|
|
||||||
json_data=$(execAPI "Record.List" "${extra_options}")
|
|
||||||
|
|
||||||
#check if record type is NS
|
|
||||||
IS_NS=$(getJsonValue type | grep -o 'NS' | head -n1)
|
|
||||||
|
|
||||||
#if there are multi @ subdomains, get the first non-NS record id only
|
|
||||||
if [ "$IS_NS" = "NS" ];then
|
|
||||||
numofline=$(getJsonValue id | sed '/^[0-9]\{7\}$/d' | wc -l)
|
|
||||||
[ $numofline -eq 3 ] && tmp_result_id="$(getJsonValue id | sed '/^[0-9]\{7\}$/d' | head -n1 )" || continue
|
|
||||||
else
|
|
||||||
tmp_result_id="$(getJsonValue id | sed '/^[0-9]\{7\}$/d')"
|
|
||||||
fi
|
|
||||||
#if result_id is not empty or unset, append a space to split record id
|
|
||||||
result_id="${result_id:+${result_id} }${tmp_result_id}"
|
|
||||||
done
|
|
||||||
echo $result_id
|
|
||||||
exit
|
exit
|
||||||
}
|
}
|
||||||
|
|
||||||
updateRecord() {
|
updateRecord() {
|
||||||
local record_id_list
|
local ip=$1
|
||||||
local extra_options
|
local record_list=`getRecordList`
|
||||||
local tmp=${sub_domains}
|
local old_ip=`jsonfilter -s "$record_list" -e "@.records[@.name='$sub_domain'].value"`
|
||||||
local tmp_sub_domains
|
local id=`jsonfilter -s "$record_list" -e "@.records[@.name='$sub_domain'].id"`
|
||||||
printMsg 'Start update records'
|
printMsg "Change record: $sub_domain.$domain from $old_ip to $ip"
|
||||||
record_id_list=$(getRecordId)
|
|
||||||
printMsg "Records IDs: ${record_id_list}"
|
local extra_options="domain=$domain&record_id=$id&value=$ip&record_type=A&record_line_id=0&sub_domain=$sub_domain"
|
||||||
for record_id in $record_id_list;do
|
local response=$(execAPI "Record.Modify" "$extra_options")
|
||||||
tmp_sub_domains=$(echo $tmp | awk '{ print $1 }')
|
|
||||||
tmp=${tmp#* }
|
local code=`jsonfilter -s "$response" -e "@.status.code"`
|
||||||
extra_options="--data-urlencode \"domain_id=${domain_id}\"\
|
if [ "$code" == "1" ]; then
|
||||||
--data-urlencode \"record_id=${record_id}\"\
|
last_modified_ip=$ip
|
||||||
--data-urlencode \"sub_domain=${tmp_sub_domains}\"\
|
fi
|
||||||
--data-urlencode \"record_type=${record_type}\"\
|
local message=`jsonfilter -s "$response" -e "@.status.message"`
|
||||||
--data-urlencode \"record_line=默认\"\
|
printMsg "response: $message"
|
||||||
--data-urlencode \"value=${pub_ip_addr}\"\
|
|
||||||
--data-urlencode \"mx=1\""
|
|
||||||
json_data=$(execAPI "Record.Modify" "${extra_options}")
|
|
||||||
printMsg "Update [${tmp_sub_domains}.${domainname}] ${record_type} record to [${pub_ip_addr}] : $(getJsonValue message)"
|
|
||||||
done
|
|
||||||
printMsg 'Update records finish'
|
|
||||||
}
|
}
|
||||||
|
|
||||||
checkip() {
|
checkip() {
|
||||||
local oldip=$pub_ip_addr
|
local wan_ip=$1
|
||||||
pub_ip_addr=$(getIp)
|
printMsg "old ip: [$last_modified_ip], new ip: [$wan_ip]"
|
||||||
printMsg "old ip: [$oldip], new ip: [$pub_ip_addr]"
|
if [ "$wan_ip" != "$last_modified_ip" ];then
|
||||||
if [ "$pub_ip_addr" != "$oldip" ];then
|
|
||||||
return 8
|
return 8
|
||||||
else
|
else
|
||||||
return 3
|
return 3
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
updateTunnelBroker() {
|
|
||||||
printMsg "Start Update Tunnel Broker"
|
|
||||||
printMsg "User Name: ***, Tunnel ID: ***"
|
|
||||||
result=$(curl -s --insecure https://ipv4.tunnelbroker.net/nic/update --data-urlencode "username=***" --data-urlencode "password=***" --data-urlencode "hostname=***")
|
|
||||||
printMsg "Update Tunnel Broker: ${result}"
|
|
||||||
printMsg "Update Tunnel Broker finish"
|
|
||||||
}
|
|
||||||
|
|
||||||
execSvc() {
|
execSvc() {
|
||||||
local ip
|
local ip
|
||||||
#check that whether the network is ok
|
#check that whether the network is ok
|
||||||
@ -158,12 +128,12 @@ execSvc() {
|
|||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
domain_id=`getDomainId $domainname`
|
|
||||||
printMsg "domain_id: ${domain_id}"
|
|
||||||
while [ 1 ];do
|
while [ 1 ];do
|
||||||
checkip
|
clearLog
|
||||||
|
ip=$(getIp)
|
||||||
|
checkip $ip
|
||||||
if [ $? -eq 8 ];then
|
if [ $? -eq 8 ];then
|
||||||
updateRecord
|
updateRecord $ip
|
||||||
#updateTunnelBroker
|
#updateTunnelBroker
|
||||||
fi
|
fi
|
||||||
sleep $wait
|
sleep $wait
|
||||||
@ -171,23 +141,19 @@ execSvc() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
execUpdate() {
|
execUpdate() {
|
||||||
domain_id=`getDomainId $domainname`
|
local ip=`getIp`
|
||||||
pub_ip_addr=$(getIp)
|
updateRecord $ip
|
||||||
updateRecord
|
|
||||||
}
|
}
|
||||||
|
|
||||||
case $1 in
|
case $1 in
|
||||||
--svc)
|
--svc)
|
||||||
is_svc=1;
|
is_svc=1;
|
||||||
printMsg 'Start in Service mode';
|
clearLog;
|
||||||
printMsg "domain: ${domainname}, sub_domains: ${sub_domains}";
|
printMsg "Start in Service mode, check in every $wait seconds";
|
||||||
|
printMsg "domain: ${domain}, sub_domain: ${sub_domain}";
|
||||||
execSvc;;
|
execSvc;;
|
||||||
--ipv6)
|
|
||||||
is_svc=0;
|
|
||||||
printMsg "Start update HE Tunnel Broker";
|
|
||||||
updateTunnelBroker;;
|
|
||||||
*)
|
*)
|
||||||
is_svc=0;
|
is_svc=0;
|
||||||
printMsg "Start update record, domain: ${domainname}, sub_domains: ${sub_domains}";
|
printMsg "Start update record, domain: ${domain}, sub_domain: ${sub_domain}";
|
||||||
execUpdate;;
|
execUpdate;;
|
||||||
esac
|
esac
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
config base_arg "base_arg"
|
config base_arg "base_arg"
|
||||||
option login_email ''
|
|
||||||
option login_password ''
|
|
||||||
option main_domain ''
|
option main_domain ''
|
||||||
option sub_domain ''
|
option sub_domain ''
|
||||||
|
option login_token ''
|
||||||
|
option wait_second ''
|
||||||
|
|
||||||
|
@ -1,18 +1,18 @@
|
|||||||
#!/bin/sh /etc/rc.common
|
#!/bin/sh /etc/rc.common
|
||||||
START=80
|
START=80
|
||||||
|
|
||||||
run_reboot()
|
|
||||||
{
|
|
||||||
killall dnspod.sh
|
|
||||||
/sbin/dnspod.sh --svc
|
|
||||||
}
|
|
||||||
|
|
||||||
start()
|
start()
|
||||||
{
|
{
|
||||||
/sbin/dnspod.sh --svc
|
/usr/sbin/dnspod.sh --svc &
|
||||||
}
|
}
|
||||||
|
|
||||||
stop()
|
stop()
|
||||||
{
|
{
|
||||||
killall dnspod.sh
|
ps | grep dnspod.sh | grep -v 'grep' | awk '{print $1}' | xargs kill
|
||||||
|
}
|
||||||
|
|
||||||
|
run_reboot()
|
||||||
|
{
|
||||||
|
stop
|
||||||
|
start
|
||||||
}
|
}
|
||||||
|
@ -18,11 +18,10 @@ s = m:section(TypedSection, "base_arg", "")
|
|||||||
s.addremove = false
|
s.addremove = false
|
||||||
s.anonymous = true
|
s.anonymous = true
|
||||||
|
|
||||||
email = s:option(Value, "login_email", translate("登录邮箱"),"登录DNSPOD的用户名")
|
email = s:option(Value, "login_token", translate("DNSPOD Token"), "格式: ID,Token")
|
||||||
pass = s:option(Value, "login_password", translate("登录密码"),"登录DNSPOD的密码")
|
main = s:option(Value, "main_domain", translate("主域名"), "想要解析的主域名,例如:baidu.com")
|
||||||
pass.password=true
|
sub = s:option(Value, "sub_domain", translate("子域名"), "想要解析的子域名,只允许填写一个。如果想解析*子域名,请填写 \\*")
|
||||||
main = s:option(Value, "main_domain", translate("主域名"),"想要解析的主域名,例如:baidu.com")
|
wait = s:option(Value, "wait_second", translate("更新周期(s)"), "请填写数字,默认为300s")
|
||||||
sub = s:option(Value, "sub_domain", translate("子域名"),"想要解析的子域名,多个以空格分开,例如:test hehe")
|
|
||||||
|
|
||||||
local apply = luci.http.formvalue("cbi.apply")
|
local apply = luci.http.formvalue("cbi.apply")
|
||||||
if apply then
|
if apply then
|
||||||
|
Loading…
x
Reference in New Issue
Block a user