2023-06-19 23:42:47 +02:00
|
|
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2020-10-08 07:21:45 +02:00
|
|
|
|
2023-08-11 04:46:45 +02:00
|
|
|
package proxy // import "miniflux.app/v2/internal/proxy"
|
2020-10-08 07:21:45 +02:00
|
|
|
|
|
|
|
import (
|
2022-10-15 08:17:17 +02:00
|
|
|
"crypto/hmac"
|
|
|
|
"crypto/sha256"
|
2020-10-08 07:21:45 +02:00
|
|
|
"encoding/base64"
|
2022-08-30 05:33:47 +02:00
|
|
|
"net/url"
|
|
|
|
"path"
|
2020-10-08 07:21:45 +02:00
|
|
|
|
2023-08-11 04:46:45 +02:00
|
|
|
"miniflux.app/v2/internal/http/route"
|
2020-10-08 07:21:45 +02:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2022-08-30 05:33:47 +02:00
|
|
|
|
2023-08-11 04:46:45 +02:00
|
|
|
"miniflux.app/v2/internal/config"
|
2020-10-08 07:21:45 +02:00
|
|
|
)
|
|
|
|
|
2022-10-15 08:17:17 +02:00
|
|
|
// ProxifyURL generates a relative URL for a proxified resource.
|
2020-10-08 07:21:45 +02:00
|
|
|
func ProxifyURL(router *mux.Router, link string) string {
|
2022-07-06 06:13:40 +02:00
|
|
|
if link != "" {
|
2023-02-25 09:36:19 +01:00
|
|
|
proxyImageUrl := config.Opts.ProxyUrl()
|
2022-08-30 05:33:47 +02:00
|
|
|
|
|
|
|
if proxyImageUrl == "" {
|
2022-10-15 08:17:17 +02:00
|
|
|
mac := hmac.New(sha256.New, config.Opts.ProxyPrivateKey())
|
|
|
|
mac.Write([]byte(link))
|
|
|
|
digest := mac.Sum(nil)
|
|
|
|
return route.Path(router, "proxy", "encodedDigest", base64.URLEncoding.EncodeToString(digest), "encodedURL", base64.URLEncoding.EncodeToString([]byte(link)))
|
|
|
|
}
|
|
|
|
|
|
|
|
proxyUrl, err := url.Parse(proxyImageUrl)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
proxyUrl.Path = path.Join(proxyUrl.Path, base64.URLEncoding.EncodeToString([]byte(link)))
|
|
|
|
return proxyUrl.String()
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// AbsoluteProxifyURL generates an absolute URL for a proxified resource.
|
|
|
|
func AbsoluteProxifyURL(router *mux.Router, host, link string) string {
|
|
|
|
if link != "" {
|
2023-02-25 09:36:19 +01:00
|
|
|
proxyImageUrl := config.Opts.ProxyUrl()
|
2022-10-15 08:17:17 +02:00
|
|
|
|
|
|
|
if proxyImageUrl == "" {
|
|
|
|
mac := hmac.New(sha256.New, config.Opts.ProxyPrivateKey())
|
|
|
|
mac.Write([]byte(link))
|
|
|
|
digest := mac.Sum(nil)
|
|
|
|
path := route.Path(router, "proxy", "encodedDigest", base64.URLEncoding.EncodeToString(digest), "encodedURL", base64.URLEncoding.EncodeToString([]byte(link)))
|
|
|
|
if config.Opts.HTTPS {
|
|
|
|
return "https://" + host + path
|
|
|
|
} else {
|
|
|
|
return "http://" + host + path
|
|
|
|
}
|
2022-08-30 05:33:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
proxyUrl, err := url.Parse(proxyImageUrl)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
proxyUrl.Path = path.Join(proxyUrl.Path, base64.URLEncoding.EncodeToString([]byte(link)))
|
|
|
|
return proxyUrl.String()
|
2022-07-06 06:13:40 +02:00
|
|
|
}
|
|
|
|
return ""
|
2020-10-08 07:21:45 +02:00
|
|
|
}
|