From 52b538b283dad0d0fdf5005a435fca106315024c Mon Sep 17 00:00:00 2001 From: Memory <134070804+Memory2314@users.noreply.github.com> Date: Wed, 3 Sep 2025 09:11:12 +0800 Subject: [PATCH] =?UTF-8?q?Revert=20"fix:=20=E4=BF=AE=E5=A4=8DDNS=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E5=92=8C=E6=80=A7=E8=83=BD=E4=BC=98=E5=8C=96=EF=BC=8C?= =?UTF-8?q?=E8=A7=A3=E5=86=B3=E8=BF=90=E8=A1=8C=E6=97=B6=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E6=98=BE=E7=A4=BA=E9=97=AE=E9=A2=98=20(#1094)"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 68bbde0d163ca08fe8b709c60b02b726598fb221. --- src/main/core/factory.ts | 5 ++ src/main/core/manager.ts | 82 ++++++++++++++++++------------- src/main/core/mihomoApi.ts | 2 +- src/main/utils/dirs.ts | 3 -- src/main/utils/init.ts | 3 ++ src/main/utils/template.ts | 23 +++++---- src/renderer/src/pages/dns.tsx | 26 ++++------ src/renderer/src/pages/mihomo.tsx | 2 +- src/shared/types.d.ts | 7 +-- 9 files changed, 79 insertions(+), 74 deletions(-) diff --git a/src/main/core/factory.ts b/src/main/core/factory.ts index b8730cc..a5bb90c 100644 --- a/src/main/core/factory.ts +++ b/src/main/core/factory.ts @@ -44,6 +44,11 @@ export async function generateProfile(): Promise { } const profile = deepMerge(currentProfile, controledMihomoConfig) + // 确保可以拿到基础日志信息 + // 使用 debug 可以调试内核相关问题 `debug/pprof` + if (['info', 'debug'].includes(profile['log-level']) === false) { + profile['log-level'] = 'info' + } runtimeConfig = profile // 先正常生成 YAML 字符串 diff --git a/src/main/core/manager.ts b/src/main/core/manager.ts index 590f692..2aa99c8 100644 --- a/src/main/core/manager.ts +++ b/src/main/core/manager.ts @@ -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[]> { 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[]> { }) child.stdout?.pipe(stdout) child.stderr?.pipe(stderr) - try { - await waitForCoreReady() - await managerLogger.info('Core API is ready, starting background services.') - 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 { - 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 + 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')) } - try { - await mihomoVersion() - clearInterval(poll) - resolve() - } catch (error) { - // Ignore connection errors and keep polling + 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')) } - }, pollInterval) + + 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 { + 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 + } + }) }) } diff --git a/src/main/core/mihomoApi.ts b/src/main/core/mihomoApi.ts index 69c44e7..c5c2888 100644 --- a/src/main/core/mihomoApi.ts +++ b/src/main/core/mihomoApi.ts @@ -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 diff --git a/src/main/utils/dirs.ts b/src/main/utils/dirs.ts index b5756d5..f0f8cbf 100644 --- a/src/main/utils/dirs.ts +++ b/src/main/utils/dirs.ts @@ -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' diff --git a/src/main/utils/init.ts b/src/main/utils/init.ts index 4ed7d49..3f6661c 100644 --- a/src/main/utils/init.ts +++ b/src/main/utils/init.ts @@ -324,6 +324,9 @@ async function migration(): Promise { 'external-controller-pipe': undefined }) } + if (externalController === undefined) { + await patchControledMihomoConfig({ 'external-controller': '' }) + } if (!showFloatingWindow && disableTray) { await patchAppConfig({ disableTray: false }) } diff --git a/src/main/utils/template.ts b/src/main/utils/template.ts index 279f0bc..8812505 100644 --- a/src/main/utils/template.ts +++ b/src/main/utils/template.ts @@ -59,8 +59,8 @@ export const defaultControledMihomoConfig: Partial = { '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 = { }, 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 = { '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' } } diff --git a/src/renderer/src/pages/dns.tsx b/src/renderer/src/pages/dns.tsx index f952382..9a93793 100644 --- a/src/renderer/src/pages/dns.tsx +++ b/src/renderer/src/pages/dns.tsx @@ -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, diff --git a/src/renderer/src/pages/mihomo.tsx b/src/renderer/src/pages/mihomo.tsx index 5c6ff60..ff1197d 100644 --- a/src/renderer/src/pages/mihomo.tsx +++ b/src/renderer/src/pages/mihomo.tsx @@ -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'], diff --git a/src/shared/types.d.ts b/src/shared/types.d.ts index 8d0258e..0afea9e 100644 --- a/src/shared/types.d.ts +++ b/src/shared/types.d.ts @@ -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