-
Notifications
You must be signed in to change notification settings - Fork 0
/
nonAssetRequest.js
381 lines (327 loc) · 14.7 KB
/
nonAssetRequest.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
var url = require('url'),
http = require('http'),
https = require('https'),
util = require('util'),
zlib = require('zlib');
var mod = function(options) {
return function(request, response) {
var that = this;
var maxTries = 5;
that.request = request;
that.response = response;
that.soupData = new Buffer('');
that.requestData = new Buffer('');
that.contentEncoding = false;
that.tries = 0;
that.getSubDomain = function() {
if (!that.request || !that.request.headers || !that.request.headers.host) {
return null;
}
var subDomainRegex = new RegExp("(.*)\." + options.domain + ".*"),
results = null;
results = that.request.headers.host.match(subDomainRegex);
if (results && results[1]) {
return results[1];
} else {
return null;
}
};
that.getSoupDomain = function() {
return that.subDomain + "soup.io";
}
that.getNewResponseLocationField = function(location) {
return location.replace(/.+\/\/([a-zA-Z]+)\.soup\.io/, "http://$1." + options.domain);
};
that.getCorrectResponseCompression = function() {
var enc = false;
if (that.request.headers && that.request.headers["accept-encoding"] && that.contentEncoding) {
var renc = that.request.headers["accept-encoding"];
if (/gzip/.test(renc)) {
enc = "gzip";
} else if (/deflate/.test(renc)) {
enc = "deflate";
}
}
return enc;
};
that.getModifiedSoupResponseHeaders = function(headers) {
var newheaders = JSON.parse(JSON.stringify(headers));
if (newheaders.location) {
newheaders.location = that.getNewResponseLocationField(newheaders.location);
}
if (newheaders["set-cookie"]) {
newheaders["set-cookie"] = newheaders["set-cookie"].map(function(c) {
return c.replace(new RegExp("soup.io", "g"), options.domain);
});
}
var enc = that.getCorrectResponseCompression();
if (enc) {
newheaders["content-encoding"] = enc;
} else if (newheaders.hasOwnProperty("content-encoding")) {
delete newheaders["content-encoding"];
}
newheaders["content-length"] = that.soupData.length;
return newheaders;
};
that.getModifiedSoupResponseData = function(callback) {
if (that.soupData.length > 0) {
that.unpackCompressedData(
that.soupData,
that.contentEncoding,
function(data) {
var buf = null;
data = data.toString();
if (isRss(data)) {
buf = modifyRssAndReturnBuffer(data);
} else if (isHtml(data)) {
buf = modifyHtmlAndReturnBuffer(data);
} else {
buf = new Buffer(replaceSoupLinksInString(data));
}
callback(buf);
}
);
} else {
callback(new Buffer(""));
}
};
that.unpackCompressedData = function(data, encoding, callback) {
var decompressor;
if (encoding == "gzip") {
decompressor = zlib.gunzip;
} else if (encoding == "deflate") {
decompressor = zlib.inflate;
} else {
decompressor = function(data, cb) {
cb(undefined, data);
};
}
decompressor(data, function(err, buffer) {
if (!err) {
callback(buffer);
} else {
that.onSoupError();
}
});
}
that.compressData = function(data, encoding, callback) {
var compressor;
if (encoding == "gzip") {
compressor = zlib.gzip;
} else if (encoding == "deflate") {
compressor = zlib.deflateRaw;
} else {
compressor = function(data, cb) {
cb(undefined, data);
};
}
compressor(data, function(err, buffer) {
if (!err) {
callback(buffer);
} else {
that.onSoupError();
}
});
}
var isHtml = function(inputdata) {
return inputdata.search(/<li\ class/) != -1;
}
var isRss = function(inputdata) {
return inputdata.search(/<rss/) != -1;
}
var replaceSoupLinksInString = function(inputdata) {
return inputdata.replace(/soup\.io/g, options.domain).
replace(/https:\/\//g, "http://");
}
var insertProxyNote = function(inputdata) {
return inputdata.
replace(/<ul id="menu">/g, '<ul id="menu" style="padding-left: 0px !important;"><li style="background-color: red; font-size: 14px; color: black;"><div style="padding: 8px 14px;">THIS IS A PROXY NOT THE REAL SOUP</div></li>');
}
var modifyHtmlAndReturnBuffer = function(inputdata) {
var lines = inputdata.split("\n");
for (var i in lines) {
lines[i] = insertProxyNote(lines[i]);
if (lines[i].search(/Permalink/) == -1) {
lines[i] = replaceSoupLinksInString(lines[i]);
}
}
return new Buffer(lines.join("\n"));
}
var modifyRssAndReturnBuffer = function(inputdata) {
inputdata = inputdata.replace(/<link>/g, "\n<link>").replace(/<\/link>/g, "</link>\n");
var lines = inputdata.split("\n");
for (var i in lines) {
if (lines[i].search(/<link>/) == -1 &&
lines[i].search(/soup:attributes/) == -1) {
lines[i] = replaceSoupLinksInString(lines[i]);
}
}
return new Buffer(lines.join("\n"));
}
that.writeResponseHead = function() {
that.response.writeHead(that.soupResponse.statusCode,
that.getModifiedSoupResponseHeaders(that.soupResponse.headers));
}
that.getModifiedSoupRequestHeader = function(oheaders) {
var headers = JSON.parse(JSON.stringify(oheaders));
headers["accept-encoding"] = "gzip,deflate";
headers["host"] = that.getSoupDomain();
if (headers.referer) {
headers.referer = headers.referer.replace(new RegExp(options.domain, "g"), "soup.io");
}
if (headers.origin) {
headers.origin = headers.origin.replace(new RegExp(options.domain, "g"), "soup.io");
}
if (headers.cookie) {
headers.cookie = headers.cookie.replace(new RegExp(options.domain, "g"), "soup.io");
}
return headers;
};
that.shouldTransformData = function(headers) {
var textTypeRegex = /text/,
applicationTypeRegex = /application/;
if (!headers["content-type"]) {
return true;
} else if (headers["content-type"].search(textTypeRegex) != -1) {
return true;
} else if (headers["content-type"].search(applicationTypeRegex) != -1) {
return true;
}
return false;
};
that.onSoupData = function(chunk) {
try {
var newbuf = new Buffer(that.soupData.length + chunk.length);
that.soupData.copy(newbuf, 0, 0);
newbuf.write(chunk, that.soupData.length, chunk.length, "binary");
that.soupData = newbuf;
} catch (e) {
options.logger.error("soupData", e);
}
};
that.onSoupEndTransform = function() {
that.getModifiedSoupResponseData(
function(data) {
that.compressData(
data,
that.getCorrectResponseCompression(),
function(data) {
that.soupData = data;
that.writeResponseHead();
if (data.length > 0) {
options.stats.dataCount[that.request.connection.remoteAddress] += data.length;
that.response.write(data);
}
that.response.end();
options.logger.access(request, that.soupResponse.statusCode, data.length);
}
);
}
);
};
that.onSoupEnd = function() {
that.writeResponseHead();
if (that.soupData.length > 0 && that.request.method != "HEAD") {
var data = that.soupData;
options.stats.dataCount[that.request.connection.remoteAddress] += that.soupData.length;
that.response.write(data);
}
options.logger.access(request, that.soupResponse.statusCode, that.soupData.length);
that.response.end();
};
that.onSoupResponse = function(res) {
that.soupResponse = res;
switch (res.headers["content-encoding"]) {
case "gzip":
that.contentEncoding = "gzip";
break;
case "deflate":
that.contentEncoding = "deflate";
break;
default:
that.contentEncoding = false;
break;
}
res.setEncoding("binary");
res.on("data", that.onSoupData);
if (that.shouldTransformData(res.headers)) {
res.on("end", that.onSoupEndTransform);
} else {
res.on("end", that.onSoupEnd);
}
};
that.onSoupError = function(error) {
if (that.tries < maxTries) {
that.tries++;
try {
options.logger.error("soupError, code: " + error.code + ", tries: " + that.tries + "/" +
maxTries + ", " + that.request.headers.host + that.request.url, error);
} catch (e) {
}
setTimeout(that.respondeWithOriginalPage, 500);
} else {
options.logger.error("aborting non asset request after " + that.tries + " tries", error);
that.response.end("Sorry but soup.io seems broken right now :(");
}
};
that.onRequestData = function(chunk) {
try {
var newbuf = new Buffer(that.requestData.length + chunk.length);
that.requestData.copy(newbuf, 0, 0);
newbuf.write(chunk, that.requestData.length, chunk.length, "binary");
that.requestData = newbuf;
} catch (e) {
options.logger.error("reqeustData", e);
}
};
that.onRequestEnd = function() {
that.respondeWithOriginalPage();
};
that.onRequestTimeout = function() {
options.logger.error("aborting non asset request due to timeout");
that.proxy_request.abort();
that.response.end("Timeout reached, sorry :(");
}
var isHttpsLink = function(url) {
var loginRegex = new RegExp("\/login");
if (url.match(loginRegex)) return true;
return false;
}
that.respondeWithOriginalPage = function() {
var subDomain = that.getSubDomain();
subDomain = subDomain?subDomain + "." : "";
that.subDomain = subDomain;
var newHeaders = that.getModifiedSoupRequestHeader(that.request.headers);
var newPath = that.request.url.replace(new RegExp(options.domain, "g"), "soup.io");
if (request.url && isHttpsLink(request.url)) {
that.proxy_request = https.request({
host: that.getSoupDomain(),
port: 443,
method: that.request.method,
path: newPath,
rejectUnauthorized: false,
headers: newHeaders
});
} else {
that.proxy_request = http.request({
host: that.getSoupDomain(),
port: 80,
method: that.request.method,
path: newPath,
headers: newHeaders
});
};
that.proxy_request.on("response", that.onSoupResponse);
that.proxy_request.on("error", that.onSoupError);
that.proxy_request.end(that.requestData);
};
try {
that.request.setEncoding("binary");
that.request.on("data", that.onRequestData);
that.request.on("end", that.onRequestEnd);
} catch (e) {
options.logger.error("respondeWithOriginalPage" , e);
}
};
};
module.exports = mod;