mirror of
https://gh.catmak.name/https://github.com/mihomo-party-org/mihomo-party
synced 2025-12-27 05:00:30 +08:00
refactor: rewrite DNS control module with override logic
This commit is contained in:
parent
d8aeb63584
commit
f7b9eb2113
@ -20,15 +20,22 @@ export async function getControledMihomoConfig(force = false): Promise<Partial<I
|
||||
|
||||
export async function patchControledMihomoConfig(patch: Partial<IMihomoConfig>): Promise<void> {
|
||||
const { useNameserverPolicy, controlDns = true, controlSniff = true } = await getAppConfig()
|
||||
if (!controlDns) {
|
||||
|
||||
// DNS 配置覆写逻辑
|
||||
if (controlDns) {
|
||||
if (!controledMihomoConfig.dns || controledMihomoConfig.dns?.ipv6 === undefined) {
|
||||
controledMihomoConfig.dns = { ...defaultControledMihomoConfig.dns }
|
||||
}
|
||||
const originalEnable = controledMihomoConfig.dns?.enable
|
||||
controledMihomoConfig.dns = deepMerge(controledMihomoConfig.dns || {}, defaultControledMihomoConfig.dns || {})
|
||||
if (originalEnable !== undefined) {
|
||||
controledMihomoConfig.dns.enable = originalEnable
|
||||
}
|
||||
} else {
|
||||
delete controledMihomoConfig.dns
|
||||
delete controledMihomoConfig.hosts
|
||||
} else {
|
||||
// 从不接管状态恢复
|
||||
if (controledMihomoConfig.dns?.ipv6 === undefined) {
|
||||
controledMihomoConfig.dns = defaultControledMihomoConfig.dns
|
||||
}
|
||||
}
|
||||
|
||||
if (!controlSniff) {
|
||||
delete controledMihomoConfig.sniffer
|
||||
} else {
|
||||
@ -37,6 +44,7 @@ export async function patchControledMihomoConfig(patch: Partial<IMihomoConfig>):
|
||||
controledMihomoConfig.sniffer = defaultControledMihomoConfig.sniffer
|
||||
}
|
||||
}
|
||||
|
||||
if (patch.hosts) {
|
||||
controledMihomoConfig.hosts = patch.hosts
|
||||
}
|
||||
@ -44,7 +52,9 @@ export async function patchControledMihomoConfig(patch: Partial<IMihomoConfig>):
|
||||
controledMihomoConfig.dns = controledMihomoConfig.dns || {}
|
||||
controledMihomoConfig.dns['nameserver-policy'] = patch.dns['nameserver-policy']
|
||||
}
|
||||
|
||||
controledMihomoConfig = deepMerge(controledMihomoConfig, patch)
|
||||
|
||||
if (!useNameserverPolicy) {
|
||||
delete controledMihomoConfig?.dns?.['nameserver-policy']
|
||||
}
|
||||
|
||||
@ -79,8 +79,10 @@ export const defaultControledMihomoConfig: Partial<IMihomoConfig> = {
|
||||
'fake-ip-filter': ['*', '+.lan', '+.local', 'time.*.com', 'ntp.*.com', '+.market.xiaomi.com'],
|
||||
'use-hosts': false,
|
||||
'use-system-hosts': false,
|
||||
nameserver: ['https://120.53.53.53/dns-query', 'https://223.5.5.5/dns-query'],
|
||||
'proxy-server-nameserver': ['https://120.53.53.53/dns-query', 'https://223.5.5.5/dns-query'],
|
||||
'respect-rules': false,
|
||||
'default-nameserver': ['tls://223.5.5.5'],
|
||||
nameserver: ['https://doh.pub/dns-query', 'https://dns.alidns.com/dns-query'],
|
||||
'proxy-server-nameserver': ['https://doh.pub/dns-query', 'https://dns.alidns.com/dns-query'],
|
||||
'direct-nameserver': []
|
||||
},
|
||||
sniffer: {
|
||||
|
||||
@ -3,7 +3,7 @@ import { useControledMihomoConfig } from '@renderer/hooks/use-controled-mihomo-c
|
||||
import BorderSwitch from '@renderer/components/base/border-swtich'
|
||||
import { LuServer } from 'react-icons/lu'
|
||||
import { useLocation, useNavigate } from 'react-router-dom'
|
||||
import { patchMihomoConfig } from '@renderer/utils/ipc'
|
||||
import { restartCore } from '@renderer/utils/ipc'
|
||||
import { useSortable } from '@dnd-kit/sortable'
|
||||
import { CSS } from '@dnd-kit/utilities'
|
||||
import { useAppConfig } from '@renderer/hooks/use-app-config'
|
||||
@ -15,15 +15,14 @@ interface Props {
|
||||
}
|
||||
const DNSCard: React.FC<Props> = (props) => {
|
||||
const { t } = useTranslation()
|
||||
const { appConfig } = useAppConfig()
|
||||
const { appConfig, patchAppConfig } = useAppConfig()
|
||||
const { iconOnly } = props
|
||||
const { dnsCardStatus = 'col-span-1', controlDns = true } = appConfig || {}
|
||||
const location = useLocation()
|
||||
const navigate = useNavigate()
|
||||
const match = location.pathname.includes('/dns')
|
||||
const { controledMihomoConfig, patchControledMihomoConfig } = useControledMihomoConfig()
|
||||
const { dns, tun } = controledMihomoConfig || {}
|
||||
const { enable = true } = dns || {}
|
||||
const { tun } = controledMihomoConfig || {}
|
||||
const {
|
||||
attributes,
|
||||
listeners,
|
||||
@ -35,20 +34,33 @@ const DNSCard: React.FC<Props> = (props) => {
|
||||
id: 'dns'
|
||||
})
|
||||
const transform = tf ? { x: tf.x, y: tf.y, scaleX: 1, scaleY: 1 } : null
|
||||
const onChange = async (enable: boolean): Promise<void> => {
|
||||
await patchControledMihomoConfig({ dns: { enable } })
|
||||
await patchMihomoConfig({ dns: { enable } })
|
||||
const onChange = async (controlDnsEnabled: boolean): Promise<void> => {
|
||||
try {
|
||||
await patchAppConfig({ controlDns: controlDnsEnabled })
|
||||
await patchControledMihomoConfig({})
|
||||
await restartCore()
|
||||
} catch (e) {
|
||||
alert(e)
|
||||
}
|
||||
}
|
||||
|
||||
if (iconOnly) {
|
||||
return (
|
||||
<div className={`${dnsCardStatus} ${!controlDns ? 'hidden' : ''} flex justify-center`}>
|
||||
<Tooltip content={t('sider.cards.dns')} placement="right">
|
||||
<div className={`${dnsCardStatus} flex justify-center`}>
|
||||
<Tooltip
|
||||
content={
|
||||
controlDns
|
||||
? `${t('sider.cards.dns')} (${t('sider.cards.dns.overrideMode')})`
|
||||
: `${t('sider.cards.dns')} (${t('sider.cards.dns.originalConfig')})`
|
||||
}
|
||||
placement="right"
|
||||
>
|
||||
<Button
|
||||
size="sm"
|
||||
isIconOnly
|
||||
color={match ? 'primary' : 'default'}
|
||||
variant={match ? 'solid' : 'light'}
|
||||
className={!controlDns ? 'opacity-60' : ''}
|
||||
onPress={() => {
|
||||
navigate('/dns')
|
||||
}}
|
||||
@ -68,14 +80,14 @@ const DNSCard: React.FC<Props> = (props) => {
|
||||
transition,
|
||||
zIndex: isDragging ? 'calc(infinity)' : undefined
|
||||
}}
|
||||
className={`${dnsCardStatus} ${!controlDns ? 'hidden' : ''} dns-card`}
|
||||
className={`${dnsCardStatus} dns-card`}
|
||||
>
|
||||
<Card
|
||||
fullWidth
|
||||
ref={setNodeRef}
|
||||
{...attributes}
|
||||
{...listeners}
|
||||
className={`${match ? 'bg-primary' : 'hover:bg-primary/30'} ${isDragging ? 'scale-[0.97] tap-highlight-transparent' : ''}`}
|
||||
className={`${match ? 'bg-primary' : 'hover:bg-primary/30'} ${isDragging ? 'scale-[0.97] tap-highlight-transparent' : ''} ${!controlDns ? 'opacity-60' : ''}`}
|
||||
>
|
||||
<CardBody className="pb-1 pt-0 px-0">
|
||||
<div className="flex justify-between">
|
||||
@ -90,8 +102,8 @@ const DNSCard: React.FC<Props> = (props) => {
|
||||
/>
|
||||
</Button>
|
||||
<BorderSwitch
|
||||
isShowBorder={match && enable}
|
||||
isSelected={enable}
|
||||
isShowBorder={match && controlDns}
|
||||
isSelected={controlDns}
|
||||
isDisabled={tun?.enable}
|
||||
onValueChange={onChange}
|
||||
/>
|
||||
|
||||
@ -42,13 +42,14 @@ const SniffCard: React.FC<Props> = (props) => {
|
||||
|
||||
if (iconOnly) {
|
||||
return (
|
||||
<div className={`${sniffCardStatus} ${!controlSniff ? 'hidden' : ''} flex justify-center`}>
|
||||
<div className={`${sniffCardStatus} flex justify-center`}>
|
||||
<Tooltip content={t('sider.cards.sniff')} placement="right">
|
||||
<Button
|
||||
size="sm"
|
||||
isIconOnly
|
||||
color={match ? 'primary' : 'default'}
|
||||
variant={match ? 'solid' : 'light'}
|
||||
className={!enable ? 'opacity-60' : ''}
|
||||
onPress={() => {
|
||||
navigate('/sniffer')
|
||||
}}
|
||||
@ -68,14 +69,14 @@ const SniffCard: React.FC<Props> = (props) => {
|
||||
transition,
|
||||
zIndex: isDragging ? 'calc(infinity)' : undefined
|
||||
}}
|
||||
className={`${sniffCardStatus} ${!controlSniff ? 'hidden' : ''} sniff-card`}
|
||||
className={`${sniffCardStatus} sniff-card`}
|
||||
>
|
||||
<Card
|
||||
fullWidth
|
||||
ref={setNodeRef}
|
||||
{...attributes}
|
||||
{...listeners}
|
||||
className={`${match ? 'bg-primary' : 'hover:bg-primary/30'} ${isDragging ? 'scale-[0.97] tap-highlight-transparent' : ''}`}
|
||||
className={`${match ? 'bg-primary' : 'hover:bg-primary/30'} ${isDragging ? 'scale-[0.97] tap-highlight-transparent' : ''} ${!enable ? 'opacity-60' : ''}`}
|
||||
>
|
||||
<CardBody className="pb-1 pt-0 px-0">
|
||||
<div className="flex justify-between">
|
||||
|
||||
@ -58,6 +58,7 @@ const SysproxySwitcher: React.FC<Props> = (props) => {
|
||||
isIconOnly
|
||||
color={match ? 'primary' : 'default'}
|
||||
variant={match ? 'solid' : 'light'}
|
||||
className={!enable ? 'opacity-60' : ''}
|
||||
onPress={() => {
|
||||
navigate('/sysproxy')
|
||||
}}
|
||||
@ -84,7 +85,7 @@ const SysproxySwitcher: React.FC<Props> = (props) => {
|
||||
ref={setNodeRef}
|
||||
{...attributes}
|
||||
{...listeners}
|
||||
className={`${match ? 'bg-primary' : 'hover:bg-primary/30'} ${isDragging ? 'scale-[0.97] tap-highlight-transparent' : ''}`}
|
||||
className={`${match ? 'bg-primary' : 'hover:bg-primary/30'} ${isDragging ? 'scale-[0.97] tap-highlight-transparent' : ''} ${!enable ? 'opacity-60' : ''}`}
|
||||
>
|
||||
<CardBody className="pb-1 pt-0 px-0">
|
||||
<div className="flex justify-between">
|
||||
|
||||
@ -56,6 +56,7 @@ const TunSwitcher: React.FC<Props> = (props) => {
|
||||
isIconOnly
|
||||
color={match ? 'primary' : 'default'}
|
||||
variant={match ? 'solid' : 'light'}
|
||||
className={!enable ? 'opacity-60' : ''}
|
||||
onPress={() => {
|
||||
navigate('/tun')
|
||||
}}
|
||||
@ -82,7 +83,7 @@ const TunSwitcher: React.FC<Props> = (props) => {
|
||||
ref={setNodeRef}
|
||||
{...attributes}
|
||||
{...listeners}
|
||||
className={`${match ? 'bg-primary' : 'hover:bg-primary/30'} ${isDragging ? 'scale-[0.97] tap-highlight-transparent' : ''}`}
|
||||
className={`${match ? 'bg-primary' : 'hover:bg-primary/30'} ${isDragging ? 'scale-[0.97] tap-highlight-transparent' : ''} ${!enable ? 'opacity-60' : ''}`}
|
||||
>
|
||||
<CardBody className="pb-1 pt-0 px-0">
|
||||
<div className="flex justify-between">
|
||||
|
||||
@ -98,7 +98,7 @@
|
||||
"mihomo.cpuPriority.low": "Low",
|
||||
"mihomo.workDir.title": "Separate Work Directory for Different Subscriptions",
|
||||
"mihomo.workDir.tooltip": "Enable to avoid conflicts when different subscriptions have proxy groups with the same name",
|
||||
"mihomo.controlDns": "Control DNS Settings",
|
||||
"mihomo.controlDns": "Override DNS Settings",
|
||||
"mihomo.controlSniff": "Control Domain Sniffing",
|
||||
"mihomo.autoCloseConnection": "Auto Close Connection",
|
||||
"mihomo.pauseSSID.title": "Direct Connection for Specific WiFi SSIDs",
|
||||
@ -216,7 +216,9 @@
|
||||
"sider.cards.override": "Override",
|
||||
"sider.cards.connections": "Connections",
|
||||
"sider.cards.core": "Core Settings",
|
||||
"sider.cards.dns": "DNS",
|
||||
"sider.cards.dns": "DNS OVR",
|
||||
"sider.cards.dns.originalConfig": "Using Original Config",
|
||||
"sider.cards.dns.overrideMode": "Override Mode",
|
||||
"sider.cards.sniff": "Sniffing",
|
||||
"sider.cards.logs": "Logs",
|
||||
"sider.cards.substore": "Sub-Store",
|
||||
@ -316,6 +318,7 @@
|
||||
"tun.notifications.firewallResetSuccess": "Firewall Reset Successful",
|
||||
"tun.error.tunPermissionDenied": "TUN interface start failed, please try to manually grant core permissions",
|
||||
"dns.title": "DNS Settings",
|
||||
"dns.enable.title": "Enable DNS",
|
||||
"dns.enhancedMode.title": "Domain Mapping Mode",
|
||||
"dns.enhancedMode.fakeIp": "Fake IP",
|
||||
"dns.enhancedMode.redirHost": "Real IP",
|
||||
@ -513,7 +516,7 @@
|
||||
"guide.override.title": "Override",
|
||||
"guide.override.description": "Mihomo Party provides powerful override functionality to customize your imported subscription configurations, such as adding rules and customizing proxy groups. You can directly import override files written by others or write your own. <b>Remember to enable the override file on the subscription you want to override</b>. For override file syntax, please refer to the <a href=\"https://mihomo.party/docs/guide/override\" target=\"_blank\">official documentation</a>.",
|
||||
"guide.dns.title": "DNS",
|
||||
"guide.dns.description": "The software takes control of the core's DNS settings by default. If you need to use the DNS settings from your subscription configuration, you can disable 'Control DNS Settings' in the application settings. The same applies to domain sniffing.",
|
||||
"guide.dns.description": "The software overrides subscription DNS settings with application DNS settings by default. If you need to use the DNS settings from your subscription configuration, you can disable 'Override DNS Settings' in the application settings. The same applies to domain sniffing.",
|
||||
"guide.end.title": "Tutorial Complete",
|
||||
"guide.end.description": "Now that you understand the basic usage of the software, import your subscription and start using it. Enjoy!\nYou can also join our official <a href=\"https://t.me/mihomo_party_group\" target=\"_blank\">Telegram group</a> for the latest news."
|
||||
}
|
||||
|
||||
@ -101,7 +101,7 @@
|
||||
"mihomo.cpuPriority.low": "低",
|
||||
"mihomo.workDir.title": "不同订阅使用独立工作目录",
|
||||
"mihomo.workDir.tooltip": "启用后可避免不同订阅中存在相同名称的代理组时发生冲突",
|
||||
"mihomo.controlDns": "控制 DNS 设置",
|
||||
"mihomo.controlDns": "覆写 DNS 设置",
|
||||
"mihomo.controlSniff": "控制域名嗅探",
|
||||
"mihomo.autoCloseConnection": "自动关闭连接",
|
||||
"mihomo.pauseSSID.title": "指定 WiFi SSID 直连",
|
||||
@ -216,7 +216,9 @@
|
||||
"sider.cards.override": "覆写",
|
||||
"sider.cards.connections": "连接",
|
||||
"sider.cards.core": "内核设置",
|
||||
"sider.cards.dns": "DNS",
|
||||
"sider.cards.dns": "DNS覆写",
|
||||
"sider.cards.dns.originalConfig": "使用原始配置",
|
||||
"sider.cards.dns.overrideMode": "覆写模式",
|
||||
"sider.cards.sniff": "域名嗅探",
|
||||
"sider.cards.logs": "日志",
|
||||
"sider.cards.substore": "Sub-Store",
|
||||
@ -316,6 +318,7 @@
|
||||
"tun.notifications.firewallResetSuccess": "防火墙重设成功",
|
||||
"tun.error.tunPermissionDenied": "虚拟网卡启动失败,请尝试手动授予内核权限",
|
||||
"dns.title": "DNS 设置",
|
||||
"dns.enable.title": "启用 DNS",
|
||||
"dns.enhancedMode.title": "域名映射模式",
|
||||
"dns.enhancedMode.fakeIp": "虚假 IP",
|
||||
"dns.enhancedMode.redirHost": "真实 IP",
|
||||
@ -513,7 +516,7 @@
|
||||
"guide.override.title": "覆写",
|
||||
"guide.override.description": "Mihomo Party 提供强大的覆写功能,可以对您导入的订阅配置进行个性化修改,如添加规则、自定义代理组等,您可以直接导入别人写好的覆写文件,也可以自己动手编写,<b>编辑好覆写文件一定要记得在需要覆写的订阅上启用</b>,覆写文件的语法请参考 <a href=\"https://mihomo.party/docs/guide/override\" target=\"_blank\">官方文档</a>",
|
||||
"guide.dns.title": "DNS",
|
||||
"guide.dns.description": "软件默认接管了内核的 DNS 设置,如果您需要使用订阅配置中的 DNS 设置,可以到应用设置中关闭\"接管 DNS 设置\",域名嗅探同理",
|
||||
"guide.dns.description": "软件默认使用应用的 DNS 设置覆写订阅配置,如果您需要使用订阅配置中的 DNS 设置,可以到应用设置中关闭\"覆写 DNS 设置\",域名嗅探同理",
|
||||
"guide.end.title": "教程结束",
|
||||
"guide.end.description": "现在您已经了解了软件的基本用法,导入您的订阅开始使用吧,祝您使用愉快!\n您还可以加入我们的官方 <a href=\"https://t.me/mihomo_party_group\" target=\"_blank\">Telegram 群组</a> 获取最新资讯"
|
||||
}
|
||||
|
||||
@ -16,6 +16,7 @@ const DNS: React.FC = () => {
|
||||
const { nameserverPolicy, useNameserverPolicy } = appConfig || {}
|
||||
const { dns, hosts } = controledMihomoConfig || {}
|
||||
const {
|
||||
enable = true,
|
||||
ipv6 = false,
|
||||
'fake-ip-range': fakeIPRange = '198.18.0.1/16',
|
||||
'fake-ip-filter': fakeIPFilter = [
|
||||
@ -40,6 +41,7 @@ const DNS: React.FC = () => {
|
||||
} = dns || {}
|
||||
const [changed, setChanged] = useState(false)
|
||||
const [values, originSetValues] = useState({
|
||||
enable,
|
||||
ipv6,
|
||||
useHosts,
|
||||
enhancedMode,
|
||||
@ -143,6 +145,7 @@ const DNS: React.FC = () => {
|
||||
color="primary"
|
||||
onPress={() => {
|
||||
const dnsConfig = {
|
||||
enable: values.enable,
|
||||
ipv6: values.ipv6,
|
||||
'fake-ip-range': values.fakeIPRange,
|
||||
'fake-ip-filter': values.fakeIPFilter,
|
||||
@ -177,6 +180,15 @@ const DNS: React.FC = () => {
|
||||
}
|
||||
>
|
||||
<SettingCard>
|
||||
<SettingItem title={t('dns.enable.title')} divider>
|
||||
<Switch
|
||||
size="sm"
|
||||
isSelected={values.enable}
|
||||
onValueChange={(v) => {
|
||||
setValues({ ...values, enable: v })
|
||||
}}
|
||||
/>
|
||||
</SettingItem>
|
||||
<SettingItem title={t('dns.enhancedMode.title')} divider>
|
||||
<Tabs
|
||||
size="sm"
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user