-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
38 lines (31 loc) · 1.11 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
const http = require('http');
const httpProxy = require('http-proxy');
const config = require('./domains.json');
let proxy = httpProxy.createProxyServer();
console.log(new Date, '[SERVER] Started');
let server = http.createServer((req, res) => {
let host = req.headers.host;
if (config[host]) {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PATCH, PUT, DELETE, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Origin, Content-Type, X-Auth-Token');
res.setHeader('Access-Control-Max-Age', 2592000);
console.log(new Date, `[REQUEST] Proxied from ${host} to ${config[host]}`);
return proxy.web(req, res, {
target: `http://localhost:${config[host]}`,
secure: false
});
}
console.error(new Date, '[REQUEST] Host not found', host);
res.statusCode = 404;
res.end();
}).listen(80);
proxy.on('error', (err, req, res) => {
console.error(new Date, '[ERROR] ', err);
res.statusCode = 404;
res.end();
});
// let mock = http.createServer((req, res) => {
// res.write('ok');
// res.end();
// }).listen(3030);