From d30d285b4d9b135021043ef95839a972ecf0d337 Mon Sep 17 00:00:00 2001 From: zxl hhyccc <45259624+zxlhhyccc@users.noreply.github.com> Date: Sun, 8 Dec 2024 14:00:19 +0800 Subject: [PATCH] luci-app-openclash: Optimized acquire `dnsmasq` configuration file path code. (#4215) * luci-app-openclash: Optimized acquire `dnsmasq` configuration file path code. --- luci-app-openclash/Makefile | 9 ++++++--- luci-app-openclash/root/etc/init.d/openclash | 16 +++++++++------- .../openclash/openclash_custom_domain_dns.sh | 18 ++++++++++-------- 3 files changed, 25 insertions(+), 18 deletions(-) diff --git a/luci-app-openclash/Makefile b/luci-app-openclash/Makefile index 82faf9578f..53f49f6846 100644 --- a/luci-app-openclash/Makefile +++ b/luci-app-openclash/Makefile @@ -122,9 +122,12 @@ endef define Package/$(PKG_NAME)/postrm #!/bin/sh - DEFAULT_DNSMASQ_CFGID=$(uci show dhcp.@dnsmasq[0] | awk -F '.' '{print $2}' | awk -F '=' '{print $1}' | head -1) - DNSMASQ_CONF_PATH=$(grep -l "^conf-dir=" "/tmp/etc/dnsmasq.conf.${DEFAULT_DNSMASQ_CFGID}") - DNSMASQ_CONF_DIR=$(grep '^conf-dir=' "$DNSMASQ_CONF_PATH" | cut -d'=' -f2 | head -n 1) + DEFAULT_DNSMASQ_CFGID="$(uci -q show "dhcp.@dnsmasq[0]" | awk 'NR==1 {split($0, conf, /[.=]/); print conf[2]}')" + 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 DNSMASQ_CONF_DIR=${DNSMASQ_CONF_DIR%*/} rm -rf /etc/openclash >/dev/null 2>&1 rm -rf /etc/config/openclash >/dev/null 2>&1 diff --git a/luci-app-openclash/root/etc/init.d/openclash b/luci-app-openclash/root/etc/init.d/openclash index a09b1e3d6c..c96e302bb0 100644 --- a/luci-app-openclash/root/etc/init.d/openclash +++ b/luci-app-openclash/root/etc/init.d/openclash @@ -14,13 +14,15 @@ BACKUP_FILE="/etc/openclash/backup/$(uci -q get openclash.config.config_path |aw CONFIG_FILE="/etc/openclash/$(uci -q get openclash.config.config_path |awk -F '/' '{print $5}' 2>/dev/null)" TMP_CONFIG_FILE="/tmp/yaml_config_tmp_$(uci -q get openclash.config.config_path |awk -F '/' '{print $5}' 2>/dev/null)" FW4=$(command -v fw4) -# Get the default DNSMasq config ID from the UCI configuration -DEFAULT_DNSMASQ_CFGID=$(uci show dhcp.@dnsmasq[0] | awk -F '.' '{print $2}' | awk -F '=' '{print $1}' | head -1) -# Locate the dnsmasq.conf file that contains the conf-dir option -DNSMASQ_CONF_PATH=$(grep -l "^conf-dir=" "/tmp/etc/dnsmasq.conf.${DEFAULT_DNSMASQ_CFGID}") -# Extract the directory path from the conf-dir line -DNSMASQ_CONF_DIR=$(grep '^conf-dir=' "$DNSMASQ_CONF_PATH" | cut -d'=' -f2 | head -n 1) -# Check if a conf-dir value was found and set variables accordingly +# 获取默认的 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 +# 设置 DNSMASQ_CONF_DIR,并去除路径末尾的斜杠 DNSMASQ_CONF_DIR=${DNSMASQ_CONF_DIR%*/} } CLASH="/etc/openclash/clash" diff --git a/luci-app-openclash/root/usr/share/openclash/openclash_custom_domain_dns.sh b/luci-app-openclash/root/usr/share/openclash/openclash_custom_domain_dns.sh index c3962e1647..22b353ff46 100644 --- a/luci-app-openclash/root/usr/share/openclash/openclash_custom_domain_dns.sh +++ b/luci-app-openclash/root/usr/share/openclash/openclash_custom_domain_dns.sh @@ -13,13 +13,15 @@ del_lock() { set_lock -# Get the default DNSMasq config ID from the UCI configuration -DEFAULT_DNSMASQ_CFGID=$(uci show dhcp.@dnsmasq[0] | awk -F '.' '{print $2}' | awk -F '=' '{print $1}' | head -1) -# Locate the dnsmasq.conf file that contains the conf-dir option -DNSMASQ_CONF_PATH=$(grep -l "^conf-dir=" "/tmp/etc/dnsmasq.conf.${DEFAULT_DNSMASQ_CFGID}") -# Extract the directory path from the conf-dir line -DNSMASQ_CONF_DIR=$(grep '^conf-dir=' "$DNSMASQ_CONF_PATH" | cut -d'=' -f2 | head -n 1) -# Check if a conf-dir value was found and set variables accordingly +# 获取默认的 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 +# 设置 DNSMASQ_CONF_DIR,并去除路径末尾的斜杠 DNSMASQ_CONF_DIR=${DNSMASQ_CONF_DIR%*/} rm -rf ${DNSMASQ_CONF_DIR}/dnsmasq_openclash_custom_domain.conf >/dev/null 2>&1 if [ "$(uci get openclash.config.enable_custom_domain_dns_server 2>/dev/null)" = "1" ] && [ "$(uci get openclash.config.enable_redirect_dns 2>/dev/null)" = "1" ]; then @@ -36,4 +38,4 @@ if [ "$(uci get openclash.config.enable_custom_domain_dns_server 2>/dev/null)" = fi fi -del_lock \ No newline at end of file +del_lock