Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
TCP proxy that also duplicates traffic to a secondary host
I built this because I needed a way to "tap" production traffic and shoot it at a new system to see how it handles load.
npm install -g duplicator
duplicator -f localhost:80 -d localhost:3000 -p 8080 [-i 127.0.0.1]
localhost:80
localhost:3000
, ignoring responses8080
Note: the cli automatically uses the cluster API to run several workers to handle connections, and restart workers if they die.
The equivalent to the call above, in node.js:
var duplicator = require('duplicator')
var server = duplicator(function(connection, forward, duplicate) {
forward('localhost:80')
duplicate('localhost:3000')
}).listen(8080, ["127.0.0.1"])
Creates a new net.Server
. When cb
is called on a successful connection, it will receive the connection to the client and two special callback functions, forward
and duplicate
.
duplicator(function(connection, forward, duplicate) {
forward('origin:80')
duplicate(Math.random() < 0.5 ? 'host1:80' : 'host2:80')
})
In this example, we always use origin:80
as the primary server to forward requests to, but we send half the duplicated rquests to host1
, and the other half to host2
.
Forward stream
to host
. stream
defaults to the current connection unless otherwise specified.
duplicator(function(connection, forward, duplicate) {
// forward every connection to localhost:80
forward('localhost:80')
})
Note that it is not safe to forward the same connection multiple times, as the responses from the different servers may interfere with one another. Therefore if you do call forward
multiple times, only the first call will succeed, and subsequent calls will merely duplicate the connection.
Duplicate on average rate
copies of stream
to host
. stream
defaults to the current connection unless otherwise specified, rate
defaults to 1.
Rate is the expected number of copies sent per connection, and is interpreted as follows:
Note that both stream
and rate
are optional, but have sensible defaults. If only two parameters are specified, the second will be interpreted as rate
if it's a number, stream
otherwise.
duplicator(function(connection, forward, duplicate) {
// duplicate 50% of requests to localhost:3000
duplicate('localhost:3000', 0.5)
// duplicate all connections to stdout
duplicate('localhost:3000', process.stdout)
})
A host can be any one of:
net.connect
would expect it ({ host: 'google.com', port: 80 }
)80
, 3000
), host is implied to be localhost
localhost:3000
, google.com:80
)net.connect
(function(cb) { net.connect({ port:80 }, cb) }
)Thanks to @netroy for contributing the cluster support!
If you'd like to contribute, please open a pull request or file an issue.
FAQs
TCP proxy that also duplicates traffic to a secondary host
The npm package duplicator receives a total of 4 weekly downloads. As such, duplicator popularity was classified as not popular.
We found that duplicator demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.