fix: privilege check and elevation autorun logic (#953)

This commit is contained in:
Memory 2025-08-14 21:10:09 +08:00 committed by GitHub
parent 35f51e1e39
commit fd708ec8bd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 36 additions and 4 deletions

View File

@ -5,6 +5,7 @@ import { exec } from 'child_process'
import { existsSync } from 'fs'
import { promisify } from 'util'
import path from 'path'
import { managerLogger } from '../utils/logger'
const appName = 'mihomo-party'
@ -83,9 +84,20 @@ export async function enableAutoRun(): Promise<void> {
const execPromise = promisify(exec)
const taskFilePath = path.join(tmpdir(), `${appName}.xml`)
await writeFile(taskFilePath, Buffer.from(`\ufeff${getTaskXml()}`, 'utf-16le'))
await execPromise(
`powershell Start-Process schtasks -Verb RunAs -ArgumentList '/create', '/tn', '${appName}', '/xml', '${taskFilePath}', '/f'`
)
const { checkAdminPrivileges } = await import('../core/manager')
const isAdmin = await checkAdminPrivileges()
if (isAdmin) {
await execPromise(`%SystemRoot%\\System32\\schtasks.exe /create /tn "${appName}" /xml "${taskFilePath}" /f`)
} else {
try {
await execPromise(
`powershell -Command "Start-Process schtasks -Verb RunAs -ArgumentList '/create', '/tn', '${appName}', '/xml', '${taskFilePath}', '/f' -WindowStyle Hidden"`
)
}
catch (e) {
await managerLogger.info('Maybe the user rejected the UAC dialog?')
}
}
}
if (process.platform === 'darwin') {
const execPromise = promisify(exec)
@ -121,7 +133,17 @@ Categories=Utility;
export async function disableAutoRun(): Promise<void> {
if (process.platform === 'win32') {
const execPromise = promisify(exec)
await execPromise(`powershell Start-Process schtasks -Verb RunAs -ArgumentList '/delete', '/tn', '${appName}', '/f'`)
const { checkAdminPrivileges } = await import('../core/manager')
const isAdmin = await checkAdminPrivileges()
if (isAdmin) {
await execPromise(`%SystemRoot%\\System32\\schtasks.exe /delete /tn "${appName}" /f`)
} else {
try {
await execPromise(`powershell -Command "Start-Process schtasks -Verb RunAs -ArgumentList '/delete', '/tn', '${appName}', '/f' -WindowStyle Hidden"`)
} catch (e) {
await managerLogger.info('Maybe the user rejected the UAC dialog?')
}
}
}
if (process.platform === 'darwin') {
const execPromise = promisify(exec)

View File

@ -104,6 +104,14 @@ const GeneralConfig: React.FC = () => {
isSelected={enable}
onValueChange={async (v) => {
try {
// 检查管理员权限
const hasAdminPrivileges = await window.electron.ipcRenderer.invoke('checkAdminPrivileges')
if (!hasAdminPrivileges) {
const notification = new Notification(t('settings.autoStart.permissions'))
notification.close()
}
if (v) {
await enableAutoRun()
} else {

View File

@ -48,6 +48,7 @@
"settings.darkMode": "Dark Mode",
"settings.lightMode": "Light Mode",
"settings.autoStart": "Auto Start",
"settings.autoStart.permissions": "Configuring scheduled tasks with administrator privileges, please click 'Yes' in the UAC dialog...",
"settings.autoCheckUpdate": "Auto Check Update",
"settings.silentStart": "Silent Start",
"settings.autoQuitWithoutCore": "Auto Enable Light Mode",

View File

@ -48,6 +48,7 @@
"settings.darkMode": "深色模式",
"settings.lightMode": "浅色模式",
"settings.autoStart": "开机自启",
"settings.autoStart.permissions": "正在以管理员权限配置计划任务请在UAC对话框中点击'是'...",
"settings.autoCheckUpdate": "自动检查更新",
"settings.silentStart": "静默启动",
"settings.autoQuitWithoutCore": "自动进入轻量模式",