tun config

This commit is contained in:
pompurin404 2024-08-03 21:27:20 +08:00
parent 4b5b0787e6
commit 46a1f8ee45
No known key found for this signature in database
6 changed files with 162 additions and 15 deletions

View File

@ -13,6 +13,11 @@ export function getControledMihomoConfig(force = false): Partial<IMihomoConfig>
} }
export function setControledMihomoConfig(patch: Partial<IMihomoConfig>): void { export function setControledMihomoConfig(patch: Partial<IMihomoConfig>): void {
if (patch.tun) {
const oldTun = controledMihomoConfig.tun || {}
const newTun = Object.assign(oldTun, patch.tun)
patch.tun = newTun
}
controledMihomoConfig = Object.assign(controledMihomoConfig, patch) controledMihomoConfig = Object.assign(controledMihomoConfig, patch)
if (patch['external-controller'] || patch.secret) { if (patch['external-controller'] || patch.secret) {
getAxios(true) getAxios(true)

View File

@ -4,6 +4,13 @@ import yaml from 'yaml'
import fs from 'fs' import fs from 'fs'
export function generateProfile(): void { export function generateProfile(): void {
const profile = Object.assign(getCurrentProfile(), getControledMihomoConfig()) const currentProfile = getCurrentProfile()
const controledMihomoConfig = getControledMihomoConfig()
const { tun: profileTun = {} } = currentProfile
const { tun: controledTun } = controledMihomoConfig
const tun = Object.assign(profileTun, controledTun)
const profile = Object.assign(currentProfile, controledMihomoConfig)
console.log('profile', profile)
profile.tun = tun
fs.writeFileSync(mihomoWorkConfigPath(), yaml.stringify(profile)) fs.writeFileSync(mihomoWorkConfigPath(), yaml.stringify(profile))
} }

View File

@ -1,6 +1,7 @@
export const defaultConfig: IAppConfig = { export const defaultConfig: IAppConfig = {
core: 'mihomo', core: 'mihomo',
silentStart: false, silentStart: false,
proxyDisplayMode: 'simple',
sysProxy: { enable: false, mode: 'manual' } sysProxy: { enable: false, mode: 'manual' }
} }
@ -11,7 +12,15 @@ export const defaultControledMihomoConfig: Partial<IMihomoConfig> = {
'mixed-port': 7890, 'mixed-port': 7890,
'allow-lan': false, 'allow-lan': false,
'log-level': 'info', 'log-level': 'info',
tun: { enable: false } tun: {
enable: false,
device: 'Mihomo',
stack: 'mixed',
'auto-route': true,
'auto-detect-interface': true,
'dns-hijack': ['any:53'],
mtu: 1500
}
} }
export const defaultProfileConfig: IProfileConfig = { export const defaultProfileConfig: IProfileConfig = {

View File

@ -71,16 +71,8 @@ const Sysproxy: React.FC = () => {
selectedKey={values.mode} selectedKey={values.mode}
onSelectionChange={(key: Key) => setValues({ ...values, mode: key as SysProxyMode })} onSelectionChange={(key: Key) => setValues({ ...values, mode: key as SysProxyMode })}
> >
<Tab <Tab className="select-none" key="manual" title="手动" />
className={`select-none ${values.mode === 'manual' ? 'font-bold' : ''}`} <Tab className="select-none" key="auto" title="PAC" />
key="manual"
title="手动"
/>
<Tab
className={`select-none ${values.mode === 'auto' ? 'font-bold' : ''}`}
key="auto"
title="PAC"
/>
</Tabs> </Tabs>
</SettingItem> </SettingItem>
<SettingItem title="代理模式"> <SettingItem title="代理模式">

View File

@ -1,7 +1,140 @@
import { Button, Input, Switch, Tab, Tabs } from '@nextui-org/react'
import BasePage from '@renderer/components/base/base-page' import BasePage from '@renderer/components/base/base-page'
import SettingCard from '@renderer/components/base/base-setting-card'
import SettingItem from '@renderer/components/base/base-setting-item'
import { useControledMihomoConfig } from '@renderer/hooks/use-controled-mihomo-config'
import { restartCore } from '@renderer/utils/ipc'
import React, { Key, useState } from 'react'
const Tun: React.FC = () => { const Tun: React.FC = () => {
return <BasePage title="Tun 设置"></BasePage> const { controledMihomoConfig, patchControledMihomoConfig } = useControledMihomoConfig()
const { tun } = controledMihomoConfig || {}
const {
device = 'Mihomo',
stack = 'mixed',
'auto-route': autoRoute = true,
'auto-detect-interface': autoDetectInterface = true,
'dns-hijack': dnsHijack = ['any:53'],
'strict-route': strictRoute = false,
mtu = 1500
} = tun || {}
const [values, setValues] = useState({
device,
stack,
autoRoute,
autoDetectInterface,
dnsHijack,
strictRoute,
mtu
})
const onSave = async (patch: Partial<IMihomoConfig>): Promise<void> => {
await patchControledMihomoConfig(patch)
await restartCore()
}
return (
<BasePage
title="Tun 设置"
header={
<Button
size="sm"
color="primary"
onPress={() =>
onSave({
tun: {
device: values.device,
stack: values.stack,
'auto-route': values.autoRoute,
'auto-detect-interface': values.autoDetectInterface,
'dns-hijack': values.dnsHijack,
'strict-route': values.strictRoute,
mtu: values.mtu
}
})
}
>
</Button>
}
>
<SettingCard>
<SettingItem title="Tun 模式堆栈" divider>
<Tabs
size="sm"
color="primary"
selectedKey={values.stack}
onSelectionChange={(key: Key) => setValues({ ...values, stack: key as TunStack })}
>
<Tab key="gvisor" title="用户" className="select-none" />
<Tab key="mixed" title="混合" className="select-none" />
<Tab key="system" title="系统" className="select-none" />
</Tabs>
</SettingItem>
<SettingItem title="Tun 网卡名称" divider>
<Input
size="sm"
spellCheck={false}
className="w-[100px]"
value={values.device}
onValueChange={(v) => {
setValues({ ...values, device: v })
}}
/>
</SettingItem>
<SettingItem title="严格路由" divider>
<Switch
size="sm"
isSelected={values.strictRoute}
onValueChange={(v) => {
setValues({ ...values, strictRoute: v })
}}
/>
</SettingItem>
<SettingItem title="自动设置全局路由" divider>
<Switch
size="sm"
isSelected={values.autoRoute}
onValueChange={(v) => {
setValues({ ...values, autoRoute: v })
}}
/>
</SettingItem>
<SettingItem title="自动选择流量出口接口" divider>
<Switch
size="sm"
isSelected={values.autoDetectInterface}
onValueChange={(v) => {
setValues({ ...values, autoDetectInterface: v })
}}
/>
</SettingItem>
<SettingItem title="MTU" divider>
<Input
size="sm"
spellCheck={false}
className="w-[100px]"
value={values.mtu.toString()}
onValueChange={(v) => {
setValues({ ...values, mtu: parseInt(v) })
}}
/>
</SettingItem>
<SettingItem title="DNS 劫持">
<Input
size="sm"
spellCheck={false}
className="w-[50%]"
value={values.dnsHijack.join(',')}
onValueChange={(v) => {
setValues({ ...values, dnsHijack: v.split(',') })
}}
/>
</SettingItem>
</SettingCard>
</BasePage>
)
} }
export default Tun export default Tun

View File

@ -3,6 +3,7 @@ type LogLevel = 'info' | 'debug' | 'warning' | 'error' | 'silent'
type SysProxyMode = 'auto' | 'manual' type SysProxyMode = 'auto' | 'manual'
type MihomoGroupType = 'Selector' type MihomoGroupType = 'Selector'
type MihomoProxyType = 'Shadowsocks' type MihomoProxyType = 'Shadowsocks'
type TunStack = 'gvisor' | 'mixed' | 'system'
interface IMihomoVersion { interface IMihomoVersion {
version: string version: string
@ -138,8 +139,8 @@ interface IAppConfig {
} }
interface IMihomoTunConfig { interface IMihomoTunConfig {
enable: boolean enable?: boolean
stack?: 'system' | 'gvisor' | 'mixed' stack?: TunStack
'auto-route'?: boolean 'auto-route'?: boolean
'auto-redirect'?: boolean 'auto-redirect'?: boolean
'auto-detect-interface'?: boolean 'auto-detect-interface'?: boolean