pkg: use modern Go stdlib functions to simplify code (#5209)

- strings.CutPrefix instead of HasPrefix+TrimPrefix (naming, legacy)
- slices.Contains instead of manual loop (plugin/server)
- min/max builtins instead of manual comparisons (nathole)
This commit is contained in:
fatedier
2026-03-06 22:14:46 +08:00
committed by GitHub
parent d644593342
commit e9f7a1a9f2
5 changed files with 13 additions and 23 deletions

View File

@@ -171,15 +171,14 @@ func Convert_ServerCommonConf_To_v1(conf *ServerCommonConf) *v1.ServerConfig {
func transformHeadersFromPluginParams(params map[string]string) v1.HeaderOperations { func transformHeadersFromPluginParams(params map[string]string) v1.HeaderOperations {
out := v1.HeaderOperations{} out := v1.HeaderOperations{}
for k, v := range params { for k, v := range params {
if !strings.HasPrefix(k, "plugin_header_") { k, ok := strings.CutPrefix(k, "plugin_header_")
if !ok || k == "" {
continue continue
} }
if k = strings.TrimPrefix(k, "plugin_header_"); k != "" { if out.Set == nil {
if out.Set == nil { out.Set = make(map[string]string)
out.Set = make(map[string]string)
}
out.Set[k] = v
} }
out.Set[k] = v
} }
return out return out
} }

View File

@@ -22,8 +22,8 @@ func GetMapWithoutPrefix(set map[string]string, prefix string) map[string]string
m := make(map[string]string) m := make(map[string]string)
for key, value := range set { for key, value := range set {
if strings.HasPrefix(key, prefix) { if trimmed, ok := strings.CutPrefix(key, prefix); ok {
m[strings.TrimPrefix(key, prefix)] = value m[trimmed] = value
} }
} }

View File

@@ -16,9 +16,8 @@ func StripUserPrefix(user, name string) string {
if user == "" { if user == "" {
return name return name
} }
prefix := user + "." if trimmed, ok := strings.CutPrefix(name, user+"."); ok {
if strings.HasPrefix(name, prefix) { return trimmed
return strings.TrimPrefix(name, prefix)
} }
return name return name
} }

View File

@@ -70,12 +70,8 @@ func ClassifyNATFeature(addresses []string, localIPs []string) (*NatFeature, err
continue continue
} }
if portNum > portMax { portMax = max(portMax, portNum)
portMax = portNum portMin = min(portMin, portNum)
}
if portNum < portMin {
portMin = portNum
}
if baseIP != ip { if baseIP != ip {
ipChanged = true ipChanged = true
} }

View File

@@ -24,6 +24,7 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"reflect" "reflect"
"slices"
"strings" "strings"
v1 "github.com/fatedier/frp/pkg/config/v1" v1 "github.com/fatedier/frp/pkg/config/v1"
@@ -64,12 +65,7 @@ func (p *httpPlugin) Name() string {
} }
func (p *httpPlugin) IsSupport(op string) bool { func (p *httpPlugin) IsSupport(op string) bool {
for _, v := range p.options.Ops { return slices.Contains(p.options.Ops, op)
if v == op {
return true
}
}
return false
} }
func (p *httpPlugin) Handle(ctx context.Context, op string, content any) (*Response, any, error) { func (p *httpPlugin) Handle(ctx context.Context, op string, content any) (*Response, any, error) {