-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathechoTCP.js
137 lines (113 loc) · 4.51 KB
/
echoTCP.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
134
135
136
137
var net = require('net');
var log = require('./logSingleton')();
var redis = require('./redisSingleton')();
var moment = require('moment-timezone');
if (!String.prototype.startsWith) {
String.prototype.startsWith = function(searchString, position){
position = position || 0;
return this.substr(position, searchString.length) === searchString;
};
}
module.exports.begin = function(port, maxRows, onListen) {
function rpush(address, payload) {
redis.RPUSH(address, payload, function(err, length) {
if (length > maxRows)
redis.LPOP(address);
});
}
var server = net.createServer({ allowHalfOpen: true }, function(sock) {
var ended = false;
var errorSeen = null;
var remoteAddress = sock.remoteAddress;
if (remoteAddress.startsWith('::ffff:'))
remoteAddress = remoteAddress.substr('::ffff:'.length);
var remotePort = sock.remotePort;
var when = moment().tz('America/Los_Angeles').format('M/D/YY HH:mm');
var localFinSent = false;
var remoteFinSent = false;
var ascii = '';
var hex = '';
var neverSawRemoteFin = false;
var closedSocketAbnormally = false;
var sentDataTwice = false;
var sentDataNever = true;
setTimeout(function() {
if (!ended)
{
neverSawRemoteFin = true;
sock.end();
}
}, 5000);
var fullResponse = 'At ' + when + ', TCP payload from ' + remoteAddress + ':' + remotePort;
sock.on('data', function(message) {
if (!ended)
{
var good = false;
if (message)
good = message.toString().indexOf('\n') < 0;
if (good && !sentDataTwice)
{
if (sentDataNever)
{
sock.write(fullResponse);
sentDataNever = false;
ascii = message.toString().replace(/[\x00-\x1F]/g, "_");
hex = message.toString('hex');
setTimeout(function() {
if (!ended)
{
response = ' of ' + ascii.length + ' bytes: ';
fullResponse += response;
sock.write(response);
}
}, 725);
setTimeout(function() {
if (!ended)
{
if (!remoteFinSent)
neverSawRemoteFin = true;
response = '"' + ascii + '" [' + hex + ']';
fullResponse += response;
sock.end(response);
localFinSent = true;
}
}, 1525);
}
else
sentDataTwice = true;
}
else
{
log.warn({ body: ascii, ip: remoteAddress, port: remotePort }, "Improper packet");
}
}
});
sock.on('end', function() {
remoteFinSent = true;
});
sock.on('error', function(error) {
if (error)
{
closedSocketAbnormally = true;
log.warn({ body: ascii, ip: remoteAddress, port: remotePort, error: error }, "Socket error");
}
});
sock.on('close', function(had_error) {
if (!localFinSent)
closedSocketAbnormally = true;
if (sentDataNever)
fullResponse += "... but client never sent data!";
else if (closedSocketAbnormally)
fullResponse += "... but socket was closed too early!";
else if (neverSawRemoteFin)
fullResponse += "... but socket wasn't shut down properly!";
else if (sentDataTwice)
fullResponse += "... but client sent data more than once!";
rpush(remoteAddress, fullResponse);
log.info({ body: ascii, ip: remoteAddress, port: remotePort, response: fullResponse }, "Success");
ended = true;
}
);
}).listen(port);
process.nextTick(function() { onListen(); });
}