2025-05-01 10:55:40 -07:00
|
|
|
const express = require('express');
|
|
|
|
|
const { createProxyMiddleware } = require('http-proxy-middleware');
|
|
|
|
|
|
|
|
|
|
const app = express();
|
|
|
|
|
const port = 8080;
|
|
|
|
|
|
2025-05-21 11:16:13 -07:00
|
|
|
// Cache proxy instances per target to avoid recreating each time
|
|
|
|
|
const proxyCache = new Map();
|
|
|
|
|
|
|
|
|
|
// Basic IPv6 validation pattern (can be improved or replaced with a library like 'ipaddr.js')
|
|
|
|
|
const isValidIPv6 = (ip) => /^[0-9a-fA-F:]+$/.test(ip);
|
|
|
|
|
|
2025-05-01 10:55:40 -07:00
|
|
|
app.use('/:ipv6', (req, res, next) => {
|
|
|
|
|
const ipv6 = req.params.ipv6;
|
|
|
|
|
|
2025-05-21 11:16:13 -07:00
|
|
|
if (!isValidIPv6(ipv6)) {
|
|
|
|
|
return res.status(400).send('Invalid IPv6 address');
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-01 10:55:40 -07:00
|
|
|
const targetUrl = `http://[${ipv6}]:8000`;
|
2025-05-21 11:16:13 -07:00
|
|
|
console.log(`Proxying to: ${targetUrl}`);
|
|
|
|
|
|
|
|
|
|
// Reuse proxy middleware for the same target
|
|
|
|
|
if (!proxyCache.has(targetUrl)) {
|
|
|
|
|
proxyCache.set(targetUrl, createProxyMiddleware({
|
|
|
|
|
target: targetUrl,
|
|
|
|
|
changeOrigin: true,
|
|
|
|
|
logLevel: 'debug',
|
|
|
|
|
pathRewrite: (path, req) => path.replace(`/${ipv6}`, '/'),
|
|
|
|
|
onError(err, req, res) {
|
|
|
|
|
console.error(`Proxy error for ${targetUrl}:`, err.message);
|
|
|
|
|
if (!res.headersSent) {
|
|
|
|
|
res.status(502).send('Bad Gateway: Failed to connect to target');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return proxyCache.get(targetUrl)(req, res, next);
|
2025-05-01 10:55:40 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
app.listen(port, '0.0.0.0', () => {
|
|
|
|
|
console.log(`Proxy server listening on http://0.0.0.0:${port}`);
|
|
|
|
|
});
|