node-tcp-proxy
Advanced tools
Comparing version 0.0.20 to 0.0.21
{ | ||
"parserOptions": { | ||
"ecmaVersion": 5 | ||
"ecmaVersion": 6 | ||
}, | ||
@@ -5,0 +5,0 @@ "plugins": [ |
var proxy = require("node-tcp-proxy"); | ||
var util = require("util"); | ||
var replace = require('buffer-replace'); | ||
var argv = require("commander"); | ||
var serviceHosts = ["apache.org", "apache.org"]; | ||
var servicePorts = [80, 80]; | ||
argv | ||
.usage("[options]") | ||
.option("--tls", "Use TLS 1.2; ", false) | ||
.parse(process.argv); | ||
var newProxy = proxy.createProxy(8080, serviceHosts, servicePorts, { | ||
var PROXY_HOST = "localhost"; | ||
var PROXY_PORT = "8080"; | ||
var SERVICE_HOST = "www.baidu.com"; | ||
var SERVICE_PORT = argv.tls ? 443: 80; | ||
var serviceHosts = [SERVICE_HOST, SERVICE_HOST]; | ||
var servicePorts = [SERVICE_PORT, SERVICE_PORT]; | ||
var options = { | ||
upstream: function(context, data) { | ||
log(context.proxySocket, data); | ||
data = replace(data, `${PROXY_HOST}:${PROXY_PORT}`, SERVICE_HOST); | ||
return data; | ||
@@ -14,2 +27,3 @@ }, | ||
log(context.serviceSocket, data); | ||
data = replace(data, SERVICE_HOST, `${PROXY_HOST}:${PROXY_PORT}`); | ||
return data; | ||
@@ -19,4 +33,4 @@ }, | ||
console.log(util.format("Service host %s:%s selected for client %s:%s.", | ||
serviceHosts[i], | ||
servicePorts[i], | ||
serviceHosts[parseInt(i)], | ||
servicePorts[parseInt(i)], | ||
proxySocket.remoteAddress, | ||
@@ -27,4 +41,11 @@ proxySocket.remotePort)); | ||
} | ||
}); | ||
}; | ||
if (argv.tls) { | ||
options.tls = "both"; | ||
} | ||
var newProxy = proxy | ||
.createProxy(PROXY_PORT, serviceHosts, servicePorts, options); | ||
function log(socket, data) { | ||
@@ -34,3 +55,3 @@ console.log(util.format("%s:%s sent:", | ||
socket.remotePort)); | ||
console.log(data); | ||
console.log(data.toString('hex')); | ||
} | ||
@@ -40,2 +61,8 @@ | ||
if (argv.tls) { | ||
console.log("TLS 1.2 is enabled."); | ||
} else { | ||
console.log("Specify --tls in command line if you want to use TLS 1.2."); | ||
} | ||
console.log("press Enter key to quit..."); | ||
@@ -42,0 +69,0 @@ setTimeout(handleTimeout, 1000); |
@@ -5,3 +5,3 @@ { | ||
"license": "MIT", | ||
"version": "0.0.20", | ||
"version": "0.0.21", | ||
"main": "index.js", | ||
@@ -8,0 +8,0 @@ "repository": { |
@@ -1,4 +0,4 @@ | ||
# node-tcp-proxy [![Build Status](https://semaphoreci.com/api/v1/tewarid/node-tcp-proxy/branches/master/badge.svg)](https://semaphoreci.com/tewarid/node-tcp-proxy) [![Codacy Badge](https://api.codacy.com/project/badge/Grade/3e3d035c4b78445bbec6fb348cf027e1)](https://www.codacy.com/app/tewarid/node-tcp-proxy?utm_source=github.com&utm_medium=referral&utm_content=tewarid/node-tcp-proxy&utm_campaign=Badge_Grade) [![Maintainability](https://api.codeclimate.com/v1/badges/119038e281e93a7d5d05/maintainability)](https://codeclimate.com/github/tewarid/node-tcp-proxy/maintainability) | ||
# node-tcp-proxy [![Build Status](https://semaphoreci.com/api/v1/tewarid/node-tcp-proxy/branches/master/badge.svg)](https://semaphoreci.com/tewarid/node-tcp-proxy) [![Codacy Badge](https://app.codacy.com/project/badge/Grade/a882a604851b494caf65b3913592da4c)](https://www.codacy.com/gh/tewarid/node-tcp-proxy/dashboard?utm_source=github.com&utm_medium=referral&utm_content=tewarid/node-tcp-proxy&utm_campaign=Badge_Grade) [![Maintainability](https://api.codeclimate.com/v1/badges/119038e281e93a7d5d05/maintainability)](https://codeclimate.com/github/tewarid/node-tcp-proxy/maintainability) | ||
A simple TCP proxy that may be used to access a service on another network. An extensible replacement for socat when used thus | ||
A [classical](https://tools.ietf.org/html/rfc1919) TCP proxy that may be used to access a service on another network. An extensible replacement for socat when used thus | ||
@@ -17,10 +17,11 @@ ```bash | ||
Optionally, `hostname` specifies the IP address to listen at. Node.js listens on unspecified IPv6 address `::` by default. If `serviceHost` and `servicePort` specify a comma separated list, the proxy will perform load balancing on a round-robin basis. | ||
Optionally, use `--hostname` to specify host or IP address to listen at. Node.js listens on unspecified IPv6 address `::` by default. If `--serviceHost` and `--servicePort` specify a comma separated list, the proxy will perform load balancing on a round-robin basis. | ||
TLS can be enabled at the proxy port using the `tls` option. If followed by `both`, TLS is also used with the service. Use `pfx` option to specify server certificate, and `passphrase` to provide the password required to access it. | ||
TLS can be enabled at the proxy port using `--tls`. Use `--pfx` followed by path to specify server certificate, and `--passphrase` to provide the password required to access it. Use `--tls both`, to also enable TLS with the service. | ||
## npm | ||
Install node-tcp-proxy from [npm](https://www.npmjs.com/package/node-tcp-proxy), thus | ||
``` | ||
Install node-tcp-proxy using [npm](https://www.npmjs.com/package/node-tcp-proxy) | ||
```bash | ||
sudo npm install -g node-tcp-proxy | ||
@@ -27,0 +28,0 @@ ``` |
@@ -33,9 +33,3 @@ var net = require("net"); | ||
this.serviceHostIndex = -1; | ||
this.options = Object.assign({ | ||
quiet: false, | ||
pfx: require.resolve('./cert.pfx'), | ||
passphrase: 'abcd', | ||
rejectUnauthorized: true, | ||
identUsers: [] | ||
}, options); | ||
this.options = this.parseOptions(options); | ||
this.proxyTlsOptions = { | ||
@@ -46,2 +40,3 @@ passphrase: this.options.passphrase, | ||
if (this.options.tls) { | ||
// eslint-disable-next-line security/detect-non-literal-fs-filename | ||
this.proxyTlsOptions.pfx = fs.readFileSync(this.options.pfx); | ||
@@ -63,2 +58,12 @@ } | ||
TcpProxy.prototype.parseOptions = function(options) { | ||
return Object.assign({ | ||
quiet: false, | ||
pfx: require.resolve('./cert.pfx'), | ||
passphrase: 'abcd', | ||
rejectUnauthorized: true, | ||
identUsers: [] | ||
}, options); | ||
}; | ||
TcpProxy.prototype.createListener = function() { | ||
@@ -68,11 +73,7 @@ var self = this; | ||
self.server = tls.createServer(self.proxyTlsOptions, function(socket) { | ||
self.handleClient(socket); | ||
self.handleClientConnection(socket); | ||
}); | ||
} else { | ||
self.server = net.createServer(function(socket) { | ||
if (self.users) { | ||
self.handleAuth(socket); | ||
} else { | ||
self.handleClient(socket); | ||
} | ||
self.handleClientConnection(socket); | ||
}); | ||
@@ -83,2 +84,10 @@ } | ||
TcpProxy.prototype.handleClientConnection = function(socket) { | ||
var self = this; | ||
if (self.users) { | ||
self.handleAuth(socket); | ||
} else { | ||
self.handleClient(socket); | ||
} | ||
}; | ||
@@ -122,3 +131,3 @@ // RFC 1413 authentication | ||
var key = uniqueKey(proxySocket); | ||
self.proxySockets[key] = proxySocket; | ||
self.proxySockets[toString(key)] = proxySocket; | ||
var context = { | ||
@@ -130,12 +139,3 @@ buffers: [], | ||
proxySocket.on("data", function(data) { | ||
if (context.connected) { | ||
context.serviceSocket.write( | ||
self.intercept(self.options.upstream, context, data)); | ||
} else { | ||
context.buffers[context.buffers.length] = | ||
self.intercept(self.options.upstream, context, data); | ||
if (context.serviceSocket === undefined) { | ||
self.createServiceSocket(context); | ||
} | ||
} | ||
self.handleUpstreamData(context, data); | ||
}); | ||
@@ -153,11 +153,19 @@ proxySocket.on("close", function(hadError) { | ||
TcpProxy.prototype.handleUpstreamData = function(context, data) { | ||
var self = this; | ||
if (context.connected) { | ||
context.serviceSocket.write( | ||
self.intercept(self.options.upstream, context, data)); | ||
} else { | ||
context.buffers[context.buffers.length] = | ||
self.intercept(self.options.upstream, context, data); | ||
if (context.serviceSocket === undefined) { | ||
self.createServiceSocket(context); | ||
} | ||
} | ||
}; | ||
TcpProxy.prototype.createServiceSocket = function(context) { | ||
var self = this; | ||
var i = self.getServiceHostIndex(context.proxySocket); | ||
var options = Object.assign({ | ||
port: self.servicePorts[i], | ||
host: self.serviceHosts[i], | ||
localAddress: self.options.localAddress, | ||
localPort: self.options.localPort | ||
}, self.serviceTlsOptions); | ||
var options = self.parseServiceOptions(context); | ||
if (self.options.tls === "both") { | ||
@@ -185,2 +193,13 @@ context.serviceSocket = tls.connect(options, function() { | ||
TcpProxy.prototype.parseServiceOptions = function(context) { | ||
var self = this; | ||
var i = self.getServiceHostIndex(context.proxySocket); | ||
return Object.assign({ | ||
port: self.servicePorts[parseInt(i, 10)], | ||
host: self.serviceHosts[parseInt(i, 10)], | ||
localAddress: self.options.localAddress, | ||
localPort: self.options.localPort | ||
}, self.serviceTlsOptions); | ||
}; | ||
TcpProxy.prototype.getServiceHostIndex = function(proxySocket) { | ||
@@ -202,3 +221,3 @@ this.serviceHostIndex++; | ||
for (var i = 0; i < context.buffers.length; i++) { | ||
context.serviceSocket.write(context.buffers[i]); | ||
context.serviceSocket.write(context.buffers[parseInt(i, 10)]); | ||
} | ||
@@ -211,3 +230,3 @@ } | ||
for (var key in this.proxySockets) { | ||
this.proxySockets[key].destroy(); | ||
this.proxySockets[toString(key)].destroy(); | ||
} | ||
@@ -214,0 +233,0 @@ this.server.unref(); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
21285
352
98