fix windows auto start

This commit is contained in:
pompurin404 2024-08-16 15:33:14 +08:00
parent fb6e81080d
commit daf5152dd3
No known key found for this signature in database
2 changed files with 45 additions and 44 deletions

View File

@ -2,7 +2,7 @@ import { electronApp, optimizer, is } from '@electron-toolkit/utils'
import { registerIpcMainHandlers } from './utils/ipc' import { registerIpcMainHandlers } from './utils/ipc'
import windowStateKeeper from 'electron-window-state' import windowStateKeeper from 'electron-window-state'
import { app, shell, BrowserWindow, Menu, dialog, Notification } from 'electron' import { app, shell, BrowserWindow, Menu, dialog, Notification } from 'electron'
import { startMihomoMemory, stopMihomoMemory } from './core/mihomoApi' import { pauseWebsockets, startMihomoMemory, stopMihomoMemory } from './core/mihomoApi'
import { addProfileItem, getAppConfig } from './config' import { addProfileItem, getAppConfig } from './config'
import { stopCore } from './core/manager' import { stopCore } from './core/manager'
import { triggerSysProxy } from './sys/sysproxy' import { triggerSysProxy } from './sys/sysproxy'
@ -43,6 +43,7 @@ app.on('window-all-closed', (e) => {
}) })
app.on('before-quit', () => { app.on('before-quit', () => {
pauseWebsockets()
stopCore() stopCore()
triggerSysProxy(false) triggerSysProxy(false)
app.exit() app.exit()

View File

@ -8,48 +8,48 @@ import path from 'path'
const appName = 'mihomo-party' const appName = 'mihomo-party'
const taskXml = ` const taskXml = `<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task"> <Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo> <RegistrationInfo>
<Date>${new Date().toISOString()}</Date> <Date>${new Date().toISOString()}</Date>
<Author>${process.env.USERNAME}</Author> <Author>${process.env.USERNAME}</Author>
</RegistrationInfo> </RegistrationInfo>
<Triggers> <Triggers>
<LogonTrigger> <LogonTrigger>
<Enabled>true</Enabled> <Enabled>true</Enabled>
</LogonTrigger> </LogonTrigger>
</Triggers> </Triggers>
<Principals> <Principals>
<Principal id="Author"> <Principal id="Author">
<LogonType>InteractiveToken</LogonType> <LogonType>InteractiveToken</LogonType>
<RunLevel>HighestAvailable</RunLevel> <RunLevel>HighestAvailable</RunLevel>
</Principal> </Principal>
</Principals> </Principals>
<Settings> <Settings>
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy> <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries> <DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>false</StopIfGoingOnBatteries> <StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>
<AllowHardTerminate>false</AllowHardTerminate> <AllowHardTerminate>false</AllowHardTerminate>
<StartWhenAvailable>true</StartWhenAvailable> <StartWhenAvailable>true</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>true</RunOnlyIfNetworkAvailable> <RunOnlyIfNetworkAvailable>true</RunOnlyIfNetworkAvailable>
<IdleSettings> <IdleSettings>
<StopOnIdleEnd>false</StopOnIdleEnd> <StopOnIdleEnd>false</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle> <RestartOnIdle>false</RestartOnIdle>
</IdleSettings> </IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand> <AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled> <Enabled>true</Enabled>
<Hidden>false</Hidden> <Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle> <RunOnlyIfIdle>false</RunOnlyIfIdle>
<WakeToRun>false</WakeToRun> <WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>PT0S</ExecutionTimeLimit> <ExecutionTimeLimit>PT0S</ExecutionTimeLimit>
<Priority>7</Priority> <Priority>7</Priority>
</Settings> </Settings>
<Actions Context="Author"> <Actions Context="Author">
<Exec> <Exec>
<Command>${exePath()}</Command> <Command>${exePath()}</Command>
</Exec> </Exec>
</Actions> </Actions>
</Task> </Task>
` `
export async function checkAutoRun(): Promise<boolean> { export async function checkAutoRun(): Promise<boolean> {
@ -77,7 +77,7 @@ export async function enableAutoRun(): Promise<void> {
if (process.platform === 'win32') { if (process.platform === 'win32') {
const execPromise = promisify(exec) const execPromise = promisify(exec)
const taskFilePath = path.join(dataDir(), `${appName}.xml`) const taskFilePath = path.join(dataDir(), `${appName}.xml`)
await writeFile(taskFilePath, taskXml) await writeFile(taskFilePath, Buffer.from(`\ufeff${taskXml}`, 'utf-16le'))
await execPromise(`schtasks /create /tn "${appName}" /xml "${taskFilePath}" /f`) await execPromise(`schtasks /create /tn "${appName}" /xml "${taskFilePath}" /f`)
} }
if (process.platform === 'darwin') { if (process.platform === 'darwin') {