perf(manager): skip PowerShell profile loading (#1401)

* perf(manager): skip PowerShell profile loading

* perf(manager): skip PowerShell profile loading *2
This commit is contained in:
e. 2025-12-01 07:49:07 +08:00 committed by GitHub
parent b76757bc19
commit 47fd7add5f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 24 additions and 20 deletions

View File

@ -163,7 +163,7 @@ export async function startCore(detached = false): Promise<Promise<void>[]> {
// 内核日志输出到独立的 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<void> {
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<void> {
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<boolean> {
}
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<boolean> {
} 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<void> {
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)

View File

@ -100,7 +100,7 @@ export async function downloadAndInstallUpdate(version: string): Promise<void> {
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<void> {
// 提升权限安装
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))

View File

@ -88,11 +88,13 @@ export async function enableAutoRun(): Promise<void> {
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<void> {
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?')
}

View File

@ -43,7 +43,7 @@ export async function openUWPTool(): Promise<void> {
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