From eb34fcf923bc72c6b4e21a980c47a287d2822286 Mon Sep 17 00:00:00 2001 From: AhmetS Date: Fri, 21 Apr 2017 11:08:55 +0100 Subject: [PATCH 1/3] Set User Agent to rocketchatbot In doing so, If we want to share a single page application, we can include requests from `rocketchatbot` in our server rules to redirect to something like prerender.io version of the page or our own pre-rendering server. Example prerender.io config: ``` RewriteCond %{HTTP_USER_AGENT} rocketchatbot|baiduspider|facebookexternalhit|twitterbot|rogerbot|linkedinbot|embedly|quora\ link\ preview|showyoubot|outbrain|pinterest|slackbot|vkShare|W3C_Validator [NC,OR] RewriteCond %{QUERY_STRING} _escaped_fragment_ # Only proxy the request to Prerender if it's a request for HTML RewriteRule ^(?!.*?(\.js|\.css|\.xml|\.less|\.png|\.jpg|\.jpeg|\.gif|\.pdf|\.doc|\.txt|\.ico|\.rss|\.zip|\.mp3|\.rar|\.exe|\.wmv|\.doc|\.avi|\.ppt|\.mpg|\.mpeg|\.tif|\.wav|\.mov|\.psd|\.ai|\.xls|\.mp4|\.m4a|\.swf|\.dat|\.dmg|\.iso|\.flv|\.m4v|\.torrent|\.ttf|\.woff))(.*) http://service.prerender.io/http://example.com/$2 [P,L] ``` --- packages/rocketchat-oembed/server/server.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/rocketchat-oembed/server/server.coffee b/packages/rocketchat-oembed/server/server.coffee index 63ab940c3d55..aad2e125b9ea 100644 --- a/packages/rocketchat-oembed/server/server.coffee +++ b/packages/rocketchat-oembed/server/server.coffee @@ -70,7 +70,7 @@ getUrlContent = (urlObj, redirectCount = 5, callback) -> gzip: true maxRedirects: redirectCount headers: - 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.0 Safari/537.36' + 'User-Agent': 'rocketchatbot' headers = null statusCode = null From 33707e2064906ee19f4b50c2e2af2faa733ea7ee Mon Sep 17 00:00:00 2001 From: Rodrigo Nascimento Date: Thu, 27 Jul 2017 14:51:49 -0300 Subject: [PATCH 2/3] Revert changes --- packages/rocketchat-oembed/server/server.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/rocketchat-oembed/server/server.coffee b/packages/rocketchat-oembed/server/server.coffee index aad2e125b9ea..63ab940c3d55 100644 --- a/packages/rocketchat-oembed/server/server.coffee +++ b/packages/rocketchat-oembed/server/server.coffee @@ -70,7 +70,7 @@ getUrlContent = (urlObj, redirectCount = 5, callback) -> gzip: true maxRedirects: redirectCount headers: - 'User-Agent': 'rocketchatbot' + 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.0 Safari/537.36' headers = null statusCode = null From ffdcf7d2e3c72c29377af6b5cdb270d672c14106 Mon Sep 17 00:00:00 2001 From: Rodrigo Nascimento Date: Thu, 27 Jul 2017 15:02:37 -0300 Subject: [PATCH 3/3] Add setting to change User Agent of OEmbed calls --- packages/rocketchat-i18n/i18n/en.i18n.json | 1 + packages/rocketchat-lib/server/startup/settings.js | 4 ++++ packages/rocketchat-oembed/server/server.js | 2 +- 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/rocketchat-i18n/i18n/en.i18n.json b/packages/rocketchat-i18n/i18n/en.i18n.json index a1a84c8d3eb8..af7db25a466d 100644 --- a/packages/rocketchat-i18n/i18n/en.i18n.json +++ b/packages/rocketchat-i18n/i18n/en.i18n.json @@ -178,6 +178,7 @@ "API_Drupal_URL": "Drupal Server URL", "API_Drupal_URL_Description": "Example: https://domain.com (excluding trailing slash)", "API_Embed": "Embed Link Previews", + "API_Embed_UserAgent": "Embed request user agent", "API_Embed_Description": "Whether embedded link previews are enabled or not when a user posts a link to a website.", "API_EmbedCacheExpirationDays": "Embed cache expiration days", "API_EmbedDisabledFor": "Disable Embed for Users", diff --git a/packages/rocketchat-lib/server/startup/settings.js b/packages/rocketchat-lib/server/startup/settings.js index c775b011018f..54ded7361d97 100644 --- a/packages/rocketchat-lib/server/startup/settings.js +++ b/packages/rocketchat-lib/server/startup/settings.js @@ -789,6 +789,10 @@ RocketChat.settings.addGroup('Message', function() { type: 'boolean', 'public': true }); + this.add('API_Embed_UserAgent', 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.0 Safari/537.36', { + type: 'string', + 'public': true + }); this.add('API_EmbedCacheExpirationDays', 30, { type: 'int', 'public': false diff --git a/packages/rocketchat-oembed/server/server.js b/packages/rocketchat-oembed/server/server.js index 36085f1597f7..8a1e4ca789a6 100644 --- a/packages/rocketchat-oembed/server/server.js +++ b/packages/rocketchat-oembed/server/server.js @@ -88,7 +88,7 @@ const getUrlContent = function(urlObj, redirectCount = 5, callback) { gzip: true, maxRedirects: redirectCount, headers: { - 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.0 Safari/537.36' + 'User-Agent': RocketChat.settings.get('API_Embed_UserAgent') } }; let headers = null;