From fd708ec8bd95984bf1f27a36b9b5bd725e1ecba0 Mon Sep 17 00:00:00 2001 From: Memory <134070804+Memory2314@users.noreply.github.com> Date: Thu, 14 Aug 2025 21:10:09 +0800 Subject: [PATCH] fix: privilege check and elevation autorun logic (#953) --- src/main/sys/autoRun.ts | 30 ++++++++++++++++--- .../components/settings/general-config.tsx | 8 +++++ src/renderer/src/locales/en-US.json | 1 + src/renderer/src/locales/zh-CN.json | 1 + 4 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/main/sys/autoRun.ts b/src/main/sys/autoRun.ts index 4758641..4c3d55b 100644 --- a/src/main/sys/autoRun.ts +++ b/src/main/sys/autoRun.ts @@ -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 { 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 { 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) diff --git a/src/renderer/src/components/settings/general-config.tsx b/src/renderer/src/components/settings/general-config.tsx index 25316df..9ac81de 100644 --- a/src/renderer/src/components/settings/general-config.tsx +++ b/src/renderer/src/components/settings/general-config.tsx @@ -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 { diff --git a/src/renderer/src/locales/en-US.json b/src/renderer/src/locales/en-US.json index b4ad798..321fc71 100644 --- a/src/renderer/src/locales/en-US.json +++ b/src/renderer/src/locales/en-US.json @@ -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", diff --git a/src/renderer/src/locales/zh-CN.json b/src/renderer/src/locales/zh-CN.json index b014d5b..d5a65a8 100644 --- a/src/renderer/src/locales/zh-CN.json +++ b/src/renderer/src/locales/zh-CN.json @@ -48,6 +48,7 @@ "settings.darkMode": "深色模式", "settings.lightMode": "浅色模式", "settings.autoStart": "开机自启", + "settings.autoStart.permissions": "正在以管理员权限配置计划任务,请在UAC对话框中点击'是'...", "settings.autoCheckUpdate": "自动检查更新", "settings.silentStart": "静默启动", "settings.autoQuitWithoutCore": "自动进入轻量模式",