1
0
Fork 0
mirror of https://code.forgejo.org/forgejo/runner.git synced 2025-08-11 17:50:58 +00:00

implement hmac generation

This commit is contained in:
Kwonunn 2024-10-24 16:24:28 +02:00 committed by Kwonunn
parent 8bc36fb69a
commit 11006f4ef3

View file

@ -1,6 +1,9 @@
package cacheproxy package cacheproxy
import ( import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
@ -35,9 +38,10 @@ type Handler struct {
cacheServerPort uint16 cacheServerPort uint16
repositoryName string repositoryName string
cacheKey string
} }
func StartHandler(repoName string, targetHost string, targetPort uint16, outboundIP string, port uint16, logger logrus.FieldLogger) (*Handler, error) { func StartHandler(repoName string, targetHost string, targetPort uint16, outboundIP string, port uint16, cacheKey string, logger logrus.FieldLogger) (*Handler, error) {
h := &Handler{} h := &Handler{}
if logger == nil { if logger == nil {
@ -49,6 +53,7 @@ func StartHandler(repoName string, targetHost string, targetPort uint16, outboun
h.logger = logger h.logger = logger
h.repositoryName = repoName h.repositoryName = repoName
h.cacheKey = h.cacheKey
if outboundIP != "" { if outboundIP != "" {
h.outboundIP = outboundIP h.outboundIP = outboundIP
@ -119,6 +124,13 @@ func (h *Handler) Close() error {
return retErr return retErr
} }
func (h *Handler) calculateMAC() string {
mac := hmac.New(sha256.New, []byte(h.cacheKey))
mac.Write([]byte(h.repositoryName))
macBytes := mac.Sum(nil)
return hex.EncodeToString(macBytes)
}
// GET /_apis/artifactcache/cache // GET /_apis/artifactcache/cache
func (h *Handler) find(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { func (h *Handler) find(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
keys := strings.Split(r.URL.Query().Get("keys"), ",") keys := strings.Split(r.URL.Query().Get("keys"), ",")