Skip to content

Commit 95ae550

Browse files
xishang0128pompurin404
and
pompurin404
authored
add dns and sniff (#3)
Co-authored-by: pompurin404 <pompurin404@mihomo.party>
1 parent e52a583 commit 95ae550

File tree

13 files changed

+637
-8
lines changed

13 files changed

+637
-8
lines changed

src/main/config/controledMihomo.ts

+10
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ export function setControledMihomoConfig(patch: Partial<IMihomoConfig>): void {
1818
const newTun = Object.assign(oldTun, patch.tun)
1919
patch.tun = newTun
2020
}
21+
if (patch.dns) {
22+
const oldDns = controledMihomoConfig.dns || {}
23+
const newDns = Object.assign(oldDns, patch.dns)
24+
patch.dns = newDns
25+
}
26+
if (patch.sniffer) {
27+
const oldSniffer = controledMihomoConfig.sniffer || {}
28+
const newSniffer = Object.assign(oldSniffer, patch.sniffer)
29+
patch.sniffer = newSniffer
30+
}
2131
controledMihomoConfig = Object.assign(controledMihomoConfig, patch)
2232
if (patch['external-controller'] || patch.secret) {
2333
getAxios(true)

src/main/resolve/factory.ts

+8
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,15 @@ export function generateProfile(): void {
99
const { tun: profileTun = {} } = currentProfile
1010
const { tun: controledTun } = controledMihomoConfig
1111
const tun = Object.assign(profileTun, controledTun)
12+
const { dns: profileDns = {} } = currentProfile
13+
const { dns: controledDns } = controledMihomoConfig
14+
const dns = Object.assign(profileDns, controledDns)
15+
const { sniffer: profileSniffer = {} } = currentProfile
16+
const { sniffer: controledSniffer } = controledMihomoConfig
17+
const sniffer = Object.assign(profileSniffer, controledSniffer)
1218
const profile = Object.assign(currentProfile, controledMihomoConfig)
1319
profile.tun = tun
20+
profile.dns = dns
21+
profile.sniffer = sniffer
1422
fs.writeFileSync(mihomoWorkConfigPath(), yaml.stringify(profile))
1523
}

src/main/utils/template.ts

+26
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,32 @@ export const defaultControledMihomoConfig: Partial<IMihomoConfig> = {
2929
'auto-detect-interface': true,
3030
'dns-hijack': ['any:53'],
3131
mtu: 1500
32+
},
33+
dns: {
34+
enable: false,
35+
ipv6: false,
36+
'enhanced-mode': 'fake-ip',
37+
'fake-ip-range': '198.18.0.1/16',
38+
'use-hosts': false,
39+
'use-system-hosts': false,
40+
nameserver: ['https://doh.pub/dns-query', 'https://dns.alidns.com/dns-query']
41+
},
42+
sniffer: {
43+
enable: true,
44+
'parse-pure-ip': true,
45+
'override-destination': false,
46+
sniff: {
47+
HTTP: {
48+
ports: [80, 443]
49+
},
50+
TLS: {
51+
ports: [443]
52+
},
53+
QUIC: {
54+
ports: [443]
55+
}
56+
},
57+
'skip-domain': ['+.push.apple.com']
3258
}
3359
}
3460

src/renderer/src/App.tsx

+6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import routes from '@renderer/routes'
1010
import ProfileCard from '@renderer/components/sider/profile-card'
1111
import ProxyCard from '@renderer/components/sider/proxy-card'
1212
import RuleCard from '@renderer/components/sider/rule-card'
13+
import DNSCard from '@renderer/components/sider/dns-card'
14+
import SniffCard from '@renderer/components/sider/sniff-card'
1315
import OverrideCard from '@renderer/components/sider/override-card'
1416
import ConnCard from '@renderer/components/sider/conn-card'
1517
import LogCard from '@renderer/components/sider/log-card'
@@ -75,6 +77,10 @@ const App: React.FC = () => {
7577
<ConnCard />
7678
</div>
7779

80+
<div className="flex justify-between mx-2">
81+
<DNSCard />
82+
<SniffCard />
83+
</div>
7884
<div className="flex justify-between mx-2">
7985
<LogCard />
8086
<RuleCard />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { Button, Card, CardBody, CardFooter } from '@nextui-org/react'
2+
import { useControledMihomoConfig } from '@renderer/hooks/use-controled-mihomo-config'
3+
import BorderSwitch from '@renderer/components/base/border-swtich'
4+
import { MdOutlineDns } from 'react-icons/md'
5+
import { useLocation, useNavigate } from 'react-router-dom'
6+
import { patchMihomoConfig } from '@renderer/utils/ipc'
7+
8+
const DNSCard: React.FC = () => {
9+
const navigate = useNavigate()
10+
const location = useLocation()
11+
const match = location.pathname.includes('/dns')
12+
const { controledMihomoConfig, patchControledMihomoConfig } = useControledMihomoConfig(true)
13+
const { dns, tun } = controledMihomoConfig || {}
14+
const { enable } = dns || {}
15+
16+
const onChange = async (enable: boolean): Promise<void> => {
17+
await patchControledMihomoConfig({ dns: { enable } })
18+
await patchMihomoConfig({ dns: { enable } })
19+
}
20+
21+
return (
22+
<Card
23+
className={`w-[50%] mr-1 mb-2 ${match ? 'bg-primary' : ''}`}
24+
isPressable
25+
onPress={() => navigate('/dns')}
26+
>
27+
<CardBody className="pb-1 pt-0 px-0">
28+
<div className="flex justify-between">
29+
<Button
30+
isIconOnly
31+
className="bg-transparent pointer-events-none"
32+
variant="flat"
33+
color="default"
34+
>
35+
<MdOutlineDns
36+
className={`${match ? 'text-white' : 'text-foreground'} text-[24px] font-bold`}
37+
/>
38+
</Button>
39+
<BorderSwitch
40+
isShowBorder={match && enable}
41+
isSelected={enable}
42+
isDisabled={tun?.enable}
43+
onValueChange={onChange}
44+
/>
45+
</div>
46+
</CardBody>
47+
<CardFooter className="pt-1">
48+
<h3 className={`select-none text-md font-bold ${match ? 'text-white' : 'text-foreground'}`}>
49+
DNS
50+
</h3>
51+
</CardFooter>
52+
</Card>
53+
)
54+
}
55+
56+
export default DNSCard

src/renderer/src/components/sider/proxy-card.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Button, Card, CardBody, CardFooter, Chip } from '@nextui-org/react'
22
import { mihomoProxies } from '@renderer/utils/ipc'
33
import { useMemo } from 'react'
4-
import { MdTableChart } from 'react-icons/md'
4+
import { LuGroup } from 'react-icons/lu'
55
import { useLocation, useNavigate } from 'react-router-dom'
66
import useSWR from 'swr'
77

@@ -30,7 +30,7 @@ const ProxyCard: React.FC = () => {
3030
variant="flat"
3131
color="default"
3232
>
33-
<MdTableChart
33+
<LuGroup
3434
className={`${match ? 'text-white' : 'text-foreground'} text-[24px] font-bold`}
3535
/>
3636
</Button>

src/renderer/src/components/sider/rule-card.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Button, Card, CardBody, CardFooter, Chip } from '@nextui-org/react'
22
import { mihomoRules } from '@renderer/utils/ipc'
3-
import { IoGitNetwork } from 'react-icons/io5'
3+
import { MdOutlineAltRoute } from 'react-icons/md'
44
import { useLocation, useNavigate } from 'react-router-dom'
55
import useSWR from 'swr'
66

@@ -26,7 +26,7 @@ const RuleCard: React.FC = () => {
2626
variant="flat"
2727
color="default"
2828
>
29-
<IoGitNetwork
29+
<MdOutlineAltRoute
3030
color="default"
3131
className={`${match ? 'text-white' : 'text-foreground'} text-[20px]`}
3232
/>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { Button, Card, CardBody, CardFooter } from '@nextui-org/react'
2+
import BorderSwitch from '@renderer/components/base/border-swtich'
3+
import { GrDomain } from 'react-icons/gr'
4+
import { useLocation, useNavigate } from 'react-router-dom'
5+
import { patchMihomoConfig } from '@renderer/utils/ipc'
6+
import { useControledMihomoConfig } from '@renderer/hooks/use-controled-mihomo-config'
7+
8+
const SniffCard: React.FC = () => {
9+
const navigate = useNavigate()
10+
const location = useLocation()
11+
const match = location.pathname.includes('/sniffer')
12+
const { controledMihomoConfig, patchControledMihomoConfig } = useControledMihomoConfig(true)
13+
const { sniffer } = controledMihomoConfig || {}
14+
const { enable } = sniffer || {}
15+
16+
const onChange = async (enable: boolean): Promise<void> => {
17+
await patchControledMihomoConfig({ sniffer: { enable } })
18+
await patchMihomoConfig({ sniffer: { enable } })
19+
}
20+
21+
return (
22+
<Card
23+
className={`w-[50%] ml-1 mb-2 ${match ? 'bg-primary' : ''}`}
24+
isPressable
25+
onPress={() => navigate('/sniffer')}
26+
>
27+
<CardBody className="pb-1 pt-0 px-0">
28+
<div className="flex justify-between">
29+
<Button
30+
isIconOnly
31+
className="bg-transparent pointer-events-none"
32+
variant="flat"
33+
color="default"
34+
>
35+
<GrDomain
36+
color="default"
37+
className={`${match ? 'text-white' : 'text-foreground'} text-[24px]`}
38+
/>
39+
</Button>
40+
<BorderSwitch
41+
isShowBorder={match && enable}
42+
isSelected={enable}
43+
onValueChange={onChange}
44+
/>
45+
</div>
46+
</CardBody>
47+
<CardFooter className="pt-1">
48+
<h3 className={`select-none text-md font-bold ${match ? 'text-white' : 'text-foreground'}`}>
49+
域名嗅探
50+
</h3>
51+
</CardFooter>
52+
</Card>
53+
)
54+
}
55+
56+
export default SniffCard

src/renderer/src/components/sider/tun-switcher.tsx

+6-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,12 @@ const TunSwitcher: React.FC = () => {
3131
}
3232
}
3333

34-
await patchControledMihomoConfig({ tun: { enable } })
35-
await patchMihomoConfig({ tun: { enable } })
34+
if (enable) {
35+
await patchControledMihomoConfig({ tun: { enable }, dns: { enable: true } })
36+
} else {
37+
await patchControledMihomoConfig({ tun: { enable }, dns: { enable: true } })
38+
await patchMihomoConfig({ tun: { enable } })
39+
}
3640
}
3741

3842
return (

0 commit comments

Comments
 (0)