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 ### Bug Fixes
- 修改混合端口后系统代理没有更新 - 修改混合端口后系统代理没有更新

View File

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

1233
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

View File

@ -7,11 +7,12 @@ import {
mihomoWorkDir mihomoWorkDir
} from '../utils/dirs' } from '../utils/dirs'
import { generateProfile } from './factory' import { generateProfile } from './factory'
import { getAppConfig, patchAppConfig } from '../config' import { getAppConfig, patchAppConfig, patchControledMihomoConfig } from '../config'
import { dialog, safeStorage } from 'electron' import { dialog, safeStorage } from 'electron'
import { pauseWebsockets } from './mihomoApi' import { pauseWebsockets } from './mihomoApi'
import { writeFile } from 'fs/promises' import { writeFile } from 'fs/promises'
import { promisify } from 'util' import { promisify } from 'util'
import { mainWindow } from '..'
let child: ChildProcess let child: ChildProcess
let retry = 10 let retry = 10
@ -42,6 +43,11 @@ export async function startCore(): Promise<void> {
stopCore() stopCore()
await startCore() 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 (data.toString().includes('External controller listen error')) {
if (retry) { if (retry) {
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 { core = 'mihomo' } = await getAppConfig()
const corePath = mihomoCorePath(core) const corePath = mihomoCorePath(core)
const execPromise = promisify(exec) const execPromise = promisify(exec)
@ -130,6 +136,11 @@ export async function manualGrantCorePermition(): Promise<void> {
const command = `do shell script "${shell}" with administrator privileges` const command = `do shell script "${shell}" with administrator privileges`
await execPromise(`osascript -e '${command}'`) 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 { export function isEncryptionAvailable(): boolean {

View File

@ -143,7 +143,9 @@ export function registerIpcMainHandlers(): void {
ipcMain.handle('triggerSysProxy', (_e, enable) => ipcErrorWrapper(triggerSysProxy)(enable)) ipcMain.handle('triggerSysProxy', (_e, enable) => ipcErrorWrapper(triggerSysProxy)(enable))
ipcMain.handle('isEncryptionAvailable', isEncryptionAvailable) ipcMain.handle('isEncryptionAvailable', isEncryptionAvailable)
ipcMain.handle('encryptString', (_e, str) => encryptString(str)) 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('getFilePath', (_e, ext) => getFilePath(ext))
ipcMain.handle('readTextFile', (_e, filePath) => ipcErrorWrapper(readTextFile)(filePath)) ipcMain.handle('readTextFile', (_e, filePath) => ipcErrorWrapper(readTextFile)(filePath))
ipcMain.handle('getRuntimeConfigStr', ipcErrorWrapper(getRuntimeConfigStr)) 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 { manualGrantCorePermition, restartCore, setupFirewall } from '@renderer/utils/ipc'
import { platform } from '@renderer/utils/init' import { platform } from '@renderer/utils/init'
import React, { Key, useState } from 'react' import React, { Key, useState } from 'react'
import BasePasswordModal from '@renderer/components/base/base-password-modal'
const Tun: React.FC = () => { const Tun: React.FC = () => {
const { controledMihomoConfig, patchControledMihomoConfig } = useControledMihomoConfig() const { controledMihomoConfig, patchControledMihomoConfig } = useControledMihomoConfig()
const { tun } = controledMihomoConfig || {} const { tun } = controledMihomoConfig || {}
const [loading, setLoading] = useState(false) const [loading, setLoading] = useState(false)
const [openPasswordModal, setOpenPasswordModal] = useState(false)
const { const {
device = 'Mihomo', device = 'Mihomo',
stack = 'mixed', stack = 'mixed',
@ -39,6 +41,22 @@ const Tun: React.FC = () => {
} }
return ( return (
<>
{openPasswordModal && (
<BasePasswordModal
onCancel={() => setOpenPasswordModal(false)}
onConfirm={async (password: string) => {
try {
await manualGrantCorePermition(password)
new Notification('内核授权成功')
await restartCore()
setOpenPasswordModal(false)
} catch (e) {
alert(e)
}
}}
/>
)}
<BasePage <BasePage
title="Tun 设置" title="Tun 设置"
header={ header={
@ -88,22 +106,22 @@ const Tun: React.FC = () => {
</Button> </Button>
</SettingItem> </SettingItem>
)} )}
{platform === 'darwin' && ( {platform !== 'win32' && (
<SettingItem title="手动授权内核" divider> <SettingItem title="手动授权内核" divider>
<Button <Button
size="sm" size="sm"
color="primary" color="primary"
isLoading={loading}
onPress={async () => { onPress={async () => {
setLoading(true) if (platform === 'darwin') {
try { try {
await manualGrantCorePermition() await manualGrantCorePermition()
new Notification('内核授权成功') new Notification('内核授权成功')
await restartCore() await restartCore()
} catch (e) { } catch (e) {
alert(e) alert(e)
} finally { }
setLoading(false) } else {
setOpenPasswordModal(true)
} }
}} }}
> >
@ -195,6 +213,7 @@ const Tun: React.FC = () => {
</SettingItem> </SettingItem>
</SettingCard> </SettingCard>
</BasePage> </BasePage>
</>
) )
} }

View File

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