mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-06-17 03:13:30 -07:00
refactor(main): split main.ts into focused runtime modules (#123)
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import fs from 'node:fs';
|
||||
import { execFileSync } from 'node:child_process';
|
||||
import path from 'node:path';
|
||||
|
||||
export type BackgroundStatsServerState = {
|
||||
@@ -65,6 +66,43 @@ export function isBackgroundStatsServerProcessAlive(pid: number): boolean {
|
||||
}
|
||||
}
|
||||
|
||||
function readProcessStartedAtMs(pid: number): number | null {
|
||||
try {
|
||||
if (process.platform === 'win32') {
|
||||
const output = execFileSync(
|
||||
'powershell.exe',
|
||||
[
|
||||
'-NoProfile',
|
||||
'-Command',
|
||||
`(Get-CimInstance Win32_Process -Filter "ProcessId=${pid}").CreationDate.ToUniversalTime().ToString("o")`,
|
||||
],
|
||||
{ encoding: 'utf8', timeout: 1000 },
|
||||
).trim();
|
||||
const parsed = Date.parse(output);
|
||||
return Number.isFinite(parsed) ? parsed : null;
|
||||
}
|
||||
|
||||
const output = execFileSync('ps', ['-o', 'lstart=', '-p', String(pid)], {
|
||||
encoding: 'utf8',
|
||||
timeout: 1000,
|
||||
}).trim();
|
||||
const parsed = Date.parse(output);
|
||||
return Number.isFinite(parsed) ? parsed : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export function verifyBackgroundStatsServerIdentity(pid: number, startedAtMs: number): boolean {
|
||||
const processStartedAtMs = readProcessStartedAtMs(pid);
|
||||
if (processStartedAtMs === null) {
|
||||
return false;
|
||||
}
|
||||
const earliestAllowedStateWriteMs = processStartedAtMs;
|
||||
const latestAllowedStateWriteMs = processStartedAtMs + 60_000;
|
||||
return startedAtMs >= earliestAllowedStateWriteMs && startedAtMs <= latestAllowedStateWriteMs;
|
||||
}
|
||||
|
||||
export function resolveBackgroundStatsServerUrl(
|
||||
state: Pick<BackgroundStatsServerState, 'port'>,
|
||||
): string {
|
||||
|
||||
Reference in New Issue
Block a user