-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
server.js
291 lines (271 loc) · 7.72 KB
/
server.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
// Dependencies
const proxy = require('http-proxy');
const https = require('https');
const http = require('http');
const crypto = require('crypto');
const assert = require('assert');
const zlib = require('zlib');
const { URL } = require('url');
// Manual constants
const ALLOWED_METHODS = http.METHODS;
const ALLOWED_PROTOS = ['http', 'https'];
const ALLOWED_GZIP_METHODS = ['transform', 'decode', 'append'];
const DEFAULT_PROTO = 'https';
const DEFAULT_USERAGENT = 'Mozilla';
const getHosts = (hosts) => {
if (!hosts) {
return [];
}
let parsed = [];
hosts = hosts.split(',');
for (let i = 0; i < hosts.length; i++) {
const host = hosts[i];
try {
(() => new URL(`${DEFAULT_PROTO}://${host}`))();
} catch (e) {
throw new Error(`Configuration error! Invalid host domain on item ${host}`);
}
parsed.push({
host: host
});
}
return parsed;
};
// Environment Constants
const PORT = process.env.PORT || 80;
const ACCESS_KEY = process.env.ACCESS_KEY && Buffer.from(process.env.ACCESS_KEY);
const USE_WHITELIST = process.env.USE_WHITELIST === 'true';
const USE_OVERRIDE_STATUS = process.env.USE_OVERRIDE_STATUS === 'true';
const REWRITE_ACCEPT_ENCODING = process.env.REWRITE_ACCEPT_ENCODING === 'true';
const APPEND_HEAD = process.env.APPEND_HEAD === 'true';
const ALLOWED_HOSTS = getHosts(process.env.ALLOWED_HOSTS);
const GZIP_METHOD = process.env.GZIP_METHOD;
assert.ok(ACCESS_KEY, 'Missing ACCESS_KEY');
assert.ok(ALLOWED_GZIP_METHODS.includes(GZIP_METHOD), `GZIP_METHOD must be one of the following values: ${JSON.stringify(ALLOWED_GZIP_METHODS)}`);
const server = http.createServer();
const httpsProxy = proxy.createProxyServer({
agent: new https.Agent({
checkServerIdentity: (host, cert) => {
return undefined;
}
}),
changeOrigin: true
});
const httpProxy = proxy.createProxyServer({
changeOrigin: true
});
const writeErr = (res, status, message) => {
res.writeHead(status, {'Content-Type': 'text/plain'});
res.end(message);
};
const onProxyError = (err, req, res) => {
console.error(err);
writeErr(res, 500, 'Proxying failed');
};
const appendHead = (proxyRes, res, append) => {
const encoding = proxyRes.headers['content-encoding'];
let handler;
let encoder;
let appendEncoded;
switch (encoding) {
case 'gzip':
handler = zlib.gzip;
break;
default:
appendEncoded = append;
}
if (handler) {
encoder = new Promise((resolve, reject) => {
handler(append, (e, buf) => {
if (e) {
reject(e);
}
appendEncoded = buf;
resolve();
});
});
}
if ('content-length' in proxyRes.headers) {
delete proxyRes.headers['content-length'];
}
const _end = res.end;
res.end = async () => {
if (!appendEncoded) {
try {
await encoder;
} catch (e) {
console.error(`Encoder error: ${e}`);
return;
}
}
res.write(appendEncoded);
_end.call(res);
};
};
const transformEncoded = (proxyRes, res, append) => {
const encoding = proxyRes.headers['content-encoding'];
let decodeHandler;
let encodeHandler;
let encoder;
let decoder;
switch (encoding) {
case 'gzip':
decodeHandler = zlib.createGunzip;
encodeHandler = zlib.createGzip;
break;
}
if (decodeHandler) {
decoder = decodeHandler();
encoder = encodeHandler();
const _write = res.write.bind(res);
const _end = res.end.bind(res);
res.write = (chunk) => {
decoder.write(chunk);
};
res.end = () => {
decoder.end();
};
if (GZIP_METHOD === 'transform') {
decoder.on('end', () => {
encoder.write(append);
encoder.end();
});
decoder.pipe(encoder, {end: false});
encoder.on('data', (chunk) => {
_write(chunk);
});
encoder.on('end', () => {
_end();
});
} else if (GZIP_METHOD === 'decode') {
decoder.on('data', (chunk) => {
_write(chunk);
});
decoder.on('end', () => {
_write(append);
_end();
});
if ('content-encoding' in proxyRes.headers) {
delete proxyRes.headers['content-encoding'];
}
}
}
if ('content-length' in proxyRes.headers) {
delete proxyRes.headers['content-length'];
}
};
const processResponse = (proxyRes, res, append) => {
if (['transform', 'decode'].includes(GZIP_METHOD) && proxyRes.headers['content-encoding']) {
transformEncoded(proxyRes, res, append);
} else {
appendHead(proxyRes, res, append);
}
};
const onProxyReq = (proxyReq, req, res, options) => {
proxyReq.setHeader('User-Agent', proxyReq.getHeader('proxy-override-user-agent') || DEFAULT_USERAGENT);
if (REWRITE_ACCEPT_ENCODING) {
proxyReq.setHeader('Accept-Encoding', 'gzip');
}
proxyReq.removeHeader('roblox-id');
proxyReq.removeHeader('proxy-access-key');
proxyReq.removeHeader('proxy-target');
};
const onProxyRes = (proxyRes, req, res) => {
const head = {
headers: Object.assign({}, proxyRes.headers),
status: {
code: proxyRes.statusCode,
message: proxyRes.statusMessage
}
};
if (USE_OVERRIDE_STATUS) {
proxyRes.statusCode = 200;
}
if (APPEND_HEAD) {
const append = `"""${JSON.stringify(head)}"""`;
processResponse(proxyRes, res, append);
}
};
httpsProxy.on('error', onProxyError);
httpsProxy.on('proxyReq', onProxyReq);
httpsProxy.on('proxyRes', onProxyRes);
httpProxy.on('error', onProxyError);
httpProxy.on('proxyReq', onProxyReq);
httpProxy.on('proxyRes', onProxyRes);
const doProxy = (target, proto, req, res) => {
var options = {
target: proto + '://' + target.host
};
if (proto === 'https') {
httpsProxy.web(req, res, options);
} else if (proto === 'http') {
httpProxy.web(req, res, options);
} else {
throw new Error(`Do proxy error: Unsupported protocol ${proto}`);
}
};
server.on('request', (req, res) => {
const method = req.headers['proxy-target-override-method'];
if (method) {
if (ALLOWED_METHODS.includes(method)) {
req.method = method;
} else {
writeErr(res, 400, 'Invalid target method');
return;
}
}
const overrideProto = req.headers['proxy-target-override-proto'];
if (overrideProto && !ALLOWED_PROTOS.includes(overrideProto)) {
writeErr(res, 400, 'Invalid target protocol');
return;
}
const accessKey = req.headers['proxy-access-key'];
const requestedTarget = req.headers['proxy-target'];
if (accessKey && requestedTarget) {
req.on('error', (err) => {
console.error(`Request error: ${err}`);
});
const accessKeyBuffer = Buffer.from(accessKey);
if (accessKeyBuffer.length === ACCESS_KEY.length && crypto.timingSafeEqual(accessKeyBuffer, ACCESS_KEY)) {
let parsedTarget;
try {
parsedTarget = new URL(`https://${requestedTarget}`);
} catch (e) {
writeErr(res, 400, 'Invalid target');
return;
}
const requestedHost = parsedTarget.host;
let hostAllowed = false;
let hostProto = DEFAULT_PROTO;
for (let i = 0; i < ALLOWED_HOSTS.length; i++) {
const iHost = ALLOWED_HOSTS[i];
if (requestedHost === iHost.host) {
hostAllowed = true;
break;
}
}
if (!USE_WHITELIST) {
hostAllowed = true;
}
if (overrideProto) {
hostProto = overrideProto;
}
if (hostAllowed) {
doProxy(parsedTarget, hostProto, req, res);
} else {
writeErr(res, 400, 'Host not whitelisted');
}
} else {
writeErr(res, 403, 'Invalid access key');
}
} else {
writeErr(res, 400, 'proxy-access-key and proxy-target headers are both required');
}
});
server.listen(PORT, (err) => {
if (err) {
console.error(`Server listening error: ${err}`);
return;
}
console.log(`Server started on port ${PORT}`);
});