mirror of
https://gh.catmak.name/https://github.com/mihomo-party-org/mihomo-party
synced 2025-12-28 05:30:29 +08:00
43 lines
1.2 KiB
TypeScript
43 lines
1.2 KiB
TypeScript
import { ChildProcess, spawn } from 'child_process'
|
|
import { getAppConfig } from '../config'
|
|
import { dataDir, resourcesFilesDir } from '../utils/dirs'
|
|
import path from 'path'
|
|
import { existsSync } from 'fs'
|
|
import { readFile, rm, writeFile } from 'fs/promises'
|
|
|
|
let child: ChildProcess
|
|
|
|
export async function startMonitor(detached = false): Promise<void> {
|
|
if (process.platform !== 'win32') return
|
|
if (existsSync(path.join(dataDir(), 'monitor.pid'))) {
|
|
const pid = parseInt(await readFile(path.join(dataDir(), 'monitor.pid'), 'utf-8'))
|
|
try {
|
|
process.kill(pid, 'SIGINT')
|
|
} catch {
|
|
// ignore
|
|
} finally {
|
|
await rm(path.join(dataDir(), 'monitor.pid'))
|
|
}
|
|
}
|
|
await stopMonitor()
|
|
const { showTraffic = true } = await getAppConfig()
|
|
if (!showTraffic) return
|
|
child = spawn(path.join(resourcesFilesDir(), 'TrafficMonitor/TrafficMonitor.exe'), [], {
|
|
cwd: path.join(resourcesFilesDir(), 'TrafficMonitor'),
|
|
detached: detached,
|
|
stdio: detached ? 'ignore' : undefined
|
|
})
|
|
if (detached) {
|
|
if (child && child.pid) {
|
|
await writeFile(path.join(dataDir(), 'monitor.pid'), child.pid.toString())
|
|
}
|
|
child.unref()
|
|
}
|
|
}
|
|
|
|
async function stopMonitor(): Promise<void> {
|
|
if (child) {
|
|
child.kill('SIGINT')
|
|
}
|
|
}
|