Skip to content

Commit

Permalink
fix task create
Browse files Browse the repository at this point in the history
  • Loading branch information
pompurin404 committed Oct 21, 2024
1 parent 911d729 commit 30bf756
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
47 changes: 45 additions & 2 deletions src/main/sys/autoRun.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -7,6 +7,47 @@ import path from 'path'

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> {
if (process.platform === 'win32') {
const execPromise = promisify(exec)
Expand Down Expand Up @@ -37,8 +78,10 @@ export async function checkAutoRun(): Promise<boolean> {
export async function enableAutoRun(): Promise<void> {
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') {
Expand Down
43 changes: 41 additions & 2 deletions src/main/sys/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -64,12 +64,51 @@ export function setNativeTheme(theme: 'system' | 'light' | 'dark'): void {
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 {
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`
)
}

0 comments on commit 30bf756

Please sign in to comment.