-
Notifications
You must be signed in to change notification settings - Fork 9
/
proxy.js
282 lines (262 loc) · 7.05 KB
/
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
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
const http = require("http"),
httpProxy = require("http-proxy");
const bound = function(that, method) {
// bind a method, to ensure `this=that` when it is called
// because prototype languages are bad
return function() {
method.apply(that, arguments);
};
};
const DynamicProxy = function(options) {
var dynamicProxy = this;
this.sessionCookie = options.sessionCookie;
this.sessionMap = options.sessionMap;
this.debug = options.verbose;
this.reverseProxy = options.reverseProxy;
this.port = options.port;
this.forwardIP = options.forwardIP;
this.forwardPort = options.forwardPort;
var log_errors = function(handler) {
return function(req) {
try {
return handler.apply(dynamicProxy, arguments);
} catch (e) {
console.log(
"Error in handler for " +
req.headers.host +
" " +
req.method +
" " +
req.url +
": ",
e
);
}
};
};
this.proxy = httpProxy.createProxyServer({
ws: true
});
this.proxy_server = http.createServer(
log_errors(dynamicProxy.handleProxyRequest)
);
this.proxy_server.on("upgrade", bound(this, this.handleWs));
};
DynamicProxy.prototype.rewriteRequest = function(request) {
if (request.url.indexOf("rstudio") != -1) {
var remap = {
"content-type": "Content-Type",
"content-length": "Content-Length"
};
// RStudio isn't spec compliant and pitches a fit on NodeJS's http module's lowercase HTTP headers
for (var i = 0; i < Object.keys(remap).length; i++) {
var key = Object.keys(remap)[i];
if (key in request.headers) {
request.headers[remap[key]] = request.headers[key];
delete request.headers[key];
}
}
if (
"Content-Type" in request.headers &&
request.headers["Content-Type"] ==
"application/x-www-form-urlencoded; charset=UTF-8"
) {
request.headers["Content-Type"] = "application/x-www-form-urlencoded";
}
}
};
DynamicProxy.prototype.targetFromSessionMap = function(key, token) {
for (let mappedSession in this.sessionMap) {
if (key == mappedSession) {
if (this.sessionMap[key].token == token) {
return this.sessionMap[key].target;
}
}
}
};
DynamicProxy.prototype.targetFromHeaders = function(request) {
let host = request.headers["x-interactive-tool-host"];
let port = request.headers["x-interactive-tool-port"];
if (!port && host.indexOf(":") > 0) {
const res = host.split(":", 2);
host = res[0];
port = res[1];
}
return {
host: host,
port: parseInt(port)
};
};
DynamicProxy.prototype.targetForRequest = function(request) {
// return proxy target for a given url
const req_host = request.headers.host;
const key = req_host.substring(0, req_host.indexOf("-"));
const token = req_host.substring(
req_host.indexOf("-") + 1,
req_host.indexOf(".")
);
let target;
if (this.sessionMap) {
target = this.targetFromSessionMap(key, token);
} else {
target = this.targetFromHeaders(request);
}
if (target) {
return target;
}
if (this.debug) {
console.log(
"No target found for " +
req_host +
" " +
request.method +
" " +
request.url
);
}
return null;
};
DynamicProxy.prototype.configureForward = function(req, target) {
var _target = Object.assign({}, target);
if (this.forwardIP) {
console.log(
"Forwarding request for " + target.host + " to " + this.forwardIP
);
req.headers["x-interactive-tool-host"] = target.host;
_target.host = this.forwardIP;
} else {
delete req.headers["x-interactive-tool-host"];
}
if (this.forwardPort) {
console.log(
"Forwarding request for " + target.port + " to " + this.forwardPort
);
req.headers["x-interactive-tool-port"] = target.port;
_target.port = this.forwardPort;
} else {
delete req.headers["x-interactive-tool-port"];
}
return _target;
};
DynamicProxy.prototype.handleProxyRequest = function(req, res) {
var othis = this;
var target = this.targetForRequest(req);
if (this.debug && target) {
console.log(
"PROXY " +
req.headers.host +
" " +
req.method +
" " +
req.url +
" to " +
target.host +
":" +
target.port
);
}
var origin = req.headers.origin;
this.rewriteRequest(req);
res.oldWriteHead = res.writeHead;
res.writeHead = function(statusCode, headers) {
if (othis.reverseProxy && statusCode === 302) {
if (res && res._headers) {
if (othis.debug) {
console.log("Original Location Header: " + res._headers.location);
}
if (res._headers.location) {
res._headers.location = res._headers.location.replace(
"http://localhost/",
"http://localhost:" + othis.port + "/"
);
}
if (othis.debug) {
console.log("Rewritten Location Header: " + res._headers.location);
}
}
}
try {
if (origin) {
res.setHeader("Access-Control-Allow-Origin", origin);
}
res.setHeader("Access-Control-Allow-Credentials", "true");
if (!headers) {
headers = {};
}
res.oldWriteHead(statusCode, headers);
} catch (error) {
console.log("Header could not be modified.");
console.log(error);
}
};
target = this.configureForward(req, target);
this.proxy.web(
req,
res,
{
target: target
},
function(e) {
console.log("Proxy error: ", e);
res.writeHead(502);
res.write("Proxy target missing");
res.end();
}
);
};
DynamicProxy.prototype.handleWs = function(req, res, head) {
// no local route found, time to proxy
var target = this.targetForRequest(req);
if (this.debug && target) {
console.log(
"PROXY WS " + req.url + " to " + target.host + ":" + target.port
);
}
var origin = req.headers.origin;
this.rewriteRequest(req);
res.oldWriteHead = res.writeHead;
res.writeHead = function(statusCode, headers) {
try {
if (origin) {
res.setHeader("Access-Control-Allow-Origin", origin);
}
res.setHeader("Access-Control-Allow-Credentials", "true");
if (!headers) {
headers = {};
}
res.oldWriteHead(statusCode, headers);
} catch (error) {
console.log("Header could not be modified.");
console.log(error);
}
};
target = this.configureForward(req, target);
this.proxy.ws(
req,
res,
head,
{
target: target
},
function(e) {
console.log("Proxy error: ", e);
res.writeHead(502);
res.write("Proxy target missing");
res.end();
}
);
};
DynamicProxy.prototype.listen = function(args_) {
const args = args_ || {};
const port = this.port || 8000;
const ip = args.ip || "localhost";
if (this.debug) {
console.log("Listening on " + ip + ":" + port);
}
this.proxy_server.listen(port, ip);
};
DynamicProxy.prototype.close = function() {
this.proxy.close();
this.proxy_server.close();
};
exports.DynamicProxy = DynamicProxy;