This repository has been archived by the owner on Oct 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 144
/
app.js
62 lines (53 loc) · 1.93 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
/***************
* node-unblocker: Web Proxy for evading firewalls and content filters,
* similar to CGIProxy or PHProxy
*
*
* This project is hosted on github: https://github.com/nfriedly/nodeunblocker.com
*
* By Nathan Friedly - http://nfriedly.com
* Released under the terms of the Affero GPL v3
*/
var url = require('url');
var querystring = require('querystring');
var express = require('express');
var unblocker = require('unblocker');
var Transform = require('stream').Transform;
var app = express();
function injectScript(data) {
if (data.contentType == 'text/html') { // https://nodejs.org/api/stream.html#stream_transform
data.stream = data.stream.pipe(
new Transform({
decodeStrings: false,
transform: function(chunk, encoding, next) {
const updated = chunk
.toString()
.replace('_blank','_self')
.replace('_parent','_self')
.replace('_top','_self')
.replace('</body>',`<script src="//code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script></body>`)
this.push(updated, "utf8");
next();
}
}))
}
}
var unblockerConfig = {
prefix: '/textbooks/',
responseMiddleware: [
injectScript
]
};
// this line must appear before any express.static calls (or anything else that sends responses)
app.use(unblocker(unblockerConfig));
// serve up static files *after* the proxy is run
app.use('/', express.static(__dirname + '/public'));
// this is for users who's form actually submitted due to JS being disabled or whatever
app.get("/no-js", function(req, res) {
// grab the "url" parameter from the querystring
var site = querystring.parse(url.parse(req.url).query).url;
// and redirect the user to /proxy/url
res.redirect(unblockerConfig.prefix + site);
});
// for compatibility with gatlin and other servers, export the app rather than passing it directly to http.createServer
module.exports = app;