Use efficient APIs and simplify loops

Minor refactors across the codebase to improve readability and use more efficient APIs: replace fmt.Sprintf+base64 encoding with fmt.Appendf when building Shadowsocks userInfo; compute elapsed using max(now-prev.at, window) to simplify logic; use strings.SplitSeq for splitting in two places; simplify test and goroutine loops to range-based iterations and use errgroup's Go helper; and align/clean up struct field formatting and test map literals. Mostly stylistic/efficiency changes with no intended behavior changes.
This commit is contained in:
MHSanaei
2026-06-23 14:12:28 +02:00
parent 852b53db79
commit 1c0b76c27a
8 changed files with 62 additions and 69 deletions
+29 -29
View File
@@ -320,33 +320,33 @@ func TestBuildProxy_VLESSNoneEncryptionOmittedForClash(t *testing.T) {
func TestBuildXhttpClashOpts_FullFieldMapping(t *testing.T) {
xhttp := map[string]any{
"path": "/api/v1",
"mode": "stream-up",
"host": "example.com",
"xPaddingBytes": "100-1000",
"xPaddingObfsMode": true,
"xPaddingKey": "mykey",
"xPaddingHeader": "X-Trace-ID",
"xPaddingPlacement": "queryInHeader",
"xPaddingMethod": "tokenish",
"uplinkHTTPMethod": "POST",
"sessionPlacement": "query",
"sessionKey": "sess",
"seqPlacement": "header",
"seqKey": "seq",
"uplinkDataPlacement": "body",
"uplinkDataKey": "udata",
"uplinkChunkSize": "64-256",
"noGRPCHeader": true,
"scMaxEachPostBytes": "500000",
"path": "/api/v1",
"mode": "stream-up",
"host": "example.com",
"xPaddingBytes": "100-1000",
"xPaddingObfsMode": true,
"xPaddingKey": "mykey",
"xPaddingHeader": "X-Trace-ID",
"xPaddingPlacement": "queryInHeader",
"xPaddingMethod": "tokenish",
"uplinkHTTPMethod": "POST",
"sessionPlacement": "query",
"sessionKey": "sess",
"seqPlacement": "header",
"seqKey": "seq",
"uplinkDataPlacement": "body",
"uplinkDataKey": "udata",
"uplinkChunkSize": "64-256",
"noGRPCHeader": true,
"scMaxEachPostBytes": "500000",
"scMinPostsIntervalMs": "50",
"xmux": map[string]any{
"maxConcurrency": "16-32",
"maxConnections": "4",
"cMaxReuseTimes": "8",
"hMaxRequestTimes": "600-900",
"hMaxReusableSecs": "1800-3000",
"hKeepAlivePeriod": float64(60),
"hMaxRequestTimes": "600-900",
"hMaxReusableSecs": "1800-3000",
"hKeepAlivePeriod": float64(60),
},
"headers": map[string]any{
"User-Agent": "chrome",
@@ -473,8 +473,8 @@ func TestBuildXhttpClashOpts_FullFieldMapping(t *testing.T) {
func TestBuildXhttpClashOpts_DPIDefaultsFiltered(t *testing.T) {
xhttp := map[string]any{
"path": "/",
"mode": "stream-up",
"path": "/",
"mode": "stream-up",
"scMaxEachPostBytes": "1000000",
"scMinPostsIntervalMs": "30",
}
@@ -494,9 +494,9 @@ func TestBuildXhttpClashOpts_PaddingObfsGate(t *testing.T) {
// Sub-test 1: obfs mode false — gated fields should not appear
t.Run("ObfsModeFalse", func(t *testing.T) {
xhttp := map[string]any{
"path": "/",
"path": "/",
"xPaddingObfsMode": false,
"xPaddingKey": "should-not-appear",
"xPaddingKey": "should-not-appear",
}
opts := buildXhttpClashOpts(xhttp)
if opts == nil {
@@ -553,9 +553,9 @@ func TestBuildXhttpClashOpts_XmuxMapsToReuseSettings(t *testing.T) {
"maxConcurrency": "16-32",
"maxConnections": "4",
"cMaxReuseTimes": "8",
"hMaxRequestTimes": "600-900",
"hMaxReusableSecs": "1800-3000",
"hKeepAlivePeriod": float64(60),
"hMaxRequestTimes": "600-900",
"hMaxReusableSecs": "1800-3000",
"hKeepAlivePeriod": float64(60),
},
}
opts := buildXhttpClashOpts(xhttp)
+1 -1
View File
@@ -747,7 +747,7 @@ func (s *SubService) genShadowsocksLink(inbound *model.Inbound, email string) st
url.QueryEscape(inboundPassword),
url.QueryEscape(clients[clientIndex].Password))
} else {
userInfo = base64.RawURLEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", method, clients[clientIndex].Password)))
userInfo = base64.RawURLEncoding.EncodeToString(fmt.Appendf(nil, "%s:%s", method, clients[clientIndex].Password))
}
externalProxies, _ := stream["externalProxy"].([]any)