Revert "fix: 修复DNS配置和性能优化,解决运行时配置显示问题 (#1094)"

This reverts commit 68bbde0d163ca08fe8b709c60b02b726598fb221.
This commit is contained in:
Memory 2025-09-03 09:11:12 +08:00 committed by GitHub
parent 68bbde0d16
commit 52b538b283
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 79 additions and 74 deletions

View File

@ -44,6 +44,11 @@ export async function generateProfile(): Promise<void> {
}
const profile = deepMerge(currentProfile, controledMihomoConfig)
// 确保可以拿到基础日志信息
// 使用 debug 可以调试内核相关问题 `debug/pprof`
if (['info', 'debug'].includes(profile['log-level']) === false) {
profile['log-level'] = 'info'
}
runtimeConfig = profile
// 先正常生成 YAML 字符串

View File

@ -7,8 +7,7 @@ import {
mihomoProfileWorkDir,
mihomoTestDir,
mihomoWorkConfigPath,
mihomoWorkDir,
mihomoIpcPath
mihomoWorkDir
} from '../utils/dirs'
import { generateProfile } from './factory'
import {
@ -29,14 +28,16 @@ import {
stopMihomoTraffic,
stopMihomoLogs,
stopMihomoMemory,
mihomoVersion
patchMihomoConfig
} from './mihomoApi'
import chokidar from 'chokidar'
import { readFile, rm, writeFile } from 'fs/promises'
import { promisify } from 'util'
import { mainWindow } from '..'
import path from 'path'
import os from 'os'
import { createWriteStream, existsSync } from 'fs'
import { uploadRuntimeConfig } from '../resolve/gistApi'
import { startMonitor } from '../resolve/trafficMonitor'
import { safeShowErrorBox } from '../utils/init'
import i18next from '../../shared/i18n'
@ -51,6 +52,8 @@ chokidar.watch(path.join(mihomoCoreDir(), 'meta-update'), {}).on('unlinkDir', as
}
})
export const mihomoIpcPath =
process.platform === 'win32' ? '\\\\.\\pipe\\MihomoParty\\mihomo' : '/tmp/mihomo-party.sock'
const ctlParam = process.platform === 'win32' ? '-ext-ctl-pipe' : '-ext-ctl-unix'
let setPublicDNSTimer: NodeJS.Timeout | null = null
@ -69,6 +72,7 @@ export async function startCore(detached = false): Promise<Promise<void>[]> {
disableSystemCA = false,
skipSafePathCheck = false
} = await getAppConfig()
const { 'log-level': logLevel } = await getControledMihomoConfig()
if (existsSync(path.join(dataDir(), 'core.pid'))) {
const pid = parseInt(await readFile(path.join(dataDir(), 'core.pid'), 'utf-8'))
try {
@ -135,42 +139,50 @@ export async function startCore(detached = false): Promise<Promise<void>[]> {
})
child.stdout?.pipe(stdout)
child.stderr?.pipe(stderr)
return new Promise((resolve, reject) => {
child.stdout?.on('data', async (data) => {
const str = data.toString()
if (str.includes('configure tun interface: operation not permitted')) {
patchControledMihomoConfig({ tun: { enable: false } })
mainWindow?.webContents.send('controledMihomoConfigUpdated')
ipcMain.emit('updateTrayMenu')
reject(i18next.t('tun.error.tunPermissionDenied'))
}
if ((process.platform !== 'win32' && str.includes('External controller unix listen error')) ||
(process.platform === 'win32' && str.includes('External controller pipe listen error'))
) {
reject(i18next.t('mihomo.error.externalControllerListenError'))
}
if (
(process.platform !== 'win32' && str.includes('RESTful API unix listening at')) ||
(process.platform === 'win32' && str.includes('RESTful API pipe listening at'))
) {
resolve([
new Promise((resolve) => {
child.stdout?.on('data', async (data) => {
if (data.toString().toLowerCase().includes('start initial compatible provider default')) {
try {
await waitForCoreReady()
await managerLogger.info('Core API is ready, starting background services.')
mainWindow?.webContents.send('groupsUpdated')
mainWindow?.webContents.send('rulesUpdated')
await uploadRuntimeConfig()
} catch {
// ignore
}
await patchMihomoConfig({ 'log-level': logLevel })
resolve()
}
})
})
])
await startMihomoTraffic()
await startMihomoConnections()
await startMihomoLogs()
await startMihomoMemory()
retry = 10
return [Promise.resolve()]
} catch (error) {
await managerLogger.error('Core failed to start or API not responding.', error)
await stopCore() // Stop the failed core process
throw error // Re-throw the error to be caught by the caller
}
}
async function waitForCoreReady(timeoutMs = 15000): Promise<void> {
const startTime = Date.now()
const pollInterval = 1000 // ms
return new Promise((resolve, reject) => {
const poll = setInterval(async () => {
if (Date.now() - startTime > timeoutMs) {
clearInterval(poll)
reject(new Error(`Core API did not respond within ${timeoutMs / 1000}s.`))
return
}
try {
await mihomoVersion()
clearInterval(poll)
resolve()
} catch (error) {
// Ignore connection errors and keep polling
}
}, pollInterval)
})
})
}

View File

@ -6,7 +6,7 @@ import { tray } from '../resolve/tray'
import { calcTraffic } from '../utils/calc'
import { getRuntimeConfig } from './factory'
import { floatingWindow } from '../resolve/floatingWindow'
import { mihomoIpcPath } from '../utils/dirs'
import { mihomoIpcPath } from './manager'
let axiosIns: AxiosInstance = null!
let mihomoTrafficWs: WebSocket | null = null

View File

@ -158,6 +158,3 @@ export function coreLogPath(): string {
const name = `core-${year}-${month}-${day}`
return path.join(logDir(), `${name}.log`)
}
export const mihomoIpcPath =
process.platform === 'win32' ? '\\\\.\\pipe\\MihomoParty\\mihomo' : '/tmp/mihomo-party.sock'

View File

@ -324,6 +324,9 @@ async function migration(): Promise<void> {
'external-controller-pipe': undefined
})
}
if (externalController === undefined) {
await patchControledMihomoConfig({ 'external-controller': '' })
}
if (!showFloatingWindow && disableTray) {
await patchAppConfig({ disableTray: false })
}

