mirror of
https://gh.catmak.name/https://github.com/mihomo-party-org/mihomo-party
synced 2025-12-27 13:10:30 +08:00
fix: privilege check and elevation autorun logic (#953)
This commit is contained in:
parent
35f51e1e39
commit
fd708ec8bd
@ -5,6 +5,7 @@ import { exec } from 'child_process'
|
|||||||
import { existsSync } from 'fs'
|
import { existsSync } from 'fs'
|
||||||
import { promisify } from 'util'
|
import { promisify } from 'util'
|
||||||
import path from 'path'
|
import path from 'path'
|
||||||
|
import { managerLogger } from '../utils/logger'
|
||||||
|
|
||||||
const appName = 'mihomo-party'
|
const appName = 'mihomo-party'
|
||||||
|
|
||||||
@ -83,9 +84,20 @@ export async function enableAutoRun(): Promise<void> {
|
|||||||
const execPromise = promisify(exec)
|
const execPromise = promisify(exec)
|
||||||
const taskFilePath = path.join(tmpdir(), `${appName}.xml`)
|
const taskFilePath = path.join(tmpdir(), `${appName}.xml`)
|
||||||
await writeFile(taskFilePath, Buffer.from(`\ufeff${getTaskXml()}`, 'utf-16le'))
|
await writeFile(taskFilePath, Buffer.from(`\ufeff${getTaskXml()}`, 'utf-16le'))
|
||||||
await execPromise(
|
const { checkAdminPrivileges } = await import('../core/manager')
|
||||||
`powershell Start-Process schtasks -Verb RunAs -ArgumentList '/create', '/tn', '${appName}', '/xml', '${taskFilePath}', '/f'`
|
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') {
|
if (process.platform === 'darwin') {
|
||||||
const execPromise = promisify(exec)
|
const execPromise = promisify(exec)
|
||||||
@ -121,7 +133,17 @@ Categories=Utility;
|
|||||||
export async function disableAutoRun(): Promise<void> {
|
export async function disableAutoRun(): Promise<void> {
|
||||||
if (process.platform === 'win32') {
|
if (process.platform === 'win32') {
|
||||||
const execPromise = promisify(exec)
|
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') {
|
if (process.platform === 'darwin') {
|
||||||
const execPromise = promisify(exec)
|
const execPromise = promisify(exec)
|
||||||
|
|||||||
@ -104,6 +104,14 @@ const GeneralConfig: React.FC = () => {
|
|||||||
isSelected={enable}
|
isSelected={enable}
|
||||||
onValueChange={async (v) => {
|
onValueChange={async (v) => {
|
||||||
try {
|
try {
|
||||||
|
// 检查管理员权限
|
||||||
|
const hasAdminPrivileges = await window.electron.ipcRenderer.invoke('checkAdminPrivileges')
|
||||||
|
|
||||||
|
if (!hasAdminPrivileges) {
|
||||||
|
const notification = new Notification(t('settings.autoStart.permissions'))
|
||||||
|
notification.close()
|
||||||
|
}
|
||||||
|
|
||||||
if (v) {
|
if (v) {
|
||||||
await enableAutoRun()
|
await enableAutoRun()
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@ -48,6 +48,7 @@
|
|||||||
"settings.darkMode": "Dark Mode",
|
"settings.darkMode": "Dark Mode",
|
||||||
"settings.lightMode": "Light Mode",
|
"settings.lightMode": "Light Mode",
|
||||||
"settings.autoStart": "Auto Start",
|
"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.autoCheckUpdate": "Auto Check Update",
|
||||||
"settings.silentStart": "Silent Start",
|
"settings.silentStart": "Silent Start",
|
||||||
"settings.autoQuitWithoutCore": "Auto Enable Light Mode",
|
"settings.autoQuitWithoutCore": "Auto Enable Light Mode",
|
||||||
|
|||||||
@ -48,6 +48,7 @@
|
|||||||
"settings.darkMode": "深色模式",
|
"settings.darkMode": "深色模式",
|
||||||
"settings.lightMode": "浅色模式",
|
"settings.lightMode": "浅色模式",
|
||||||
"settings.autoStart": "开机自启",
|
"settings.autoStart": "开机自启",
|
||||||
|
"settings.autoStart.permissions": "正在以管理员权限配置计划任务,请在UAC对话框中点击'是'...",
|
||||||
"settings.autoCheckUpdate": "自动检查更新",
|
"settings.autoCheckUpdate": "自动检查更新",
|
||||||
"settings.silentStart": "静默启动",
|
"settings.silentStart": "静默启动",
|
||||||
"settings.autoQuitWithoutCore": "自动进入轻量模式",
|
"settings.autoQuitWithoutCore": "自动进入轻量模式",
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user