bobwatcherx/nodeunblock
0
1/***************2 * node-unblocker: Web Proxy for evading firewalls and content filters,3 * similar to CGIProxy or PHProxy4 *5 *6 * This project is hosted on github: https://github.com/nfriedly/nodeunblocker.com7 *8 * By Nathan Friedly - http://nfriedly.com9 * Released under the terms of the Affero GPL v310 */11 12var url = require('url');13var querystring = require('querystring');14var express = require('express');15var Unblocker = require('unblocker');16var Transform = require('stream').Transform;17var youtube = require('unblocker/examples/youtube/youtube.js')18 19var app = express();20 21var google_analytics_id = process.env.GA_ID || null;22 23function addGa(html) {24 if (google_analytics_id) {25 var ga = [26 "<script type=\"text/javascript\">",27 "var _gaq = []; // overwrite the existing one, if any",28 "_gaq.push(['_setAccount', '" + google_analytics_id + "']);",29 "_gaq.push(['_trackPageview']);",30 "(function() {",31 " var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;",32 " ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';",33 " var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);",34 "})();",35 "</script>"36 ].join("\n");37 html = html.replace("</body>", ga + "\n\n</body>");38 }39 return html;40}41 42function googleAnalyticsMiddleware(data) {43 if (data.contentType == 'text/html') {44 45 // https://nodejs.org/api/stream.html#stream_transform46 data.stream = data.stream.pipe(new Transform({47 decodeStrings: false,48 transform: function(chunk, encoding, next) {49 this.push(addGa(chunk.toString()));50 next();51 }52 }));53 }54}55 56var unblocker = new Unblocker({57 prefix: '/proxy/',58 requestMiddleware: [59 youtube.processRequest60 ],61 responseMiddleware: [62 googleAnalyticsMiddleware63 ]64});65 66// this line must appear before any express.static calls (or anything else that sends responses)67app.use(unblocker);68 69// serve up static files *after* the proxy is run70app.use('/', express.static(__dirname + '/public'));71 72// this is for users who's form actually submitted due to JS being disabled or whatever73app.get("/no-js", function(req, res) {74 // grab the "url" parameter from the querystring75 var site = querystring.parse(url.parse(req.url).query).url;76 // and redirect the user to /proxy/url77 res.redirect(unblockerConfig.prefix + site);78});79 80const port = process.env.PORT || process.env.VCAP_APP_PORT || 7860;81 82app.listen(port, function() {83 console.log(`node unblocker process listening at http://localhost:${port}/`);84}).on("upgrade", unblocker.onUpgrade); // onUpgrade handles websockets85 