From cbb887bdd849ce143e14df1f260531ac443a76c1 Mon Sep 17 00:00:00 2001 From: coolsnowwolf Date: Fri, 4 May 2018 14:40:33 +0800 Subject: [PATCH] add luci-app-dnspod package --- package/lean/luci-app-dnspod/Makefile | 51 +++++ package/lean/luci-app-dnspod/files/dnspod.sh | 193 ++++++++++++++++++ .../files/root/etc/config/dnspod | 6 + .../files/root/etc/init.d/dnspod | 18 ++ .../usr/lib/lua/luci/controller/dnspod.lua | 5 + .../usr/lib/lua/luci/model/cbi/dnspod.lua | 32 +++ 6 files changed, 305 insertions(+) create mode 100644 package/lean/luci-app-dnspod/Makefile create mode 100755 package/lean/luci-app-dnspod/files/dnspod.sh create mode 100644 package/lean/luci-app-dnspod/files/root/etc/config/dnspod create mode 100755 package/lean/luci-app-dnspod/files/root/etc/init.d/dnspod create mode 100644 package/lean/luci-app-dnspod/files/root/usr/lib/lua/luci/controller/dnspod.lua create mode 100644 package/lean/luci-app-dnspod/files/root/usr/lib/lua/luci/model/cbi/dnspod.lua diff --git a/package/lean/luci-app-dnspod/Makefile b/package/lean/luci-app-dnspod/Makefile new file mode 100644 index 000000000..6d55ff6c6 --- /dev/null +++ b/package/lean/luci-app-dnspod/Makefile @@ -0,0 +1,51 @@ +include $(TOPDIR)/rules.mk + +PKG_NAME:=luci-app-dnspod +PKG_VERSION=1.0 +PKG_RELEASE:=1 + +PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME) + +include $(INCLUDE_DIR)/package.mk +include $(INCLUDE_DIR)/host-build.mk + +define Package/luci-app-dnspod + SECTION:=luci + CATEGORY:=LuCI + SUBMENU:=3. Applications + TITLE:=Auto DNS for dnspod + PKGARCH:=all + DEPENDS:= +curl +libcurl +endef + +define Package/luci-app-dnspod/description + auto get pub ip,dnspod ddns,must include curl with ssl +endef + +define Build/Prepare +endef + +define Build/Configure +endef + +define Build/Compile +endef + +define Package/luci-app-dnspod/install + $(INSTALL_DIR) $(1)/etc/config + $(INSTALL_DIR) $(1)/etc/init.d + $(INSTALL_DIR) $(1)/usr/lib/lua/luci/model/cbi + $(INSTALL_DIR) $(1)/usr/lib/lua/luci/controller + + $(INSTALL_CONF) ./files/root/etc/config/dnspod $(1)/etc/config/dnspod + $(INSTALL_BIN) ./files/root/etc/init.d/dnspod $(1)/etc/init.d/dnspod + $(INSTALL_DATA) ./files/root/usr/lib/lua/luci/model/cbi/dnspod.lua $(1)/usr/lib/lua/luci/model/cbi/dnspod.lua + $(INSTALL_DATA) ./files/root/usr/lib/lua/luci/controller/dnspod.lua $(1)/usr/lib/lua/luci/controller/dnspod.lua + $(INSTALL_DIR) $(1)/usr/sbin + $(INSTALL_BIN) ./files/dnspod.sh $(1)/usr/sbin +endef + +$(eval $(call BuildPackage,luci-app-dnspod)) + + + diff --git a/package/lean/luci-app-dnspod/files/dnspod.sh b/package/lean/luci-app-dnspod/files/dnspod.sh new file mode 100755 index 000000000..55366f4b4 --- /dev/null +++ b/package/lean/luci-app-dnspod/files/dnspod.sh @@ -0,0 +1,193 @@ +#!/bin/sh +#written by benson huang +#admin@zhetenger.com +#http://www.terryche.me/openwrt-dnspod-ddns.html + +#dnspod帐号名 +email=`uci get dnspod.base_arg.login_email` + +#dnspod密码 +password=`uci get dnspod.base_arg.login_password 2>/dev/null` + +#域名 +domainname=`uci get dnspod.base_arg.main_domain 2>/dev/null` + +#需要更新的子域名列表,多个的话,以空格分割。 +#例如: +# sub_domains='www home' +sub_domains=`uci get dnspod.base_arg.sub_domain 2>/dev/null` + +#check for changed ip every 300 seconds +wait=300 + +#检查是否安装curl +curl_status=`which curl 2>/dev/null` +[ -n "$curl_status" ] || { echo "curl is not installed";exit 3; } + +os=$(uname -a | egrep -io 'openwrt' | tr [A-Z] [a-z]) + +API_url="https://dnsapi.cn" +format='json' +lang='en' +record_type='A' +offset="2" +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 + +printMsg() { + local msg="$1" + if [ $is_svc -eq 1 ];then + logger -t ${PROGRAM} "${msg}" + else + echo $msg + fi +} + +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://members.3322.org/dyndns/getip +} + +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() { + local action="$1" + local extra_options="$2" + eval "curl -k -A 'xddns' ${API_url}/${action} ${common_options} ${extra_options}" +} + +getDomainId() { + local extra_options="--data-urlencode \"domain=${domainname}\"" + json_data=$(execAPI "Domain.info" "${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 +} + +updateRecord() { + local record_id_list + local extra_options + local tmp=${sub_domains} + local tmp_sub_domains + printMsg 'Start update records' + record_id_list=$(getRecordId) + printMsg "Records IDs: ${record_id_list}" + for record_id in $record_id_list;do + tmp_sub_domains=$(echo $tmp | awk '{ print $1 }') + tmp=${tmp#* } + extra_options="--data-urlencode \"domain_id=${domain_id}\"\ + --data-urlencode \"record_id=${record_id}\"\ + --data-urlencode \"sub_domain=${tmp_sub_domains}\"\ + --data-urlencode \"record_type=${record_type}\"\ + --data-urlencode \"record_line=默认\"\ + --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() { + local oldip=$pub_ip_addr + pub_ip_addr=$(getIp) + printMsg "old ip: [$oldip], new ip: [$pub_ip_addr]" + if [ "$pub_ip_addr" != "$oldip" ];then + return 8 + else + return 3 + 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() { + local ip + #check that whether the network is ok + while [ 1 ];do + ip=$(getIp) + if [ -n "$ip" ];then + printMsg "WAN IP: ${ip}" + break; + else + printMsg "Can't get wan ip" + sleep 30 + fi + done + + domain_id=`getDomainId $domainname` + printMsg "domain_id: ${domain_id}" + while [ 1 ];do + checkip + if [ $? -eq 8 ];then + updateRecord + #updateTunnelBroker + fi + sleep $wait + done +} + +execUpdate() { + domain_id=`getDomainId $domainname` + pub_ip_addr=$(getIp) + updateRecord +} + +case $1 in + --svc) + is_svc=1; + printMsg 'Start in Service mode'; + printMsg "domain: ${domainname}, sub_domains: ${sub_domains}"; + execSvc;; + --ipv6) + is_svc=0; + printMsg "Start update HE Tunnel Broker"; + updateTunnelBroker;; + *) + is_svc=0; + printMsg "Start update record, domain: ${domainname}, sub_domains: ${sub_domains}"; + execUpdate;; +esac diff --git a/package/lean/luci-app-dnspod/files/root/etc/config/dnspod b/package/lean/luci-app-dnspod/files/root/etc/config/dnspod new file mode 100644 index 000000000..6cebd6d7a --- /dev/null +++ b/package/lean/luci-app-dnspod/files/root/etc/config/dnspod @@ -0,0 +1,6 @@ +config base_arg "base_arg" + option login_email '' + option login_password '' + option main_domain '' + option sub_domain '' + diff --git a/package/lean/luci-app-dnspod/files/root/etc/init.d/dnspod b/package/lean/luci-app-dnspod/files/root/etc/init.d/dnspod new file mode 100755 index 000000000..70c2a1253 --- /dev/null +++ b/package/lean/luci-app-dnspod/files/root/etc/init.d/dnspod @@ -0,0 +1,18 @@ +#!/bin/sh /etc/rc.common +START=80 + +run_reboot() +{ + killall dnspod.sh + /sbin/dnspod.sh --svc +} + +start() +{ + /sbin/dnspod.sh --svc +} + +stop() +{ + killall dnspod.sh +} diff --git a/package/lean/luci-app-dnspod/files/root/usr/lib/lua/luci/controller/dnspod.lua b/package/lean/luci-app-dnspod/files/root/usr/lib/lua/luci/controller/dnspod.lua new file mode 100644 index 000000000..56517f10f --- /dev/null +++ b/package/lean/luci-app-dnspod/files/root/usr/lib/lua/luci/controller/dnspod.lua @@ -0,0 +1,5 @@ +module("luci.controller.dnspod", package.seeall) + +function index() + entry({"admin", "network", "dnspod"}, cbi("dnspod"), _("动态DNSPOD"), 100) +end diff --git a/package/lean/luci-app-dnspod/files/root/usr/lib/lua/luci/model/cbi/dnspod.lua b/package/lean/luci-app-dnspod/files/root/usr/lib/lua/luci/model/cbi/dnspod.lua new file mode 100644 index 000000000..a36605037 --- /dev/null +++ b/package/lean/luci-app-dnspod/files/root/usr/lib/lua/luci/model/cbi/dnspod.lua @@ -0,0 +1,32 @@ +--[[ +LuCI - Lua Configuration Interface + +Copyright 2010 Jo-Philipp Wich + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 +]]-- + +require("luci.sys") + +m = Map("dnspod", translate("动态DNSPOD"), translate("配置动态DNSPOD")) + +s = m:section(TypedSection, "base_arg", "") +s.addremove = false +s.anonymous = true + +email = s:option(Value, "login_email", translate("登录邮箱"),"登录DNSPOD的用户名") +pass = s:option(Value, "login_password", translate("登录密码"),"登录DNSPOD的密码") +pass.password=true +main = s:option(Value, "main_domain", translate("主域名"),"想要解析的主域名,例如:baidu.com") +sub = s:option(Value, "sub_domain", translate("子域名"),"想要解析的子域名,多个以空格分开,例如:test hehe") + +local apply = luci.http.formvalue("cbi.apply") +if apply then + io.popen("/etc/init.d/dnspod restart &") +end + +return m