diff --git a/src/main/core/manager.ts b/src/main/core/manager.ts index 664e655..bc7dbd2 100644 --- a/src/main/core/manager.ts +++ b/src/main/core/manager.ts @@ -163,7 +163,7 @@ export async function startCore(detached = false): Promise[]> { // 内核日志输出到独立的 core-日期.log 文件 const stdout = createWriteStream(coreLogPath(), { flags: 'a' }) const stderr = createWriteStream(coreLogPath(), { flags: 'a' }) - + child = spawn( corePath, ['-d', diffWorkDir ? mihomoProfileWorkDir(current) : mihomoWorkDir(), ctlParam, dynamicIpcPath], @@ -307,7 +307,7 @@ async function cleanupWindowsNamedPipes(): Promise { try { const { stdout } = await execPromise( - `powershell -Command "[Console]::OutputEncoding = [System.Text.Encoding]::UTF8; Get-Process | Where-Object {$_.ProcessName -like '*mihomo*'} | Select-Object Id,ProcessName | ConvertTo-Json"`, + `powershell -NoProfile -Command "[Console]::OutputEncoding = [System.Text.Encoding]::UTF8; Get-Process | Where-Object {$_.ProcessName -like '*mihomo*'} | Select-Object Id,ProcessName | ConvertTo-Json"`, { encoding: 'utf8' } ) @@ -458,7 +458,7 @@ async function checkProfile(): Promise { const { current } = await getProfileConfig() const corePath = mihomoCorePath(core) const execFilePromise = promisify(execFile) - + try { await execFilePromise(corePath, [ '-t', @@ -598,7 +598,7 @@ export async function checkAdminPrivileges(): Promise { } const execPromise = promisify(exec) - + try { // fltmc 检测管理员权限 await execPromise('chcp 65001 >nul 2>&1 && fltmc', { encoding: 'utf8' }) @@ -607,7 +607,7 @@ export async function checkAdminPrivileges(): Promise { } catch (fltmcError: any) { const errorCode = fltmcError?.code || 0 await managerLogger.debug(`fltmc failed with code ${errorCode}, trying net session as fallback`) - + try { // net session 备用 await execPromise('chcp 65001 >nul 2>&1 && net session', { encoding: 'utf8' }) @@ -678,13 +678,13 @@ export async function restartAsAdmin(forTun: boolean = true): Promise { try { // 处理路径和参数的引号 const escapedExePath = exePath.replace(/'/g, "''") - const argsString = 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 -Command "Start-Process -FilePath '${escapedExePath}' -ArgumentList '${argsString}' -Verb RunAs"` + command = `powershell -NoProfile -Command "Start-Process -FilePath '${escapedExePath}' -ArgumentList '${argsString}' -Verb RunAs"` } else { - command = `powershell -Command "Start-Process -FilePath '${escapedExePath}' -Verb RunAs"` + command = `powershell -NoProfile -Command "Start-Process -FilePath '${escapedExePath}' -Verb RunAs"` } await managerLogger.info('Restarting as administrator with command', command) diff --git a/src/main/resolve/autoUpdater.ts b/src/main/resolve/autoUpdater.ts index 390c519..4373383 100644 --- a/src/main/resolve/autoUpdater.ts +++ b/src/main/resolve/autoUpdater.ts @@ -100,7 +100,7 @@ export async function downloadAndInstallUpdate(version: string): Promise { try { const installerPath = path.join(dataDir(), file) const isAdmin = await checkAdminPrivileges() - + if (isAdmin) { await appLogger.info('Running installer with existing admin privileges') spawn(installerPath, ['/S', '--force-run'], { @@ -111,20 +111,20 @@ export async function downloadAndInstallUpdate(version: string): Promise { // 提升权限安装 const escapedPath = installerPath.replace(/'/g, "''") const args = ['/S', '--force-run'] - const argsString = args.map(arg => arg.replace(/'/g, "''")).join("', '") - - const command = `powershell -Command "Start-Process -FilePath '${escapedPath}' -ArgumentList '${argsString}' -Verb RunAs -WindowStyle Hidden"` - + const argsString = args.map((arg) => arg.replace(/'/g, "''")).join("', '") + + const command = `powershell -NoProfile -Command "Start-Process -FilePath '${escapedPath}' -ArgumentList '${argsString}' -Verb RunAs -WindowStyle Hidden"` + await appLogger.info('Starting installer with elevated privileges') - + const execPromise = promisify(exec) await execPromise(command, { windowsHide: true }) - + await appLogger.info('Installer started successfully with elevation') } } catch (installerError) { await appLogger.error('Failed to start installer, trying fallback', installerError) - + // Fallback: 尝试使用shell.openPath打开安装包 try { await shell.openPath(path.join(dataDir(), file)) diff --git a/src/main/sys/autoRun.ts b/src/main/sys/autoRun.ts index 0b1c252..5f999b7 100644 --- a/src/main/sys/autoRun.ts +++ b/src/main/sys/autoRun.ts @@ -88,11 +88,13 @@ export async function enableAutoRun(): Promise { const isAdmin = await checkAdminPrivileges() await writeFile(taskFilePath, Buffer.from(`\ufeff${getTaskXml(isAdmin)}`, 'utf-16le')) if (isAdmin) { - await execPromise(`%SystemRoot%\\System32\\schtasks.exe /create /tn "${appName}" /xml "${taskFilePath}" /f`) + 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"` + `powershell -NoProfile -Command "Start-Process schtasks -Verb RunAs -ArgumentList '/create', '/tn', '${appName}', '/xml', '${taskFilePath}', '/f' -WindowStyle Hidden"` ) } catch (e) { @@ -140,7 +142,9 @@ export async function disableAutoRun(): Promise { 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"`) + await execPromise( + `powershell -NoProfile -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?') } diff --git a/src/main/sys/misc.ts b/src/main/sys/misc.ts index 9cbc40b..f64191d 100644 --- a/src/main/sys/misc.ts +++ b/src/main/sys/misc.ts @@ -43,7 +43,7 @@ export async function openUWPTool(): Promise { if (!isAdmin) { const escapedPath = uwpToolPath.replace(/'/g, "''") - const command = `powershell -Command "Start-Process -FilePath '${escapedPath}' -Verb RunAs -Wait"` + const command = `powershell -NoProfile -Command "Start-Process -FilePath '${escapedPath}' -Verb RunAs -Wait"` await execPromise(command, { windowsHide: true }) return