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
3676b9f2
authored
Dec 09, 2025
by
Calcium-Ion
Committed by
GitHub
Dec 09, 2025
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2358 from seefs001/fix/regrex-repeat-compile
fix: regex repeat compile
parents
6ade0b82
1dd8c0f5
Show 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 @
3676b9f2
...
@@ -23,11 +23,11 @@ func Marshal(v any) ([]byte, error) {
...
@@ -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"
...
...
common/str.go
View file @
3676b9f2
...
@@ -3,12 +3,19 @@ package common
...
@@ -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 {
...
@@ -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 {
...
@@ -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 {
...
@@ -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
}
}
go.mod
View file @
3676b9f2
...
@@ -33,7 +33,7 @@ require (
...
@@ -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
...
...
go.sum
View file @
3676b9f2
...
@@ -222,6 +222,8 @@ github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUA
...
@@ -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=
...
...
relay/common/override.go
View file @
3676b9f2
...
@@ -11,6 +11,8 @@ import (
...
@@ -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
...
@@ -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
...
...
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