feat(api): mount terminal, SFTP, tunnels, /proxy HTTP+WS, and systems routes
This commit is contained in:
parent
ec3f9c88b5
commit
98d36c8288
5 changed files with 1077 additions and 15 deletions
|
|
@ -19,11 +19,19 @@ import { EmergencyRecoveryService } from './security/recovery-store.js';
|
|||
import { VaultService } from './security/vault.js';
|
||||
import { KeyManager } from './ssh/key-manager.js';
|
||||
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 { CustomSystemsEngine } from './systems/systems-engine.js';
|
||||
import { createAuditRouter } from './api/routes/audit.routes.js';
|
||||
import { createAuthRouter } from './api/routes/auth.routes.js';
|
||||
import { createConfigRouter } from './api/routes/config.routes.js';
|
||||
import { createServersRouter } from './api/routes/servers.routes.js';
|
||||
import { createSftpRouter } from './api/routes/sftp.routes.js';
|
||||
import { createSystemsRouter } from './api/routes/systems.routes.js';
|
||||
import { createTerminalRouter } from './api/routes/terminal.routes.js';
|
||||
import { createProxyRouter, createTunnelsRouter } from './api/routes/tunnels.routes.js';
|
||||
import { createVaultRouter } from './api/routes/vault.routes.js';
|
||||
import { WsHub } from './api/ws-hub.js';
|
||||
|
||||
|
|
@ -50,6 +58,10 @@ export interface GCoordinatorInstance {
|
|||
knownHosts: KnownHostsService;
|
||||
sshPool: SSHPool;
|
||||
keyManager: KeyManager;
|
||||
ptyManager: PTYManager;
|
||||
sftpService: SftpService;
|
||||
tunnelManager: TunnelManager;
|
||||
customSystemsEngine: CustomSystemsEngine;
|
||||
serverInspector: ServerInspector;
|
||||
monitoringService: MonitoringService;
|
||||
wsHub: WsHub;
|
||||
|
|
@ -103,26 +115,22 @@ export async function createGCoordinatorServer(
|
|||
app.set('trust proxy', 1);
|
||||
}
|
||||
|
||||
app.use((_req, res, next) => {
|
||||
res.setHeader('X-Content-Type-Options', 'nosniff');
|
||||
res.setHeader('X-Frame-Options', 'DENY');
|
||||
res.setHeader('Referrer-Policy', 'no-referrer');
|
||||
res.setHeader(
|
||||
'Content-Security-Policy',
|
||||
"default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; connect-src 'self' ws: wss:; frame-ancestors 'none'; base-uri 'self'",
|
||||
);
|
||||
next();
|
||||
});
|
||||
|
||||
app.use(attachClientIp(trustCloudflare));
|
||||
app.use(express.json({ limit: '2mb' }));
|
||||
|
||||
const httpServer = http.createServer(app);
|
||||
const wsHub = new WsHub(httpServer, authService, vaultService);
|
||||
|
||||
const knownHosts = new KnownHostsService(db, auditService);
|
||||
const sshPool = new SSHPool(configManager, vaultService, knownHosts);
|
||||
const keyManager = new KeyManager(sshPool, vaultService, configManager, auditService);
|
||||
const ptyManager = new PTYManager(sshPool, configManager, wsHub);
|
||||
const sftpService = new SftpService(sshPool);
|
||||
const tunnelManager = new TunnelManager(sshPool, configManager, vaultService, authService);
|
||||
const customSystemsEngine = new CustomSystemsEngine(
|
||||
sshPool,
|
||||
configManager,
|
||||
vaultService,
|
||||
ptyManager,
|
||||
wsHub,
|
||||
);
|
||||
const serverInspector = new ServerInspector(sshPool, configManager);
|
||||
const probeRunner = new ProbeRunner(sshPool);
|
||||
const monitoringService = new MonitoringService(
|
||||
|
|
@ -133,6 +141,71 @@ export async function createGCoordinatorServer(
|
|||
probeRunner,
|
||||
);
|
||||
|
||||
wsHub.registerUpgradeHandler('/proxy/', (req, socket, head) => {
|
||||
tunnelManager.handleWsProxyUpgrade(req, socket, head);
|
||||
});
|
||||
wsHub.attachPtyHandlers({
|
||||
onInput: (sessionId, data) => {
|
||||
ptyManager.writeInput(sessionId, data);
|
||||
},
|
||||
onResize: (sessionId, cols, rows) => {
|
||||
ptyManager.resize(sessionId, cols, rows);
|
||||
},
|
||||
});
|
||||
wsHub.onChannelSubscribe((ctx, channel) => {
|
||||
if (channel.startsWith('pty:')) {
|
||||
const sessionId = channel.slice('pty:'.length);
|
||||
const scrollback = ptyManager.getScrollback(sessionId);
|
||||
if (scrollback !== null && scrollback.length > 0) {
|
||||
ctx.send({
|
||||
type: 'pty:output',
|
||||
sessionId,
|
||||
data: scrollback,
|
||||
replay: true,
|
||||
});
|
||||
}
|
||||
const session = ptyManager.getSession(sessionId);
|
||||
if (session?.exited) {
|
||||
ctx.send({
|
||||
type: 'pty:exit',
|
||||
sessionId,
|
||||
exitCode: session.exitCode,
|
||||
});
|
||||
}
|
||||
} else if (channel.startsWith('stream:')) {
|
||||
const jobId = channel.slice('stream:'.length);
|
||||
for (const frame of customSystemsEngine.getStreamReplayFrames(jobId)) {
|
||||
ctx.send(frame);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
app.use((req, res, next) => {
|
||||
res.setHeader('X-Content-Type-Options', 'nosniff');
|
||||
res.setHeader('Referrer-Policy', 'no-referrer');
|
||||
if (!req.path.startsWith('/proxy/')) {
|
||||
res.setHeader('X-Frame-Options', 'DENY');
|
||||
res.setHeader(
|
||||
'Content-Security-Policy',
|
||||
"default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; connect-src 'self' ws: wss:; frame-ancestors 'none'; base-uri 'self'",
|
||||
);
|
||||
}
|
||||
next();
|
||||
});
|
||||
|
||||
app.use(attachClientIp(trustCloudflare));
|
||||
// Mount /proxy before express.json so proxied POST/PUT payloads stream untouched upstream
|
||||
app.use('/proxy', createProxyRouter({ authService, tunnelManager }));
|
||||
|
||||
const jsonParser = express.json({ limit: '2mb' });
|
||||
app.use((req, res, next) => {
|
||||
if (req.path.endsWith('/sftp/upload')) {
|
||||
next();
|
||||
return;
|
||||
}
|
||||
jsonParser(req, res, next);
|
||||
});
|
||||
|
||||
sshPool.onStateChange((states) => {
|
||||
wsHub.broadcastChannel('metrics', { type: 'servers:state', states });
|
||||
for (const s of states) {
|
||||
|
|
@ -157,9 +230,15 @@ export async function createGCoordinatorServer(
|
|||
});
|
||||
|
||||
monitoringService.start();
|
||||
void tunnelManager.syncAutoStart();
|
||||
customSystemsEngine.syncBackgroundChecks();
|
||||
|
||||
vaultService.onPanicLock(() => {
|
||||
monitoringService.stopAll();
|
||||
customSystemsEngine.stopBackgroundChecks();
|
||||
customSystemsEngine.cancelAllStreams();
|
||||
tunnelManager.stopAll();
|
||||
ptyManager.closeAll(true);
|
||||
sshPool.closeAll('panic_lock');
|
||||
authService.revokeAllSessions();
|
||||
wsHub.handlePanicLock();
|
||||
|
|
@ -173,14 +252,28 @@ export async function createGCoordinatorServer(
|
|||
warnings: event.warnings,
|
||||
errors: event.errors,
|
||||
});
|
||||
if (event.entityType === 'server' || event.entityType === 'settings' || event.entityType === 'all') {
|
||||
if (
|
||||
event.entityType === 'server' ||
|
||||
event.entityType === 'system' ||
|
||||
event.entityType === 'settings' ||
|
||||
event.entityType === 'all'
|
||||
) {
|
||||
if (event.entityType === 'server' && event.id) {
|
||||
sshPool.closeServer(event.id);
|
||||
ptyManager.closeServerSessions(event.id);
|
||||
tunnelManager.stopServerTunnels(event.id);
|
||||
customSystemsEngine.invalidateCache(event.id);
|
||||
} else if (event.entityType === 'system' && event.id) {
|
||||
customSystemsEngine.invalidateCache(undefined, event.id);
|
||||
} else {
|
||||
customSystemsEngine.invalidateCache();
|
||||
}
|
||||
if (event.entityType === 'settings') {
|
||||
authService.syncRecovery();
|
||||
}
|
||||
monitoringService.syncServers();
|
||||
customSystemsEngine.syncBackgroundChecks();
|
||||
void tunnelManager.syncAutoStart();
|
||||
}
|
||||
});
|
||||
|
||||
|
|
@ -216,6 +309,8 @@ export async function createGCoordinatorServer(
|
|||
stepUpGuard,
|
||||
onVaultUnlocked: () => {
|
||||
monitoringService.syncServers();
|
||||
customSystemsEngine.syncBackgroundChecks();
|
||||
void tunnelManager.syncAutoStart();
|
||||
},
|
||||
onVaultChanged: (snapshotReason) => {
|
||||
authService.syncRecovery({ snapshotReason });
|
||||
|
|
@ -224,6 +319,19 @@ export async function createGCoordinatorServer(
|
|||
|
||||
app.use('/api/vault', vaultRouter);
|
||||
app.use('/api/security', panicLockHandler);
|
||||
app.use('/api/terminal', createTerminalRouter({ authService, configManager, auditService, ptyManager }));
|
||||
app.use(
|
||||
'/api/tunnels',
|
||||
createTunnelsRouter({ authService, configManager, auditService, tunnelManager }),
|
||||
);
|
||||
app.use(
|
||||
'/api/systems',
|
||||
createSystemsRouter({ authService, auditService, systemsEngine: customSystemsEngine }),
|
||||
);
|
||||
app.use(
|
||||
'/api/servers/:id/sftp',
|
||||
createSftpRouter({ authService, configManager, auditService, sftpService }),
|
||||
);
|
||||
app.use(
|
||||
'/api/servers',
|
||||
createServersRouter({
|
||||
|
|
@ -284,12 +392,19 @@ export async function createGCoordinatorServer(
|
|||
knownHosts,
|
||||
sshPool,
|
||||
keyManager,
|
||||
ptyManager,
|
||||
sftpService,
|
||||
tunnelManager,
|
||||
customSystemsEngine,
|
||||
serverInspector,
|
||||
monitoringService,
|
||||
wsHub,
|
||||
port: actualPort,
|
||||
async close() {
|
||||
monitoringService.close();
|
||||
customSystemsEngine.close();
|
||||
tunnelManager.stopAll();
|
||||
ptyManager.close();
|
||||
sshPool.closeAll('shutdown');
|
||||
wsHub.close();
|
||||
await configManager.close();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue