- 
      
- 
        Save couto/5335990 to your computer and use it in GitHub Desktop. 
    make it a little friendlier for the terminal
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | var net = require("net"), | |
| args = {}; | |
| process.argv.forEach(function (val, index) { | |
| var dest, listen; | |
| if (val === '--destination') { | |
| dest = process.argv[index + 1].split(':'); | |
| args.destination = dest[0]; | |
| args.destPort = dest[1] || 80; | |
| } | |
| if (val === '--listen') { | |
| listen = process.argv[index + 1]; | |
| args.listen = listen; | |
| } | |
| }) | |
| var Proxy = function () {}; | |
| Proxy.to = function (dst_host, dst_port) { | |
| var proxy = new Proxy(); | |
| proxy.dstHost = dst_host; | |
| proxy.dstPort = dst_port; | |
| proxy.mid = []; | |
| return proxy; | |
| }; | |
| Proxy.prototype.use = function (fn) { | |
| this.mid.push(fn); | |
| return this; | |
| } | |
| Proxy.prototype.handle = function (data, src, dst) { | |
| var next = Proxy.noop, | |
| i = this.mid.length - 1; | |
| for (i; i >= 0; i--) { | |
| next = this.mid[i].bind(this, data, src, dst, next); | |
| } | |
| next(); | |
| return this; | |
| }; | |
| Proxy.prototype.listen = function(port) { | |
| var self = this; | |
| this.server = net.createServer(function (sockA) { | |
| var preb = [], | |
| sockB; | |
| sockA.on("data", pre = function(data) { | |
| preb.push(data); | |
| }); | |
| sockB = net.connect(self.dstPort, self.dstHost, function () { | |
| sockA.removeListener("data", pre); | |
| while (preb.length) { | |
| self.handle(preb.shift(), sockA, sockB); | |
| } | |
| sockA.on("data", function (data) { | |
| self.handle(data, sockA, sockB); | |
| }); | |
| sockB.on("data", function (data) { | |
| self.handle(data, sockB, sockA); | |
| }); | |
| sockA.on("end", function () { | |
| sockB.removeAllListeners(); | |
| sockB.end(); | |
| }); | |
| sockB.on("end", function () { | |
| sockA.removeAllListeners(); | |
| sockA.end(); | |
| }); | |
| }); | |
| }); | |
| this.server.listen(port, function () { | |
| console.log('\033[0;32m ✔ \033[0m' + | |
| 'redirecting from:\033[0;36m localhost:' + | |
| port + | |
| '\033[0m to: \033[0;36m' + | |
| self.dstHost + | |
| ':' + | |
| self.dstPort + | |
| '\033[0m' | |
| ); | |
| }); | |
| return this; | |
| }; | |
| // built-in middlewares | |
| Proxy.noop = function () {}; | |
| Proxy.send = function (data, src, dst, next) { | |
| dst.write(data); | |
| next(); | |
| }; | |
| Proxy.log = function (data, src, dst, next) { | |
| console.log(" → " + src.remoteAddress + ":" + src.remotePort + " ▸ " + dst.remoteAddress + ":" + dst.remotePort); | |
| console.log(" → " data.toString()); | |
| next(); | |
| }; | |
| // redirect all connections on port 8080 to google.com:80 | |
| // node proxy.js --destination google.com:80 --listen 8080 | |
| Proxy | |
| .to(args.destination, args.destPort) | |
| .use(Proxy.log) | |
| .use(Proxy.send) | |
| .listen(args.listen || 3000); | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
Simple TCP Proxy.
I used this to make two Virtual Machines, under the same host, talk to each other.