Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ The original nodeunblocker.com is gone, but it's now easier than ever to deploy
[![Deploy to Cyclic](https://deploy.cyclic.sh/button.svg)](https://deploy.cyclic.sh/)
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fnfriedly%2Fnodeunblocker.com)

## Now with tetr.io support

Added a fix for tetr.io when running over reverse proxy or any tunneling service

To disable it just add `//` to the beginning of [this line of code in app.js](https://github.com/nfriedly/nodeunblocker.com/blob/5355bbfa8cfe2b5d2e6e5aae41ea21936a3be9a5/app.js#L106) like so:

```js
// tetrioPatch
```

Also fixed Analytics Api to newest Analytics 4

## Now with YouTube support (sort of)

I went ahead and activated the [youtube example](https://github.com/nfriedly/node-unblocker/blob/master/examples/youtube/youtube.js), it replaces youtube.com video pages with a custom page that just streams the video (but actually works).
Expand Down
70 changes: 57 additions & 13 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,17 @@ var google_analytics_id = process.env.GA_ID || null;
function addGa(html) {
if (google_analytics_id) {
var ga = [
"<script type=\"text/javascript\">",
"var _gaq = []; // overwrite the existing one, if any",
"_gaq.push(['_setAccount', '" + google_analytics_id + "']);",
"_gaq.push(['_trackPageview']);",
"(function() {",
" var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;",
" ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';",
" var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);",
"})();",
"</script>"
].join("\n");
html = html.replace("</body>", ga + "\n\n</body>");
"<!-- Google tag (gtag.js) -->",
"<script async src=\"https://www.googletagmanager.com/gtag/js?id=" + google_analytics_id + "\"></script>",
"<script>",
" window.dataLayer = window.dataLayer || [];",
" function gtag(){dataLayer.push(arguments);}",
" gtag('js', new Date());",
"\n",
" gtag('config', " + google_analytics_id + ");",
"</script>"
].join("\n");
html = html.replace("<head>", "<head>\n\n" + ga);
Comment on lines +26 to +36
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, yeah, this has been broken for a while hasn't it. I probably would have just removed it, but this is fine too.

}
return html;
}
Expand All @@ -53,13 +52,58 @@ function googleAnalyticsMiddleware(data) {
}
}

function forceUpgrade(html) {
var meta = [
"<meta http-equiv=\"Content-Security-Policy\" content=\"upgrade-insecure-requests\">"
].join("\n");
html = html.replace("</head>", meta + "\n\n</head>");
return html;
}

function forceHttpsUpgradeMiddleware(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) {
this.push(forceUpgrade(chunk.toString()));
next();
}
}));
}
}
Comment on lines +55 to +75
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this code used anywhere, or was it just the initial attempt and can be removed now?


function tetrioPatch(html) {
var meta = [
"<meta http-equiv=\"Content-Security-Policy\" content=\"upgrade-insecure-requests\">"
].join("\n");
html = html.replace("<meta name=googlebot content=notranslate>", "<meta name=googlebot content=notranslate>\n\n" + meta);
return html;
}

function tetrioPatchMiddleware(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) {
this.push(tetrioPatch(chunk.toString()));
next();
}
}));
}
}
Comment on lines +77 to +97
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the basic issue that tetr.io requires https, and so this is one way of forcing https?

What does this do if you only have http, and don't have a https server?

Also, if it's specifically for tetr.io, then the if check should be conditioned on that in addition to the contentType, similar to how we check for youtube.com here: https://github.com/nfriedly/node-unblocker/blob/ee8358df24dc6abe2cc4884c48e3d0e44b57c0b2/examples/youtube/youtube.js#L7-L8

Lastly, if we do keep this, I think this should go in a separate file that's just imported into app.js.


var unblocker = new Unblocker({
prefix: '/proxy/',
requestMiddleware: [
youtube.processRequest
],
responseMiddleware: [
googleAnalyticsMiddleware
googleAnalyticsMiddleware,
tetrioPatch
]
});

Expand Down