-
Notifications
You must be signed in to change notification settings - Fork 23
/
index.js
220 lines (202 loc) · 7.24 KB
/
index.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
const net = require('net');
const ref = require('ref')
const fs = require('fs');
const bson = require('bson');
const parseArgs = require('minimist');
var args = parseArgs(process.argv);
/*process.on('uncaughtException', function (err) {
console.log("Client triggered an error.");
console.log(err.message);
})*/
if(args.h || args.help) {
console.log("Help:");
console.log("--address, -a: The ip of the dummy mongodb instance");
console.log(" Default: 127.0.0.1");
console.log("--port, -p: The port of the dummy mongodb instance");
console.log(" Default: 27016");
console.log("-l, --listen: The port to listen on");
console.log(" Default: 27017");
console.log("-o, --out: The file to output logs to");
console.log(" Default: proxy.log");
console.log("-h, --help: Displays this message and returns");
process.exit()
}
const EXTERNAL_PORT = args.listen || args.l || 27017;
const MONGODB_HOST = args.address || args.a || "127.0.0.1"
const MONGODB_PORT = args.port || args.p || 27016;
const LOG_FILE = args.out || args.o || "proxy.log";
//A really hacky logging method.
var oldLog = console.log;
console.log = function(data) {
data = data.toString().replace(/[^\x00-\x7F]/g, "").trim();
if(LOG_FILE) {
fs.appendFileSync(LOG_FILE, "[" + new Date().toISOString() + "] " + data + "\r\n");
}
oldLog("[" + new Date().toISOString() + "] " + data)
}
function printFromAddress(fromIp, data) {
console.log("[" + fromIp + " -> S] " + data);
}
function printToAddress(fromIp, data) {
console.log("[S -> " + fromIp + "] " + data);
}
var server = net.createServer(function (socket) {
var clientId = socket.remoteAddress + ":" + socket.remotePort;
var tag = "[" + clientId + "] ";
console.log(clientId + " connected.")
socket.on('data', function (msg) {
//console.log('<< From client to proxy ', msg.toString());
var serviceSocket = new net.Socket();
console.log(clientId + " -> Server:");
var packet = parseMessage(msg, clientId);
if(packet != null && packet instanceof OpQuery) {
console.log(packet.query.toString());
var fingerprint = "";
if(packet.query && packet.query.length > 0 && packet.query[0].client) {
fingerprint = packet.query[0].client;
}
if(packet.query && packet.query.length > 0) {
printFromAddress(clientId, "login", JSON.stringify(packet.query[0]))
} else {
printFromAddress(clientId, JSON.stringify(packet.query[0]))
}
}
serviceSocket.connect(MONGODB_PORT, MONGODB_HOST, function () {
serviceSocket.write(msg);
});
serviceSocket.on("data", function (data) {
//console.log('<< From remote to proxy', data.toString());
//console.log("Server -> " + clientId + ":");
parseMessage(data, clientId);
printToAddress(clientId, data);
socket.write(data);
//console.log('>> From proxy to client', data.toString());
});
});
socket.on('close', function() {
console.log(clientId + " disconnected.");
});
});
server.listen(EXTERNAL_PORT);
console.log("Proxy listening on " + EXTERNAL_PORT + ".");
var offset = 0;
function parseMessage(data, identifier) {
offset = 0;
var header = new MsgHeader(data);
//Only opcodes 2004(QUERY) and 1(REPLY) are currently implemented.
//Though there are other opcodes, these are not used as frequently.
//This should cover a decent amount of data.
switch(header.opCode) {
case 2004:
var packet = new OpQuery(data);
printFromAddress(identifier, packet.toString());
return packet;
case 1:
var packet = new OpReply(data);
printFromAddress(identifier, packet.toString());
return packet;
default:
printFromAddress(identifier, "Unimplemented opcode " + header.opCode);
printFromAddress(identifier, "Raw packet (" + header.opCode + "): \n" + data.toString())
return null;
}
}
function OpReply(data) {
this.flags = readInt32(data);
this.cursorId = readInt64(data);
this.startingFrom = readInt32(data);
this.docCount = readInt32(data);
var docs = [];
bson.deserializeStream(data, offset, this.docCount, docs, 0);
this.documents = docs;
this.toString = function() {
var result = "";
if(this.flags > 0) {
result += "Flags: " + this.flags + "\n";
}
if(this.documents.length > 0) {
result += "Documents: " + JSON.stringify(this.documents) + "\n";
}
return result;
}
}
function OpQuery(data) {
this.flags = readInt32(data);
this.collectionName = readCString(data);
this.numToSkip = readInt32(data);
this.numToReturn = readInt32(data);
var returnCount = this.numToReturn;
var docs = [];
bson.deserializeStream(data, offset, 1, docs, this.numToSkip);
this.query = docs;
//We're not finished yet, which implies the optional returnFieldsSelector is set.
if(offset < data.length) {
var fields = [];
bson.deserializeStream(data, offset, 1, fields, this.numToSkip);
this.returnFieldsSelector = fields
}
this.toString = function() {
var result = "";
if(this.flags > 0) {
result += "Flags: " + this.query + "\n";
}
if(this.query.length > 0) {
result += "Query: " + JSON.stringify(this.query) + "\n";
}
if(this.returnFieldsSelector) {
result += "Return Fields: " + JSON.stringify(this.returnFieldsSelector) + "\n";
}
return result;
}
}
function readCString(data) {
var cstring = ref.readCString(data, offset);
console.log(cstring);
offset += Buffer.byteLength(cstring, 'utf8') + 1;
return cstring;
}
function parseQuery(data) {
}
function MsgHeader(data) {
this.messageLength = readInt32(data);
this.requestID = readInt32(data);
this.responseTo = readInt32(data);
this.opCode = readInt32(data);
}
function readBinary(stream) {
var length = readInt32(stream);
var binary = "";
for(var i = 0; i < length; i++) {
binary += stream[offset + i];
}
offset += length + 1;
return binary;
}
function ObjectId(stream) {
this.epoch = new Buffer(stream[offset++], stream[offset++], stream[offset++], stream[offset++])
this.machineId = new Buffer(stream[offset++], stream[offset++], stream[offset++])
this.processId = new Buffer(stream[offset++], stream[offset++])
this.counter = new Buffer(stream[offset++], stream[offset++], stream[offset++])
this.toString = function() {
var buf = Buffer.concat([this.epoch, this.machineId, this.processId, this.counter]);
return buf.toString('hex');
}
}
function readDouble(stream) {
var value = stream.readDoubleLE(offset);
offset += 8;
return value;
}
function readByte(stream) {
return stream[offset++];
}
function readInt32(data) {
var value = data.readInt32LE(offset);
offset += 4;
return value;
}
function readInt64(data) {
var value = ref.readInt64LE(data, offset);
offset += 8;
return value;
}