WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 34239ab

Browse files
Merge pull request #141 from thaJeztah/nat_cleanups
check for net.ErrClosed instead of "use of closed network connection"
2 parents 66f189f + 3597cdd commit 34239ab

File tree

3 files changed

+18
-25
lines changed

3 files changed

+18
-25
lines changed

proxy/udp_proxy.go

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package proxy
22

33
import (
44
"encoding/binary"
5+
"errors"
56
"net"
6-
"strings"
77
"sync"
88
"syscall"
99
"time"
@@ -115,10 +115,9 @@ func (proxy *UDPProxy) Run() {
115115
for {
116116
read, from, err := proxy.listener.ReadFromUDP(readBuf)
117117
if err != nil {
118-
// NOTE: Apparently ReadFrom doesn't return
119-
// ECONNREFUSED like Read do (see comment in
120-
// UDPProxy.replyLoop)
121-
if !isClosedError(err) {
118+
// NOTE: Apparently ReadFrom doesn't return ECONNREFUSED like
119+
// Read does (see comment in [UDPProxy.replyLoop]).
120+
if !errors.Is(err, net.ErrClosed) {
122121
proxy.Logger.Printf("Stopping proxy on udp/%v for udp/%v (%s)", proxy.frontendAddr, proxy.backendAddr, err)
123122
}
124123
break
@@ -164,13 +163,3 @@ func (proxy *UDPProxy) FrontendAddr() net.Addr { return proxy.frontendAddr }
164163

165164
// BackendAddr returns the proxied UDP address.
166165
func (proxy *UDPProxy) BackendAddr() net.Addr { return proxy.backendAddr }
167-
168-
func isClosedError(err error) bool {
169-
/* This comparison is ugly, but unfortunately, net.go doesn't export errClosing.
170-
* See:
171-
* http://golang.org/src/pkg/net/net.go
172-
* https://code.google.com/p/go/issues/detail?id=4337
173-
* https://groups.google.com/forum/#!msg/golang-nuts/0_aaCvBmOcM/SptmDyX1XJMJ
174-
*/
175-
return strings.HasSuffix(err.Error(), "use of closed network connection")
176-
}

sockets/inmem_socket.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
package sockets
22

33
import (
4-
"errors"
54
"net"
65
"sync"
76
)
87

9-
var errClosed = errors.New("use of closed network connection")
10-
118
// InmemSocket implements net.Listener using in-memory only connections.
129
type InmemSocket struct {
1310
chConn chan net.Conn
@@ -36,13 +33,15 @@ func (s *InmemSocket) Addr() net.Addr {
3633
return dummyAddr(s.addr)
3734
}
3835

39-
// Accept implements the Accept method in the Listener interface; it waits for the next call and returns a generic Conn.
36+
// Accept implements the Accept method in the Listener interface; it waits
37+
// for the next call and returns a generic Conn. It returns a [net.ErrClosed]
38+
// if the connection is already closed.
4039
func (s *InmemSocket) Accept() (net.Conn, error) {
4140
select {
4241
case conn := <-s.chConn:
4342
return conn, nil
4443
case <-s.chClose:
45-
return nil, errClosed
44+
return nil, net.ErrClosed
4645
}
4746
}
4847

@@ -58,13 +57,14 @@ func (s *InmemSocket) Close() error {
5857
return nil
5958
}
6059

61-
// Dial is used to establish a connection with the in-mem server
60+
// Dial is used to establish a connection with the in-mem server.
61+
// It returns a [net.ErrClosed] if the connection is already closed.
6262
func (s *InmemSocket) Dial(network, addr string) (net.Conn, error) {
6363
srvConn, clientConn := net.Pipe()
6464
select {
6565
case s.chConn <- srvConn:
6666
case <-s.chClose:
67-
return nil, errClosed
67+
return nil, net.ErrClosed
6868
}
6969

7070
return clientConn, nil

sockets/inmem_socket_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package sockets
22

3-
import "testing"
3+
import (
4+
"errors"
5+
"net"
6+
"testing"
7+
)
48

59
func TestInmemSocket(t *testing.T) {
610
l := NewInmemSocket("test", 0)
@@ -33,7 +37,7 @@ func TestInmemSocket(t *testing.T) {
3337

3438
_ = l.Close()
3539
_, err = l.Dial("test", "test")
36-
if err != errClosed {
37-
t.Fatalf("expected `errClosed` error, got %v", err)
40+
if !errors.Is(err, net.ErrClosed) {
41+
t.Fatalf(`expected "net.ErrClosed" error, got %[1]v (%[1]T)`, err)
3842
}
3943
}

0 commit comments

Comments
 (0)