Created
April 24, 2021 15:08
-
-
Save future0906/d247d2031283a43a2214948b30df7707 to your computer and use it in GitHub Desktop.
TencentDDNS(DnsPod) openwrt plugin for dualwan edition(腾讯DDNS-DNSPOD 双wan口版本运行脚本)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
NAME=tencentddns | |
log_file=/var/log/$NAME.log | |
console_log() { | |
echo "$DATE [TENCENTDDNS] $@" >> $log_file | |
} | |
uci_get_by_name() { | |
local ret=$(uci get $NAME.$1.$2 2>/dev/null) | |
echo ${ret:=$3} | |
} | |
uci_bool_by_name() { | |
case "$(uci_get_by_name $1 $2)" in | |
1|on|true|yes|enabled) return 0;; | |
esac | |
return 1 | |
} | |
intelnetip() { | |
tmp_ip=`curl -sL --connect-timeout 3 ns1.dnspod.net:6666` | |
if [ "Z$tmp_ip" == "Z" ]; then | |
tmp_ip=`curl -sL --connect-timeout 3 members.3322.org/dyndns/getip` | |
fi | |
if [ "Z$tmp_ip" == "Z" ]; then | |
tmp_ip=`curl -sL --connect-timeout 3 14.215.150.17:6666` | |
fi | |
if [ "Z$tmp_ip" == "Z" ]; then | |
tmp_ip=`curl -sL --connect-timeout 3 whatismyip.akamai.com` | |
fi | |
echo -n $tmp_ip | |
} | |
resolve2ip() { | |
# resolve2ip domain<string> | |
domain=$1 | |
tmp_ip=`nslookup $domain f1g1ns1.dnspod.net 2>/dev/null | sed '/^Server/d; /#53$/d' | grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'` | |
if [ "Z$tmp_ip" == "Z" ]; then | |
tmp_ip=`nslookup $domain f1g1ns2.dnspod.net 2>/dev/null | sed '/^Server/d; /#53$/d' | grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'` | |
fi | |
if [ "Z$tmp_ip" == "Z" ]; then | |
tmp_ip=`nslookup $domain 114.114.115.115 2>/dev/null | sed '/^Server/d; /#53$/d' | grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'` | |
fi | |
if [ "Z$tmp_ip" == "Z" ]; then | |
tmp_ip=`curl -sL --connect-timeout 3 "119.29.29.29/d?dn=$domain"` | tr ";" "\n" | |
fi | |
echo -n $tmp_ip | |
} | |
check_tencentddns() { | |
ip=$1 | |
domain="$sub_dm.$main_dm" | |
console_log "CheckDdns: domain with ip DOMAIN:$domain, WAN-IP: ${ip}" | |
if [ "Z$ip" == "Z" ]; then | |
console_log "CheckDdns:ERROR, cant get WAN-IP..." | |
echo $rrid_list | |
return 0 | |
fi | |
current_ip_list=$(resolve2ip $domain) | |
if [ "Z$current_ip_list" == "Z" ]; then | |
console_log "CheckDdns:Empty result from resolve2ip DOMAIN:$domain" | |
echo $rrid_list | |
return 1 | |
fi | |
for current_ip in $current_ip_list; do | |
console_log "CheckDdns:Current DOMAIN-IP: ${current_ip}" | |
if [ "Z$ip" == "Z$current_ip" ]; then | |
rrid=$(query_recordid | get_recordid4 $ip) | |
if [ "Z$rrid" != "Z" ]; then | |
console_log "CheckDdns:IP dont need UPDATE, RecordID:$rrid, new RecordID List:$rrid_list" | |
echo $(pop_record_id $rrid) | |
return 0 | |
fi | |
fi | |
done | |
console_log "CheckDdns:Not found WAN-IP:$ip in DOMAIN:$domain UPDATING..." | |
echo $rrid_list | |
return 1 | |
} | |
urlencode() { | |
# urlencode url<string> | |
out='' | |
for c in $(echo -n $1 | sed 's/[^\n]/&\n/g'); do | |
case $c in | |
[a-zA-Z0-9._-]) out="$out$c" ;; | |
*) out="$out$(printf '%%%02X' "'$c")" ;; | |
esac | |
done | |
echo -n $out | |
} | |
send_request() { | |
curl -s -X POST https://dnsapi.cn/$1 -d "login_token=$ak_id,$ak_token&format=json&"$2 | |
} | |
get_recordid() { | |
sed 's/"records"/\n/g' | sed -n '2p' | sed 's/ttl/\n/g' | sed -n 's/.*"id[^0-9]*\([0-9]*\).*/\1\n/p' | sort -ru | sed /^$/d | |
} | |
get_recordid2() { | |
sed 's/"record"/\n/g' | sed -n '2p' | sed -n 's/.*"id[^0-9]*\([0-9]*\).*/\1\n/p' | sort -ru | sed /^$/d | |
} | |
get_recordid3() { | |
jsonfilter -e "@.records[*]['id']" | |
} | |
get_recordid4() { | |
jsonfilter -e "@.records[@.value='$1']['id']" | |
} | |
query_recordid() { | |
send_request "Record.List" "domain=$main_dm&sub_domain=$sub_dm&record_type=A" | |
} | |
update_record() { | |
send_request "Record.Modify" "domain=$main_dm&sub_domain=$sub_dm&record_id=$1&record_type=A&record_line=%e9%bb%98%e8%ae%a4&value=$2" | |
} | |
add_record() { | |
send_request "Record.Create" "domain=$main_dm&sub_domain=$sub_dm&record_type=A&record_line=%e9%bb%98%e8%ae%a4&value=$1" | |
} | |
del_record() { | |
send_request "Record.Remove" "domain=$main_dm&record_id=$1" | |
} | |
get_first_record_id() { | |
echo "$rrid_list" | cut -d " " -f1 | |
} | |
pop_record_id() { | |
echo "$rrid_list" | sed "s/$1//g" | |
} | |
do_ddns_record() { | |
ip=$1 | |
console_log "DNS_RECORD:IP:$ip, RecordIDList:$rrid_list" | |
if [ "Z$rrid" == "Z" ]; then | |
rrid=$(get_first_record_id) | |
rrid_list=$(pop_record_id $rrid) | |
fi | |
console_log "DNS_RECORD:IP:$ip, RecordID:$rrid, RecordIDList:$rrid_list" | |
if [ "Z$rrid" == "Z" ]; then | |
rrid=`add_record $ip | get_recordid2` | |
console_log "DNS_RECORD:ADD record $rrid" | |
else | |
update_record $rrid $ip >/dev/null 2>&1 | |
console_log "DNS_RECORD:UPDATE record $rrid" | |
fi | |
if [ "Z$rrid" == "Z" ]; then | |
# failed | |
console_log "DNS_RECORD:# ERROR, Please Check Config/Time" | |
else | |
# save rrid | |
console_log "DNS_RECORD:# UPDATED($ip)" | |
fi | |
echo $rrid_list | |
console_log "DDNS_RECORD: ReturnValue:$rrid_list" | |
} | |
clean_log() { | |
if [ $(cat $log_file 2>/dev/null | wc -l) -ge 16 ]; then | |
rm -f $log_file && touch $log_file | |
console_log "Log Cleaned" | |
fi | |
} | |
do_get_iface_ip() { | |
iface=$1 | |
if [ "Z$iface" == "Zinternet" -o "Z$iface" == "Z" ]; then | |
ip=$(intelnetip) | |
else | |
ip=$(ubus call network.interface.$iface status | grep '"address"' | grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | head -1) | |
fi | |
echo $ip | |
} | |
do_iface_ddns() { | |
iface=$1 | |
ip=`do_get_iface_ip $iface` | |
console_log "DoDdns:WAN_IF:$iface, IP:$ip, RecordIDList:$rrid_list" | |
rrid_list=$(check_tencentddns $ip) | |
ret_val=$? | |
if [ $ret_val -eq 0 ]; then | |
console_log "DoDdns: CheckDdns ret $ret_val, returning" | |
echo $rrid_list | |
return | |
fi | |
rrid_list=$(do_ddns_record $ip) | |
echo "$rrid_list" | |
} | |
do_cleanup() { | |
console_log "DoCleanUp:Start purge dns record" | |
query_recordid | get_recordid | while read rr; do | |
console_log "DoCleanUp:Clean record $sub_dm.$main_dm: $rr" | |
del_record $rr >/dev/null | |
timestamp=$(date -u "+%Y-%m-%dT%H%%3A%M%%3A%SZ") | |
done | |
} | |
check_pre_cleanup() { | |
console_log "CleanUpCheck: Check if need clean up all record" | |
if uci_bool_by_name base clean ; then | |
do_cleanup | |
fi | |
} | |
[ -x /usr/bin/openssl -a -x /usr/bin/curl -a -x /bin/sed -a -x /usr/bin/jsonfilter ] || | |
( echo "Need [ openssl + curl + sed + jsonfilter]" && exit 1 ) | |
ak_id=$(uci_get_by_name base key_id) | |
ak_token=$(uci_get_by_name base key_token) | |
main_dm=$(uci_get_by_name base main_domain) | |
sub_dm=$(uci_get_by_name base sub_domain) | |
iface=$(uci_get_by_name base interface) | |
DATE=$(date +'%Y-%m-%d %H:%M:%S') | |
timestamp=$(date -u "+%Y-%m-%dT%H%%3A%M%%3A%SZ") | |
clean_log | |
check_pre_cleanup | |
# Need run pre cleanup before get rrid list | |
rrid_list=$(query_recordid | get_recordid3) | |
for each_if in $iface; do | |
console_log "Start do interface ddns, current record_id_list:$rrid_list" | |
rrid_list=$(do_iface_ddns $each_if) | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment