Skip to content
This repository has been archived by the owner on Feb 5, 2025. It is now read-only.

Commit

Permalink
fix default network interface error
Browse files Browse the repository at this point in the history
  • Loading branch information
pompurin404 committed Sep 15, 2024
1 parent c8ae4d8 commit 968bc6b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 22 deletions.
44 changes: 28 additions & 16 deletions src/main/core/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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()
Expand All @@ -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

Expand Down Expand Up @@ -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)
}
}
19 changes: 13 additions & 6 deletions src/main/sys/sysproxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 968bc6b

Please sign in to comment.