security(tunnels,proxy): dual-mode isolated proxy origin, direct SSH dialing, HTTPS upstream, SOCKS5 & TTL tunnels

This commit is contained in:
tyuXX 2026-11-27 22:48:53 +00:00
parent 838e69ba4e
commit ca67c85fbf
7 changed files with 1243 additions and 135 deletions

View file

@ -32,7 +32,7 @@ import { KnownHostsService } from './ssh/known-hosts.js';
import { PTYManager } from './ssh/pty-manager.js';
import { SftpService } from './ssh/sftp-service.js';
import { SSHPool } from './ssh/ssh-pool.js';
import { TunnelManager } from './ssh/tunnel-manager.js';
import { TunnelManager, extractSubdomainTunnel } from './ssh/tunnel-manager.js';
import { CustomSystemsEngine } from './systems/systems-engine.js';
import { createAiRouter } from './api/routes/ai.routes.js';
import { createAlertsRouter } from './api/routes/alerts.routes.js';
@ -97,6 +97,7 @@ export interface GCoordinatorInstance {
aiService: AiService;
wsHub: WsHub;
port: number;
proxyPort?: number;
close(): Promise<void>;
}
@ -179,7 +180,13 @@ export async function createGCoordinatorServer(
const keyManager = new KeyManager(sshPool, vaultService, configManager, auditService);
const ptyManager = new PTYManager(sshPool, configManager, wsHub, getRuntimeLimits);
const sftpService = new SftpService(sshPool);
const tunnelManager = new TunnelManager(sshPool, configManager, vaultService, authService);
const tunnelManager = new TunnelManager(sshPool, configManager, vaultService, authService, {
getRuntimeLimits,
allowLanTunnels: options.allowLanTunnels,
proxyBaseDomain: options.proxyBaseDomain,
proxyPort: options.proxyPort,
trustCloudflare,
});
const customSystemsEngine = new CustomSystemsEngine(
sshPool,
configManager,
@ -243,6 +250,12 @@ export async function createGCoordinatorServer(
getRuntimeLimits,
);
wsHub.registerHostUpgradeHandler((req, socket, head) => {
const tunnelSub = extractSubdomainTunnel(req.headers.host, options.proxyBaseDomain);
if (!tunnelSub) return false;
tunnelManager.handleSubdomainProxyWs(req, socket, head, tunnelSub);
return true;
});
wsHub.registerUpgradeHandler('/proxy/', (req, socket, head) => {
tunnelManager.handleWsProxyUpgrade(req, socket, head);
});
@ -299,6 +312,15 @@ export async function createGCoordinatorServer(
}
});
app.use((req, res, next) => {
const tunnelSub = extractSubdomainTunnel(req.headers.host, options.proxyBaseDomain);
if (tunnelSub) {
tunnelManager.handleSubdomainProxyHttp(req, res, tunnelSub);
return;
}
next();
});
app.use((req, res, next) => {
res.setHeader('X-Content-Type-Options', 'nosniff');
res.setHeader('Referrer-Policy', 'no-referrer');
@ -331,6 +353,7 @@ export async function createGCoordinatorServer(
const jsonParser = express.json({ limit: '2mb' });
const backupJsonParser = express.json({ limit: '16mb' });
app.use((req, res, next) => {
if (req.path.endsWith('/sftp/upload')) {
next();
@ -621,6 +644,8 @@ export async function createGCoordinatorServer(
}
});
await tunnelManager.startProxyServer();
await new Promise<void>((resolve, reject) => {
httpServer.once('error', reject);
httpServer.listen(options.port, options.host ?? '0.0.0.0', () => {
@ -659,6 +684,7 @@ export async function createGCoordinatorServer(
aiService,
wsHub,
port: actualPort,
proxyPort: tunnelManager.actualProxyPort,
async close() {
aiService.close();
backupScheduler.close();
@ -668,6 +694,7 @@ export async function createGCoordinatorServer(
monitoringService.close();
customSystemsEngine.close();
tunnelManager.stopAll();
await tunnelManager.stopProxyServer();
ptyManager.close();
sshPool.closeAll('shutdown');
authService.close();