Skip to content

Commit 8e1004f

Browse files
authored
make hosts support multiple IPs and modify sniff (#5)
1 parent 8093aa9 commit 8e1004f

File tree

3 files changed

+34
-19
lines changed

3 files changed

+34
-19
lines changed

src/main/utils/template.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ export const defaultControledMihomoConfig: Partial<IMihomoConfig> = {
4242
},
4343
sniffer: {
4444
enable: true,
45-
'parse-pure-ip': false,
45+
'parse-pure-ip': true,
46+
'force-dns-mapping': true,
4647
'override-destination': false,
4748
sniff: {
4849
HTTP: {

src/renderer/src/pages/dns.tsx

+14-12
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,20 @@ const DNS: React.FC = () => {
5454
setValues({ ...values, [type]: newValues })
5555
}
5656
const handleHostsChange = (domain: string, value: string, index: number): void => {
57-
const newHosts = [...values.hosts]
57+
const processValue = (val: string): string | string[] =>
58+
val.includes(',') ? val.split(',').map(s => s.trim()) : val.trim()
59+
const isEmpty = (d: string, v: string | string[]): boolean =>
60+
d === '' && (Array.isArray(v) ? v.every(item => item === '') : v === '')
5861

59-
if (index === newHosts.length) {
60-
if (domain.trim() !== '' || value.trim() !== '') {
61-
newHosts.push({ domain: domain.trim(), value: value.trim() })
62-
}
63-
} else {
64-
if (domain.trim() === '' && value.trim() === '') {
65-
newHosts.splice(index, 1)
62+
const newHosts = [...values.hosts]
63+
if (!isEmpty(domain.trim(), processValue(value))) {
64+
if (index === newHosts.length) {
65+
newHosts.push({ domain: domain.trim(), value: processValue(value) })
6666
} else {
67-
newHosts[index] = { domain: domain.trim(), value: value.trim() }
67+
newHosts[index] = { domain: domain.trim(), value: processValue(value) }
6868
}
69+
} else if (index < newHosts.length) {
70+
newHosts.splice(index, 1)
6971
}
7072
setValues({ ...values, hosts: newHosts })
7173
}
@@ -225,7 +227,7 @@ const DNS: React.FC = () => {
225227
placeholder="域名"
226228
value={domain}
227229
onValueChange={(v) =>
228-
handleHostsChange(v, Array.isArray(value) ? value.join(', ') : value, index)
230+
handleHostsChange(v, Array.isArray(value) ? value.join(',') : value, index)
229231
}
230232
/>
231233
</div>
@@ -234,8 +236,8 @@ const DNS: React.FC = () => {
234236
<Input
235237
size="sm"
236238
fullWidth
237-
placeholder="IP 或域名"
238-
value={Array.isArray(value) ? value.join(', ') : value}
239+
placeholder="域名或IP"
240+
value={Array.isArray(value) ? value.join(',') : value}
239241
onValueChange={(v) => handleHostsChange(domain, v, index)}
240242
/>
241243
{index < values.hosts.length && (

src/renderer/src/pages/sniffer.tsx

+18-6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const Sniffer: React.FC = () => {
1212
const { sniffer } = controledMihomoConfig || {}
1313
const {
1414
'parse-pure-ip': parsePureIP = true,
15+
'force-dns-mapping': forceDNSMapping = true,
1516
'override-destination': overrideDestination = false,
1617
sniff = {
1718
HTTP: { ports: [80, 443], 'override-destination': false },
@@ -24,6 +25,7 @@ const Sniffer: React.FC = () => {
2425

2526
const [values, setValues] = useState({
2627
parsePureIP,
28+
forceDNSMapping,
2729
overrideDestination,
2830
sniff,
2931
skipDomain,
@@ -74,6 +76,7 @@ const Sniffer: React.FC = () => {
7476
onSave({
7577
sniffer: {
7678
'parse-pure-ip': values.parsePureIP,
79+
'force-dns-mapping': values.forceDNSMapping,
7780
'override-destination': values.overrideDestination,
7881
sniff: values.sniff,
7982
'skip-domain': values.skipDomain,
@@ -107,7 +110,16 @@ const Sniffer: React.FC = () => {
107110
}}
108111
/>
109112
</SettingItem>
110-
<SettingItem title="强制嗅探IP地址" divider>
113+
<SettingItem title="对真实IP映射嗅探" divider>
114+
<Switch
115+
size="sm"
116+
isSelected={values.forceDNSMapping}
117+
onValueChange={(v) => {
118+
setValues({ ...values, forceDNSMapping: v })
119+
}}
120+
/>
121+
</SettingItem>
122+
<SettingItem title="对未映射IP地址嗅探" divider>
111123
<Switch
112124
size="sm"
113125
isSelected={values.parsePureIP}
@@ -116,23 +128,23 @@ const Sniffer: React.FC = () => {
116128
}}
117129
/>
118130
</SettingItem>
119-
<SettingItem title="嗅探 HTTP 端口" divider>
131+
<SettingItem title="HTTP 端口嗅探" divider>
120132
<Input
121133
size="sm"
122134
className="w-[50%]"
123135
value={values.sniff.HTTP?.ports.join(',')}
124136
onValueChange={(v) => handleSniffPortChange('HTTP', v)}
125137
/>
126138
</SettingItem>
127-
<SettingItem title="嗅探 TLS 端口" divider>
139+
<SettingItem title="TLS 端口嗅探" divider>
128140
<Input
129141
size="sm"
130142
className="w-[50%]"
131143
value={values.sniff.TLS?.ports.join(',')}
132144
onValueChange={(v) => handleSniffPortChange('TLS', v)}
133145
/>
134146
</SettingItem>
135-
<SettingItem title="嗅探 QUIC 端口" divider>
147+
<SettingItem title="QUIC 端口嗅探" divider>
136148
<Input
137149
size="sm"
138150
className="w-[50%]"
@@ -141,7 +153,7 @@ const Sniffer: React.FC = () => {
141153
/>
142154
</SettingItem>
143155
<div className="flex flex-col items-stretch">
144-
<h3 className="select-none">跳过嗅探</h3>
156+
<h3 className="select-none">跳过嗅探域名</h3>
145157
{[...values.skipDomain, ''].map((d, index) => (
146158
<div key={index} className="flex mt-2">
147159
<Input
@@ -167,7 +179,7 @@ const Sniffer: React.FC = () => {
167179
</div>
168180
<Divider className="my-2" />
169181
<div className="flex flex-col items-stretch">
170-
<h3 className="select-none mb-2">强制嗅探</h3>
182+
<h3 className="select-none mb-2">强制嗅探域名</h3>
171183
{[...values.forceDomain, ''].map((d, index) => (
172184
<div key={index} className="flex mb-2">
173185
<Input

0 commit comments

Comments
 (0)