|
| 1 | +/** |
| 2 | + * External-DNS Webhook Proxy |
| 3 | + * |
| 4 | + * Lightweight HTTP proxy that forwards external-dns webhook requests |
| 5 | + * from the Kubernetes cluster to the Firewalla webhook provider. |
| 6 | + * |
| 7 | + * This runs as a sidecar in the external-dns pod and proxies requests |
| 8 | + * to the actual webhook provider running on the Firewalla device. |
| 9 | + */ |
| 10 | + |
| 11 | +const http = require('http'); |
| 12 | +const https = require('https'); |
| 13 | + |
| 14 | +// Configuration from environment variables |
| 15 | +const FIREWALLA_HOST = process.env.FIREWALLA_HOST || '192.168.229.1'; |
| 16 | +const FIREWALLA_PROVIDER_PORT = process.env.FIREWALLA_PROVIDER_PORT || '8888'; |
| 17 | +const FIREWALLA_HEALTH_PORT = process.env.FIREWALLA_HEALTH_PORT || '8080'; |
| 18 | +const WEBHOOK_PORT = process.env.WEBHOOK_PORT || '8888'; |
| 19 | +const METRICS_PORT = process.env.METRICS_PORT || '8080'; |
| 20 | + |
| 21 | +// Build Firewalla URLs |
| 22 | +const FIREWALLA_PROVIDER_URL = `http://${FIREWALLA_HOST}:${FIREWALLA_PROVIDER_PORT}`; |
| 23 | +const FIREWALLA_HEALTH_URL = `http://${FIREWALLA_HOST}:${FIREWALLA_HEALTH_PORT}`; |
| 24 | + |
| 25 | +console.log('Starting External-DNS Webhook Proxy'); |
| 26 | +console.log(`Firewalla Provider: ${FIREWALLA_PROVIDER_URL}`); |
| 27 | +console.log(`Firewalla Health: ${FIREWALLA_HEALTH_URL}`); |
| 28 | +console.log(`Webhook Port: ${WEBHOOK_PORT}`); |
| 29 | +console.log(`Metrics Port: ${METRICS_PORT}`); |
| 30 | + |
| 31 | +/** |
| 32 | + * Proxy HTTP request to Firewalla |
| 33 | + */ |
| 34 | +function proxyRequest(clientReq, clientRes, targetUrl) { |
| 35 | + const startTime = Date.now(); |
| 36 | + const url = new URL(clientReq.url, targetUrl); |
| 37 | + |
| 38 | + const options = { |
| 39 | + hostname: url.hostname, |
| 40 | + port: url.port, |
| 41 | + path: url.pathname + url.search, |
| 42 | + method: clientReq.method, |
| 43 | + headers: { |
| 44 | + ...clientReq.headers, |
| 45 | + 'host': url.host, |
| 46 | + 'x-forwarded-for': clientReq.socket.remoteAddress, |
| 47 | + 'x-forwarded-proto': 'http', |
| 48 | + 'x-forwarded-host': clientReq.headers.host |
| 49 | + } |
| 50 | + }; |
| 51 | + |
| 52 | + console.log(`[${clientReq.method}] ${clientReq.url} -> ${url.href}`); |
| 53 | + |
| 54 | + const proxyReq = http.request(options, (proxyRes) => { |
| 55 | + // Forward status code and headers |
| 56 | + clientRes.writeHead(proxyRes.statusCode, proxyRes.headers); |
| 57 | + |
| 58 | + // Pipe response body |
| 59 | + proxyRes.pipe(clientRes); |
| 60 | + |
| 61 | + proxyRes.on('end', () => { |
| 62 | + const duration = Date.now() - startTime; |
| 63 | + console.log(`[${clientReq.method}] ${clientReq.url} - ${proxyRes.statusCode} (${duration}ms)`); |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + proxyReq.on('error', (err) => { |
| 68 | + console.error(`Proxy error for ${clientReq.url}:`, err.message); |
| 69 | + if (!clientRes.headersSent) { |
| 70 | + clientRes.writeHead(502, { 'Content-Type': 'application/json' }); |
| 71 | + clientRes.end(JSON.stringify({ |
| 72 | + error: 'Bad Gateway', |
| 73 | + message: `Failed to connect to Firewalla: ${err.message}` |
| 74 | + })); |
| 75 | + } |
| 76 | + }); |
| 77 | + |
| 78 | + // Forward request body if present |
| 79 | + if (clientReq.method !== 'GET' && clientReq.method !== 'HEAD') { |
| 80 | + clientReq.pipe(proxyReq); |
| 81 | + } else { |
| 82 | + proxyReq.end(); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +/** |
| 87 | + * Webhook server - proxies external-dns requests to Firewalla |
| 88 | + */ |
| 89 | +const webhookServer = http.createServer((req, res) => { |
| 90 | + proxyRequest(req, res, FIREWALLA_PROVIDER_URL); |
| 91 | +}); |
| 92 | + |
| 93 | +/** |
| 94 | + * Metrics/Health server - provides health and readiness endpoints |
| 95 | + */ |
| 96 | +const metricsServer = http.createServer((req, res) => { |
| 97 | + const url = new URL(req.url, `http://${req.headers.host}`); |
| 98 | + |
| 99 | + // Health check endpoint |
| 100 | + if (url.pathname === '/health' || url.pathname === '/healthz') { |
| 101 | + // Proxy to Firewalla health endpoint |
| 102 | + proxyRequest(req, res, FIREWALLA_HEALTH_URL); |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + // Readiness check endpoint |
| 107 | + if (url.pathname === '/ready' || url.pathname === '/readyz') { |
| 108 | + // Check if we can reach Firewalla |
| 109 | + const healthCheck = http.get(`${FIREWALLA_HEALTH_URL}/healthz`, (healthRes) => { |
| 110 | + if (healthRes.statusCode === 200) { |
| 111 | + res.writeHead(200, { 'Content-Type': 'text/plain' }); |
| 112 | + res.end('ready'); |
| 113 | + } else { |
| 114 | + res.writeHead(503, { 'Content-Type': 'text/plain' }); |
| 115 | + res.end('not ready'); |
| 116 | + } |
| 117 | + }); |
| 118 | + |
| 119 | + healthCheck.on('error', (err) => { |
| 120 | + console.error('Readiness check failed:', err.message); |
| 121 | + res.writeHead(503, { 'Content-Type': 'text/plain' }); |
| 122 | + res.end('not ready'); |
| 123 | + }); |
| 124 | + |
| 125 | + healthCheck.setTimeout(5000, () => { |
| 126 | + healthCheck.destroy(); |
| 127 | + res.writeHead(503, { 'Content-Type': 'text/plain' }); |
| 128 | + res.end('not ready - timeout'); |
| 129 | + }); |
| 130 | + return; |
| 131 | + } |
| 132 | + |
| 133 | + // Metrics endpoint (basic) |
| 134 | + if (url.pathname === '/metrics') { |
| 135 | + res.writeHead(200, { 'Content-Type': 'text/plain' }); |
| 136 | + res.end('# No metrics implemented yet\n'); |
| 137 | + return; |
| 138 | + } |
| 139 | + |
| 140 | + // Unknown endpoint |
| 141 | + res.writeHead(404, { 'Content-Type': 'text/plain' }); |
| 142 | + res.end('Not Found'); |
| 143 | +}); |
| 144 | + |
| 145 | +// Start servers |
| 146 | +webhookServer.listen(WEBHOOK_PORT, '0.0.0.0', () => { |
| 147 | + console.log(`Webhook proxy listening on port ${WEBHOOK_PORT}`); |
| 148 | +}); |
| 149 | + |
| 150 | +metricsServer.listen(METRICS_PORT, '0.0.0.0', () => { |
| 151 | + console.log(`Metrics server listening on port ${METRICS_PORT}`); |
| 152 | +}); |
| 153 | + |
| 154 | +// Graceful shutdown |
| 155 | +const shutdown = (signal) => { |
| 156 | + console.log(`Received ${signal}, shutting down gracefully...`); |
| 157 | + |
| 158 | + webhookServer.close(() => { |
| 159 | + console.log('Webhook server closed'); |
| 160 | + metricsServer.close(() => { |
| 161 | + console.log('Metrics server closed'); |
| 162 | + process.exit(0); |
| 163 | + }); |
| 164 | + }); |
| 165 | + |
| 166 | + // Force exit after 10 seconds |
| 167 | + setTimeout(() => { |
| 168 | + console.error('Forced shutdown after timeout'); |
| 169 | + process.exit(1); |
| 170 | + }, 10000); |
| 171 | +}; |
| 172 | + |
| 173 | +process.on('SIGTERM', () => shutdown('SIGTERM')); |
| 174 | +process.on('SIGINT', () => shutdown('SIGINT')); |
| 175 | + |
| 176 | +// Error handling |
| 177 | +process.on('uncaughtException', (err) => { |
| 178 | + console.error('Uncaught exception:', err); |
| 179 | + process.exit(1); |
| 180 | +}); |
| 181 | + |
| 182 | +process.on('unhandledRejection', (reason, promise) => { |
| 183 | + console.error('Unhandled rejection at:', promise, 'reason:', reason); |
| 184 | + process.exit(1); |
| 185 | +}); |
0 commit comments