Skip to content

Commit 356f6cc

Browse files
committed
containers: Fix up the allowHosts not being defined in constructor and add a test
1 parent f10a026 commit 356f6cc

10 files changed

Lines changed: 1111 additions & 899 deletions

File tree

examples/core-tests/container_src/server.js

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,49 @@
1-
import { createServer } from 'http';
1+
import { createServer, get } from 'http';
2+
import { get as httpsGet } from 'https';
23

34
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+
447
if (req.url?.startsWith('/containerFetchNoContent')) {
548
res.writeHead(204);
649
res.end();

0 commit comments

Comments
 (0)