├── .gitignore ├── LICENSE ├── README.md ├── bin.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Mathias Buus 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # tcp-throughput-proxy 2 | 3 | Proxy that allows you to monitor how much incoming trafic it is receiving. 4 | 5 | ``` 6 | npm install -g tcp-throughput-proxy 7 | ``` 8 | 9 | ## Usage 10 | 11 | ``` sh 12 | # start a proxy listening on port 10000 proxing to localhost:20000 13 | tcp-throughput-proxy --from 10000 --to localhost:20000 14 | ``` 15 | 16 | Optionally if you only want to monitor throughput and not proxy anywhere 17 | 18 | ``` sh 19 | # just monitor throughput 20 | tcp-throughput-proxy --from 10000 21 | ``` 22 | 23 | Then to start monitoring throughput connect to the monitor server. 24 | Per default the monitor is listening on the `from` port + 1. 25 | To connect to it simply open a tcp connection to the monitor server 26 | 27 | ``` sh 28 | # attach to the monitor server 29 | nc localhost 10001 30 | ``` 31 | 32 | The monitor server should start printing out stats in the following format 33 | 34 | ``` 35 | 0 open connections 36 | Receiving 0 b/s 37 | ``` 38 | 39 | If you open up another tcp connection to the proxy server and start writing 40 | data you should see the receive speed go up. For example to write a bunch of random 41 | data to it do 42 | 43 | ``` sh 44 | # pipe a bunch of random data to the proxy 45 | cat /dev/random | nc localhost 10000 46 | ``` 47 | 48 | After executing the above you should see receive speed in the monitor connection go up 49 | 50 | ## License 51 | 52 | MIT 53 | -------------------------------------------------------------------------------- /bin.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | var net = require('net') 4 | var minimist = require('minimist') 5 | var speedometer = require('speedometer') 6 | var log = require('single-line-stream') 7 | var pretty = require('pretty-bytes') 8 | var pump = require('pump') 9 | 10 | var argv = minimist(process.argv.slice(2), { 11 | alias: {from: 'f', monitor: 'm', help: 'h', to: 't'} 12 | }) 13 | 14 | if (argv.help) { 15 | console.error( 16 | 'Usage: tcp-throughput-proxy [options]\n' + 17 | ' --from, -f [port]\n' + 18 | ' --to, -t [host:port]\n' + 19 | ' --monitor, -m [from-port + 1]\n' 20 | ) 21 | process.exit(0) 22 | } 23 | 24 | var proxyPort = 0 25 | var proxyHost = null 26 | 27 | if (argv.to) { 28 | proxyPort = typeof argv.to === 'number' ? argv.to : argv.to.split(':')[1] 29 | proxyHost = typeof argv.to === 'number' ? '127.0.0.1' : argv.to.split(':')[0] 30 | } 31 | 32 | if (!argv.from) argv.from = proxyPort || 10000 33 | if (!argv.monitor) argv.monitor = argv.from + 1 34 | 35 | var receiving = speedometer() 36 | var sending = argv.to ? speedometer() : null 37 | var connections = 0 38 | 39 | var server = net.createServer(function (socket) { 40 | if (proxyPort) pump(socket, net.connect(proxyPort, proxyHost).on('data', onsend), socket) 41 | connections++ 42 | socket.on('end', onend) 43 | socket.on('error', onerror) 44 | socket.on('close', onclose) 45 | socket.on('data', onreceive) 46 | }) 47 | 48 | var monitor = net.createServer(function (socket) { 49 | socket.on('end', onend) 50 | socket.on('error', onerror) 51 | 52 | var stream = log() 53 | var interval = setInterval(progress, 500) 54 | stream.pipe(socket) 55 | 56 | socket.on('close', function () { 57 | clearInterval(interval) 58 | }) 59 | 60 | function progress () { 61 | stream.write( 62 | connections + ' open connections\n' + 63 | 'Receiving ' + pretty(receiving()) + '/s\n' + 64 | (sending ? 'Sending ' + pretty(sending()) + '/s\n' : '') 65 | ) 66 | } 67 | }) 68 | 69 | monitor.listen(argv.monitor, function () { 70 | server.listen(argv.from, function () { 71 | if (argv.to) console.log('Proxing to %s from %d', proxyHost + ':' + proxyPort, argv.from) 72 | else console.log('Server is listening on %d', argv.from) 73 | console.log('Monitor server is listening on %d', argv.monitor) 74 | }) 75 | }) 76 | 77 | function onreceive (data) { 78 | receiving(data.length) 79 | } 80 | 81 | function onsend (data) { 82 | sending(data.length) 83 | } 84 | 85 | function onclose () { 86 | connections-- 87 | } 88 | 89 | function onerror () { 90 | this.destroy() 91 | } 92 | 93 | function onend () { 94 | this.end() 95 | } 96 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "tcp-throughput-proxy", 3 | "version": "1.1.0", 4 | "description": "Proxy that allows you to monitor how much incoming trafic it is receiving.", 5 | "main": "bin.js", 6 | "dependencies": { 7 | "minimist": "^1.2.0", 8 | "pretty-bytes": "^2.0.1", 9 | "pump": "^1.0.1", 10 | "single-line-stream": "^1.1.0", 11 | "speedometer": "^1.0.0" 12 | }, 13 | "devDependencies": {}, 14 | "bin": { 15 | "tcp-throughput-proxy": "./bin.js" 16 | }, 17 | "repository": { 18 | "type": "git", 19 | "url": "https://github.com/mafintosh/tcp-throughput-proxy.git" 20 | }, 21 | "author": "Mathias Buus (@mafintosh)", 22 | "license": "MIT", 23 | "bugs": { 24 | "url": "https://github.com/mafintosh/tcp-throughput-proxy/issues" 25 | }, 26 | "homepage": "https://github.com/mafintosh/tcp-throughput-proxy" 27 | } 28 | --------------------------------------------------------------------------------