Skip to content

Commit 35fc25e

Browse files
authored
fix: close UDP associations once relay is done (#238)
1 parent 0387dfb commit 35fc25e

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

service/udp.go

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ func (h *associationHandler) HandleAssociation(ctx context.Context, clientConn n
161161
}
162162
clientProxyBytes, err := clientConn.Read(readBuf)
163163
if errors.Is(err, net.ErrClosed) || errors.Is(err, io.EOF) {
164-
debugUDP(l, "Client closed connection")
164+
debugUDP(l, "Client connection closed")
165165
break
166166
}
167167
pkt := readBuf[:clientProxyBytes]
@@ -199,7 +199,10 @@ func (h *associationHandler) HandleAssociation(ctx context.Context, clientConn n
199199
return onet.NewConnectionError("ERR_CREATE_SOCKET", "Failed to create a `PacketConn`", err)
200200
}
201201
l = l.With(slog.Any("tgtListener", targetConn.LocalAddr()))
202-
go relayTargetToClient(targetConn, clientConn, cryptoKey, assocMetrics, l)
202+
go func() {
203+
relayTargetToClient(targetConn, clientConn, cryptoKey, assocMetrics, l)
204+
clientConn.Close()
205+
}()
203206
} else {
204207
unpackStart := time.Now()
205208
textData, err := shadowsocks.Unpack(nil, pkt, cryptoKey)
@@ -348,8 +351,8 @@ type association struct {
348351

349352
var _ net.Conn = (*association)(nil)
350353

351-
func (c *association) Read(p []byte) (int, error) {
352-
pkt, ok := <-c.readCh
354+
func (a *association) Read(p []byte) (int, error) {
355+
pkt, ok := <-a.readCh
353356
if !ok {
354357
return 0, net.ErrClosed
355358
}
@@ -361,32 +364,32 @@ func (c *association) Read(p []byte) (int, error) {
361364
return n, nil
362365
}
363366

364-
func (c *association) Write(b []byte) (n int, err error) {
365-
return c.pc.WriteTo(b, c.clientAddr)
367+
func (a *association) Write(b []byte) (n int, err error) {
368+
return a.pc.WriteTo(b, a.clientAddr)
366369
}
367370

368-
func (c *association) Close() error {
369-
close(c.readCh)
370-
return c.pc.Close()
371+
func (a *association) Close() error {
372+
close(a.readCh)
373+
return nil
371374
}
372375

373-
func (c *association) LocalAddr() net.Addr {
374-
return c.pc.LocalAddr()
376+
func (a *association) LocalAddr() net.Addr {
377+
return a.pc.LocalAddr()
375378
}
376379

377-
func (c *association) RemoteAddr() net.Addr {
378-
return c.clientAddr
380+
func (a *association) RemoteAddr() net.Addr {
381+
return a.clientAddr
379382
}
380383

381-
func (c *association) SetDeadline(t time.Time) error {
384+
func (a *association) SetDeadline(t time.Time) error {
382385
return errors.ErrUnsupported
383386
}
384387

385-
func (c *association) SetReadDeadline(t time.Time) error {
388+
func (a *association) SetReadDeadline(t time.Time) error {
386389
return errors.ErrUnsupported
387390
}
388391

389-
func (c *association) SetWriteDeadline(t time.Time) error {
392+
func (a *association) SetWriteDeadline(t time.Time) error {
390393
return errors.ErrUnsupported
391394
}
392395

0 commit comments

Comments
 (0)