-
Notifications
You must be signed in to change notification settings - Fork 7
/
cmd.js
executable file
·76 lines (65 loc) · 2.19 KB
/
cmd.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env node
var through = require('through2');
var tcpSpy = require('./');
var chalk = require('chalk')
var argv = require('minimist')(process.argv.slice(2), {boolean:true});
var pretty = require('pretty-stream');
var mstdout = require('multi-stdout');
var os = require('os');
var hexStream = function() {
return through(function(chunk, enc, callback) {
var arr = Array.prototype.slice.call(chunk).map(function(x) {
var str = x.toString(16);
return '00'.slice(str.toString().length) + str;
});
callback(null, arr.join(' ')+' ');
});
};
var formatStream = function(prefix) {
return pretty({
prefix: function(data) {
return prefix+' '+chalk.grey(data);
},
binary: function(data) {
return chalk.bgBlue(data);
},
truncate: argv.t || argv.truncate
});
};
if (argv._.length !== 2) {
console.log('Usage:');
console.log('');
console.log(' tcp-spy port [forward_host:]forward_port [options]');
console.log('');
console.log('Options:');
console.log('');
console.log(' --hex -x Force hexadecimal output');
console.log(' --truncate -t Truncate binary output');
console.log('');
process.exit(1);
}
var port = Number(argv._[0]);
var forwardPort = argv._[1];
var host;
if (typeof forwardPort === 'string' && forwardPort.indexOf(':') !== -1) {
var f = forwardPort.split(':');
host = f[0];
forwardPort = f[1];
}
var connId = 0;
var spy = tcpSpy({port: port, forwardPort: forwardPort, forwardHost: host});
var transform = argv.x || argv.hex ? hexStream : through
spy.on('connection', function(client, server) {
var id = ++connId;
id = '00'.slice(id.toString().length) + id;
var padding = through(function(data, enc, cb) {
cb(null, data);
}, function(cb) {
this.push(id + ' ' + chalk.green('***') + chalk.red(' Connection finished'));
cb();
});
padding.push(id + ' ' + chalk.green('***') + chalk.yellow(' Connection established')+os.EOL);
client.pipe(transform()).pipe(formatStream(id + ' ' + chalk.bold(chalk.magenta('-->')))).pipe(padding).pipe(mstdout());
server.pipe(transform()).pipe(formatStream(id + ' ' + chalk.bold(chalk.cyan('<--')))).pipe(mstdout());
});
process.stdout.setMaxListeners(0);