/* 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);