Skip to content

Commit a3c129f

Browse files
author
pompurin404
committed
supports manual grant core for macos
1 parent 7e30114 commit a3c129f

File tree

4 files changed

+43
-4
lines changed

4 files changed

+43
-4
lines changed

src/main/core/manager.ts

+13-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ let retry = 10
1919
export async function startCore(): Promise<void> {
2020
const { core = 'mihomo' } = await getAppConfig()
2121
const corePath = mihomoCorePath(core)
22-
await grantCorePermition(corePath)
22+
await autoGrantCorePermition(corePath)
2323
await generateProfile()
2424
await checkProfile()
2525
stopCore()
@@ -90,7 +90,7 @@ async function checkProfile(): Promise<void> {
9090
}
9191
}
9292

93-
export async function grantCorePermition(corePath: string): Promise<void> {
93+
export async function autoGrantCorePermition(corePath: string): Promise<void> {
9494
const { encryptedPassword } = await getAppConfig()
9595
const execPromise = promisify(exec)
9696
if (encryptedPassword && isEncryptionAvailable()) {
@@ -117,6 +117,17 @@ export async function grantCorePermition(corePath: string): Promise<void> {
117117
}
118118
}
119119

120+
export async function manualGrantCorePermition(): Promise<void> {
121+
const { core = 'mihomo' } = await getAppConfig()
122+
const corePath = mihomoCorePath(core)
123+
const execPromise = promisify(exec)
124+
if (process.platform === 'darwin') {
125+
const shell = `chown root:admin ${corePath}\nchmod +sx ${corePath}`
126+
const command = `do shell script "${shell}" with administrator privileges`
127+
await execPromise(`osascript -e '${command}'`)
128+
}
129+
}
130+
120131
export function isEncryptionAvailable(): boolean {
121132
return safeStorage.isEncryptionAvailable()
122133
}

src/main/utils/ipc.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ import {
4444
setOverride,
4545
updateOverrideItem
4646
} from '../config'
47-
import { isEncryptionAvailable, restartCore } from '../core/manager'
47+
import { isEncryptionAvailable, manualGrantCorePermition, restartCore } from '../core/manager'
4848
import { triggerSysProxy } from '../resolve/sysproxy'
4949
import { checkUpdate } from '../resolve/autoUpdater'
5050
import { exePath, mihomoCorePath, mihomoWorkConfigPath, resourcesDir } from './dirs'
@@ -128,6 +128,7 @@ export function registerIpcMainHandlers(): void {
128128
ipcMain.handle('triggerSysProxy', (_e, enable) => ipcErrorWrapper(triggerSysProxy)(enable))
129129
ipcMain.handle('isEncryptionAvailable', isEncryptionAvailable)
130130
ipcMain.handle('encryptString', (_e, str) => safeStorage.encryptString(str))
131+
ipcMain.handle('manualGrantCorePermition', ipcErrorWrapper(manualGrantCorePermition))
131132
ipcMain.handle('getFilePath', (_e, ext) => getFilePath(ext))
132133
ipcMain.handle('readTextFile', (_e, filePath) => ipcErrorWrapper(readTextFile)(filePath))
133134
ipcMain.handle('getRuntimeConfigStr', ipcErrorWrapper(getRuntimeConfigStr))

src/renderer/src/pages/tun.tsx

+24-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import BasePage from '@renderer/components/base/base-page'
33
import SettingCard from '@renderer/components/base/base-setting-card'
44
import SettingItem from '@renderer/components/base/base-setting-item'
55
import { useControledMihomoConfig } from '@renderer/hooks/use-controled-mihomo-config'
6-
import { restartCore, setupFirewall } from '@renderer/utils/ipc'
6+
import { manualGrantCorePermition, restartCore, setupFirewall } from '@renderer/utils/ipc'
77
import { platform } from '@renderer/utils/init'
88
import React, { Key, useState } from 'react'
99

@@ -88,6 +88,29 @@ const Tun: React.FC = () => {
8888
</Button>
8989
</SettingItem>
9090
)}
91+
{platform === 'darwin' && (
92+
<SettingItem title="手动授权内核" divider>
93+
<Button
94+
size="sm"
95+
color="primary"
96+
isLoading={loading}
97+
onPress={async () => {
98+
setLoading(true)
99+
try {
100+
await manualGrantCorePermition()
101+
new Notification('内核授权成功')
102+
await restartCore()
103+
} catch (e) {
104+
alert(e)
105+
} finally {
106+
setLoading(false)
107+
}
108+
}}
109+
>
110+
手动授权内核
111+
</Button>
112+
</SettingItem>
113+
)}
91114
<SettingItem title="Tun 模式堆栈" divider>
92115
<Tabs
93116
size="sm"

src/renderer/src/utils/ipc.ts

+4
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,10 @@ export async function encryptString(str: string): Promise<Buffer> {
205205
return ipcErrorWrapper(await window.electron.ipcRenderer.invoke('encryptString', str))
206206
}
207207

208+
export async function manualGrantCorePermition(): Promise<void> {
209+
return ipcErrorWrapper(await window.electron.ipcRenderer.invoke('manualGrantCorePermition'))
210+
}
211+
208212
export async function getFilePath(ext: string[]): Promise<string[] | undefined> {
209213
return ipcErrorWrapper(await window.electron.ipcRenderer.invoke('getFilePath', ext))
210214
}

0 commit comments

Comments
 (0)