-
Notifications
You must be signed in to change notification settings - Fork 2
/
loggers.js
112 lines (101 loc) · 3.58 KB
/
loggers.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
//
// TLS MITM - an SSL relay for analyzing network communications
// Copyright (C) 2014 Josh Stone
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
//
var out = require("./out.js");
var fs = require("fs");
try {
var moment = require("moment");
} catch(e) {
out.red("ERROR loading `moment` library");
out.red("Try installing it with 'npm install moment'");
process.exit(2);
}
var printreadable = function(data, padding) {
var str = "";
var i, j;
for(i = 0; i < data.length; i++) {
var character = data.readUInt8(i);
if(character > 0x20 && character < 0x7f) {
str += String.fromCharCode(character);
} else if(character == 0x0a) {
str += "\n";
for(j = 0; j < padding; j++) {
str += " ";
}
} else if(character == 0x20) {
str += " ";
} else if(character == 0x0d) {
// we skip LFs
} else {
str += ".";
}
}
return str.replace(/^\s*$/gm, "").replace(/\n+/, "\n").trim();
}
module.exports.filer = function(relay, direction, data) {
out.cyan("Filer output logger not implemented yet");
}
module.exports.screen = function(relay, direction, data) {
var clientid = "[" + relay.id + "] ";
var printer = direction == "client" ? out.green : out.blue;
printer(clientid + printreadable(data, clientid.length + 9));
}
function encodeIP(ip) {
var octets = ip.split(/\./);
console.log(octets);
out.yellow([parseInt(octets[0]),
parseInt(octets[1]),
parseInt(octets[2]),
parseInt(octets[3])]);
return new Buffer([parseInt(octets[0]),
parseInt(octets[1]),
parseInt(octets[2]),
parseInt(octets[3])]);
}
module.exports.filer = function(filename) {
var outclient = fs.openSync(filename, "w");
this.send = function(relay, direction, data) {
// a header needs the following fields:
//
// timestamp -- UNIX timestamp as 32-bit integer
// relay ID -- 32-bit integer
// direction -- 8-bit integer (0 = client, 1 = server)
// client IP -- 32-bit integer (IP address)
// client port -- 16-bit integer
// server IP -- 32-bit integer (IP address)
// server port -- 16-bit integer
// bytes -- 32-bit integer denoting message length
var header = new Buffer(29);
var clientip = encodeIP(relay.clientip);
var clientport = relay.clientport;
var serverip = encodeIP(relay.serverip);
var serverport = relay.serverport;
header.write("MESG" , 0);
header.writeUInt32LE((new moment()).unix() , 4);
header.writeUInt32LE(relay.id , 8);
header.writeUInt8(direction == "client" ? 0 : 1 , 12);
clientip.copy(header, 13, 0, 4);
header.writeUInt16LE(clientport , 17);
serverip.copy(header, 19, 0, 4);
header.writeUInt16LE(serverport , 23);
header.writeUInt32LE(data.length , 25);
fs.writeSync(outclient, header, 0, header.length, null);
fs.writeSync(outclient, data, 0, data.length, null);
}
}