|
1 | | -import { createServer } from 'http'; |
| 1 | +import { createServer, get } from 'http'; |
| 2 | +import { get as httpsGet } from 'https'; |
2 | 3 |
|
3 | 4 | const server = createServer(function (req, res) { |
| 5 | + const url = new URL(req.url, 'http://localhost:8080'); |
| 6 | + |
| 7 | + // Outbound HTTP proxy: the container makes an HTTP request to the given host. |
| 8 | + // This lets tests verify egress interception from outside. |
| 9 | + const proxyTarget = url.searchParams.get('proxy'); |
| 10 | + if (proxyTarget) { |
| 11 | + get('http://' + proxyTarget, proxyRes => { |
| 12 | + let body = ''; |
| 13 | + proxyRes.on('data', chunk => { |
| 14 | + body += chunk; |
| 15 | + }); |
| 16 | + proxyRes.on('end', () => { |
| 17 | + res.writeHead(proxyRes.statusCode, { 'Content-Type': 'text/plain' }); |
| 18 | + res.end(body); |
| 19 | + }); |
| 20 | + }).on('error', err => { |
| 21 | + res.writeHead(520, { 'Content-Type': 'text/plain' }); |
| 22 | + res.end('proxy error: ' + err.message); |
| 23 | + }); |
| 24 | + return; |
| 25 | + } |
| 26 | + |
| 27 | + // Outbound HTTPS proxy: uses NODE_EXTRA_CA_CERTS (set via env) to trust |
| 28 | + // the Cloudflare containers CA for intercepted HTTPS. |
| 29 | + const proxyHttpsTarget = url.searchParams.get('proxy_https'); |
| 30 | + if (proxyHttpsTarget) { |
| 31 | + httpsGet('https://' + proxyHttpsTarget, proxyRes => { |
| 32 | + let body = ''; |
| 33 | + proxyRes.on('data', chunk => { |
| 34 | + body += chunk; |
| 35 | + }); |
| 36 | + proxyRes.on('end', () => { |
| 37 | + res.writeHead(proxyRes.statusCode, { 'Content-Type': 'text/plain' }); |
| 38 | + res.end(body); |
| 39 | + }); |
| 40 | + }).on('error', err => { |
| 41 | + res.writeHead(520, { 'Content-Type': 'text/plain' }); |
| 42 | + res.end('proxy_https error: ' + err.message); |
| 43 | + }); |
| 44 | + return; |
| 45 | + } |
| 46 | + |
4 | 47 | if (req.url?.startsWith('/containerFetchNoContent')) { |
5 | 48 | res.writeHead(204); |
6 | 49 | res.end(); |
|
0 commit comments