dnspod support customized command to get wan ip (#1705)

fix dnspod clear log bug
This commit is contained in:
Michael Qu 2019-10-08 23:42:11 +09:00 committed by coolsnowwolf
parent 6e2f8766af
commit d354ed7a32
3 changed files with 21 additions and 9 deletions

View File

@ -13,7 +13,7 @@ domain=`uci get dnspod.base_arg.main_domain 2>/dev/null`
#由于*linux会被脚本解析为当前目录下的所有文件所以请填写\* #由于*linux会被脚本解析为当前目录下的所有文件所以请填写\*
# sub_domain='\*' # sub_domain='\*'
sub_domain=`uci get dnspod.base_arg.sub_domain 2>/dev/null` sub_domain=`uci get dnspod.base_arg.sub_domain 2>/dev/null`
if [ $sub_domain = "\*" ];then if [ $sub_domain = "\*" ]; then
sub_domain="*" sub_domain="*"
fi fi
@ -24,6 +24,12 @@ if [[ $wait_second =~ ^[1-9][0-9]*$ ]]; then
wait=$wait_second wait=$wait_second
fi fi
#外网ip获取命令
command=`uci get dnspod.base_arg.command_to_get_ip 2>/dev/null`
if [ -z "$command" ]; then
command='curl -s whatismyip.akamai.com'
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; }
@ -32,7 +38,7 @@ 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" LOG_FILE="/tmp/dnspod.log"
MAX_LOG_SIZE=1*1024*1024 MAX_LOG_SIZE=`expr 1 * 1024 * 1024` # 默认最大限制1M
PROGRAM=$(basename $0) PROGRAM=$(basename $0)
format='json' format='json'
@ -45,21 +51,22 @@ last_modified_ip=
getFileSize() { getFileSize() {
local file=$1 local file=$1
wc -c $file wc -c $file | awk '{print $1}'
} }
clearLog() { clearLog() {
local file_size=$(getFileSize $LOG_FILE) local file_size=$(getFileSize $LOG_FILE)
if [ $file_size -gt $MAX_LOG_SIZE ]; then if [ $file_size -gt $MAX_LOG_SIZE ]; then
echo "" > $LOG_FILE echo "" > $LOG_FILE
printMsg "Log file [$LOG_FILE], size: [$file_size bytes]."
printMsg "Exceeds max log size [$MAX_LOG_SIZE]. Clear the log."
fi fi
} }
printMsg() { printMsg() {
local time=$(date "+%Y-%m-%d %H:%M:%S") 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
echo "$time: $msg" >> $LOG_FILE echo "$time: $msg" >> $LOG_FILE
# logger -t ${PROGRAM} "${msg}" # logger -t ${PROGRAM} "${msg}"
else else
@ -69,7 +76,7 @@ printMsg() {
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 whatismyip.akamai.com eval $command
} }
execAPI() { execAPI() {
@ -107,7 +114,7 @@ updateRecord() {
checkip() { checkip() {
local wan_ip=$1 local wan_ip=$1
printMsg "old ip: [$last_modified_ip], new ip: [$wan_ip]" printMsg "old ip: [$last_modified_ip], new ip: [$wan_ip]"
if [ "$wan_ip" != "$last_modified_ip" ];then if [ "$wan_ip" != "$last_modified_ip" ]; then
return 8 return 8
else else
return 3 return 3
@ -119,7 +126,7 @@ execSvc() {
#check that whether the network is ok #check that whether the network is ok
while [ 1 ];do while [ 1 ];do
ip=$(getIp) ip=$(getIp)
if [ -n "$ip" ];then if [ -n "$ip" ]; then
printMsg "WAN IP: ${ip}" printMsg "WAN IP: ${ip}"
break; break;
else else
@ -132,7 +139,7 @@ execSvc() {
clearLog clearLog
ip=$(getIp) ip=$(getIp)
checkip $ip checkip $ip
if [ $? -eq 8 ];then if [ $? -eq 8 ]; then
updateRecord $ip updateRecord $ip
#updateTunnelBroker #updateTunnelBroker
fi fi
@ -142,6 +149,7 @@ execSvc() {
execUpdate() { execUpdate() {
local ip=`getIp` local ip=`getIp`
printMsg "WAN IP: ${ip}"
updateRecord $ip updateRecord $ip
} }
@ -151,9 +159,11 @@ case $1 in
clearLog; clearLog;
printMsg "Start in Service mode, check in every $wait seconds"; printMsg "Start in Service mode, check in every $wait seconds";
printMsg "domain: ${domain}, sub_domain: ${sub_domain}"; printMsg "domain: ${domain}, sub_domain: ${sub_domain}";
printMsg "Use command: [$command] to get wan ip";
execSvc;; execSvc;;
*) *)
is_svc=0; is_svc=0;
printMsg "Start update record, domain: ${domain}, sub_domain: ${sub_domain}"; printMsg "Start update record, domain: ${domain}, sub_domain: ${sub_domain}";
printMsg "Use command: [$command] to get wan ip";
execUpdate;; execUpdate;;
esac esac

View File

@ -3,4 +3,5 @@ config base_arg "base_arg"
option sub_domain '' option sub_domain ''
option login_token '' option login_token ''
option wait_second '' option wait_second ''
option command_to_get_ip 'curl -s whatismyip.akamai.com'

View File

@ -22,6 +22,7 @@ email = s:option(Value, "login_token", translate("DNSPOD Token"), "格式: ID,To
main = s:option(Value, "main_domain", translate("主域名"), "想要解析的主域名,例如:baidu.com") main = s:option(Value, "main_domain", translate("主域名"), "想要解析的主域名,例如:baidu.com")
sub = s:option(Value, "sub_domain", translate("子域名"), "想要解析的子域名,只允许填写一个。如果想解析*子域名,请填写 \\*") sub = s:option(Value, "sub_domain", translate("子域名"), "想要解析的子域名,只允许填写一个。如果想解析*子域名,请填写 \\*")
wait = s:option(Value, "wait_second", translate("更新周期(s)"), "请填写数字默认为300s") wait = s:option(Value, "wait_second", translate("更新周期(s)"), "请填写数字默认为300s")
command = s:option(Value, "command_to_get_ip", translate("外网ip获取命令"), "默认为 curl -s whatismyip.akamai.com")
local apply = luci.http.formvalue("cbi.apply") local apply = luci.http.formvalue("cbi.apply")
if apply then if apply then