node-tcp-proxy
Advanced tools
Comparing version 0.0.8 to 0.0.9
@@ -5,3 +5,3 @@ { | ||
"license": "MIT", | ||
"version": "0.0.8", | ||
"version": "0.0.9", | ||
"main": "index.js", | ||
@@ -16,3 +16,3 @@ "repository": { | ||
"dependencies": { | ||
"optimist": "^0.6.1" | ||
"commander": "^2.15.1" | ||
}, | ||
@@ -19,0 +19,0 @@ "devDependencies": { |
@@ -1,2 +0,2 @@ | ||
# 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://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) | ||
@@ -14,3 +14,3 @@ A simple TCP proxy that may be used to access a service on another network. An extensible replacement for socat when used thus | ||
```bash | ||
tcpproxy --proxyPort port1 [--hostname [IP]] --serviceHost host --servicePort port2 [--q] | ||
tcpproxy --proxyPort port1 [--hostname [IP]] --serviceHost host --servicePort port2 [--q] [--tls [both]] [--pfx file] [--passphrase secret] | ||
``` | ||
@@ -20,2 +20,4 @@ | ||
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. | ||
## npm | ||
@@ -22,0 +24,0 @@ |
#!/usr/bin/env node | ||
var argv = require("optimist") | ||
.usage("Usage: $0 --proxyPort [port] [--hostname [IP]]" | ||
+ " --serviceHost [host] --servicePort [port] [--q]") | ||
.demand(["proxyPort", "serviceHost", "servicePort"]) | ||
.boolean("q") | ||
.argv; | ||
var argv = require("commander"); | ||
argv | ||
.usage("[options]") | ||
.version("0.0.9") | ||
.option("-p, --proxyPort <number>", "Proxy port number", parseInt) | ||
.option("-h, --hostname [name]", "Name or IP address of host") | ||
.option("-n, --serviceHost <name>", "Name or IP address of service host") | ||
.option("-s, --servicePort <number>", "Service port number", parseInt) | ||
.option("-q, --q", "Be quiet") | ||
.option("-t, --tls [both]", "Use TLS", false) | ||
.option("-c, --pfx [file]", "Private key file", "cert.pfx") | ||
.option("-p, --passphrase [value]", | ||
"Passphrase to access private key file", "abcd") | ||
.parse(process.argv); | ||
var options = { | ||
hostname: argv.hostname, | ||
quiet: argv.q | ||
quiet: argv.q, | ||
tls: argv.tls, | ||
pfx: argv.pfx, | ||
passphrase: argv.passphrase | ||
}; | ||
@@ -14,0 +26,0 @@ |
var net = require("net"); | ||
var tls = require('tls'); | ||
var fs = require('fs'); | ||
@@ -26,38 +28,57 @@ function uniqueKey(socket) { | ||
const proxy = this; | ||
proxy.server = net.createServer(function(proxySocket) { | ||
var key = uniqueKey(proxySocket); | ||
proxy.proxySockets[key] = proxySocket; | ||
var context = { | ||
buffers: [], | ||
connected: false, | ||
proxySocket: proxySocket | ||
}; | ||
proxy.createServiceSocket(context); | ||
proxySocket.on("data", function(data) { | ||
if (context.connected) { | ||
context.serviceSocket.write(data); | ||
} else { | ||
context.buffers[context.buffers.length] = data; | ||
} | ||
if (proxy.options.tls !== false) { | ||
var tlsOptions = { | ||
pfx: fs.readFileSync(proxy.options.pfx), | ||
passphrase: proxy.options.passphrase | ||
}; | ||
proxy.server = tls.createServer(tlsOptions, function(socket) { | ||
proxy.handleClient(socket); | ||
}); | ||
proxySocket.on("close", function(hadError) { | ||
delete proxy.proxySockets[uniqueKey(proxySocket)]; | ||
context.serviceSocket.destroy(); | ||
} else { | ||
proxy.server = net.createServer(function(socket) { | ||
proxy.handleClient(socket); | ||
}); | ||
}); | ||
} | ||
proxy.server.listen(proxy.proxyPort, proxy.options.hostname); | ||
}; | ||
TcpProxy.prototype.createServiceSocket = function(context) { | ||
TcpProxy.prototype.handleClient = function(proxySocket) { | ||
const proxy = this; | ||
context.serviceSocket = new net.Socket(); | ||
context.serviceSocket.connect(proxy.servicePort, proxy.serviceHost, | ||
function() { | ||
context.connected = true; | ||
if (context.buffers.length > 0) { | ||
for (var i = 0; i < context.buffers.length; i++) { | ||
context.serviceSocket.write(context.buffers[i]); | ||
} | ||
var key = uniqueKey(proxySocket); | ||
proxy.proxySockets[key] = proxySocket; | ||
var context = { | ||
buffers: [], | ||
connected: false, | ||
proxySocket: proxySocket | ||
}; | ||
proxy.createServiceSocket(context); | ||
proxySocket.on("data", function(data) { | ||
if (context.connected) { | ||
context.serviceSocket.write(data); | ||
} else { | ||
context.buffers[context.buffers.length] = data; | ||
} | ||
}); | ||
proxySocket.on("close", function(hadError) { | ||
delete proxy.proxySockets[uniqueKey(proxySocket)]; | ||
context.serviceSocket.destroy(); | ||
}); | ||
}; | ||
TcpProxy.prototype.createServiceSocket = function(context) { | ||
const proxy = this; | ||
if (proxy.options.tls === "both") { | ||
context.serviceSocket = | ||
tls.connect(proxy.servicePort, proxy.serviceHost, { | ||
rejectUnauthorized: proxy.options.rejectUnauthorized | ||
}, function() { | ||
proxy.writeBuffer(context); | ||
}); | ||
} else { | ||
context.serviceSocket = new net.Socket(); | ||
context.serviceSocket.connect(proxy.servicePort, proxy.serviceHost, | ||
function() { | ||
proxy.writeBuffer(context); | ||
}); | ||
} | ||
context.serviceSocket.on("data", function(data) { | ||
@@ -72,5 +93,13 @@ context.proxySocket.write(data); | ||
}); | ||
return context; | ||
} | ||
}; | ||
TcpProxy.prototype.writeBuffer = function(context) { | ||
context.connected = true; | ||
if (context.buffers.length > 0) { | ||
for (var i = 0; i < context.buffers.length; i++) { | ||
context.serviceSocket.write(context.buffers[i]); | ||
} | ||
} | ||
}; | ||
TcpProxy.prototype.end = function() { | ||
@@ -77,0 +106,0 @@ this.server.close(); |
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
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
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
11931
9
160
44
1
2
+ Addedcommander@^2.15.1
+ Addedcommander@2.20.3(transitive)
- Removedoptimist@^0.6.1
- Removedminimist@0.0.10(transitive)
- Removedoptimist@0.6.1(transitive)
- Removedwordwrap@0.0.3(transitive)