allow disable controled dns and sniff

This commit is contained in:
pompurin404 2024-08-14 14:26:06 +08:00
parent a3c129f9e1
commit 0c5ab9de41
No known key found for this signature in database
5 changed files with 50 additions and 2 deletions

View File

@ -4,6 +4,7 @@ import yaml from 'yaml'
import { getAxios, startMihomoMemory, startMihomoTraffic } from '../core/mihomoApi' import { getAxios, startMihomoMemory, startMihomoTraffic } from '../core/mihomoApi'
import { generateProfile } from '../resolve/factory' import { generateProfile } from '../resolve/factory'
import { getAppConfig } from './app' import { getAppConfig } from './app'
import { defaultControledMihomoConfig } from '../utils/template'
let controledMihomoConfig: Partial<IMihomoConfig> // mihomo.yaml let controledMihomoConfig: Partial<IMihomoConfig> // mihomo.yaml
@ -16,12 +17,21 @@ export async function getControledMihomoConfig(force = false): Promise<Partial<I
} }
export async function patchControledMihomoConfig(patch: Partial<IMihomoConfig>): Promise<void> { export async function patchControledMihomoConfig(patch: Partial<IMihomoConfig>): Promise<void> {
const { useNameserverPolicy } = await getAppConfig() const { useNameserverPolicy, controlDns, controlSniff } = await getAppConfig()
if (patch.tun) { if (patch.tun) {
const oldTun = controledMihomoConfig.tun || {} const oldTun = controledMihomoConfig.tun || {}
const newTun = Object.assign(oldTun, patch.tun) const newTun = Object.assign(oldTun, patch.tun)
patch.tun = newTun patch.tun = newTun
} }
if (!controlDns) {
delete controledMihomoConfig.dns
delete controledMihomoConfig.hosts
} else {
if (controledMihomoConfig.hosts === undefined) {
controledMihomoConfig.dns = defaultControledMihomoConfig.dns
controledMihomoConfig.hosts = defaultControledMihomoConfig.hosts
}
}
if (patch.dns) { if (patch.dns) {
const oldDns = controledMihomoConfig.dns || {} const oldDns = controledMihomoConfig.dns || {}
const newDns = Object.assign(oldDns, patch.dns) const newDns = Object.assign(oldDns, patch.dns)
@ -30,6 +40,13 @@ export async function patchControledMihomoConfig(patch: Partial<IMihomoConfig>):
} }
patch.dns = newDns patch.dns = newDns
} }
if (!controlSniff) {
delete controledMihomoConfig.sniffer
} else {
if (!controledMihomoConfig.sniffer) {
controledMihomoConfig.sniffer = defaultControledMihomoConfig.sniffer
}
}
if (patch.sniffer) { if (patch.sniffer) {
const oldSniffer = controledMihomoConfig.sniffer || {} const oldSniffer = controledMihomoConfig.sniffer || {}
const newSniffer = Object.assign(oldSniffer, patch.sniffer) const newSniffer = Object.assign(oldSniffer, patch.sniffer)

View File

@ -7,6 +7,8 @@ export const defaultConfig: IAppConfig = {
autoCheckUpdate: true, autoCheckUpdate: true,
autoCloseConnection: true, autoCloseConnection: true,
useNameserverPolicy: false, useNameserverPolicy: false,
controlDns: true,
controlSniff: true,
nameserverPolicy: {}, nameserverPolicy: {},
siderOrder: [ siderOrder: [
'mode', 'mode',

View File

@ -33,6 +33,8 @@ const App: React.FC = () => {
const { appConfig, patchAppConfig } = useAppConfig() const { appConfig, patchAppConfig } = useAppConfig()
const { const {
appTheme = 'system', appTheme = 'system',
controlDns = true,
controlSniff = true,
siderOrder = [ siderOrder = [
'sysproxy', 'sysproxy',
'tun', 'tun',
@ -119,6 +121,8 @@ const App: React.FC = () => {
})} })}
> >
{order.map((key: string) => { {order.map((key: string) => {
if (key === 'dns' && controlDns === false) return null
if (key === 'sniff' && controlSniff === false) return null
return componentMap[key] return componentMap[key]
})} })}
</SortableContext> </SortableContext>

View File

@ -8,7 +8,8 @@ import {
enableAutoRun, enableAutoRun,
disableAutoRun, disableAutoRun,
quitApp, quitApp,
checkUpdate checkUpdate,
patchControledMihomoConfig
} from '@renderer/utils/ipc' } from '@renderer/utils/ipc'
import { IoLogoGithub } from 'react-icons/io5' import { IoLogoGithub } from 'react-icons/io5'
import { version } from '@renderer/utils/init' import { version } from '@renderer/utils/init'
@ -26,6 +27,8 @@ const Settings: React.FC = () => {
const { appConfig, patchAppConfig } = useAppConfig() const { appConfig, patchAppConfig } = useAppConfig()
const { const {
silentStart = false, silentStart = false,
controlDns = true,
controlSniff = true,
delayTestUrl, delayTestUrl,
delayTestTimeout, delayTestTimeout,
autoCheckUpdate, autoCheckUpdate,
@ -188,6 +191,26 @@ const Settings: React.FC = () => {
}} }}
/> />
</SettingItem> </SettingItem>
<SettingItem title="接管DNS设置" divider>
<Switch
size="sm"
isSelected={controlDns}
onValueChange={async (v) => {
await patchAppConfig({ controlDns: v })
await patchControledMihomoConfig({})
}}
/>
</SettingItem>
<SettingItem title="接管域名嗅探设置" divider>
<Switch
size="sm"
isSelected={controlSniff}
onValueChange={async (v) => {
await patchAppConfig({ controlSniff: v })
await patchControledMihomoConfig({})
}}
/>
</SettingItem>
<SettingItem title="自动断开连接"> <SettingItem title="自动断开连接">
<Switch <Switch
size="sm" size="sm"

View File

@ -214,6 +214,8 @@ interface IAppConfig {
delayTestUrl?: string delayTestUrl?: string
delayTestTimeout?: number delayTestTimeout?: number
encryptedPassword?: Buffer encryptedPassword?: Buffer
controlDns?: boolean
controlSniff?: boolean
useNameserverPolicy: boolean useNameserverPolicy: boolean
nameserverPolicy: { [key: string]: string | string[] } nameserverPolicy: { [key: string]: string | string[] }
} }