-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
132 lines (74 loc) · 3.37 KB
/
app.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
const Alloy = require('alloyproxy'),
rewrite = require('alloyproxy/libs/rewriting.js'),
config = require('./config.json'),
fs = require('fs'),
http = require('http'),
https = require('https'),
querystring = require('querystring'),
express = require('express'),
app = express();
if (!config.prefix.startsWith('/')) config.prefix = '/' + config.prefix;
if (!config.prefix.endsWith('/')) config.prefix = config.prefix + '/';
var server, protocol = 'http://';
const ssl_options = {
key: fs.readFileSync('./ssl/default.key'),
cert: fs.readFileSync('./ssl/default.crt')
};
if (config.ssl == true) { server = https.createServer(ssl_options, app); protocol = 'https://' } else server = http.createServer(app);
btoa = (str) => {
str = new Buffer.from(str).toString('base64');
return str;
};
atob = (str) => {
str = new Buffer.from(str, 'base64').toString('utf-8');
return str;
};
const Unblocker = new Alloy({
prefix: config.prefix,
error: (proxy) => { proxy.res.send(fs.readFileSync('./error.html', { encoding: 'utf8' }).replace('%ERR%', proxy.error.info.message.replace(/</gi, '<‌').replace(/>/gi, '>‌'))); }, // Doing replace functions on "<" and ">" to prevent XSS.
request: [],
response: [],
injection: true,
});
// The main part of the proxy.
app.get(config.prefix, (req, res, next) => {
if (req.query.url) {
var url = atob(req.query.url);
if (url.startsWith('//')) url = 'http:' + url;
if (url.startsWith('https://') || url.startsWith('http://')) { return res.redirect(config.prefix + rewrite.url(url)) }
else return res.redirect(config.prefix + rewrite.url('http://' + url))
} else return next();
});
app.use(config.prefix, (req, res, next) => {
req.url = config.prefix + req.url.slice(1);
if (config.cookie_auth && !config.cookie_auth == false) {
if (req.headers['cookie'] && req.headers['cookie'].match(config.cookie_auth)) return Unblocker.app(req, res, next);
res.send(fs.readFileSync('./error.html', { encoding: 'utf8' }).replace('%ERR%', 'Authorization required'));
res.statusCode = 400;
} else Unblocker.app(req, res, next);
});
app.post(`/session/`, async(req, res, next) => {
req.body = await new Promise(resolve => {
var body = '';
req.on('data', chunk => body += chunk).on('end', () => {
try {
if (body.startsWith('{') && body.endsWith('}')) { resolve(JSON.parse(body)) }
else {
resolve(querystring.parse(body));
};
} catch(err) { resolve({}) }
});
});
if (req.body.url) {
if (req.body.url.startsWith('//')) { req.body.url = 'http:' + req.body.url; } else if (req.body.url.startsWith('https://') || req.body.url.startsWith('http://')) { req.body.url = req.body.url } else { req.body.url = 'http://' + req.body.url};
if (config.cookie_auth && !config.cookie_auth == false) {
res.set('Set-Cookie', config.cookie_auth + `; path=${config.prefix};`);
};
return res.redirect(config.prefix + rewrite.url(req.body.url));
} else next();
});
// Frontend.
app.use('/', express.static('public'));
// WebSocket handler.
Unblocker.ws(server);
server.listen(process.env.PORT || config.port, () => console.log(`Running on ${protocol}0.0.0.0:${config.port}`));