Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
new-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
1dd8c0f5
authored
Dec 03, 2025
by
Seefs
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: regex repeat compile
parent
64bf6d1e
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
23 additions
and
18 deletions
+23
-18
common/json.go
+3
-3
common/str.go
+14
-12
go.mod
+1
-1
go.sum
+2
-0
relay/common/override.go
+3
-2
No files found.
common/json.go
View file @
1dd8c0f5
...
...
@@ -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"
...
...
common/str.go
View file @
1dd8c0f5
...
...
@@ -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
}
go.mod
View file @
1dd8c0f5
...
...
@@ -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
...
...
go.sum
View file @
1dd8c0f5
...
...
@@ -219,6 +219,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=
...
...
relay/common/override.go
View file @
1dd8c0f5
...
...
@@ -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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment