mirror of
https://gh.catmak.name/https://github.com/mihomo-party-org/mihomo-party
synced 2026-02-11 04:00:32 +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 { exec } from 'child_process'
|
||||||
import { promisify } from 'util'
|
import { promisify } from 'util'
|
||||||
import { net } from 'electron'
|
import { net } from 'electron'
|
||||||
|
import axios from 'axios'
|
||||||
import { getAppConfig, patchAppConfig } from '../config'
|
import { getAppConfig, patchAppConfig } from '../config'
|
||||||
|
|
||||||
const execPromise = promisify(exec)
|
const execPromise = promisify(exec)
|
||||||
|
const helperSocketPath = '/tmp/mihomo-party-helper.sock'
|
||||||
|
|
||||||
let setPublicDNSTimer: NodeJS.Timeout | null = null
|
let setPublicDNSTimer: NodeJS.Timeout | null = null
|
||||||
let recoverDNSTimer: NodeJS.Timeout | null = null
|
let recoverDNSTimer: NodeJS.Timeout | null = null
|
||||||
@ -41,10 +43,18 @@ async function getOriginDNS(): Promise<void> {
|
|||||||
|
|
||||||
async function setDNS(dns: string): Promise<void> {
|
async function setDNS(dns: string): Promise<void> {
|
||||||
const service = await getDefaultService()
|
const service = await getDefaultService()
|
||||||
// networksetup 需要 root 权限,通过 osascript 请求管理员权限执行
|
try {
|
||||||
const shell = `networksetup -setdnsservers "${service}" ${dns}`
|
await axios.post(
|
||||||
const command = `do shell script "${shell}" with administrator privileges`
|
'http://localhost/dns',
|
||||||
await execPromise(`osascript -e '${command}'`)
|
{ 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> {
|
export async function setPublicDNS(): Promise<void> {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user