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, patchAppConfig,
patchControledMihomoConfig patchControledMihomoConfig
} from '../config' } from '../config'
import { app, dialog, ipcMain, safeStorage } from 'electron' import { app, dialog, ipcMain, net, safeStorage } from 'electron'
import { import {
startMihomoTraffic, startMihomoTraffic,
startMihomoConnections, startMihomoConnections,
@ -34,7 +34,7 @@ import { mainWindow } from '..'
import path from 'path' import path from 'path'
import { existsSync } from 'fs' 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 { try {
await stopCore(true) await stopCore(true)
await startCore() 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 child: ChildProcess
let retry = 10 let retry = 10
@ -307,6 +309,7 @@ async function setDNS(dns: string, password?: string): Promise<void> {
async function setPublicDNS(): Promise<void> { async function setPublicDNS(): Promise<void> {
if (process.platform !== 'darwin') return if (process.platform !== 'darwin') return
if (net.isOnline()) {
const { originDNS, encryptedPassword } = await getAppConfig() const { originDNS, encryptedPassword } = await getAppConfig()
if (!originDNS) { if (!originDNS) {
let password: string | undefined let password: string | undefined
@ -316,10 +319,15 @@ async function setPublicDNS(): Promise<void> {
await getOriginDNS(password) await getOriginDNS(password)
await setDNS('223.5.5.5', password) await setDNS('223.5.5.5', password)
} }
} else {
if (setPublicDNSTimer) clearTimeout(setPublicDNSTimer)
setPublicDNSTimer = setTimeout(() => setPublicDNS(), 5000)
}
} }
async function recoverDNS(): Promise<void> { async function recoverDNS(): Promise<void> {
if (process.platform !== 'darwin') return if (process.platform !== 'darwin') return
if (net.isOnline()) {
const { originDNS, encryptedPassword } = await getAppConfig() const { originDNS, encryptedPassword } = await getAppConfig()
if (originDNS) { if (originDNS) {
let password: string | undefined let password: string | undefined
@ -329,4 +337,8 @@ async function recoverDNS(): Promise<void> {
await setDNS(originDNS, password) await setDNS(originDNS, password)
await patchAppConfig({ originDNS: undefined }) 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 { execFile } from 'child_process'
import path from 'path' import path from 'path'
import { resourcesFilesDir } from '../utils/dirs' import { resourcesFilesDir } from '../utils/dirs'
import { net } from 'electron'
let defaultBypass: string[] let defaultBypass: string[]
let triggerSysProxyTimer: NodeJS.Timeout | null = null
if (process.platform === 'linux') 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'] 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> { export async function triggerSysProxy(enable: boolean): Promise<void> {
if (net.isOnline()) {
if (enable) { if (enable) {
await disableSysProxy() await disableSysProxy()
await enableSysProxy() await enableSysProxy()
} else { } else {
await disableSysProxy() await disableSysProxy()
} }
} else {
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 { sysProxy } = await getAppConfig()
const { mode, host, bypass = defaultBypass } = sysProxy const { mode, host, bypass = defaultBypass } = sysProxy
const { 'mixed-port': port = 7890 } = await getControledMihomoConfig() 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) const execFilePromise = promisify(execFile)
if (process.platform === 'win32') { if (process.platform === 'win32') {
try { try {