Skip to content

Commit

Permalink
tun config
Browse files Browse the repository at this point in the history
  • Loading branch information
pompurin404 committed Aug 3, 2024
1 parent e0782f6 commit e779399
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 15 deletions.
5 changes: 5 additions & 0 deletions src/main/config/controledMihomo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ export function getControledMihomoConfig(force = false): Partial<IMihomoConfig>
}

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)
if (patch['external-controller'] || patch.secret) {
getAxios(true)
Expand Down
9 changes: 8 additions & 1 deletion src/main/resolve/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ import yaml from 'yaml'
import fs from 'fs'

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))
}
11 changes: 10 additions & 1 deletion src/main/utils/template.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export const defaultConfig: IAppConfig = {
core: 'mihomo',
silentStart: false,
proxyDisplayMode: 'simple',
sysProxy: { enable: false, mode: 'manual' }
}

Expand All @@ -11,7 +12,15 @@ export const defaultControledMihomoConfig: Partial<IMihomoConfig> = {
'mixed-port': 7890,
'allow-lan': false,
'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 = {
Expand Down
12 changes: 2 additions & 10 deletions src/renderer/src/pages/syspeoxy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,8 @@ const Sysproxy: React.FC = () => {
selectedKey={values.mode}
onSelectionChange={(key: Key) => setValues({ ...values, mode: key as SysProxyMode })}
>
<Tab
className={`select-none ${values.mode === 'manual' ? 'font-bold' : ''}`}
key="manual"
title="手动"
/>
<Tab
className={`select-none ${values.mode === 'auto' ? 'font-bold' : ''}`}
key="auto"
title="PAC"
/>
<Tab className="select-none" key="manual" title="手动" />
<Tab className="select-none" key="auto" title="PAC" />
</Tabs>
</SettingItem>
<SettingItem title="代理模式">
Expand Down
135 changes: 134 additions & 1 deletion src/renderer/src/pages/tun.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,140 @@
import { Button, Input, Switch, Tab, Tabs } from '@nextui-org/react'
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 = () => {
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
5 changes: 3 additions & 2 deletions src/shared/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ type LogLevel = 'info' | 'debug' | 'warning' | 'error' | 'silent'
type SysProxyMode = 'auto' | 'manual'
type MihomoGroupType = 'Selector'
type MihomoProxyType = 'Shadowsocks'
type TunStack = 'gvisor' | 'mixed' | 'system'

interface IMihomoVersion {
version: string
Expand Down Expand Up @@ -138,8 +139,8 @@ interface IAppConfig {
}

interface IMihomoTunConfig {
enable: boolean
stack?: 'system' | 'gvisor' | 'mixed'
enable?: boolean
stack?: TunStack
'auto-route'?: boolean
'auto-redirect'?: boolean
'auto-detect-interface'?: boolean
Expand Down

0 comments on commit e779399

Please sign in to comment.