Browse Source

Merge pull request #1625 from zxlhhyccc/ssrp

luci-app-ssr-plus: Optimized acquire `dnsmasq` configuration file path code.
coolsnowwolf 10 months ago
parent
commit
4af6d727d8

+ 12 - 10
luci-app-ssr-plus/root/etc/init.d/shadowsocksr

@@ -18,16 +18,18 @@ LOG_FILE=/var/log/ssrplus.log
 TMP_PATH=/var/etc/ssrplus
 TMP_BIN_PATH=$TMP_PATH/bin
 # 设置 DNSMASQ_CONF_DIR 和 TMP_DNSMASQ_PATH
-[ -f /etc/openwrt_release ] && {
-# 获取默认的 DNSMasq 配置 ID
-DEFAULT_DNSMASQ_CFGID=$(uci show dhcp.@dnsmasq[0] | awk -F '.' '{print $2}' | awk -F '=' '{print $1}' | head -n 1)
-# 查找包含 conf-dir 选项的 dnsmasq.conf 文件路径
-DNSMASQ_CONF_PATH=$(grep -l "^conf-dir=" "/tmp/etc/dnsmasq.conf.${DEFAULT_DNSMASQ_CFGID}")
-# 从 conf-dir 行中提取目录路径
-DNSMASQ_CONF_DIR=$(grep '^conf-dir=' "$DNSMASQ_CONF_PATH" | cut -d'=' -f2 | head -n 1)
-# 设置 TMP_DNSMASQ_PATH,并去除路径末尾的斜杠
-TMP_DNSMASQ_PATH="${DNSMASQ_CONF_DIR%*/}/dnsmasq-ssrplus.d"
-}
+if [ -f /etc/openwrt_release ]; then
+    # 获取默认的 DNSMASQ 配置 ID
+    DEFAULT_DNSMASQ_CFGID="$(uci -q show "dhcp.@dnsmasq[0]" | awk 'NR==1 {split($0, conf, /[.=]/); print conf[2]}')"
+    # 从 conf-dir 行中提取配置目录路径
+    if [ -f "/tmp/etc/dnsmasq.conf.$DEFAULT_DNSMASQ_CFGID" ]; then
+        DNSMASQ_CONF_DIR="$(awk -F '=' '/^conf-dir=/ {print $2}' "/tmp/etc/dnsmasq.conf.$DEFAULT_DNSMASQ_CFGID")"
+    else
+        DNSMASQ_CONF_DIR="/tmp/dnsmasq.d"
+    fi
+    # 设置 TMP_DNSMASQ_PATH,并去除路径末尾的斜杠
+    TMP_DNSMASQ_PATH="${DNSMASQ_CONF_DIR%*/}/dnsmasq-ssrplus.d"
+fi
 
 chain_config_file=		   #generate shadowtls chain proxy config file
 tcp_config_file=

+ 11 - 27
luci-app-ssr-plus/root/usr/share/shadowsocksr/update.lua

@@ -11,40 +11,24 @@ local args = arg[1]
 local uci = luci.model.uci.cursor()
 
 -- 以下设置更新数据库至 DNSMASQ 配置路径
--- 获取 DNSMasq 配置 ID
-local DEFAULT_DNSMASQ_CFGID = uci:get_first("dhcp", "dnsmasq", ".name")
+-- 获取 DNSMASQ 配置 ID
+local DNSMASQ_UCI_CONFIG = uci:get_first("dhcp", "dnsmasq", ".name")
 
-if not DEFAULT_DNSMASQ_CFGID then
-    error("未找到默认的 DNSMasq 配置 ID")
-end
-
--- 查找包含 conf-dir 选项的 dnsmasq.conf 文件路径
-local DNSMASQ_CONF_PATH_CMD = string.format("grep -l '^conf-dir=' /tmp/etc/dnsmasq.conf.%s*", DEFAULT_DNSMASQ_CFGID)
-local DNSMASQ_CONF_PATH = io.popen(DNSMASQ_CONF_PATH_CMD):read("*l")
-
-if not DNSMASQ_CONF_PATH or DNSMASQ_CONF_PATH:match("^%s*$") then
-    error("无法找到包含 conf-dir 选项的 dnsmasq.conf 文件路径")
-end
-
-DNSMASQ_CONF_PATH = DNSMASQ_CONF_PATH:gsub("%s+", "") -- 去除空白字符
+-- 获取 DNSMASQ 默认配置文件
+local DNSMASQ_CONF_PATH = "/tmp/etc/dnsmasq.conf." .. DNSMASQ_UCI_CONFIG
 
--- 获取 DNSMASQ 配置路径
-local DNSMASQ_CONF_DIR_CMD = string.format("grep '^conf-dir=' %s | cut -d'=' -f2 | head -n 1", DNSMASQ_CONF_PATH)
-local DNSMASQ_CONF_DIR = io.popen(DNSMASQ_CONF_DIR_CMD):read("*l")
-
-if not DNSMASQ_CONF_DIR or DNSMASQ_CONF_DIR:match("^%s*$") then
-    error("无法提取 conf-dir 配置,请检查 dnsmasq.conf 文件内容")
+-- 检查 DNSMASQ 配置文件是否存在,如果存在则提取 conf-dir
+for line in io.lines(DNSMASQ_CONF_PATH) do
+    local conf_dir = line:match("^conf%-dir=(.+)")
+    if conf_dir then
+        DNSMASQ_CONF_DIR = conf_dir:gsub("%s+", "") -- 去除空白字符
+        break
+    end
 end
 
-DNSMASQ_CONF_DIR = DNSMASQ_CONF_DIR:gsub("%s+", "") -- 去除空白字符
-
 -- 设置 dnsmasq-ssrplus.d 目录路径,并去除路径末尾的斜杠
 local TMP_DNSMASQ_PATH = DNSMASQ_CONF_DIR:match("^(.-)/?$") .. "/dnsmasq-ssrplus.d"
 
-if not TMP_DNSMASQ_PATH or TMP_DNSMASQ_PATH:match("^%s*$") then
-    error("无法找到包含 dnsmasq 选项的 dnsmasq-ssrplus.d 目录路径")
-end
-
 local TMP_PATH = "/var/etc/ssrplus"
 -- match comments/title/whitelist/ip address/excluded_domain
 local comment_pattern = "^[!\\[@]+"