chore: import upstream snapshot with attribution

This commit is contained in:
wehub-resource-sync
2026-07-13 13:39:12 +08:00
commit d8dcd5f6d1
8604 changed files with 2479390 additions and 0 deletions
+53
View File
@@ -0,0 +1,53 @@
import { createHash, createHmac } from "node:crypto";
let machineIdSync: (original?: boolean) => string;
try {
// Use require() to bypass webpack static analysis that breaks the default export
const mod = require("node-machine-id");
machineIdSync = mod.machineIdSync || mod.default?.machineIdSync;
} catch {
machineIdSync = () => "";
}
const BUILTIN_DEFAULT_SALT = "omniroute-cli-auth-v1";
function getActiveSalt(): string {
return process.env.OMNIROUTE_CLI_SALT || BUILTIN_DEFAULT_SALT;
}
function deriveToken(rawId: string, salt: string): string {
return createHmac("sha256", rawId).update(salt).digest("hex");
}
let cached: string | null = null;
let cachedSalt: string | null = null;
export function getMachineTokenSync(salt?: string): string {
const activeSalt = salt ?? getActiveSalt();
try {
// machineIdSync(true) returns the original unhashed hardware ID.
const rawId = machineIdSync(true);
if (activeSalt === cachedSalt && cached !== null) return cached;
const token = deriveToken(rawId, activeSalt);
if (!salt) {
cached = token;
cachedSalt = activeSalt;
}
return token;
} catch {
return "";
}
}
export function getLegacyCliTokenSync(salt?: string): string {
const activeSalt = salt ?? getActiveSalt();
try {
const machineId = machineIdSync();
return createHash("sha256")
.update(machineId + activeSalt)
.digest("hex")
.substring(0, 32);
} catch {
return "";
}
}