mirror of
https://gh.catmak.name/https://github.com/mihomo-party-org/mihomo-party
synced 2025-12-26 20:50:30 +08:00
44 lines
1016 B
TypeScript
44 lines
1016 B
TypeScript
import { ChildProcess, execSync, spawn } from 'child_process'
|
|
import { logPath, mihomoCorePath, mihomoWorkDir } from '../utils/dirs'
|
|
import { generateProfile } from '../resolve/factory'
|
|
import { appConfig } from '../config'
|
|
import fs from 'fs'
|
|
let child: ChildProcess
|
|
|
|
export async function startCore(): Promise<void> {
|
|
const corePath = mihomoCorePath(appConfig.core ?? 'mihomo')
|
|
generateProfile()
|
|
stopCore()
|
|
if (process.platform !== 'win32') {
|
|
execSync(`chmod +x ${corePath}`)
|
|
}
|
|
child = spawn(corePath, ['-d', mihomoWorkDir()])
|
|
child.stdout?.on('data', (data) => {
|
|
fs.writeFileSync(
|
|
logPath(),
|
|
data
|
|
.toString()
|
|
.split('\n')
|
|
.map((line: string) => {
|
|
if (line) return `[Mihomo]: ${line}`
|
|
return ''
|
|
})
|
|
.filter(Boolean)
|
|
.join('\n'),
|
|
{
|
|
flag: 'a'
|
|
}
|
|
)
|
|
})
|
|
}
|
|
|
|
export function stopCore(): void {
|
|
if (child) {
|
|
child.kill('SIGINT')
|
|
}
|
|
}
|
|
|
|
export function restartCore(): void {
|
|
startCore()
|
|
}
|