From 8cfee2f5e587254aed6f3d78394412365d5cc1ff Mon Sep 17 00:00:00 2001 From: ezequielnick <107352853+ezequielnick@users.noreply.github.com> Date: Wed, 6 Aug 2025 22:49:39 +0800 Subject: [PATCH] fix: resolve Windows admin mode restart issue --- src/main/core/manager.ts | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/main/core/manager.ts b/src/main/core/manager.ts index 101de38..b51d51e 100644 --- a/src/main/core/manager.ts +++ b/src/main/core/manager.ts @@ -361,24 +361,34 @@ export async function restartAsAdmin(): Promise { throw new Error('This function is only available on Windows') } - const execPromise = promisify(exec) const exePath = process.execPath const args = process.argv.slice(1) + const restartArgs = [...args, '--admin-restart-for-tun'] try { - const restartArgs = [...args, '--admin-restart-for-tun'] + // 处理路径和参数的引号 const escapedExePath = exePath.replace(/'/g, "''") - const escapedArgs = restartArgs.map(arg => `'${arg.replace(/'/g, "''")}'`).join(', ') + const argsString = restartArgs.map(arg => arg.replace(/'/g, "''")).join("', '") let command: string if (restartArgs.length > 0) { - command = `powershell -WindowStyle Hidden -Command "Start-Process -FilePath '${escapedExePath}' -ArgumentList ${escapedArgs} -Verb RunAs"` + command = `powershell -Command "Start-Process -FilePath '${escapedExePath}' -ArgumentList '${argsString}' -Verb RunAs"` } else { - command = `powershell -WindowStyle Hidden -Command "Start-Process -FilePath '${escapedExePath}' -Verb RunAs"` + command = `powershell -Command "Start-Process -FilePath '${escapedExePath}' -Verb RunAs"` } console.log('Restarting as administrator with command:', command) - await execPromise(command, { windowsHide: true }) + + // 执行PowerShell命令 + exec(command, { windowsHide: true }, (error, _stdout, stderr) => { + if (error) { + console.error('PowerShell execution error:', error) + console.error('stderr:', stderr) + } else { + console.log('PowerShell command executed successfully') + } + }) + await new Promise(resolve => setTimeout(resolve, 1500)) const { app } = await import('electron')