View File

@ -59,8 +59,8 @@ export const defaultControledMihomoConfig: Partial<IMihomoConfig> = {
'tproxy-port': 0,
'allow-lan': false,
'unified-delay': true,
'tcp-concurrent': true,
'log-level': 'warning',
'tcp-concurrent': false,
'log-level': 'info',
'find-process-mode': 'strict',
'bind-address': '*',
'lan-allowed-ips': ['0.0.0.0/0', '::/0'],
@ -80,17 +80,16 @@ export const defaultControledMihomoConfig: Partial<IMihomoConfig> = {
},
dns: {
enable: true,
ipv6: true,
ipv6: false,
'enhanced-mode': 'fake-ip',
'fake-ip-range': '198.18.0.1/16',
'fake-ip-filter': ['+.lan', '+.local', '+.arpa', 'time.*.com', 'ntp.*.com', '+.market.xiaomi.com', 'localhost.ptlogin2.qq.com', '+.msftncsi.com', 'www.msftconnecttest.com'],
'fake-ip-filter-mode': 'blacklist',
'fake-ip-filter': ['*', '+.lan', '+.local', 'time.*.com', 'ntp.*.com', '+.market.xiaomi.com'],
'use-hosts': false,
'use-system-hosts': false,
'respect-rules': false,
'default-nameserver': ['system', '223.6.6.6', '8.8.8.8', '2400:3200::1', '2001:4860:4860::8888'],
nameserver: ['8.8.8.8', 'https://doh.pub/dns-query', 'https://dns.alidns.com/dns-query'],
'proxy-server-nameserver': ['https://doh.pub/dns-query', 'https://dns.alidns.com/dns-query', 'tls://223.5.5.5'],
'default-nameserver': ['tls://223.5.5.5'],
nameserver: ['https://doh.pub/dns-query', 'https://dns.alidns.com/dns-query'],
'proxy-server-nameserver': ['https://doh.pub/dns-query', 'https://dns.alidns.com/dns-query'],
'direct-nameserver': [],
fallback: [],
'fallback-filter': {
@ -138,10 +137,10 @@ export const defaultControledMihomoConfig: Partial<IMihomoConfig> = {
'geo-update-interval': 24,
'geodata-mode': false,
'geox-url': {
geoip: 'https://cdn.jsdelivr.net/gh/MetaCubeX/meta-rules-dat@release/geoip-lite.dat',
geosite: 'https://cdn.jsdelivr.net/gh/MetaCubeX/meta-rules-dat@release/geosite.dat',
mmdb: 'https://cdn.jsdelivr.net/gh/MetaCubeX/meta-rules-dat@release/geoip.metadb',
asn: 'https://cdn.jsdelivr.net/gh/MetaCubeX/meta-rules-dat@release/GeoLite2-ASN.mmdb'
geoip: 'https://github.com/MetaCubeX/meta-rules-dat/releases/download/latest/geoip-lite.dat',
geosite: 'https://github.com/MetaCubeX/meta-rules-dat/releases/download/latest/geosite.dat',
mmdb: 'https://github.com/MetaCubeX/meta-rules-dat/releases/download/latest/geoip.metadb',
asn: 'https://github.com/MetaCubeX/meta-rules-dat/releases/download/latest/GeoLite2-ASN.mmdb'
}
}

View File

@ -17,30 +17,25 @@ const DNS: React.FC = () => {
const { dns, hosts } = controledMihomoConfig || {}
const {
enable = true,
ipv6 = true,
ipv6 = false,
'fake-ip-range': fakeIPRange = '198.18.0.1/16',
'fake-ip-filter': fakeIPFilter = [
'*',
'+.lan',
'+.local',
'+.arpa',
'time.*.com',
'ntp.*.com',
'+.market.xiaomi.com',
'localhost.ptlogin2.qq.com',
'+.msftncsi.com',
'www.msftconnecttest.com'
'+.market.xiaomi.com'
],
'fake-ip-filter-mode': fakeIPFilterMode = 'blacklist',
'enhanced-mode': enhancedMode = 'fake-ip',
'use-hosts': useHosts = false,
'use-system-hosts': useSystemHosts = false,
'respect-rules': respectRules = false,
'default-nameserver': defaultNameserver = ['system', '223.6.6.6', '8.8.8.8', '2400:3200::1', '2001:4860:4860::8888'],
nameserver = ['8.8.8.8', 'https://doh.pub/dns-query', 'https://dns.alidns.com/dns-query'],
'default-nameserver': defaultNameserver = ['tls://223.5.5.5'],
nameserver = ['https://doh.pub/dns-query', 'https://dns.alidns.com/dns-query'],
'proxy-server-nameserver': proxyServerNameserver = [
'https://doh.pub/dns-query',
'https://dns.alidns.com/dns-query',
'tls://223.5.5.5'
'https://dns.alidns.com/dns-query'
],
'direct-nameserver': directNameserver = [],
fallback = [],
@ -59,7 +54,6 @@ const DNS: React.FC = () => {
enhancedMode,
fakeIPRange,
fakeIPFilter,
fakeIPFilterMode,
useSystemHosts,
respectRules,
defaultNameserver,
@ -67,10 +61,10 @@ const DNS: React.FC = () => {
proxyServerNameserver,
directNameserver,
fallback,
fallbackGeoip: fallbackFilter.geoip,
fallbackGeoipCode: fallbackFilter['geoip-code'],
fallbackIpcidr: fallbackFilter.ipcidr,
fallbackDomain: fallbackFilter.domain,
fallbackGeoip: fallbackFilter?.geoip || true,
fallbackGeoipCode: fallbackFilter?.['geoip-code'] || 'CN',
fallbackIpcidr: fallbackFilter?.ipcidr || ['240.0.0.0/4', '0.0.0.0/32'],
fallbackDomain: fallbackFilter?.domain || ['+.google.com', '+.facebook.com', '+.youtube.com'],
useNameserverPolicy,
nameserverPolicy: Object.entries(nameserverPolicy || {}).map(([domain, value]) => ({
domain,

View File

@ -49,7 +49,7 @@ const Mihomo: React.FC = () => {
secret,
authentication = [],
'skip-auth-prefixes': skipAuthPrefixes = ['127.0.0.1/32', '::1/128'],
'log-level': logLevel = 'warning',
'log-level': logLevel = 'info',
'find-process-mode': findProcessMode = 'strict',
'allow-lan': allowLan,
'lan-allowed-ips': lanAllowedIps = ['0.0.0.0/0', '::/0'],

View File

@ -354,12 +354,7 @@ interface IMihomoDNSConfig {
'default-nameserver'?: string[]
nameserver?: string[]
fallback?: string[]
'fallback-filter'?: {
geoip?: boolean
'geoip-code'?: string
ipcidr?: string[]
domain?: string[]
}
'fallback-filter'?: { [key: string]: boolean | string | string[] }
'proxy-server-nameserver'?: string[]
'direct-nameserver'?: string[]
'direct-nameserver-follow-policy'?: boolean