mirror of
https://gh.catmak.name/https://github.com/mihomo-party-org/mihomo-party
synced 2026-02-10 19:50:28 +08:00
fix: ensure admin restart waits for new process before quitting
This commit is contained in:
parent
a0bac512dd
commit
bab949e16a
@ -260,31 +260,29 @@ export async function restartAsAdmin(forTun: boolean = true): Promise<void> {
|
|||||||
const args = process.argv.slice(1)
|
const args = process.argv.slice(1)
|
||||||
const restartArgs = forTun ? [...args, '--admin-restart-for-tun'] : args
|
const restartArgs = forTun ? [...args, '--admin-restart-for-tun'] : args
|
||||||
|
|
||||||
try {
|
const escapedExePath = exePath.replace(/'/g, "''")
|
||||||
const escapedExePath = exePath.replace(/'/g, "''")
|
const argsString = restartArgs.map((arg) => arg.replace(/'/g, "''")).join("', '")
|
||||||
const argsString = restartArgs.map((arg) => arg.replace(/'/g, "''")).join("', '")
|
|
||||||
|
|
||||||
const command =
|
const command =
|
||||||
restartArgs.length > 0
|
restartArgs.length > 0
|
||||||
? `powershell -NoProfile -Command "Start-Process -FilePath '${escapedExePath}' -ArgumentList '${argsString}' -Verb RunAs"`
|
? `powershell -NoProfile -Command "Start-Process -FilePath '${escapedExePath}' -ArgumentList '${argsString}' -Verb RunAs -Wait:$false; exit 0"`
|
||||||
: `powershell -NoProfile -Command "Start-Process -FilePath '${escapedExePath}' -Verb RunAs"`
|
: `powershell -NoProfile -Command "Start-Process -FilePath '${escapedExePath}' -Verb RunAs -Wait:$false; exit 0"`
|
||||||
|
|
||||||
managerLogger.info('Restarting as administrator with command', command)
|
managerLogger.info('Restarting as administrator with command', command)
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
exec(command, { windowsHide: true }, (error, _stdout, stderr) => {
|
exec(command, { windowsHide: true }, (error, _stdout, stderr) => {
|
||||||
if (error) {
|
if (error) {
|
||||||
managerLogger.error('PowerShell execution error', error)
|
managerLogger.error('PowerShell execution error', error)
|
||||||
managerLogger.error('stderr', stderr)
|
managerLogger.error('stderr', stderr)
|
||||||
} else {
|
reject(new Error(`Failed to restart as administrator: ${error.message}`))
|
||||||
managerLogger.info('PowerShell command executed successfully')
|
return
|
||||||
}
|
}
|
||||||
|
managerLogger.info('PowerShell command executed successfully, quitting app')
|
||||||
|
setTimeout(() => app.quit(), 500)
|
||||||
|
resolve()
|
||||||
})
|
})
|
||||||
|
})
|
||||||
app.quit()
|
|
||||||
} catch (error) {
|
|
||||||
managerLogger.error('Failed to restart as administrator', error)
|
|
||||||
throw new Error(`Failed to restart as administrator: ${error}`)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function requestTunPermissions(): Promise<void> {
|
export async function requestTunPermissions(): Promise<void> {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user