diff --git a/common/json.go b/common/json.go index a65da462e..54f8baa34 100644 --- a/common/json.go +++ b/common/json.go @@ -23,11 +23,11 @@ func Marshal(v any) ([]byte, error) { } func GetJsonType(data json.RawMessage) string { - data = bytes.TrimSpace(data) - if len(data) == 0 { + trimmed := bytes.TrimSpace(data) + if len(trimmed) == 0 { return "unknown" } - firstChar := bytes.TrimSpace(data)[0] + firstChar := trimmed[0] switch firstChar { case '{': return "object" diff --git a/common/str.go b/common/str.go index 6debce28b..a5ac5d447 100644 --- a/common/str.go +++ b/common/str.go @@ -3,12 +3,19 @@ package common import ( "encoding/base64" "encoding/json" - "math/rand" "net/url" "regexp" "strconv" "strings" "unsafe" + + "github.com/samber/lo" +) + +var ( + maskURLPattern = regexp.MustCompile(`(http|https)://[^\s/$.?#].[^\s]*`) + maskDomainPattern = regexp.MustCompile(`\b(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}\b`) + maskIPPattern = regexp.MustCompile(`\b(?:\d{1,3}\.){3}\d{1,3}\b`) ) func GetStringIfEmpty(str string, defaultValue string) string { @@ -19,12 +26,10 @@ func GetStringIfEmpty(str string, defaultValue string) string { } func GetRandomString(length int) string { - //rand.Seed(time.Now().UnixNano()) - key := make([]byte, length) - for i := 0; i < length; i++ { - key[i] = keyChars[rand.Intn(len(keyChars))] + if length <= 0 { + return "" } - return string(key) + return lo.RandomString(length, lo.AlphanumericCharset) } func MapToJsonStr(m map[string]interface{}) string { @@ -170,8 +175,7 @@ func maskHostForPlainDomain(domain string) string { // api.openai.com -> ***.***.com func MaskSensitiveInfo(str string) string { // Mask URLs - urlPattern := regexp.MustCompile(`(http|https)://[^\s/$.?#].[^\s]*`) - str = urlPattern.ReplaceAllStringFunc(str, func(urlStr string) string { + str = maskURLPattern.ReplaceAllStringFunc(str, func(urlStr string) string { u, err := url.Parse(urlStr) if err != nil { return urlStr @@ -224,14 +228,12 @@ func MaskSensitiveInfo(str string) string { }) // Mask domain names without protocol (like openai.com, www.openai.com) - domainPattern := regexp.MustCompile(`\b(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}\b`) - str = domainPattern.ReplaceAllStringFunc(str, func(domain string) string { + str = maskDomainPattern.ReplaceAllStringFunc(str, func(domain string) string { return maskHostForPlainDomain(domain) }) // Mask IP addresses - ipPattern := regexp.MustCompile(`\b(?:\d{1,3}\.){3}\d{1,3}\b`) - str = ipPattern.ReplaceAllString(str, "***.***.***.***") + str = maskIPPattern.ReplaceAllString(str, "***.***.***.***") return str } diff --git a/go.mod b/go.mod index 11a846e2f..87af3c22e 100644 --- a/go.mod +++ b/go.mod @@ -33,7 +33,7 @@ require ( github.com/mewkiz/flac v1.0.13 github.com/pkg/errors v0.9.1 github.com/pquerna/otp v1.5.0 - github.com/samber/lo v1.39.0 + github.com/samber/lo v1.52.0 github.com/shirou/gopsutil v3.21.11+incompatible github.com/shopspring/decimal v1.4.0 github.com/stripe/stripe-go/v81 v81.4.0 diff --git a/go.sum b/go.sum index a39ab97ce..1138c747a 100644 --- a/go.sum +++ b/go.sum @@ -222,6 +222,8 @@ github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUA github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE= github.com/samber/lo v1.39.0 h1:4gTz1wUhNYLhFSKl6O+8peW0v2F4BCY034GRpU9WnuA= github.com/samber/lo v1.39.0/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA= +github.com/samber/lo v1.52.0 h1:Rvi+3BFHES3A8meP33VPAxiBZX/Aws5RxrschYGjomw= +github.com/samber/lo v1.52.0/go.mod h1:4+MXEGsJzbKGaUEQFKBq2xtfuznW9oz/WrgyzMzRoM0= github.com/shirou/gopsutil v3.21.11+incompatible h1:+1+c1VGhc88SSonWP6foOcLhvnKlUeu/erjjvaPEYiI= github.com/shirou/gopsutil v3.21.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA= github.com/shopspring/decimal v1.4.0 h1:bxl37RwXBklmTi0C79JfXCEBD1cqqHt0bbgBAGFp81k= diff --git a/relay/common/override.go b/relay/common/override.go index 1d0794d26..3850218c3 100644 --- a/relay/common/override.go +++ b/relay/common/override.go @@ -11,6 +11,8 @@ import ( "github.com/tidwall/sjson" ) +var negativeIndexRegexp = regexp.MustCompile(`\.(-\d+)`) + type ConditionOperation struct { Path string `json:"path"` // JSON路径 Mode string `json:"mode"` // full, prefix, suffix, contains, gt, gte, lt, lte @@ -186,8 +188,7 @@ func checkSingleCondition(jsonStr, contextJSON string, condition ConditionOperat } func processNegativeIndex(jsonStr string, path string) string { - re := regexp.MustCompile(`\.(-\d+)`) - matches := re.FindAllStringSubmatch(path, -1) + matches := negativeIndexRegexp.FindAllStringSubmatch(path, -1) if len(matches) == 0 { return path