mirror of
https://github.com/go-gitea/gitea.git
synced 2026-11-11 19:39:02 +00:00
Pure refactors with no behavior change, mostly reusing existing helpers and dropping duplicated or dead code. Net −222 lines. - Reuse existing helpers instead of inlined copies: `getIssueIndex`, `NewLabel`, `ToCommitUser`, `GetPackageFileDescriptors`, `util.SplitTrimSpace` - Merge copy-pasted blocks in review comment deletion, project deletion and the pre/post-receive hook templates - Drop dead code: an error `toStringKeys` never returned, impossible `bytes.Buffer` write errors, redundant nil-map branches, a `base64` wrapper - Use stdlib where it fits: `strings.Cut`, `bytes.IndexAny`, `cmp.Compare`, `min`/`max`, `slices.Concat`, map lookups instead of key scans
42 lines
1.3 KiB
Go
42 lines
1.3 KiB
Go
// Copyright 2022 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package markup
|
|
|
|
import (
|
|
"crypto/hmac"
|
|
"crypto/sha1"
|
|
"encoding/base64"
|
|
"net/url"
|
|
"strings"
|
|
|
|
"gitea.dev/modules/setting"
|
|
)
|
|
|
|
// CamoEncode encodes a lnk to fit with the go-camo and camo proxy links. The purposes of camo-proxy are:
|
|
// 1. Allow accessing "http://" images on a HTTPS site by using the "https://" URLs provided by camo-proxy.
|
|
// 2. Hide the visitor's real IP (protect privacy) when accessing external images.
|
|
func CamoEncode(link string) string {
|
|
if strings.HasPrefix(link, setting.Camo.ServerURL) {
|
|
return link
|
|
}
|
|
|
|
mac := hmac.New(sha1.New, []byte(setting.Camo.HMACKey))
|
|
_, _ = mac.Write([]byte(link)) // hmac does not return errors
|
|
macSum := base64.RawURLEncoding.EncodeToString(mac.Sum(nil))
|
|
encodedURL := base64.RawURLEncoding.EncodeToString([]byte(link))
|
|
|
|
return strings.TrimSuffix(setting.Camo.ServerURL, "/") + "/" + macSum + "/" + encodedURL
|
|
}
|
|
|
|
func camoHandleLink(link string) string {
|
|
if setting.Camo.Enabled {
|
|
lnkURL, err := url.Parse(link)
|
|
if err == nil && (lnkURL.Scheme == "http" || lnkURL.Scheme == "https") && !strings.HasPrefix(link, setting.AppURL) &&
|
|
(setting.Camo.Always || lnkURL.Scheme != "https") {
|
|
return CamoEncode(link)
|
|
}
|
|
}
|
|
return link
|
|
}
|