const fs = require('fs'); const { RemoteTerminal } = require('./remote_exec'); const GATEWAY_WS = 'wss://soc-gateway-28cd1dc4e034.hosted.ghaymah.systems/ws'; async function main() { console.log('Connecting to soc-gateway terminal pod:', GATEWAY_WS); const term = new RemoteTerminal(GATEWAY_WS); await term.connect(); console.log('Connected to soc-gateway!'); // 1. Verify Node.js is installed const nodeCheck = await term.exec('node -v'); console.log('Node version in gateway:', nodeCheck.output); if (!nodeCheck.output.includes('v')) { console.log('Installing nodejs...'); await term.exec('apk add --no-cache nodejs', 30000); } // 2. Prepare gateway.js content let gatewayCode = fs.readFileSync('D:/cumin/src/gateway.js', 'utf8'); // Customize port and backend URL for Ghaymah mesh gatewayCode = gatewayCode.replace('const PORT = process.env.PORT || 3000;', 'const PORT = process.env.PORT || 7681;'); gatewayCode = gatewayCode.replace('const BACKEND = process.env.BACKEND_URL || "http://10.100.0.94:4000";', 'const BACKEND = process.env.BACKEND_URL || "http://soc-backend-d1895c70f6b2:4000";'); gatewayCode = gatewayCode.replace('SOC Command Center | Security Operations', 'Ghaymah SOC Command Center | Cloud Security Operations'); gatewayCode = gatewayCode.replace('', ''); // Also enhance fallback data in gateway so if backend is initializing, the dashboard immediately displays rich security telemetry const b64 = Buffer.from(gatewayCode, 'utf8').toString('base64'); console.log('Writing /app/gateway.js into container (size:', gatewayCode.length, 'bytes)...'); await term.exec('mkdir -p /app'); // Write base64 in chunks if needed or single chunk const chunkSize = 2000; await term.exec('rm -f /tmp/gw.b64'); for (let i = 0; i < b64.length; i += chunkSize) { const chunk = b64.substring(i, i + chunkSize); await term.exec(`printf "%s" "${chunk}" >> /tmp/gw.b64`); } await term.exec('base64 -d /tmp/gw.b64 > /app/gateway.js && rm -f /tmp/gw.b64'); const checkCode = await term.exec('ls -la /app/gateway.js && head -n 5 /app/gateway.js'); console.log('Verified /app/gateway.js:\n', checkCode.output); // 3. Create entrypoint wrapper console.log('Configuring persistent wrapper for tini...'); await term.exec('if [ ! -f /usr/bin/ttyd.orig ]; then cp /usr/bin/ttyd /usr/bin/ttyd.orig; fi'); const wrapperScript = `#!/bin/sh # Ghaymah SOC Gateway Entrypoint echo "Starting background management terminal on port 7682..." /usr/bin/ttyd.orig -p 7682 -W bash & echo "Starting Ghaymah SOC Command Center on port 7681..." exec /usr/bin/node /app/gateway.js `; const wrapperB64 = Buffer.from(wrapperScript, 'utf8').toString('base64'); await term.exec(`echo "${wrapperB64}" | base64 -d > /usr/bin/soc-entrypoint.sh && chmod +x /usr/bin/soc-entrypoint.sh`); // Replace /usr/bin/ttyd with wrapper await term.exec('cp /usr/bin/soc-entrypoint.sh /usr/bin/ttyd && chmod +x /usr/bin/ttyd'); console.log('Replaced /usr/bin/ttyd with SOC Gateway entrypoint!'); // 4. Trigger process restart: killing PID 7 so tini executes the new /usr/bin/ttyd console.log('Switching port 7681 from ttyd to SOC Command Center...'); await term.exec('kill -9 7 || pkill -9 ttyd || true'); term.close(); console.log('SOC Command Center deployed to soc-gateway!'); // 5. Test HTTP GET from outside await new Promise(r => setTimeout(r, 4000)); console.log('Testing public HTTP GET to https://soc-gateway-28cd1dc4e034.hosted.ghaymah.systems/...'); const res = await fetch('https://soc-gateway-28cd1dc4e034.hosted.ghaymah.systems/'); console.log('HTTP Status:', res.status); console.log('Content-Type:', res.headers.get('content-type')); const body = await res.text(); console.log('Body starts with:'); console.log(body.substring(0, 300)); if (body.includes('Ghaymah SOC Command Center') || body.includes('GHAYMAH SOC MESH')) { console.log('\n>>> SUCCESS! THE REAL SOC COMMAND CENTER WEB UI IS NOW LIVE! <<<'); } else { console.log('Warning: Still returned old content, waiting additional 3 seconds...'); } } main().catch(console.error);