Skip to content

Instantly share code, notes, and snippets.

@fernvenue
Created June 25, 2024 01:35
Show Gist options
  • Save fernvenue/f3f74f6b4d8ec6fff5772497ab5f5ad6 to your computer and use it in GitHub Desktop.
Save fernvenue/f3f74f6b4d8ec6fff5772497ab5f5ad6 to your computer and use it in GitHub Desktop.
Simple web server to reverse anything in whitelist.
const http = require('http');
const https = require('https');
const url = require('url');
const whitelist = ['www.example.com'];
const server = http.createServer((req, res) => {
const parsedUrl = url.parse(req.url);
if (!parsedUrl.pathname.startsWith('/archive/http')) {
console.error(`Received invalid request: ${req.method} ${req.url}`);
res.writeHead(404);
res.end();
return;
}
const targetUrl = parsedUrl.pathname.substring(9);
const hostname = url.parse(targetUrl).hostname;
if (!whitelist.includes(hostname)) {
console.warn(`Blocked request to non-whitelisted host: ${hostname}`);
res.writeHead(403);
res.end();
return;
}
const options = url.parse(targetUrl);
options.headers = {
'User-Agent': req.headers['user-agent']
};
const httpOrHttps = targetUrl.startsWith('https') ? https : http;
const proxyReq = httpOrHttps.request(options, (proxyRes) => {
console.log(`Proxied request to ${targetUrl} with status ${proxyRes.statusCode}`);
res.writeHead(proxyRes.statusCode, proxyRes.headers);
proxyRes.pipe(res);
});
proxyReq.on('error', (err) => {
console.error(`Proxy request to ${targetUrl} failed: ${err}`);
res.writeHead(404);
res.end();
});
req.pipe(proxyReq);
});
server.listen(9090, '127.0.0.1', () => {
console.log('Server running at http://127.0.0.1:9090/archive/');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment