forked from sentanos/rprxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
134 lines (114 loc) · 3.2 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
var proxy = require('http-proxy');
var express = require('express');
var https = require('https');
var url = require('url');
var path = require('path');
var api = require('./api.js');
var blocked = require('./static/blocked.json');
var reBlocked = require('./static/re_blocked.json');
var port = process.env.PORT || 80;
var subdomainsAsPath = true;
var serveHomepage = true;
var serveHomepageOnAllSubdomains = false;
var httpsProxy = proxy.createProxyServer({
agent: new https.Agent({
checkServerIdentity: function (host, cert) {
return undefined;
}
}),
changeOrigin: true
});
var httpProxy = proxy.createProxyServer({
changeOrigin: true
});
function stripSub (link) {
var original = url.parse(link);
var sub = '';
var path = original.path;
if (subdomainsAsPath) {
var split = path.split('/');
sub = split[1] && split[1] + '.';
split.splice(1, 1);
path = split.join('/');
}
return [path || '/', sub];
}
function getSubdomain (req, rewrite) {
var sub;
if (subdomainsAsPath) {
var res = stripSub(req.url);
if (rewrite) {
req.url = res[0];
}
sub = res[1];
} else {
var domain = req.headers.host;
sub = domain.slice(0, domain.lastIndexOf('.', domain.lastIndexOf('.') - 1) + 1);
}
return sub;
}
function onProxyError (err, req, res) {
console.error(err);
res.writeHead(500, {
'Content-Type': 'text/plain'
});
res.end('Proxying failed.');
}
function onProxyReq (proxyReq, req, res, options) {
proxyReq.setHeader('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36');
proxyReq.removeHeader('roblox-id');
}
httpsProxy.on('error', onProxyError);
httpsProxy.on('proxyReq', onProxyReq);
httpProxy.on('error', onProxyError);
httpProxy.on('proxyReq', onProxyReq);
var app = express();
app.use('/proxy', express.static('./static'));
app.use('/proxy', api);
app.use(function (req, res, next) {
if (serveHomepage && stripSub(req.url)[0] === '/') {
if (serveHomepageOnAllSubdomains || !getSubdomain(req)) {
res.sendFile(path.join(__dirname, '/static/home.html'));
return;
}
}
next();
});
app.use(function (req, res, next) {
for (var i = 0; i < blocked.length; i++) {
if (req.url === blocked[i]) {
res.end('URL blocked.');
return;
}
}
for (i = 0; i < reBlocked.length; i++) {
if (req.url.match(reBlocked[i])) {
res.end('URL blocked.');
return;
}
}
next();
});
app.use(function (req, res, next) {
console.log('PROXY REQUEST; HOST: ' + req.headers.host + '; URL: ' + req.url + '; OPT: ' + req.body + '; COOKIE: ' + req.headers.cookie + ';');
var subdomain = getSubdomain(req, true);
var proto = subdomain === 'wiki.' ? 'http' : 'https';
var options = {
target: proto + '://' + (subdomain || 'www.') + 'roblox.com'
};
if (proto === 'https') {
httpsProxy.web(req, res, options);
} else if (proto === 'http') {
httpProxy.web(req, res, options);
}
});
app.use(function (err, req, res, next) {
console.error(err);
res.writeHead(500, {
'Content-Type': 'text/plain'
});
res.end('Proxy handler failed.');
});
app.listen(port, function () {
console.log('Listening on port ' + port);
});