jcrugzz / tcp-proxy

A simple tcp proxy with pluggable duplex streams for custom protocol proxying
https://github.com/jcrugzz/tcp-proxy
44 stars 16 forks source link

the example doesn't work #3

Open hareeqi opened 7 years ago

hareeqi commented 7 years ago

I used the package published in NPM and it throws

TypeError: Cannot read property 'target' of undefined

options.target = options.target || this.target;

my code

var net = require('net');
var TcpProxy = require('tcp-proxy');

var targets = [
  {
    host: 'localhost',
    port: 5050
  },
  {
    host: 'localhost',
    port: 5050
  }
];

var proxies = targets.map(function (target) {
  return new TcpProxy({
    target: target
  });
});

var nextProxy = function () {
  var proxy = proxies.shift();
  proxies.push(proxy);
  return proxy;
};

var server = net.createServer(function(socket) {
  nextProxy().proxy(socket);
});

server.listen(8000);
hareeqi commented 7 years ago

this should fix the example

var net = require('net');
var TcpProxy = require('tcp-proxy');

var targets = [
  {
    host: 'localhost',
    port: 5050
  },
  {
    host: 'localhost',
    port: 5050
  }
];

var nextTarget = function () {
  var target = targets.shift();
  targets.push(target);
  return {target:target};
};

var server = net.createServer(function(socket) {
  new TcpProxy(socket,nextTarget())
});

server.listen(8000);