Skip to content

Instantly share code, notes, and snippets.

@fawazahmed0
Last active May 23, 2025 08:05
Show Gist options
  • Save fawazahmed0/313684855862a50bedcf3b9804c0eca5 to your computer and use it in GitHub Desktop.
Save fawazahmed0/313684855862a50bedcf3b9804c0eca5 to your computer and use it in GitHub Desktop.
simple http proxy
/*
client connects to proxy.example (via HTTP or HTTPS - depends on the proxy setup, but either is basically fine)
client sends "CONNECT remote-server.example:443" to the proxy server
The proxy server connects to that address
The proxy server responds to client with 200 OK, and then all future bytes are passed raw between client & the remote server
*/
// https://stackoverflow.com/questions/18340414/what-should-i-do-with-a-connect-event
const net = require('net');
const http = require('http');
const PORT = 8080;
const server = http.createServer();
server.on('connect', (req, clientSocket) => {
const url = new URL(`http://${req.url}`);
const serverSocket = net.connect(url.port || 443, url.hostname, () => {
clientSocket.write('HTTP/1.1 200 Connection Established\r\n\r\n');
clientSocket.pipe(serverSocket);
serverSocket.pipe(clientSocket);
});
});
server.listen(PORT);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment