Browse Source

luci: support config speed check mode

Nick Peng 2 years ago
parent
commit
5bc8b3ad62

+ 9 - 0
package/luci-compat/files/luci/i18n/smartdns.zh-cn.po

@@ -369,6 +369,15 @@ msgstr ""
 msgid "Smartdns server name"
 msgstr "SmartDNS的服务器名称,默认为smartdns,留空为主机名"
 
+msgid "Speed check mode is invalid."
+msgstr "测速模式无效。"
+
+msgid "Speed Check Mode"
+msgstr "测速模式"
+
+msgid "Smartdns speed check mode. "
+msgstr "SmartDns测速模式设置。"
+
 msgid ""
 "Specify an IP address to return for any host in the given domains, Queries "
 "in the domains are never forwarded and always replied to with the specified "

+ 60 - 3
package/luci-compat/files/luci/model/cbi/smartdns/smartdns.lua

@@ -55,6 +55,52 @@ o.default     = 53
 o.datatype    = "port"
 o.rempty      = false
 
+---- Speed check mode;
+o = s:taboption("advanced", Value, "speed_check_mode", translate("Speed Check Mode"), translate("Smartdns speed check mode."));
+o.rmempty = true;
+o.placeholder = "default";
+o.default = o.enabled;
+o:value("ping,tcp:80,tcp:443");
+o:value("ping,tcp:443,tcp:80");
+o:value("tcp:80,tcp:443,ping");
+o:value("tcp:443,tcp:80,ping");
+o:value("none", translate("None"));
+function o.validate (section_id, value) 
+    if value == "" then
+        return value
+    end
+
+    if value == nil then
+        return nil, translate("Speed check mode is invalid.")
+    end
+
+    if value == "none" then
+        return value
+    end
+
+    local mode = value:split(",");
+    for _, v in ipairs(mode) do repeat
+        if v == "ping" then
+            break
+        end
+
+        if v == nil then
+            return nil, translate("Speed check mode is invalid.")
+        end
+        
+        local port = v:split(":");
+        if "tcp" == port[1] then
+            if tonumber(port[2]) then
+                break
+            end
+        end
+        
+        return nil, translate("Speed check mode is invalid.")
+    until true end
+
+    return value
+end
+
 ---- Enable TCP server
 o = s:taboption("advanced", Flag, "tcp_server", translate("TCP Server"), translate("Enable TCP DNS Server"))
 o.rmempty     = false
@@ -337,14 +383,21 @@ o.datatype = "hostname"
 o.rempty = true
 uci:foreach("smartdns", "server", function(section)
     local server_group = section.server_group
+    if server_group == nil then
+        return
+    end
     o:value(server_group);
 end)
 
 function o.validate (section_id, value) 
-    if (value == "") then
+    if value == "" then
         return value
     end
 
+    if value == nil then
+        return nil, translate('Server Group not exists')
+    end
+
     local exists = false
     uci:foreach("smartdns", "server", function(section)
         local server_group = section.server_group
@@ -357,7 +410,7 @@ function o.validate (section_id, value)
         end
     end)
 
-    if (exists == false) then
+    if exists == false then
         return nil, translate('Server Group not exists')
     end
 
@@ -517,7 +570,11 @@ o.rmempty = false
 o.datatype = 'string'
 function o.validate(self, value, section)
     if value == "" then
-        return nil
+        return nil, translate("URL format error, format: http:// or https://")
+    end
+
+    if value == nil then
+        return nil, translate("URL format error, format: http:// or https://")
     end
 
     if value.find(value, "http://") then

+ 15 - 0
package/luci/files/luci/i18n/smartdns.zh-cn.po

@@ -75,6 +75,9 @@ msgstr "协议类型"
 msgid "DNS domain result cache size"
 msgstr "缓存DNS的结果,缓存大小,配置零则不缓存"
 
+msgid "default"
+msgstr "默认"
+
 msgid "Description"
 msgstr "描述"
 
