Merge pull request #2358 from seefs001/fix/regrex-repeat-compile

fix: regex repeat compile
This commit is contained in:
Calcium-Ion
2025-12-09 14:01:07 +08:00
committed by GitHub
5 changed files with 23 additions and 18 deletions

View File

@@ -23,11 +23,11 @@ func Marshal(v any) ([]byte, error) {
} }
func GetJsonType(data json.RawMessage) string { func GetJsonType(data json.RawMessage) string {
data = bytes.TrimSpace(data) trimmed := bytes.TrimSpace(data)
if len(data) == 0 { if len(trimmed) == 0 {
return "unknown" return "unknown"
} }
firstChar := bytes.TrimSpace(data)[0] firstChar := trimmed[0]
switch firstChar { switch firstChar {
case '{': case '{':
return "object" return "object"

View File

@@ -3,12 +3,19 @@ package common
import ( import (
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
"math/rand"
"net/url" "net/url"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
"unsafe" "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 { func GetStringIfEmpty(str string, defaultValue string) string {
@@ -19,12 +26,10 @@ func GetStringIfEmpty(str string, defaultValue string) string {
} }
func GetRandomString(length int) string { func GetRandomString(length int) string {
//rand.Seed(time.Now().UnixNano()) if length <= 0 {
key := make([]byte, length) return ""
for i := 0; i < length; i++ {
key[i] = keyChars[rand.Intn(len(keyChars))]
} }
return string(key) return lo.RandomString(length, lo.AlphanumericCharset)
} }
func MapToJsonStr(m map[string]interface{}) string { func MapToJsonStr(m map[string]interface{}) string {
@@ -170,8 +175,7 @@ func maskHostForPlainDomain(domain string) string {
// api.openai.com -> ***.***.com // api.openai.com -> ***.***.com
func MaskSensitiveInfo(str string) string { func MaskSensitiveInfo(str string) string {
// Mask URLs // Mask URLs
urlPattern := regexp.MustCompile(`(http|https)://[^\s/$.?#].[^\s]*`) str = maskURLPattern.ReplaceAllStringFunc(str, func(urlStr string) string {
str = urlPattern.ReplaceAllStringFunc(str, func(urlStr string) string {
u, err := url.Parse(urlStr) u, err := url.Parse(urlStr)
if err != nil { if err != nil {
return urlStr return urlStr
@@ -224,14 +228,12 @@ func MaskSensitiveInfo(str string) string {
}) })
// Mask domain names without protocol (like openai.com, www.openai.com) // 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 = maskDomainPattern.ReplaceAllStringFunc(str, func(domain string) string {
str = domainPattern.ReplaceAllStringFunc(str, func(domain string) string {
return maskHostForPlainDomain(domain) return maskHostForPlainDomain(domain)
}) })
// Mask IP addresses // Mask IP addresses
ipPattern := regexp.MustCompile(`\b(?:\d{1,3}\.){3}\d{1,3}\b`) str = maskIPPattern.ReplaceAllString(str, "***.***.***.***")
str = ipPattern.ReplaceAllString(str, "***.***.***.***")
return str return str
} }

2
go.mod
View File

@@ -33,7 +33,7 @@ require (
github.com/mewkiz/flac v1.0.13 github.com/mewkiz/flac v1.0.13
github.com/pkg/errors v0.9.1 github.com/pkg/errors v0.9.1
github.com/pquerna/otp v1.5.0 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/shirou/gopsutil v3.21.11+incompatible
github.com/shopspring/decimal v1.4.0 github.com/shopspring/decimal v1.4.0
github.com/stripe/stripe-go/v81 v81.4.0 github.com/stripe/stripe-go/v81 v81.4.0

2
go.sum
View File

@@ -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/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 h1:4gTz1wUhNYLhFSKl6O+8peW0v2F4BCY034GRpU9WnuA=
github.com/samber/lo v1.39.0/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA= 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 h1:+1+c1VGhc88SSonWP6foOcLhvnKlUeu/erjjvaPEYiI=
github.com/shirou/gopsutil v3.21.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA= github.com/shirou/gopsutil v3.21.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
github.com/shopspring/decimal v1.4.0 h1:bxl37RwXBklmTi0C79JfXCEBD1cqqHt0bbgBAGFp81k= github.com/shopspring/decimal v1.4.0 h1:bxl37RwXBklmTi0C79JfXCEBD1cqqHt0bbgBAGFp81k=

View File

@@ -11,6 +11,8 @@ import (
"github.com/tidwall/sjson" "github.com/tidwall/sjson"
) )
var negativeIndexRegexp = regexp.MustCompile(`\.(-\d+)`)
type ConditionOperation struct { type ConditionOperation struct {
Path string `json:"path"` // JSON路径 Path string `json:"path"` // JSON路径
Mode string `json:"mode"` // full, prefix, suffix, contains, gt, gte, lt, lte 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 { func processNegativeIndex(jsonStr string, path string) string {
re := regexp.MustCompile(`\.(-\d+)`) matches := negativeIndexRegexp.FindAllStringSubmatch(path, -1)
matches := re.FindAllStringSubmatch(path, -1)
if len(matches) == 0 { if len(matches) == 0 {
return path return path