support manual grant core permition

This commit is contained in:
pompurin404 2024-08-25 14:42:09 +08:00
parent 41efcd910c
commit c72618570a
No known key found for this signature in database
7 changed files with 841 additions and 764 deletions

View File

@ -1,3 +1,7 @@
### New Features
- Linux支持手动授权内核
### Bug Fixes
- 修改混合端口后系统代理没有更新

View File

@ -25,7 +25,7 @@
"@electron-toolkit/utils": "^3.0.0",
"@mihomo-party/sysproxy": "^2.0.0",
"adm-zip": "^0.5.15",
"axios": "^1.7.3",
"axios": "^1.7.5",
"dayjs": "^1.11.13",
"webdav": "^5.7.1",
"ws": "^8.18.0",
@ -40,22 +40,22 @@
"@electron-toolkit/tsconfig": "^1.0.1",
"@nextui-org/react": "^2.4.6",
"@types/adm-zip": "^0.5.5",
"@types/node": "^22.1.0",
"@types/node": "^22.5.0",
"@types/pubsub-js": "^1.8.6",
"@types/react": "^18.3.3",
"@types/react": "^18.3.4",
"@types/react-dom": "^18.3.0",
"@types/ws": "^8.5.12",
"@vitejs/plugin-react": "^4.3.1",
"apexcharts": "^3.52.0",
"autoprefixer": "^10.4.20",
"electron": "^31.3.1",
"electron-builder": "^25.0.3",
"electron": "^31.4.0",
"electron-builder": "^25.0.5",
"electron-vite": "^2.3.0",
"electron-window-state": "^5.0.3",
"eslint": "^8.57.0",
"eslint-plugin-react": "^7.35.0",
"framer-motion": "^11.3.21",
"meta-json-schema": "^1.18.6",
"framer-motion": "^11.3.30",
"meta-json-schema": "^1.18.7",
"monaco-yaml": "^5.2.2",
"nanoid": "^5.0.7",
"next-themes": "^0.3.0",
@ -66,18 +66,18 @@
"react-apexcharts": "^1.4.1",
"react-dom": "^18.3.1",
"react-error-boundary": "^4.0.13",
"react-icons": "^5.2.1",
"react-icons": "^5.3.0",
"react-markdown": "^9.0.1",
"react-monaco-editor": "^0.56.0",
"react-router-dom": "^6.26.0",
"react-virtuoso": "^4.9.0",
"react-monaco-editor": "^0.56.1",
"react-router-dom": "^6.26.1",
"react-virtuoso": "^4.10.1",
"swr": "^2.2.5",
"tailwindcss": "^3.4.7",
"tailwindcss": "^3.4.10",
"tar": "^7.4.3",
"tsx": "^4.16.5",
"tsx": "^4.18.0",
"types-pac": "^1.0.2",
"typescript": "^5.5.4",
"vite": "^5.3.5",
"vite": "^5.4.2",
"vite-plugin-monaco-editor": "^1.1.0"
}
}

File diff suppressed because it is too large Load Diff

View File

