mirror of
https://gh.catmak.name/https://github.com/mihomo-party-org/mihomo-party
synced 2026-02-10 19:50:28 +08:00
fix: use helper service for dns settings to avoid permission error on macos
This commit is contained in:
parent
8af815ee60
commit
3d68e57158
@ -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,11 +43,19 @@ async function getOriginDNS(): Promise<void> {
|
||||
|
||||
async function setDNS(dns: string): Promise<void> {
|
||||
const service = await getDefaultService()
|
||||
// networksetup 需要 root 权限,通过 osascript 请求管理员权限执行
|
||||
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<void> {
|
||||
if (process.platform !== 'darwin') return
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user