refact: extract helpers and named constants to reduce duplication
- Extract setClientIPLocked() in lease.go to deduplicate the TrimSpace + RegisterIdentityIP pattern across Register, Renew, Touch - Extract applyOptionalPolicy() in admin.go to unify the identical UDP/TCP policy restore switch patterns (16 lines to 2 lines) - Replace magic number 1<<16 with named adminBodyLimit constant
cognitive committed
Apr 4, 2026 at 10:53 UTC
21fd6faf2f907e5fa2d3c9bc01c2763e9a8562b7
2 files changed
+36
-26
cmd/relay-server/admin.go
+25
-21
@@ -16,7 +16,10 @@ import (
16
"github.com/gosuda/portal/v2/utils"
17
)
18
19
-const cookieName = "portal_admin"
19
+const (
20
+ cookieName = "portal_admin"
21
+ adminBodyLimit = 1 << 16
22
+)
23
24
type adminAuth struct {
25
sessions map[string]time.Time
@@ -195,7 +198,7 @@ func (f *Frontend) serveAdmin(w http.ResponseWriter, r *http.Request) {
198
if !utils.RequireMethod(w, r, http.MethodPost) {
199
return
200
}
198
- req, ok := utils.DecodeJSONRequestAs[types.AdminLandingPageSettingsRequest](w, r, 1<<16, invalidRequestBody)
201
+ req, ok := utils.DecodeJSONRequestAs[types.AdminLandingPageSettingsRequest](w, r, adminBodyLimit, invalidRequestBody)
202
if !ok {
203
return
204
}
@@ -222,7 +225,7 @@ func (f *Frontend) serveAdmin(w http.ResponseWriter, r *http.Request) {
225
if !utils.RequireMethod(w, r, http.MethodPost) {
226
return
227
}
225
- req, ok := utils.DecodeJSONRequestAs[types.AdminApprovalModeRequest](w, r, 1<<16, invalidRequestBody)
228
+ req, ok := utils.DecodeJSONRequestAs[types.AdminApprovalModeRequest](w, r, adminBodyLimit, invalidRequestBody)
229
if !ok {
230
return
231
}
@@ -276,7 +279,7 @@ func (f *Frontend) serveAdmin(w http.ResponseWriter, r *http.Request) {
279
},
280
"bps": {
281
post: func() bool {
279
- req, ok := utils.DecodeJSONRequestAs[types.AdminBPSRequest](w, r, 1<<16, invalidRequestBody)
282
+ req, ok := utils.DecodeJSONRequestAs[types.AdminBPSRequest](w, r, adminBodyLimit, invalidRequestBody)
283
if !ok {
284
return true
285
}
@@ -386,7 +389,7 @@ func (f *Frontend) handleLogin(w http.ResponseWriter, r *http.Request) {
389
return
390
}
391
389
- req, ok := utils.DecodeJSONRequestAs[types.AdminLoginRequest](w, r, 1<<16, utils.InvalidRequestError(errors.New("invalid request body")))
392
+ req, ok := utils.DecodeJSONRequestAs[types.AdminLoginRequest](w, r, adminBodyLimit, utils.InvalidRequestError(errors.New("invalid request body")))
393
if !ok {
394
return
395
}
@@ -464,6 +467,21 @@ type persistedAdminState struct {
467
LandingPageEnabled *bool `json:"landing_page_enabled,omitempty"`
468
}
469
470
+func applyOptionalPolicy(enabled *bool, maxLeases *int, getEnabled func() bool, getMax func() int, set func(bool, int)) {
471
+ if enabled == nil && maxLeases == nil {
472
+ return
473
+ }
474
+ e := getEnabled()
475
+ m := getMax()
476
+ if enabled != nil {
477
+ e = *enabled
478
+ }
479
+ if maxLeases != nil {
480
+ m = *maxLeases
481
+ }
482
+ set(e, m)
483
+}
484
+
485
func (s persistedAdminState) apply(runtime *policy.Runtime) error {
486
if runtime == nil {
487
return nil
@@ -480,21 +498,7 @@ func (s persistedAdminState) apply(runtime *policy.Runtime) error {
498
runtime.SetBannedIdentityKeys(utils.NormalizeIdentityKeys(s.BannedIdentityKeys))
499
runtime.IPFilter().SetBannedIPs(s.BannedIPs)
500
runtime.BPSManager().SetIdentityBPSLimits(utils.NormalizeIdentityKeyBPS(s.IdentityBPS))
483
- switch {
484
- case s.UDPEnabled != nil && s.UDPMaxLeases != nil:
485
- runtime.SetUDPPolicy(*s.UDPEnabled, *s.UDPMaxLeases)
486
- case s.UDPEnabled != nil:
487
- runtime.SetUDPPolicy(*s.UDPEnabled, runtime.UDPMaxLeases())
488
- case s.UDPMaxLeases != nil:
489
- runtime.SetUDPPolicy(runtime.IsUDPEnabled(), *s.UDPMaxLeases)
490
- }
491
- switch {
492
- case s.TCPPortEnabled != nil && s.TCPPortMaxLeases != nil:
493
- runtime.SetTCPPortPolicy(*s.TCPPortEnabled, *s.TCPPortMaxLeases)
494
- case s.TCPPortEnabled != nil:
495
- runtime.SetTCPPortPolicy(*s.TCPPortEnabled, runtime.TCPPortMaxLeases())
496
- case s.TCPPortMaxLeases != nil:
497
- runtime.SetTCPPortPolicy(runtime.IsTCPPortEnabled(), *s.TCPPortMaxLeases)
498
- }
501
+ applyOptionalPolicy(s.UDPEnabled, s.UDPMaxLeases, runtime.IsUDPEnabled, runtime.UDPMaxLeases, runtime.SetUDPPolicy)
502
+ applyOptionalPolicy(s.TCPPortEnabled, s.TCPPortMaxLeases, runtime.IsTCPPortEnabled, runtime.TCPPortMaxLeases, runtime.SetTCPPortPolicy)
503
return nil
504
}
portal/lease.go
+11
-5
@@ -100,9 +100,7 @@ func (r *leaseRegistry) Register(record *leaseRecord) error {
100
record.Hostname = hostname
101
r.leasesByKey[key] = record
102
r.routes[hostname] = key
103
- if strings.TrimSpace(record.ClientIP) != "" {
104
- r.policy.IPFilter().RegisterIdentityIP(key, record.ClientIP)
105
- }
103
+ r.setClientIPLocked(key, record.ClientIP)
104
r.mu.Unlock()
105
106
if replaced != nil && replaced != record {
@@ -111,6 +109,14 @@ func (r *leaseRegistry) Register(record *leaseRecord) error {
109
return nil
110
}
111
112
+// setClientIPLocked updates the record's client IP and registers it with the
113
+// IP filter. Caller must hold r.mu.
114
+func (r *leaseRegistry) setClientIPLocked(identityKey, clientIP string) {
115
+ if strings.TrimSpace(clientIP) != "" {
116
+ r.policy.IPFilter().RegisterIdentityIP(identityKey, clientIP)
117
+ }
118
+}
119
+
120
func (r *leaseRegistry) Renew(identity types.Identity, ttl time.Duration, clientIP, reportedIP string) (*leaseRecord, error) {
121
r.mu.Lock()
122
defer r.mu.Unlock()
@@ -125,11 +131,11 @@ func (r *leaseRegistry) Renew(identity types.Identity, ttl time.Duration, client
131
record.LastSeenAt = now
132
if strings.TrimSpace(clientIP) != "" {
133
record.ClientIP = clientIP
128
- r.policy.IPFilter().RegisterIdentityIP(record.Key(), clientIP)
134
}
135
if strings.TrimSpace(reportedIP) != "" {
136
record.ReportedIP = reportedIP
137
}
138
+ r.setClientIPLocked(record.Key(), clientIP)
139
return record, nil
140
}
141
@@ -232,8 +238,8 @@ func (r *leaseRegistry) Touch(identity types.Identity, clientIP string, now time
238
record.LastSeenAt = now
239
if strings.TrimSpace(clientIP) != "" {
240
record.ClientIP = clientIP
235
- r.policy.IPFilter().RegisterIdentityIP(record.Key(), clientIP)
241
}
242
+ r.setClientIPLocked(record.Key(), clientIP)
243
return record
244
}
245