From 4063f407a1f941e741b9cfe533fa64944799d1ae Mon Sep 17 00:00:00 2001 From: Ben Schumacher Date: Tue, 17 Nov 2020 14:48:51 +0100 Subject: [PATCH] Use different user-agent for twitter links (#16210) --- app/post_metadata.go | 6 ++++++ services/httpservice/client.go | 2 +- services/httpservice/httpservice.go | 4 ++-- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/app/post_metadata.go b/app/post_metadata.go index bd59f5ed91..f6b1550d45 100644 --- a/app/post_metadata.go +++ b/app/post_metadata.go @@ -410,6 +410,12 @@ func (a *App) getLinkMetadata(requestURL string, timestamp int64, isNewPost bool client := a.HTTPService().MakeClient(false) client.Timeout = time.Duration(*a.Config().ExperimentalSettings.LinkMetadataTimeoutMilliseconds) * time.Millisecond + mmTransport := a.HTTPService().MakeTransport(false) + client.Transport = mmTransport.Transport + + if strings.HasPrefix(requestURL, "https://twitter.com/") || strings.HasPrefix(requestURL, "https://mobile.twitter.com/") { + request.Header.Add("User-Agent", "facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)") + } var res *http.Response res, err = client.Do(request) diff --git a/services/httpservice/client.go b/services/httpservice/client.go index 5d89c93664..d24550e74f 100644 --- a/services/httpservice/client.go +++ b/services/httpservice/client.go @@ -136,7 +136,7 @@ func dialContextFilter(dial DialContextFunction, allowHost func(host string) boo } } -func NewTransport(enableInsecureConnections bool, allowHost func(host string) bool, allowIP func(ip net.IP) bool) http.RoundTripper { +func NewTransport(enableInsecureConnections bool, allowHost func(host string) bool, allowIP func(ip net.IP) bool) *MattermostTransport { dialContext := (&net.Dialer{ Timeout: ConnectTimeout, KeepAlive: 30 * time.Second, diff --git a/services/httpservice/httpservice.go b/services/httpservice/httpservice.go index 375b0a6a65..8aad6e0c97 100644 --- a/services/httpservice/httpservice.go +++ b/services/httpservice/httpservice.go @@ -25,7 +25,7 @@ type HTTPService interface { // - A timeout for end-to-end requests // - A Mattermost-specific user agent header // - Additional security for untrusted and insecure connections - MakeTransport(trustURLs bool) http.RoundTripper + MakeTransport(trustURLs bool) *MattermostTransport } type HTTPServiceImpl struct { @@ -52,7 +52,7 @@ func (h *HTTPServiceImpl) MakeClient(trustURLs bool) *http.Client { } } -func (h *HTTPServiceImpl) MakeTransport(trustURLs bool) http.RoundTripper { +func (h *HTTPServiceImpl) MakeTransport(trustURLs bool) *MattermostTransport { insecure := h.configService.Config().ServiceSettings.EnableInsecureOutgoingConnections != nil && *h.configService.Config().ServiceSettings.EnableInsecureOutgoingConnections if trustURLs {