├── LICENSE ├── README.md └── proxy.js /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Tejaswi Kasat 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # nodejs-proxy-server 2 | A simple proxy server using net and stream modules 3 | 4 | Run it using 5 | ``` 6 | node proxy.js 7 | ``` 8 | 9 | Read full explanation here: 10 | https://thejsway.medium.com/build-your-own-forward-and-reverse-proxy-server-using-node-js-from-scratch-eaa0f8d69e1f 11 | -------------------------------------------------------------------------------- /proxy.js: -------------------------------------------------------------------------------- 1 | // Import of net module 2 | const net = require("net"); 3 | const server = net.createServer(); 4 | 5 | server.on("connection", (clientToProxySocket) => { 6 | console.log("Client connected to proxy"); 7 | clientToProxySocket.once("data", (data) => { 8 | let isTLSConnection = data.toString().indexOf("CONNECT") !== -1; 9 | 10 | let serverPort = 80; 11 | let serverAddress; 12 | console.log(data.toString()); 13 | if (isTLSConnection) { 14 | serverPort = 443; 15 | serverAddress = data 16 | .toString() 17 | .split("CONNECT")[1] 18 | .split(" ")[1] 19 | .split(":")[0]; 20 | } else { 21 | serverAddress = data.toString().split("Host: ")[1].split("\r\n")[0]; 22 | } 23 | console.log(serverAddress); 24 | 25 | // Creating a connection from proxy to destination server 26 | let proxyToServerSocket = net.createConnection( 27 | { 28 | host: serverAddress, 29 | port: serverPort, 30 | }, 31 | () => { 32 | console.log("Proxy to server set up"); 33 | } 34 | ); 35 | 36 | 37 | if (isTLSConnection) { 38 | clientToProxySocket.write("HTTP/1.1 200 OK\r\n\r\n"); 39 | } else { 40 | proxyToServerSocket.write(data); 41 | } 42 | 43 | clientToProxySocket.pipe(proxyToServerSocket); 44 | proxyToServerSocket.pipe(clientToProxySocket); 45 | 46 | proxyToServerSocket.on("error", (err) => { 47 | console.log("Proxy to server error"); 48 | console.log(err); 49 | }); 50 | 51 | clientToProxySocket.on("error", (err) => { 52 | console.log("Client to proxy error"); 53 | console.log(err) 54 | }); 55 | }); 56 | }); 57 | 58 | server.on("error", (err) => { 59 | console.log("Some internal server error occurred"); 60 | console.log(err); 61 | }); 62 | 63 | server.on("close", () => { 64 | console.log("Client disconnected"); 65 | }); 66 | 67 | server.listen( 68 | { 69 | host: "0.0.0.0", 70 | port: 8080, 71 | }, 72 | () => { 73 | console.log("Server listening on 0.0.0.0:8080"); 74 | } 75 | ); 76 | --------------------------------------------------------------------------------