fix task create

This commit is contained in:
pompurin404 2024-10-21 13:24:08 +08:00
parent 911d72975e
commit 30bf7567fb
No known key found for this signature in database
3 changed files with 87 additions and 5 deletions

View File

@ -23,7 +23,7 @@ import iconv from 'iconv-lite'
let quitTimeout: NodeJS.Timeout | null = null let quitTimeout: NodeJS.Timeout | null = null
export let mainWindow: BrowserWindow | null = null export let mainWindow: BrowserWindow | null = null
if (process.platform === 'win32' && !process.argv.includes('noadmin')) { if (process.platform === 'win32' && !is.dev && !process.argv.includes('noadmin')) {
try { try {
createElevateTask() createElevateTask()
} catch (createError) { } catch (createError) {

View File

@ -1,4 +1,4 @@
import { exePath, homeDir } from '../utils/dirs' import { exePath, homeDir, taskDir } from '../utils/dirs'
import { mkdir, readFile, rm, writeFile } from 'fs/promises' import { mkdir, readFile, rm, writeFile } from 'fs/promises'
import { exec } from 'child_process' import { exec } from 'child_process'
import { existsSync } from 'fs' import { existsSync } from 'fs'
@ -7,6 +7,47 @@ import path from 'path'
const appName = 'mihomo-party' const appName = 'mihomo-party'
const taskXml = `<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<Triggers>
<LogonTrigger>
<Enabled>true</Enabled>
<Delay>PT3S</Delay>
</LogonTrigger>
</Triggers>
<Principals>
<Principal id="Author">
<LogonType>InteractiveToken</LogonType>
<RunLevel>HighestAvailable</RunLevel>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>
<AllowHardTerminate>false</AllowHardTerminate>
<StartWhenAvailable>true</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>true</RunOnlyIfNetworkAvailable>
<IdleSettings>
<StopOnIdleEnd>false</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>PT0S</ExecutionTimeLimit>
<Priority>7</Priority>
</Settings>
<Actions Context="Author">
<Exec>
<Command>"${exePath()}"</Command>
</Exec>
</Actions>
</Task>
`
export async function checkAutoRun(): Promise<boolean> { export async function checkAutoRun(): Promise<boolean> {
if (process.platform === 'win32') { if (process.platform === 'win32') {
const execPromise = promisify(exec) const execPromise = promisify(exec)
@ -37,8 +78,10 @@ export async function checkAutoRun(): Promise<boolean> {
export async function enableAutoRun(): Promise<void> { 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(taskDir(), `${appName}.xml`)
await writeFile(taskFilePath, Buffer.from(`\ufeff${taskXml}`, 'utf-16le'))
await execPromise( await execPromise(
`C:\\\\Windows\\System32\\schtasks.exe /create /tn "${appName}" /tr "\\"${exePath()}\\"" /sc onlogon /delay 0000:03 /rl HIGHEST /it /f` `C:\\\\Windows\\System32\\schtasks.exe /create /tn "${appName}" /xml "${taskFilePath}" /f`
) )
} }
if (process.platform === 'darwin') { if (process.platform === 'darwin') {

View File

@ -12,7 +12,7 @@ import {
resourcesFilesDir, resourcesFilesDir,
taskDir taskDir
} from '../utils/dirs' } from '../utils/dirs'
import { copyFileSync } from 'fs' import { copyFileSync, writeFileSync } from 'fs'
export function getFilePath(ext: string[]): string[] | undefined { export function getFilePath(ext: string[]): string[] | undefined {
return dialog.showOpenDialogSync({ return dialog.showOpenDialogSync({
@ -64,12 +64,51 @@ export function setNativeTheme(theme: 'system' | 'light' | 'dark'): void {
nativeTheme.themeSource = theme nativeTheme.themeSource = theme
} }
const elevateTaskXml = `<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<Triggers />
<Principals>
<Principal id="Author">
<LogonType>InteractiveToken</LogonType>
<RunLevel>HighestAvailable</RunLevel>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>Parallel</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>
<AllowHardTerminate>false</AllowHardTerminate>
<StartWhenAvailable>false</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<IdleSettings>
<StopOnIdleEnd>false</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>PT72H</ExecutionTimeLimit>
<Priority>7</Priority>
</Settings>
<Actions Context="Author">
<Exec>
<Command>"${path.join(taskDir(), `mihomo-party-run.exe`)}"</Command>
<Arguments>"${exePath()}"</Arguments>
</Exec>
</Actions>
</Task>
`
export function createElevateTask(): void { export function createElevateTask(): void {
const taskFilePath = path.join(taskDir(), `mihomo-party-run.xml`)
writeFileSync(taskFilePath, Buffer.from(`\ufeff${elevateTaskXml}`, 'utf-16le'))
copyFileSync( copyFileSync(
path.join(resourcesFilesDir(), 'mihomo-party-run.exe'), path.join(resourcesFilesDir(), 'mihomo-party-run.exe'),
path.join(taskDir(), 'mihomo-party-run.exe') path.join(taskDir(), 'mihomo-party-run.exe')
) )
execSync( execSync(
`C:\\\\Windows\\System32\\schtasks.exe /create /tn "mihomo-party-run" /tr "\\"${path.join(taskDir(), `mihomo-party-run.exe`)}\\" \\"${exePath()}\\"" /sc once /st 00:00 /rl HIGHEST /f` `C:\\\\Windows\\System32\\schtasks.exe /create /tn "mihomo-party-run" /xml "${taskFilePath}" /f`
) )
} }