forked from alexbirkett/tcp-logging-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp-logging-proxy.js
executable file
·133 lines (105 loc) · 3.28 KB
/
tcp-logging-proxy.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env node
// -*- js -*-
const net = require('net');
const fs = require('fs');
const chalk = require('chalk');
function getParameters() {
var args = process.argv.slice(2);
if (args < 3) {
console.error(chalk.red('Usage: tcp-logging-proxy <input port> <output port> <host / ip address>'));
return null;
}
return args;
}
function getFileName() {
var now = new Date();
return now.getTime();
}
function getTimeNow() {
return new Date()
.toISOString()
.replace(/T/, ' ')
.replace(/\..+/, '');
}
function getOutputDir() {
return './output/';
}
function buildFilePathBase(fileName) {
return getOutputDir() + '/' + fileName;
}
function closeLogStreamIfRequired(socketClosed, logStream) {
if (socketClosed) {
logStream.end();
}
socketClosed = true;
return socketClosed;
}
function log(logStream, message, data) {
var logMessage = chalk.cyan(getTimeNow()) + ' ' + message;
if (typeof data !== 'undefined') {
logMessage = logMessage + ': ' + data;
}
logStream.write(logMessage);
console.log(logMessage);
}
function handleConnection(incomingSocket) {
var filePathBase = buildFilePathBase(getFileName());
var logStream = fs.createWriteStream(filePathBase + '.meta');
var upStream = fs.createWriteStream(filePathBase + '.upstream');
var downStream = fs.createWriteStream(filePathBase + '.downstream');
log(logStream, chalk.green('Client connected'), incomingSocket.remoteAddress);
var outgoingSocket = net.createConnection(outputPort, host);
var socketClosed = false;
incomingSocket.on('data', (data) => {
log(logStream, chalk.yellow(`(client ${incomingSocket.remoteAddress}) -> (server)`), data);
upStream.write(data);
});
incomingSocket.on('close', (data) => {
log(logStream, chalk.red('Incoming socket closed'));
socketClosed = closeLogStreamIfRequired(socketClosed, logStream);
});
outgoingSocket.on('data', (data) => {
log(logStream, chalk.blue(`(client ${incomingSocket.remoteAddress}) <- (server)`), data);
downStream.write(data);
});
outgoingSocket.on('close', (data) => {
log(logStream, chalk.red('Outgoing socket closed'));
socketClosed = closeLogStreamIfRequired(socketClosed, logStream);
});
incomingSocket.pipe(outgoingSocket);
outgoingSocket.pipe(incomingSocket);
};
function handleConnectionDelayed(connection) {
setTimeout(() => {
handleConnection(connection);
}, 1000);
};
function createServer(inputPort, outputPort, host) {
var server = net.createServer();
server.on('listening', () => {
console.log(chalk.green('--------------------------------------------------'));
console.log(chalk.green(` TCP Proxy of ${host}:${outputPort} running on port ${inputPort} `));
console.log(chalk.green('--------------------------------------------------'));
});
server.on('connection', handleConnection);
server.on('close', (socket) => {
console.log('Socket closed');
});
server.listen(inputPort);
}
function createDirectory(name, callback) {
fs.mkdir(name, null, callback);
}
parameters = getParameters();
if (parameters) {
var inputPort = parameters[0];
var outputPort = parameters[1];
var host = parameters[2];
createDirectory(getOutputDir(), function(err) {
if (err === null || err.code === 'EEXIST') {
createServer(inputPort, outputPort, host);
} else {
console.log('Could not create output dir ' + err);
}
});
}