From 3d68e57158b3c82c341749f4799e8b1acf6e0fab Mon Sep 17 00:00:00 2001 From: xmk23333 Date: Thu, 15 Jan 2026 18:44:02 +0800 Subject: [PATCH] fix: use helper service for dns settings to avoid permission error on macos --- src/main/core/dns.ts | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/main/core/dns.ts b/src/main/core/dns.ts index b380e5a..ac3d899 100644 --- a/src/main/core/dns.ts +++ b/src/main/core/dns.ts @@ -1,9 +1,11 @@ import { exec } from 'child_process' import { promisify } from 'util' import { net } from 'electron' +import axios from 'axios' import { getAppConfig, patchAppConfig } from '../config' const execPromise = promisify(exec) +const helperSocketPath = '/tmp/mihomo-party-helper.sock' let setPublicDNSTimer: NodeJS.Timeout | null = null let recoverDNSTimer: NodeJS.Timeout | null = null @@ -41,10 +43,18 @@ async function getOriginDNS(): Promise { async function setDNS(dns: string): Promise { const service = await getDefaultService() - // networksetup 需要 root 权限,通过 osascript 请求管理员权限执行 - const shell = `networksetup -setdnsservers "${service}" ${dns}` - const command = `do shell script "${shell}" with administrator privileges` - await execPromise(`osascript -e '${command}'`) + try { + await axios.post( + 'http://localhost/dns', + { service, dns }, + { socketPath: helperSocketPath } + ) + } catch { + // fallback to osascript if helper not available + const shell = `networksetup -setdnsservers "${service}" ${dns}` + const command = `do shell script "${shell}" with administrator privileges` + await execPromise(`osascript -e '${command}'`) + } } export async function setPublicDNS(): Promise {