From 87bb44930344a9895b3c2877c3fe249bdc995ed8 Mon Sep 17 00:00:00 2001 From: fscarmen <62703343+fscarmen@users.noreply.github.com> Date: Fri, 27 Mar 2026 10:56:29 +0800 Subject: [PATCH] feat: add prefer_go option for local DNS and enhance system compatibility. https://github.com/fscarmen/sing-box/issues/280 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Set `prefer_go: true` for the local DNS server to resolve issues where `systemd-resolved` might be unavailable or inaccessible on certain Linux distributions (e.g., Alpine Linux, lightweight Debian). - Improve DNS resolution reliability by prioritizing Go's internal resolver over the system's C library or DBus interfaces. 功能:为本地 DNS 添加 prefer_go 选项并增强系统兼容性 - 为本地 (local) DNS 服务器设置 `prefer_go: true`,以解决在某些 Linux 发行版(如 Alpine Linux、精简版 Debian)上因缺少 `systemd-resolved` 服务而导致的解析失败或报错问题。 - 通过优先使用 Go 内置的解析器而非系统的 C 库或 DBus 接口,提升 DNS 解析的可靠性和一致性。 --- sing-box.sh | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/sing-box.sh b/sing-box.sh index 8135f95..efbf714 100644 --- a/sing-box.sh +++ b/sing-box.sh @@ -1542,8 +1542,9 @@ sing-box_variables() { } check_dependencies() { - # 如果是 Alpine,先升级 wget ,安装 systemctl-py 版 + # 如果是 Alpine,先升级 wget if [ "$SYSTEM" = 'Alpine' ]; then + IS_PREFER_GO=true local CHECK_WGET=$(wget 2>&1 | sed -n 1p) grep -qi 'busybox' <<< "$CHECK_WGET" && ${PACKAGE_INSTALL[int]} wget >/dev/null 2>&1 local DEPS_CHECK=("bash" "rc-update" "iptables" "ip6tables") @@ -1556,6 +1557,9 @@ check_dependencies() { ${PACKAGE_UPDATE[int]} >/dev/null 2>&1 ${PACKAGE_INSTALL[int]} ${DEPS_ALPINE[@]} >/dev/null 2>&1 fi + else + # 非 Alpine 系统,检查 systemd-resolved 状态,用于 DNS 配置里的 prefer_go 字段 + [ -x "$(type -p systemctl)" ] && systemctl is-active --quiet systemd-resolved && IS_PREFER_GO=false || IS_PREFER_GO=true fi # 检测 Linux 系统的依赖,升级库并重新安装依赖 @@ -1941,7 +1945,8 @@ EOF "dns":{ "servers":[ { - "type":"local" + "type":"local", + "prefer_go": ${IS_PREFER_GO} } ], "strategy": "${STRATEGY}" @@ -4168,4 +4173,4 @@ elif [ "$IS_FAST_INSTALL" = 'is_fast_install' ]; then else menu_setting menu -fi \ No newline at end of file +fi