diff --git a/src/main/index.ts b/src/main/index.ts index 815ee39..5ced391 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -23,7 +23,7 @@ import iconv from 'iconv-lite' let quitTimeout: NodeJS.Timeout | 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 { createElevateTask() } catch (createError) { diff --git a/src/main/sys/autoRun.ts b/src/main/sys/autoRun.ts index 02d60d4..fb4db8d 100644 --- a/src/main/sys/autoRun.ts +++ b/src/main/sys/autoRun.ts @@ -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 { exec } from 'child_process' import { existsSync } from 'fs' @@ -7,6 +7,47 @@ import path from 'path' const appName = 'mihomo-party' +const taskXml = ` + + + + true + PT3S + + + + + InteractiveToken + HighestAvailable + + + + IgnoreNew + false + false + false + true + true + + false + false + + true + true + false + false + false + PT0S + 7 + + + + "${exePath()}" + + + + ` + export async function checkAutoRun(): Promise { if (process.platform === 'win32') { const execPromise = promisify(exec) @@ -37,8 +78,10 @@ export async function checkAutoRun(): Promise { export async function enableAutoRun(): Promise { if (process.platform === 'win32') { const execPromise = promisify(exec) + const taskFilePath = path.join(taskDir(), `${appName}.xml`) + await writeFile(taskFilePath, Buffer.from(`\ufeff${taskXml}`, 'utf-16le')) 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') { diff --git a/src/main/sys/misc.ts b/src/main/sys/misc.ts index 0239cab..855b4f2 100644 --- a/src/main/sys/misc.ts +++ b/src/main/sys/misc.ts @@ -12,7 +12,7 @@ import { resourcesFilesDir, taskDir } from '../utils/dirs' -import { copyFileSync } from 'fs' +import { copyFileSync, writeFileSync } from 'fs' export function getFilePath(ext: string[]): string[] | undefined { return dialog.showOpenDialogSync({ @@ -64,12 +64,51 @@ export function setNativeTheme(theme: 'system' | 'light' | 'dark'): void { nativeTheme.themeSource = theme } +const elevateTaskXml = ` + + + + + InteractiveToken + HighestAvailable + + + + Parallel + false + false + false + false + false + + false + false + + true + true + false + false + false + PT72H + 7 + + + + "${path.join(taskDir(), `mihomo-party-run.exe`)}" + "${exePath()}" + + + +` + export function createElevateTask(): void { + const taskFilePath = path.join(taskDir(), `mihomo-party-run.xml`) + writeFileSync(taskFilePath, Buffer.from(`\ufeff${elevateTaskXml}`, 'utf-16le')) copyFileSync( path.join(resourcesFilesDir(), 'mihomo-party-run.exe'), path.join(taskDir(), 'mihomo-party-run.exe') ) 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` ) }