Skip to content

Commit 0c5ab9d

Browse files
author
pompurin404
committed
allow disable controled dns and sniff
1 parent a3c129f commit 0c5ab9d

File tree

5 files changed

+50
-2
lines changed

5 files changed

+50
-2
lines changed

src/main/config/controledMihomo.ts

+18-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import yaml from 'yaml'
44
import { getAxios, startMihomoMemory, startMihomoTraffic } from '../core/mihomoApi'
55
import { generateProfile } from '../resolve/factory'
66
import { getAppConfig } from './app'
7+
import { defaultControledMihomoConfig } from '../utils/template'
78

89
let controledMihomoConfig: Partial<IMihomoConfig> // mihomo.yaml
910

@@ -16,12 +17,21 @@ export async function getControledMihomoConfig(force = false): Promise<Partial<I
1617
}
1718

1819
export async function patchControledMihomoConfig(patch: Partial<IMihomoConfig>): Promise<void> {
19-
const { useNameserverPolicy } = await getAppConfig()
20+
const { useNameserverPolicy, controlDns, controlSniff } = await getAppConfig()
2021
if (patch.tun) {
2122
const oldTun = controledMihomoConfig.tun || {}
2223
const newTun = Object.assign(oldTun, patch.tun)
2324
patch.tun = newTun
2425
}
26+
if (!controlDns) {
27+
delete controledMihomoConfig.dns
28+
delete controledMihomoConfig.hosts
29+
} else {
30+
if (controledMihomoConfig.hosts === undefined) {
31+
controledMihomoConfig.dns = defaultControledMihomoConfig.dns
32+
controledMihomoConfig.hosts = defaultControledMihomoConfig.hosts
33+
}
34+
}
2535
if (patch.dns) {
2636
const oldDns = controledMihomoConfig.dns || {}
2737
const newDns = Object.assign(oldDns, patch.dns)
@@ -30,6 +40,13 @@ export async function patchControledMihomoConfig(patch: Partial<IMihomoConfig>):
3040
}
3141
patch.dns = newDns
3242
}
43+
if (!controlSniff) {
44+
delete controledMihomoConfig.sniffer
45+
} else {
46+
if (!controledMihomoConfig.sniffer) {
47+
controledMihomoConfig.sniffer = defaultControledMihomoConfig.sniffer
48+
}
49+
}
3350
if (patch.sniffer) {
3451
const oldSniffer = controledMihomoConfig.sniffer || {}
3552
const newSniffer = Object.assign(oldSniffer, patch.sniffer)

src/main/utils/template.ts

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ export const defaultConfig: IAppConfig = {
77
autoCheckUpdate: true,
88
autoCloseConnection: true,
99
useNameserverPolicy: false,
10+
controlDns: true,
11+
controlSniff: true,
1012
nameserverPolicy: {},
1113
siderOrder: [
1214
'mode',

src/renderer/src/App.tsx

+4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ const App: React.FC = () => {
3333
const { appConfig, patchAppConfig } = useAppConfig()
3434
const {
3535
appTheme = 'system',
36+
controlDns = true,
37+
controlSniff = true,
3638
siderOrder = [
3739
'sysproxy',
3840
'tun',
@@ -119,6 +121,8 @@ const App: React.FC = () => {
119121
})}
120122
>
121123
{order.map((key: string) => {
124+
if (key === 'dns' && controlDns === false) return null
125+
if (key === 'sniff' && controlSniff === false) return null
122126
return componentMap[key]
123127
})}
124128
</SortableContext>

src/renderer/src/pages/settings.tsx

+24-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import {
88
enableAutoRun,
99
disableAutoRun,
1010
quitApp,
11-
checkUpdate
11+
checkUpdate,
12+
patchControledMihomoConfig
1213
} from '@renderer/utils/ipc'
1314
import { IoLogoGithub } from 'react-icons/io5'
1415
import { version } from '@renderer/utils/init'
@@ -26,6 +27,8 @@ const Settings: React.FC = () => {
2627
const { appConfig, patchAppConfig } = useAppConfig()
2728
const {
2829
silentStart = false,
30+
controlDns = true,
31+
controlSniff = true,
2932
delayTestUrl,
3033
delayTestTimeout,
3134
autoCheckUpdate,
@@ -188,6 +191,26 @@ const Settings: React.FC = () => {
188191
}}
189192
/>
190193
</SettingItem>
194+
<SettingItem title="接管DNS设置" divider>
195+
<Switch
196+
size="sm"
197+
isSelected={controlDns}
198+
onValueChange={async (v) => {
199+
await patchAppConfig({ controlDns: v })
200+
await patchControledMihomoConfig({})
201+
}}
202+
/>
203+
</SettingItem>
204+
<SettingItem title="接管域名嗅探设置" divider>
205+
<Switch
206+
size="sm"
207+
isSelected={controlSniff}
208+
onValueChange={async (v) => {
209+
await patchAppConfig({ controlSniff: v })
210+
await patchControledMihomoConfig({})
211+
}}
212+
/>
213+
</SettingItem>
191214
<SettingItem title="自动断开连接">
192215
<Switch
193216
size="sm"

src/shared/types.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,8 @@ interface IAppConfig {
214214
delayTestUrl?: string
215215
delayTestTimeout?: number
216216
encryptedPassword?: Buffer
217+
controlDns?: boolean
218+
controlSniff?: boolean
217219
useNameserverPolicy: boolean
218220
nameserverPolicy: { [key: string]: string | string[] }
219221
}

0 commit comments

Comments
 (0)