@@ -265,6 +268,9 @@ msgstr "NFTSet名称,格式:[#[4|6]:[family#table#set]]"
 msgid "NOT RUNNING"
 msgstr "未运行"
 
+msgid "No"
+msgstr "否"
+
 msgid "No check certificate"
 msgstr "停用证书校验"
 
@@ -401,6 +407,12 @@ msgstr ""
 msgid "Smartdns server name"
 msgstr "SmartDNS的服务器名称,默认为smartdns,留空为主机名"
 
+msgid "Smartdns speed check mode."
+msgstr "SmartDNS测速模式。"
+
+msgid "Speed Check Mode"
+msgstr "测速模式"
+
 msgid ""
 "Specify an IP address to return for any host in the given domains, Queries "
 "in the domains are never forwarded and always replied to with the specified "
@@ -509,3 +521,6 @@ msgstr "类型"
 
 msgid "udp"
 msgstr "udp"
+
+msgid "Yes"
+msgstr "是"

+ 88 - 4
package/luci/files/root/www/luci-static/resources/view/smartdns/smartdns.js

@@ -158,6 +158,46 @@ return view.extend({
 		///////////////////////////////////////
 		// advanced settings;
 		///////////////////////////////////////
+		// Speed check mode;
+		o = s.taboption("advanced", form.Value, "speed_check_mode", _("Speed Check Mode"), _("Smartdns speed check mode."));
+		o.rmempty = true;
+		o.placeholder = "default";
+		o.value("", _("default"));
+		o.value("ping,tcp:80,tcp:443");
+		o.value("ping,tcp:443,tcp:80");
+		o.value("tcp:80,tcp:443,ping");
+		o.value("tcp:443,tcp:80,ping");
+		o.value("none", _("None"));
+		o.validate = function (section_id, value) {
+			if (value == "") {
+				return true;
+			}
+
+			if (value == "none") {
+				return true;
+			}
+
+			var check_mode = value.split(",")
+			for (var i = 0; i < check_mode.length; i++) {
+				if (check_mode[i] == "ping") {
+					continue;
+				}
+
+				if (check_mode[i].indexOf("tcp:") == 0) {
+					var port = check_mode[i].split(":")[1];
+					if (port == "") {
+						return _("TCP port is empty");
+					}
+
+					continue;
+				}
+
+				return _("Speed check mode is invalid.");
+			}
+
+			return true;
+		}
+
 		// Enable TCP server;
 		o = s.taboption("advanced", form.Flag, "tcp_server", _("TCP Server"), _("Enable TCP DNS Server"));
 		o.rmempty = false;
@@ -726,7 +766,7 @@ return view.extend({
 
 		so = ss.option(form.FileUpload, "domain_list_file", _("Domain List File"),
 			_("Upload domain list file, or configure auto download from Download File Setting page."));
-		so.rmempty = true
+		so.rmempty = false
 		so.datatype = "file"
 		so.rempty = true
 		so.root_directory = "/etc/smartdns/domain-set"
@@ -739,11 +779,55 @@ return view.extend({
 		so.value("ipv6", "IPv6");
 		so.modalonly = true;
 
-		so = ss.option(form.Flag, "no_speed_check", _("Skip Speed Check"),
-			_("Do not check speed."));
+		// Support DualStack ip selection;
+		so = ss.option(form.ListValue, "dualstack_ip_selection", _("Dual-stack IP Selection"),
+			_("Enable IP selection between IPV4 and IPV6"));
 		so.rmempty = true;
-		so.default = so.disabled;
+		so.default = "default";
+		so.modalonly = true;
+		so.value("", _("default"));
+		so.value("yes", _("Yes"));
+		so.value("no", _("No"));
+
+		so = ss.option(form.Value, "speed_check_mode", _("Speed Check Mode"), _("Smartdns speed check mode."));
+		so.rmempty = true;
+		so.placeholder = "default";
 		so.modalonly = true;
+		so.value("", _("default"));
+		so.value("ping,tcp:80,tcp:443");
+		so.value("ping,tcp:443,tcp:80");
+		so.value("tcp:80,tcp:443,ping");
+		so.value("tcp:443,tcp:80,ping");
+		so.value("none", _("None"));
+		so.validate = function (section_id, value) {
+			if (value == "") {
+				return true;
+			}
+
+			if (value == "none") {
+				return true;
+			}
+
+			var check_mode = value.split(",")
+			for (var i = 0; i < check_mode.length; i++) {
+				if (check_mode[i] == "ping") {
+					continue;
+				}
+
+				if (check_mode[i].indexOf("tcp:") == 0) {
+					var port = check_mode[i].split(":")[1];
+					if (port == "") {
+						return _("TCP port is empty");
+					}
+
+					continue;
+				}
+
+				return _("Speed check mode is invalid.");
+			}
+
+			return true;
+		}
 
 		so = ss.option(form.Flag, "force_aaaa_soa", _("Force AAAA SOA"), _("Force AAAA SOA."));
 		so.rmempty = true;

+ 11 - 4
package/openwrt/files/etc/init.d/smartdns

@@ -278,7 +278,7 @@ load_domain_rules()
 	config_get ipset_name "$section" "ipset_name" ""
 	[ ! -z "$ipset_name" ] && domain_set_args="$domain_set_args -ipset $ipset_name"
 
-	config_get ipset_name "$section" "nftset_name" ""
+	config_get nftset_name "$section" "nftset_name" ""
 	[ ! -z "$nftset_name" ] && domain_set_args="$domain_set_args -nftset '$nftset_name'"
 
 	config_get forwarding_domain_set_file "$section" "forwarding_domain_set_file" ""
@@ -317,8 +317,12 @@ load_domain_rule_list()
 	[ "$block_domain_type" = "ipv4" ] && domain_set_args="$domain_set_args -address #4"
 	[ "$block_domain_type" = "ipv6" ] && domain_set_args="$domain_set_args -address #6"
 
-	config_get_bool no_speed_check "$section" "no_speed_check" "0"
-	[ "$no_speed_check" = "1" ] && domain_set_args="$domain_set_args -speed-check-mode none"
+	config_get speed_check_mode "$section" "speed_check_mode" ""
+	[ ! -z "$speed_check_mode" ] && domain_set_args="$domain_set_args -speed-check-mode $speed_check_mode"
+
+	config_get dualstack_ip_selection "$section" "dualstack_ip_selection" ""
+	[ "$dualstack_ip_selection" = "no" ] && domain_set_args="$domain_set_args -dualstack-ip-selection no"
+	[ "$dualstack_ip_selection" = "yes" ] && domain_set_args="$domain_set_args -dualstack-ip-selection yes"
 
 	config_get_bool force_aaaa_soa "$section" "force_aaaa_soa" "0"
 	[ "$force_aaaa_soa" = "1" ] && domain_set_args="$domain_set_args -address #6"
@@ -326,7 +330,7 @@ load_domain_rule_list()
 	config_get ipset_name "$section" "ipset_name" ""
 	[ ! -z "$ipset_name" ] && domain_set_args="$domain_set_args -ipset $ipset_name"
 
-	config_get ipset_name "$section" "nftset_name" ""
+	config_get nftset_name "$section" "nftset_name" ""
 	[ ! -z "$nftset_name" ] && domain_set_args="$domain_set_args -nftset '$nftset_name'"
 
 	config_get domain_list_file "$section" "domain_list_file" ""
@@ -429,6 +433,9 @@ load_service()
 	config_get ipv6_server "$section" "ipv6_server" "1"
 	config_get tcp_server "$section" "tcp_server" "1"
 
+	config_get speed_check_mode "$section" "speed_check_mode" ""
+	[ ! -z "$speed_check_mode" ] && conf_append "speed-check-mode" "$speed_check_mode"
+
 	config_get dualstack_ip_selection "$section" "dualstack_ip_selection" "0"
 	[ "$dualstack_ip_selection" = "0" ] && conf_append "dualstack-ip-selection" "no"