MM-10417 Add local image proxy and enable by default (#9967)

* MM-10417 Add local image proxy and enable by default

* Remove unused function

* Add dependencies for willnorris/imageproxy

* Fixed compilation errors

* Lock to the master version of willnorris/imageproxy

* Fix atmos/camo proxy when no SiteURL is specified

* Re-add default values for deprecated settings

* Fix unit tests added by merge

* Pass imageproxy to App struct

* Remove unneeded locking when creating the image proxy

* Remove empty test file
Этот коммит содержится в:
Harrison Healey
2019-01-24 16:11:32 -04:00
коммит произвёл GitHub
родитель e961b4cd0d
Коммит ba5566d1a0
80 изменённых файлов: 19742 добавлений и 177 удалений

70
services/imageproxy/atmos_camo.go Обычный файл
Просмотреть файл

@@ -0,0 +1,70 @@
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package imageproxy
import (
"crypto/hmac"
"crypto/sha1"
"encoding/hex"
"net/http"
"strings"
)
type AtmosCamoBackend struct {
proxy *ImageProxy
}
func makeAtmosCamoBackend(proxy *ImageProxy) *AtmosCamoBackend {
return &AtmosCamoBackend{
proxy: proxy,
}
}
func (backend *AtmosCamoBackend) GetImage(w http.ResponseWriter, r *http.Request, imageURL string) {
http.Redirect(w, r, backend.GetProxiedImageURL(imageURL), http.StatusFound)
}
func (backend *AtmosCamoBackend) GetProxiedImageURL(imageURL string) string {
cfg := *backend.proxy.ConfigService.Config()
siteURL := *cfg.ServiceSettings.SiteURL
proxyURL := *cfg.ImageProxySettings.RemoteImageProxyURL
options := *cfg.ImageProxySettings.RemoteImageProxyOptions
return getAtmosCamoImageURL(imageURL, siteURL, proxyURL, options)
}
func getAtmosCamoImageURL(imageURL, siteURL, proxyURL, options string) string {
// Don't proxy blank images, relative URLs, absolute URLs on this server, or URLs that are already going through the proxy
if imageURL == "" || imageURL[0] == '/' || (siteURL != "" && strings.HasPrefix(imageURL, siteURL)) || strings.HasPrefix(imageURL, proxyURL) {
return imageURL
}
mac := hmac.New(sha1.New, []byte(options))
mac.Write([]byte(imageURL))
digest := hex.EncodeToString(mac.Sum(nil))
return proxyURL + "/" + digest + "/" + hex.EncodeToString([]byte(imageURL))
}
func (backend *AtmosCamoBackend) GetUnproxiedImageURL(proxiedURL string) string {
proxyURL := *backend.proxy.ConfigService.Config().ImageProxySettings.RemoteImageProxyURL + "/"
if !strings.HasPrefix(proxiedURL, proxyURL) {
return proxiedURL
}
path := proxiedURL[len(proxyURL):]
slash := strings.IndexByte(path, '/')
if slash == -1 {
return proxiedURL
}
decoded, err := hex.DecodeString(path[slash+1:])
if err != nil {
return proxiedURL
}
return string(decoded)
}