forked from AverageMarcus/CorsProxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
40 lines (32 loc) · 920 Bytes
/
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
"use strict";
const request = require('request');
const http = require('http');
const url = require('url');
let PORT = process.env.PORT || 8000;
process.argv.forEach(function (arg) {
if(arg.indexOf('--port=') === 0) {
PORT = parseInt(arg.replace('--port=', ''), 10);
}
});
http.createServer((req, response) => {
let remoteURL = url.parse(req.url.substring(1));
if(!remoteURL.hostname || remoteURL.hostname === 'localhost') return response.end();
for(let key in req.headers) {
if(key.match(/host|cookie/ig)){
delete req.headers[key];
}
}
let config = {
url: remoteURL,
followAllRedirects: true,
method: req.method,
headers: req.headers
};
if(req.method !== 'HEAD') {
config.body = req;
}
response.setHeader('Access-Control-Allow-Origin', '*');
request(config)
.on('error', () => response.end())
.pipe(response, {end:true});
}).listen(PORT);