Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | INFRASTRUCTURE & AUTOMATION4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : MacLustr Storage Node8 * File : server/src/config.ts9 * Description : Runtime configuration, data paths and locally generated10 * secrets (master key, ticket key, admin token, LAN11 * certificate).12 * Module : server13 * Repository : https://git.spboucher.ai/maclustr-storage-ios14 * Project Website : https://storage.spboucher.ai15 * Created : 2026-12-0116 * Last Modified : 2026-12-0117 *18 * Author : Simon-Pierre Boucher19 * Position : Professor of Finance20 * Institution : Université du Québec en Outaouais (UQO)21 * Organization : SP Boucher22 * Website : https://spboucher.ai23 * Git : https://git.spboucher.ai24 *25 * Preferred Contact: admin@spboucher.ai26 * Technical Contact: dev@spboucher.ai27 * Security/Admin : admin@spboucher.ai28 *29 * Copyright (c) 202630 * Simon-Pierre Boucher31 * All rights reserved where applicable. This file is governed by the32 * applicable project license, copyright notices and relevant law.33 * Existing third-party rights and notices remain unaffected.34 *35 * License : None specified (no license file in this repository)36 *37 * Do not embed credentials, private keys, access tokens,38 * certificates or confidential personal information.39 *40 * =============================================================================41 * END OF SP BOUCHER SOURCE HEADER42 * =============================================================================43 */4445import { randomBytes, createHash, X509Certificate } from "node:crypto";46import { existsSync, mkdirSync, readFileSync, writeFileSync, chmodSync } from "node:fs";47import { execFileSync } from "node:child_process";48import { homedir } from "node:os";49import { join } from "node:path";5051export const VERSION = "1.0.0";52export const PROTOCOL = "MSP/1";53const MiB = 1024 * 1024;5455function envInt(name: string, def: number): number {56 const v = process.env[name];57 if (!v) return def;58 const n = Number(v);59 if (!Number.isFinite(n)) throw new Error(`${name} is not a number`);60 return n;61}6263const dataDir = process.env.MSC_DATA_DIR || join(homedir(), "maclustr-storage");64const secretDir = process.env.MSC_SECRET_DIR || join(homedir(), ".config", "maclustr-storage");6566export const config = {67 host: process.env.MSC_HOST || "0.0.0.0",68 port: envInt("MSC_PORT", 8870),69 lanPort: envInt("MSC_LAN_PORT", 8871), // 0 disables the LAN listener70 bonjour: process.env.MSC_BONJOUR !== "0",71 publicHost: process.env.MSC_PUBLIC_HOST || "storage.spboucher.ai",72 trustProxy: (process.env.MSC_TRUST_PROXY || "10.67.0.1,127.0.0.1,::1").split(","),73 dataDir,74 secretDir,75 fragmentDir: join(dataDir, "fragments"),76 uploadDir: join(dataDir, "uploads"),77 tmpDir: join(dataDir, "tmp"),78 dbPath: join(dataDir, "coordinator.db"),79 fragmentSize: envInt("MSC_FRAGMENT_MIB", 8) * MiB, // 4..64 MiB80 mobileReplicas: envInt("MSC_MOBILE_REPLICAS", 2),81 heartbeatSeconds: envInt("MSC_HEARTBEAT_S", 30),82 offlineAfterSeconds: envInt("MSC_OFFLINE_AFTER_S", 100),83 verifyIntervalSeconds: envInt("MSC_VERIFY_INTERVAL_S", 6 * 3600),84 leaseSeconds: envInt("MSC_LEASE_S", 900),85 ticketSeconds: envInt("MSC_TICKET_S", 24 * 3600),86 maxQueuedPerDevice: envInt("MSC_MAX_QUEUED", 12),87 maxAttempts: envInt("MSC_MAX_ATTEMPTS", 8),88 pairingTtlSeconds: envInt("MSC_PAIRING_TTL_S", 600),89 clockSkewSeconds: 300,90 tickMs: envInt("MSC_TICK_MS", 3000),91};9293if (config.fragmentSize < 4 * MiB || config.fragmentSize > 64 * MiB) throw new Error("MSC_FRAGMENT_MIB must be within 4..64");9495function secretFile(name: string, bytes: number): Buffer {96 const p = join(secretDir, name);97 if (!existsSync(p)) {98 writeFileSync(p, randomBytes(bytes).toString("hex") + "\n", { mode: 0o600 });99 }100 chmodSync(p, 0o600);101 return Buffer.from(readFileSync(p, "utf8").trim(), "hex");102}103104export type Secrets = { master: Buffer; ticket: Buffer; adminTokenHash: Buffer; lanKeyPath: string; lanCertPath: string; lanPin: string };105106export function loadSecrets(): Secrets {107 for (const d of [config.dataDir, config.fragmentDir, config.uploadDir, config.tmpDir]) mkdirSync(d, { recursive: true });108 mkdirSync(secretDir, { recursive: true, mode: 0o700 });109 chmodSync(secretDir, 0o700);110 const master = secretFile("master.key", 32); // losing this key makes every stored object unreadable111 const ticket = secretFile("ticket.key", 32);112 const admin = secretFile("admin.token", 32);113 const lanKeyPath = join(secretDir, "lan-key.pem");114 const lanCertPath = join(secretDir, "lan-cert.pem");115 if (!existsSync(lanKeyPath) || !existsSync(lanCertPath)) {116 execFileSync("openssl", ["req", "-x509", "-newkey", "ec", "-pkeyopt", "ec_paramgen_curve:prime256v1", "-nodes", "-days", "3650",117 "-subj", "/CN=MacLustr Storage Coordinator LAN", "-keyout", lanKeyPath, "-out", lanCertPath], { stdio: "ignore" });118 chmodSync(lanKeyPath, 0o600);119 }120 const cert = new X509Certificate(readFileSync(lanCertPath));121 const lanPin = createHash("sha256").update(cert.raw).digest("hex");122 return { master, ticket, adminTokenHash: createHash("sha256").update(admin.toString("hex")).digest(), lanKeyPath, lanCertPath, lanPin };123}124