-
Notifications
You must be signed in to change notification settings - Fork 786
Adds tetr.io support #43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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); | ||
| } | ||
| return html; | ||
| } | ||
|
|
@@ -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
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| ] | ||
| }); | ||
|
|
||
|
|
||
There was a problem hiding this comment.
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.