diff --git a/src/main/core/manager.ts b/src/main/core/manager.ts index 0570f82..49841ee 100644 --- a/src/main/core/manager.ts +++ b/src/main/core/manager.ts @@ -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 { async function setPublicDNS(): Promise { 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 { 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) } } diff --git a/src/main/sys/sysproxy.ts b/src/main/sys/sysproxy.ts index f6b1588..22367f2 100644 --- a/src/main/sys/sysproxy.ts +++ b/src/main/sys/sysproxy.ts @@ -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 { - 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 { +async function enableSysProxy(): Promise { 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 { } } -export async function disableSysProxy(): Promise { +async function disableSysProxy(): Promise { const execFilePromise = promisify(execFile) if (process.platform === 'win32') { try {