@ -7,11 +7,12 @@ import {
mihomoWorkDir
} from '../utils/dirs'
import { generateProfile } from './factory'
import { getAppConfig, patchAppConfig } from '../config'
import { getAppConfig, patchAppConfig, patchControledMihomoConfig } from '../config'
import { dialog, safeStorage } from 'electron'
import { pauseWebsockets } from './mihomoApi'
import { writeFile } from 'fs/promises'
import { promisify } from 'util'
import { mainWindow } from '..'
let child: ChildProcess
let retry = 10
@ -42,6 +43,11 @@ export async function startCore(): Promise<void> {
stopCore()
await startCore()
}
if (data.toString().includes('configure tun interface: operation not permitted')) {
await patchControledMihomoConfig({ tun: { enable: false } })
mainWindow?.webContents.send('controledMihomoConfigUpdated')
dialog.showErrorBox('虚拟网卡启动失败', '请尝试手动授予内核权限')
}
if (data.toString().includes('External controller listen error')) {
if (retry) {
retry--
@ -121,7 +127,7 @@ export async function autoGrantCorePermition(corePath: string): Promise<void> {
}
}
export async function manualGrantCorePermition(): Promise<void> {
export async function manualGrantCorePermition(password?: string): Promise<void> {
const { core = 'mihomo' } = await getAppConfig()
const corePath = mihomoCorePath(core)
const execPromise = promisify(exec)
@ -130,6 +136,11 @@ export async function manualGrantCorePermition(): Promise<void> {
const command = `do shell script "${shell}" with administrator privileges`
await execPromise(`osascript -e '${command}'`)
}
if (process.platform === 'linux') {
await execPromise(
`echo "${password}" | sudo -S setcap cap_net_bind_service,cap_net_admin,cap_sys_ptrace,cap_dac_read_search,cap_dac_override,cap_net_raw=+ep ${corePath}`
)
}
}
export function isEncryptionAvailable(): boolean {

View File

@ -143,7 +143,9 @@ export function registerIpcMainHandlers(): void {
ipcMain.handle('triggerSysProxy', (_e, enable) => ipcErrorWrapper(triggerSysProxy)(enable))
ipcMain.handle('isEncryptionAvailable', isEncryptionAvailable)
ipcMain.handle('encryptString', (_e, str) => encryptString(str))
ipcMain.handle('manualGrantCorePermition', ipcErrorWrapper(manualGrantCorePermition))
ipcMain.handle('manualGrantCorePermition', (_e, password) =>
ipcErrorWrapper(manualGrantCorePermition)(password)
)
ipcMain.handle('getFilePath', (_e, ext) => getFilePath(ext))
ipcMain.handle('readTextFile', (_e, filePath) => ipcErrorWrapper(readTextFile)(filePath))
ipcMain.handle('getRuntimeConfigStr', ipcErrorWrapper(getRuntimeConfigStr))

View File

@ -6,11 +6,13 @@ import { useControledMihomoConfig } from '@renderer/hooks/use-controled-mihomo-c
import { manualGrantCorePermition, restartCore, setupFirewall } from '@renderer/utils/ipc'
import { platform } from '@renderer/utils/init'
import React, { Key, useState } from 'react'
import BasePasswordModal from '@renderer/components/base/base-password-modal'
const Tun: React.FC = () => {
const { controledMihomoConfig, patchControledMihomoConfig } = useControledMihomoConfig()
const { tun } = controledMihomoConfig || {}
const [loading, setLoading] = useState(false)
const [openPasswordModal, setOpenPasswordModal] = useState(false)
const {
device = 'Mihomo',
stack = 'mixed',
@ -39,162 +41,179 @@ const Tun: React.FC = () => {
}
return (
<BasePage
title="Tun 设置"
header={
<Button
size="sm"
color="primary"
onPress={() =>
onSave({
tun: {
device: values.device,
stack: values.stack,
'auto-route': values.autoRoute,
'auto-redirect': values.autoRedirect,
'auto-detect-interface': values.autoDetectInterface,
'dns-hijack': values.dnsHijack,
'strict-route': values.strictRoute,
mtu: values.mtu
}
})
}
>
</Button>
}
>
<SettingCard>
{platform === 'win32' && (
<SettingItem title="重设防火墙" divider>
<Button
size="sm"
color="primary"
isLoading={loading}
onPress={async () => {
setLoading(true)
try {
await setupFirewall()
new Notification('防火墙重设成功')
await restartCore()
} catch (e) {
alert(e)
} finally {
setLoading(false)
}
}}
>
</Button>
</SettingItem>
)}
{platform === 'darwin' && (
<SettingItem title="手动授权内核" divider>
<Button
size="sm"
color="primary"
isLoading={loading}
onPress={async () => {
setLoading(true)
try {
await manualGrantCorePermition()
new Notification('内核授权成功')
await restartCore()
} catch (e) {
alert(e)
} finally {
setLoading(false)
}
}}
>
</Button>
</SettingItem>
)}
<SettingItem title="Tun 模式堆栈" divider>
<Tabs
<>
{openPasswordModal && (
<BasePasswordModal
onCancel={() => setOpenPasswordModal(false)}
onConfirm={async (password: string) => {
try {
await manualGrantCorePermition(password)
new Notification('内核授权成功')
await restartCore()
setOpenPasswordModal(false)
} catch (e) {
alert(e)
}
}}
/>
)}
<BasePage
title="Tun 设置"
header={
<Button
size="sm"
color="primary"
selectedKey={values.stack}
onSelectionChange={(key: Key) => setValues({ ...values, stack: key as TunStack })}
onPress={() =>
onSave({
tun: {
device: values.device,
stack: values.stack,
'auto-route': values.autoRoute,
'auto-redirect': values.autoRedirect,
'auto-detect-interface': values.autoDetectInterface,
'dns-hijack': values.dnsHijack,
'strict-route': values.strictRoute,
mtu: values.mtu
}
})
}
>
<Tab key="gvisor" title="用户" />
<Tab key="mixed" title="混合" />
<Tab key="system" title="系统" />
</Tabs>
</SettingItem>
<SettingItem title="Tun 网卡名称" divider>
<Input
size="sm"
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>
{platform === 'linux' && (
<SettingItem title="自动设置TCP重定向" divider>
<Switch
</Button>
}
>
<SettingCard>
{platform === 'win32' && (
<SettingItem title="重设防火墙" divider>
<Button
size="sm"
color="primary"
isLoading={loading}
onPress={async () => {
setLoading(true)
try {
await setupFirewall()
new Notification('防火墙重设成功')
await restartCore()
} catch (e) {
alert(e)
} finally {
setLoading(false)
}
}}
>
</Button>
</SettingItem>
)}
{platform !== 'win32' && (
<SettingItem title="手动授权内核" divider>
<Button
size="sm"
color="primary"
onPress={async () => {
if (platform === 'darwin') {
try {
await manualGrantCorePermition()
new Notification('内核授权成功')
await restartCore()
} catch (e) {
alert(e)
}
} else {
setOpenPasswordModal(true)
}
}}
>
</Button>
</SettingItem>
)}
<SettingItem title="Tun 模式堆栈" divider>
<Tabs
size="sm"
isSelected={values.autoRedirect}
color="primary"
selectedKey={values.stack}
onSelectionChange={(key: Key) => setValues({ ...values, stack: key as TunStack })}
>
<Tab key="gvisor" title="用户" />
<Tab key="mixed" title="混合" />
<Tab key="system" title="系统" />
</Tabs>
</SettingItem>
<SettingItem title="Tun 网卡名称" divider>
<Input
size="sm"
className="w-[100px]"
value={values.device}
onValueChange={(v) => {
setValues({ ...values, autoRedirect: v })
setValues({ ...values, device: 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"
type="number"
className="w-[100px]"
value={values.mtu.toString()}
onValueChange={(v) => {
setValues({ ...values, mtu: parseInt(v) })
}}
/>
</SettingItem>
<SettingItem title="DNS 劫持">
<Input
size="sm"
className="w-[50%]"
value={values.dnsHijack.join(',')}
onValueChange={(v) => {
const arr = v !== '' ? v.split(',') : []
setValues({ ...values, dnsHijack: arr })
}}
/>
</SettingItem>
</SettingCard>
</BasePage>
<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>
{platform === 'linux' && (
<SettingItem title="自动设置TCP重定向" divider>
<Switch
size="sm"
isSelected={values.autoRedirect}
onValueChange={(v) => {
setValues({ ...values, autoRedirect: 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"
type="number"
className="w-[100px]"
value={values.mtu.toString()}
onValueChange={(v) => {
setValues({ ...values, mtu: parseInt(v) })
}}
/>
</SettingItem>
<SettingItem title="DNS 劫持">
<Input
size="sm"
className="w-[50%]"
value={values.dnsHijack.join(',')}
onValueChange={(v) => {
const arr = v !== '' ? v.split(',') : []
setValues({ ...values, dnsHijack: arr })
}}
/>
</SettingItem>
</SettingCard>
</BasePage>
</>
)
}

View File

@ -213,8 +213,10 @@ export async function encryptString(str: string): Promise<number[]> {
return ipcErrorWrapper(await window.electron.ipcRenderer.invoke('encryptString', str))
}
export async function manualGrantCorePermition(): Promise<void> {
return ipcErrorWrapper(await window.electron.ipcRenderer.invoke('manualGrantCorePermition'))
export async function manualGrantCorePermition(password?: string): Promise<void> {
return ipcErrorWrapper(
await window.electron.ipcRenderer.invoke('manualGrantCorePermition', password)
)
}
export async function getFilePath(ext: string[]): Promise<string[] | undefined> {