fix default network interface error

This commit is contained in:
pompurin404 2024-09-15 19:29:46 +08:00
parent d9694d868e
commit 91d708576f
No known key found for this signature in database
2 changed files with 41 additions and 22 deletions

View File

@ -15,7 +15,7 @@ import {
patchAppConfig,
patchControledMihomoConfig
} from '../config'
import { app, dialog, ipcMain, safeStorage } from 'electron'
import { app, dialog, ipcMain, net, safeStorage } from 'electron'
import {
startMihomoTraffic,
startMihomoConnections,
@ -34,7 +34,7 @@ import { mainWindow } from '..'
import path from 'path'
import { existsSync } from 'fs'
chokidar.watch(path.join(mihomoCoreDir(), 'meta-update')).on('unlinkDir', async () => {
chokidar.watch(path.join(mihomoCoreDir(), 'meta-update'), {}).on('unlinkDir', async () => {
try {
await stopCore(true)
await startCore()
@ -43,6 +43,8 @@ chokidar.watch(path.join(mihomoCoreDir(), 'meta-update')).on('unlinkDir', async
}
})
let setPublicDNSTimer: NodeJS.Timeout | null = null
let recoverDNSTimer: NodeJS.Timeout | null = null
let child: ChildProcess
let retry = 10
@ -307,26 +309,36 @@ async function setDNS(dns: string, password?: string): Promise<void> {
async function setPublicDNS(): Promise<void> {
if (process.platform !== 'darwin') return
const { originDNS, encryptedPassword } = await getAppConfig()
if (!originDNS) {
let password: string | undefined
if (encryptedPassword && isEncryptionAvailable()) {
password = safeStorage.decryptString(Buffer.from(encryptedPassword))
if (net.isOnline()) {
const { originDNS, encryptedPassword } = await getAppConfig()
if (!originDNS) {
let password: string | undefined
if (encryptedPassword && isEncryptionAvailable()) {
password = safeStorage.decryptString(Buffer.from(encryptedPassword))
}
await getOriginDNS(password)
await setDNS('223.5.5.5', password)
}
await getOriginDNS(password)
await setDNS('223.5.5.5', password)
} else {
if (setPublicDNSTimer) clearTimeout(setPublicDNSTimer)
setPublicDNSTimer = setTimeout(() => setPublicDNS(), 5000)
}
}
async function recoverDNS(): Promise<void> {
if (process.platform !== 'darwin') return
const { originDNS, encryptedPassword } = await getAppConfig()
if (originDNS) {
let password: string | undefined
if (encryptedPassword && isEncryptionAvailable()) {
password = safeStorage.decryptString(Buffer.from(encryptedPassword))
if (net.isOnline()) {
const { originDNS, encryptedPassword } = await getAppConfig()
if (originDNS) {
let password: string | undefined
if (encryptedPassword && isEncryptionAvailable()) {
password = safeStorage.decryptString(Buffer.from(encryptedPassword))
}
await setDNS(originDNS, password)
await patchAppConfig({ originDNS: undefined })
}
await setDNS(originDNS, password)
await patchAppConfig({ originDNS: undefined })
} else {
if (recoverDNSTimer) clearTimeout(recoverDNSTimer)
recoverDNSTimer = setTimeout(() => recoverDNS(), 5000)
}
}

View File

@ -5,8 +5,10 @@ import { promisify } from 'util'
import { execFile } from 'child_process'
import path from 'path'
import { resourcesFilesDir } from '../utils/dirs'
import { net } from 'electron'
let defaultBypass: string[]
let triggerSysProxyTimer: NodeJS.Timeout | null = null
if (process.platform === 'linux')
defaultBypass = ['localhost', '127.0.0.1', '192.168.0.0/16', '10.0.0.0/8', '172.16.0.0/12', '::1']
@ -47,15 +49,20 @@ if (process.platform === 'win32')
]
export async function triggerSysProxy(enable: boolean): Promise<void> {
if (enable) {
await disableSysProxy()
await enableSysProxy()
if (net.isOnline()) {
if (enable) {
await disableSysProxy()
await enableSysProxy()
} else {
await disableSysProxy()
}
} else {
await disableSysProxy()
if (triggerSysProxyTimer) clearTimeout(triggerSysProxyTimer)
triggerSysProxyTimer = setTimeout(() => triggerSysProxy(enable), 5000)
}
}
export async function enableSysProxy(): Promise<void> {
async function enableSysProxy(): Promise<void> {
const { sysProxy } = await getAppConfig()
const { mode, host, bypass = defaultBypass } = sysProxy
const { 'mixed-port': port = 7890 } = await getControledMihomoConfig()
@ -97,7 +104,7 @@ export async function enableSysProxy(): Promise<void> {
}
}
export async function disableSysProxy(): Promise<void> {
async function disableSysProxy(): Promise<void> {
const execFilePromise = promisify(execFile)
if (process.platform === 'win32') {
try {