Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | INFRASTRUCTURE & AUTOMATION4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : MacLustr Vision8 * File : server/src/relay.ts9 * Module : server10 * Repository : https://git.spboucher.ai/maclustr-vision11 * Project Website : https://camera.spboucher.ai12 * Created : 2026-11-3013 * Last Modified : 2026-11-3014 *15 * Author : Simon-Pierre Boucher16 * Position : Professor of Finance17 * Institution : Université du Québec en Outaouais (UQO)18 * Organization : SP Boucher19 * Website : https://spboucher.ai20 * Git : https://git.spboucher.ai21 *22 * Preferred Contact: admin@spboucher.ai23 * Technical Contact: dev@spboucher.ai24 * Security/Admin : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142// Relais local vers les caméras.43// macOS (Local Network Privacy) refuse le réseau local aux binaires tiers (node, go2rtc, FFmpeg) lancés par44// PM2/launchd sous un compte normal, mais l'autorise aux outils Apple. Chaque connexion locale45// (127.0.0.1:port) est donc relayée par /usr/bin/nc vers la caméra : aucun privilège root nécessaire.46// Les destinations viennent uniquement du registre des caméras, jamais d'une entrée extérieure.47import { spawn } from 'node:child_process';48import net from 'node:net';49import { logger } from './log.ts';5051const log = logger('relay');52const NC = '/usr/bin/nc';5354export type RelayTarget = () => { host: string; port: number } | null;5556export function startRelay(localPort: number, target: RelayTarget): net.Server {57 const server = net.createServer((sock) => {58 const t = target();59 if (!t || !/^[A-Za-z0-9._-]+$/.test(t.host)) {60 sock.destroy();61 return;62 }63 const nc = spawn(NC, ['-G', '5', t.host, String(t.port)], { stdio: ['pipe', 'pipe', 'ignore'] });64 sock.setNoDelay(true);65 const close = () => {66 sock.destroy();67 if (nc.exitCode === null) nc.kill('SIGKILL');68 };69 sock.pipe(nc.stdin);70 nc.stdout.pipe(sock);71 sock.on('error', close);72 sock.on('close', close);73 nc.stdin.on('error', close);74 nc.stdout.on('error', close);75 nc.on('error', close);76 nc.on('exit', () => sock.destroy());77 });78 server.on('error', (e) => log.error('relais', { port: localPort, err: String(e) }));79 server.listen(localPort, '127.0.0.1');80 return server;81}8283/** Test de joignabilité TCP par l'outil Apple (même contrainte que ci-dessus). */84export function lanReachable(host: string, port: number, timeoutS = 3): Promise<boolean> {85 return new Promise((resolve) => {86 if (!/^[A-Za-z0-9._-]+$/.test(host)) return resolve(false);87 const nc = spawn(NC, ['-z', '-G', String(timeoutS), host, String(port)], { stdio: 'ignore' });88 const timer = setTimeout(() => nc.kill('SIGKILL'), (timeoutS + 2) * 1000);89 nc.on('error', () => resolve(false));90 nc.on('exit', (code) => {91 clearTimeout(timer);92 resolve(code === 0);93 });94 });95}96