fix: dnsmasq path detection and WiFi rfkill persistence

- dns-hijack.js: replace `which` with multi-path search (/usr/sbin etc.)
  since dnsmasq is often not in normal user PATH
- install.sh: auto-enable NetworkManager for WiFi provisioning
- install.sh: unblock WiFi rfkill at install time
- install.sh: create clawd-rfkill.service for persistent boot-time unlock

Made-with: Cursor
This commit is contained in:
stswangzhiping
2026-03-16 10:06:30 +08:00
parent eb9f4ab1c3
commit 8a6e5b3666
3 changed files with 64 additions and 5 deletions

7
.commitmsg Normal file
View File

@@ -0,0 +1,7 @@
fix: dnsmasq path detection and WiFi rfkill persistence
- dns-hijack.js: replace `which` with multi-path search (/usr/sbin etc.)
since dnsmasq is often not in normal user PATH
- install.sh: auto-enable NetworkManager for WiFi provisioning
- install.sh: unblock WiFi rfkill at install time
- install.sh: create clawd-rfkill.service for persistent boot-time unlock

View File

@@ -48,6 +48,47 @@ if command -v dnsmasq &>/dev/null; then
info "dnsmasq ✓"
fi
# ── 启用 NetworkManagerWiFi 配网需要)──────────────────────────────────────
if command -v nmcli &>/dev/null; then
if ! systemctl is-active --quiet NetworkManager 2>/dev/null; then
info "启用 NetworkManager..."
systemctl enable --now NetworkManager 2>/dev/null || true
fi
info "NetworkManager ✓"
fi
# ── WiFi rfkill 解锁(部分设备默认禁用 WiFi────────────────────────────────
for rf in /sys/class/rfkill/rfkill*; do
if [ -f "$rf/type" ] && [ "$(cat "$rf/type")" = "wlan" ]; then
if [ "$(cat "$rf/soft")" = "1" ]; then
info "解锁 WiFi ($(basename "$rf"))..."
echo 0 > "$rf/soft"
fi
fi
done
# 持久化:创建 systemd 服务确保开机自动解锁 WiFi
RFKILL_SERVICE="/etc/systemd/system/clawd-rfkill.service"
if [ ! -f "$RFKILL_SERVICE" ]; then
cat > "$RFKILL_SERVICE" <<EOF
[Unit]
Description=Unblock WiFi for clawd
Before=NetworkManager.service clawd.service
After=sys-subsystem-net-devices-wlan0.device
[Service]
Type=oneshot
ExecStart=/bin/sh -c 'for rf in /sys/class/rfkill/rfkill*; do [ "\$(cat \$rf/type)" = "wlan" ] && echo 0 > \$rf/soft; done'
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable clawd-rfkill
info "WiFi rfkill 解锁服务已创建(开机自动执行)✓"
fi
# ── 安装 clawd ───────────────────────────────────────────────────────────────
INSTALL_DIR="/opt/clawd"
CONFIG_DIR="/etc/clawd"

View File

@@ -64,15 +64,14 @@ class DnsHijack {
// 终止系统可能残留的 dnsmasq
try { execSync('pkill -f "dnsmasq.*clawd"', { timeout: 3000 }); } catch (_) {}
// 查 dnsmasq 是否安装
try {
execSync('which dnsmasq', { timeout: 3000 });
} catch (_) {
// 查 dnsmasq 二进制(/usr/sbin 可能不在普通用户 PATH 中)
const dnsmasqBin = findBin('dnsmasq');
if (!dnsmasqBin) {
log.error('dns', 'dnsmasq 未安装,请运行: apt install dnsmasq');
return;
}
this._watchdog = new Watchdog('dns', 'dnsmasq', [
this._watchdog = new Watchdog('dns', dnsmasqBin, [
'--no-daemon',
`--conf-file=${DNSMASQ_CONF}`,
], {
@@ -96,4 +95,16 @@ class DnsHijack {
}
}
function findBin(name) {
const searchPaths = ['/usr/sbin', '/usr/bin', '/sbin', '/bin', '/usr/local/sbin', '/usr/local/bin'];
for (const dir of searchPaths) {
const full = path.join(dir, name);
if (fs.existsSync(full)) return full;
}
// 兜底尝试 whichPATH 可能已包含)
try {
return execSync(`which ${name}`, { encoding: 'utf8', timeout: 3000 }).trim() || null;
} catch (_) { return null; }
}
module.exports = { DnsHijack, CAPTIVE_DOMAIN };