fix: lint, body limit
Kim committed
Mar 6, 2026 at 10:22 UTC
7b6232615369f1e26fa307c31cd8626dd24e6bbf
3 files changed
+16
-60
cmd/relay-server/registry.go
+3
-1
@@ -12,6 +12,8 @@ import (
12
"gosuda.org/portal/types"
13
)
14
15
+const sdkRequestBodyLimitBytes = 4 << 20 // 4 MiB
16
+
17
// SDKRegistry handles HTTP API for client lease registration.
18
type SDKRegistry struct {
19
ipManager *policy.IPFilter
@@ -227,7 +229,7 @@ func (r *SDKRegistry) requireMethod(w http.ResponseWriter, req *http.Request, me
229
}
230
231
func (r *SDKRegistry) decodeRequestBody(w http.ResponseWriter, req *http.Request, dst any, logMessage string) bool {
230
- req.Body = http.MaxBytesReader(w, req.Body, 1<<16)
232
+ req.Body = http.MaxBytesReader(w, req.Body, sdkRequestBodyLimitBytes)
233
if err := json.NewDecoder(req.Body).Decode(dst); err != nil {
234
log.Error().Err(err).Msg(logMessage)
235
writeAPIError(w, http.StatusBadRequest, "invalid_request", "invalid request body")
sdk/listener.go
+9
-7
@@ -82,8 +82,6 @@ type Listener struct {
82
tlsConfig *tls.Config
83
lease *types.Lease
84
httpClient *http.Client
85
- baseCtx context.Context
86
- baseCancel context.CancelFunc
85
stopCh chan struct{}
86
acceptCh chan net.Conn
87
relayAddr string
@@ -137,7 +135,6 @@ func NewListener(relayAddr string, lease *types.Lease, tlsConfig *tls.Config, re
135
}
136
lease.TLS = true
137
140
- baseCtx, baseCancel := context.WithCancel(context.Background())
138
return &Listener{
139
relayAddr: apiURL,
140
lease: lease,
@@ -147,8 +144,6 @@ func NewListener(relayAddr string, lease *types.Lease, tlsConfig *tls.Config, re
144
},
145
tlsConfig: tlsConfig,
146
closeFns: closeFns,
150
- baseCtx: baseCtx,
151
- baseCancel: baseCancel,
147
stopCh: make(chan struct{}),
148
acceptCh: make(chan net.Conn, 128),
149
reverseWorkers: reverseWorkers,
@@ -212,7 +207,6 @@ func (l *Listener) Accept() (net.Conn, error) {
207
func (l *Listener) Close() error {
208
var retErr error
209
l.closeOnce.Do(func() {
215
- l.baseCancel()
210
close(l.stopCh)
211
212
l.mu.Lock()
@@ -579,7 +573,15 @@ func (l *Listener) newStopAwareContext(timeout time.Duration) (context.Context,
573
if timeout <= 0 {
574
timeout = defaultReverseDialTimeout
575
}
582
- return context.WithTimeout(l.baseCtx, timeout)
576
+ ctx, cancel := context.WithTimeout(context.Background(), timeout)
577
+ go func() {
578
+ select {
579
+ case <-l.stopCh:
580
+ cancel()
581
+ case <-ctx.Done():
582
+ }
583
+ }()
584
+ return ctx, cancel
585
}
586
587
func (l *Listener) closeConnOnStop(conn net.Conn) func() {
sdk/listener_test.go
+4
-52
@@ -1,7 +1,6 @@
1
package sdk
2
3
import (
4
- "context"
4
"crypto/tls"
5
"errors"
6
"fmt"
@@ -187,15 +186,10 @@ func TestBuildReverseConnectRequest(t *testing.T) {
186
187
func TestOpenReverseConnection_RejectsNonHTTPSRelay(t *testing.T) {
188
t.Parallel()
190
-
191
- baseCtx, baseCancel := context.WithCancel(context.Background())
192
- defer baseCancel()
189
l := &Listener{
190
relayAddr: "http://localhost:4017",
191
lease: &types.Lease{ID: "lease-1", ReverseToken: "token-1"},
192
reverseDialTimeout: 2 * time.Second,
197
- baseCtx: baseCtx,
198
- baseCancel: baseCancel,
193
stopCh: make(chan struct{}),
194
}
195
@@ -230,15 +224,10 @@ func TestOpenReverseConnection_StopUnblocksTLSHandshake(t *testing.T) {
224
buf := make([]byte, 1)
225
_, _ = conn.Read(buf)
226
}()
233
-
234
- baseCtx, baseCancel := context.WithCancel(context.Background())
235
- defer baseCancel()
227
l := &Listener{
228
relayAddr: "https://" + ln.Addr().String(),
229
lease: &types.Lease{ID: "lease-1", ReverseToken: "token-1"},
230
reverseDialTimeout: 5 * time.Second,
240
- baseCtx: baseCtx,
241
- baseCancel: baseCancel,
231
stopCh: make(chan struct{}),
232
}
233
@@ -280,14 +269,9 @@ func TestWriteReverseConnectRequest_RespectsWriteDeadline(t *testing.T) {
269
if err != nil {
270
t.Fatalf("parse request URL: %v", err)
271
}
283
-
284
- baseCtx, baseCancel := context.WithCancel(context.Background())
285
- defer baseCancel()
272
l := &Listener{
273
lease: &types.Lease{ReverseToken: "token-1"},
274
reverseDialTimeout: 25 * time.Millisecond,
289
- baseCtx: baseCtx,
290
- baseCancel: baseCancel,
275
stopCh: make(chan struct{}),
276
}
277
@@ -316,13 +300,8 @@ func TestReadReverseConnectResponse_RespectsReadDeadline(t *testing.T) {
300
local, peer := net.Pipe()
301
defer local.Close()
302
defer peer.Close()
319
-
320
- baseCtx, baseCancel := context.WithCancel(context.Background())
321
- defer baseCancel()
303
l := &Listener{
304
reverseDialTimeout: 25 * time.Millisecond,
324
- baseCtx: baseCtx,
325
- baseCancel: baseCancel,
305
stopCh: make(chan struct{}),
306
}
307
@@ -401,13 +380,8 @@ func TestReadReverseConnectResponseParsesEnvelopeError(t *testing.T) {
380
local, peer := net.Pipe()
381
defer local.Close()
382
defer peer.Close()
404
-
405
- baseCtx, baseCancel := context.WithCancel(context.Background())
406
- defer baseCancel()
383
l := &Listener{
384
reverseDialTimeout: 500 * time.Millisecond,
409
- baseCtx: baseCtx,
410
- baseCancel: baseCancel,
385
stopCh: make(chan struct{}),
386
}
387
@@ -513,10 +487,7 @@ func TestReverseConnectRejectionErrorIsFatal(t *testing.T) {
487
488
func TestWaitForReverseStart_HTTPMode(t *testing.T) {
489
t.Parallel()
516
-
517
- baseCtx, baseCancel := context.WithCancel(context.Background())
518
- defer baseCancel()
519
- l := &Listener{baseCtx: baseCtx, baseCancel: baseCancel, stopCh: make(chan struct{})}
490
+ l := &Listener{stopCh: make(chan struct{})}
491
local, peer := net.Pipe()
492
defer local.Close()
493
defer peer.Close()
@@ -543,12 +514,7 @@ func TestWaitForReverseStart_HTTPMode(t *testing.T) {
514
515
func TestWaitForReverseStart_TLSMode(t *testing.T) {
516
t.Parallel()
546
-
547
- baseCtx, baseCancel := context.WithCancel(context.Background())
548
- defer baseCancel()
517
l := &Listener{
550
- baseCtx: baseCtx,
551
- baseCancel: baseCancel,
518
stopCh: make(chan struct{}),
519
tlsConfig: &tls.Config{MinVersion: tls.VersionTLS12},
520
}
@@ -578,10 +544,7 @@ func TestWaitForReverseStart_TLSMode(t *testing.T) {
544
545
func TestWaitForReverseStart_IgnoresKeepaliveMarker(t *testing.T) {
546
t.Parallel()
581
-
582
- baseCtx, baseCancel := context.WithCancel(context.Background())
583
- defer baseCancel()
584
- l := &Listener{baseCtx: baseCtx, baseCancel: baseCancel, stopCh: make(chan struct{})}
547
+ l := &Listener{stopCh: make(chan struct{})}
548
local, peer := net.Pipe()
549
defer local.Close()
550
defer peer.Close()
@@ -612,12 +575,7 @@ func TestWaitForReverseStart_IgnoresKeepaliveMarker(t *testing.T) {
575
576
func TestWaitForReverseStart_TLSRejectsHTTPMarker(t *testing.T) {
577
t.Parallel()
615
-
616
- baseCtx, baseCancel := context.WithCancel(context.Background())
617
- defer baseCancel()
578
l := &Listener{
619
- baseCtx: baseCtx,
620
- baseCancel: baseCancel,
579
stopCh: make(chan struct{}),
580
tlsConfig: &tls.Config{MinVersion: tls.VersionTLS12},
581
}
@@ -647,10 +605,7 @@ func TestWaitForReverseStart_TLSRejectsHTTPMarker(t *testing.T) {
605
606
func TestWaitForReverseStart_HTTPRejectsTLSMarker(t *testing.T) {
607
t.Parallel()
650
-
651
- baseCtx, baseCancel := context.WithCancel(context.Background())
652
- defer baseCancel()
653
- l := &Listener{baseCtx: baseCtx, baseCancel: baseCancel, stopCh: make(chan struct{})}
608
+ l := &Listener{stopCh: make(chan struct{})}
609
local, peer := net.Pipe()
610
defer local.Close()
611
defer peer.Close()
@@ -677,10 +632,7 @@ func TestWaitForReverseStart_HTTPRejectsTLSMarker(t *testing.T) {
632
633
func TestWaitForReverseStart_StopCancelsWait(t *testing.T) {
634
t.Parallel()
680
-
681
- baseCtx, baseCancel := context.WithCancel(context.Background())
682
- defer baseCancel()
683
- l := &Listener{baseCtx: baseCtx, baseCancel: baseCancel, stopCh: make(chan struct{})}
635
+ l := &Listener{stopCh: make(chan struct{})}
636
local, peer := net.Pipe()
637
defer local.Close()
638