make hosts support multiple IPs and modify sniff (#5)

This commit is contained in:
汐殇 2024-08-08 09:02:14 +08:00 committed by GitHub
parent 8093aa95bb
commit 8e1004f82d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 34 additions and 19 deletions

View File

@ -42,7 +42,8 @@ export const defaultControledMihomoConfig: Partial<IMihomoConfig> = {
},
sniffer: {
enable: true,
'parse-pure-ip': false,
'parse-pure-ip': true,
'force-dns-mapping': true,
'override-destination': false,
sniff: {
HTTP: {

View File

@ -54,18 +54,20 @@ const DNS: React.FC = () => {
setValues({ ...values, [type]: newValues })
}
const handleHostsChange = (domain: string, value: string, index: number): void => {
const newHosts = [...values.hosts]
const processValue = (val: string): string | string[] =>
val.includes(',') ? val.split(',').map(s => s.trim()) : val.trim()
const isEmpty = (d: string, v: string | string[]): boolean =>
d === '' && (Array.isArray(v) ? v.every(item => item === '') : v === '')
if (index === newHosts.length) {
if (domain.trim() !== '' || value.trim() !== '') {
newHosts.push({ domain: domain.trim(), value: value.trim() })
}
} else {
if (domain.trim() === '' && value.trim() === '') {
newHosts.splice(index, 1)
const newHosts = [...values.hosts]
if (!isEmpty(domain.trim(), processValue(value))) {
if (index === newHosts.length) {
newHosts.push({ domain: domain.trim(), value: processValue(value) })
} else {
newHosts[index] = { domain: domain.trim(), value: value.trim() }
newHosts[index] = { domain: domain.trim(), value: processValue(value) }
}
} else if (index < newHosts.length) {
newHosts.splice(index, 1)
}
setValues({ ...values, hosts: newHosts })
}
@ -225,7 +227,7 @@ const DNS: React.FC = () => {
placeholder="域名"
value={domain}
onValueChange={(v) =>
handleHostsChange(v, Array.isArray(value) ? value.join(', ') : value, index)
handleHostsChange(v, Array.isArray(value) ? value.join(',') : value, index)
}
/>
</div>
@ -234,8 +236,8 @@ const DNS: React.FC = () => {
<Input
size="sm"
fullWidth
placeholder="IP 或域名"
value={Array.isArray(value) ? value.join(', ') : value}
placeholder="域名或IP"
value={Array.isArray(value) ? value.join(',') : value}
onValueChange={(v) => handleHostsChange(domain, v, index)}
/>
{index < values.hosts.length && (

View File

@ -12,6 +12,7 @@ const Sniffer: React.FC = () => {
const { sniffer } = controledMihomoConfig || {}
const {
'parse-pure-ip': parsePureIP = true,
'force-dns-mapping': forceDNSMapping = true,
'override-destination': overrideDestination = false,
sniff = {
HTTP: { ports: [80, 443], 'override-destination': false },
@ -24,6 +25,7 @@ const Sniffer: React.FC = () => {
const [values, setValues] = useState({
parsePureIP,
forceDNSMapping,
overrideDestination,
sniff,
skipDomain,
@ -74,6 +76,7 @@ const Sniffer: React.FC = () => {
onSave({
sniffer: {
'parse-pure-ip': values.parsePureIP,
'force-dns-mapping': values.forceDNSMapping,
'override-destination': values.overrideDestination,
sniff: values.sniff,
'skip-domain': values.skipDomain,
@ -107,7 +110,16 @@ const Sniffer: React.FC = () => {
}}
/>
</SettingItem>
<SettingItem title="强制嗅探IP地址" divider>
<SettingItem title="对真实IP映射嗅探" divider>
<Switch
size="sm"
isSelected={values.forceDNSMapping}
onValueChange={(v) => {
setValues({ ...values, forceDNSMapping: v })
}}
/>
</SettingItem>
<SettingItem title="对未映射IP地址嗅探" divider>
<Switch
size="sm"
isSelected={values.parsePureIP}
@ -116,7 +128,7 @@ const Sniffer: React.FC = () => {
}}
/>
</SettingItem>
<SettingItem title="嗅探 HTTP 端口" divider>
<SettingItem title="HTTP 端口嗅探" divider>
<Input
size="sm"
className="w-[50%]"
@ -124,7 +136,7 @@ const Sniffer: React.FC = () => {
onValueChange={(v) => handleSniffPortChange('HTTP', v)}
/>
</SettingItem>
<SettingItem title="嗅探 TLS 端口" divider>
<SettingItem title="TLS 端口嗅探" divider>
<Input
size="sm"
className="w-[50%]"
@ -132,7 +144,7 @@ const Sniffer: React.FC = () => {
onValueChange={(v) => handleSniffPortChange('TLS', v)}
/>
</SettingItem>
<SettingItem title="嗅探 QUIC 端口" divider>
<SettingItem title="QUIC 端口嗅探" divider>
<Input
size="sm"
className="w-[50%]"
@ -141,7 +153,7 @@ const Sniffer: React.FC = () => {
/>
</SettingItem>
<div className="flex flex-col items-stretch">
<h3 className="select-none"></h3>
<h3 className="select-none"></h3>
{[...values.skipDomain, ''].map((d, index) => (
<div key={index} className="flex mt-2">
<Input
@ -167,7 +179,7 @@ const Sniffer: React.FC = () => {
</div>
<Divider className="my-2" />
<div className="flex flex-col items-stretch">
<h3 className="select-none mb-2"></h3>
<h3 className="select-none mb-2"></h3>
{[...values.forceDomain, ''].map((d, index) => (
<div key={index} className="flex mb-2">
<Input