From cdc7d449a606f83b814ee0df3674749b94b285a0 Mon Sep 17 00:00:00 2001 From: wwqgtxx Date: Thu, 22 Dec 2022 12:42:38 +0800 Subject: [PATCH] Fix: safeConnClose not working (#2463) --- adapter/outbound/http.go | 4 +++- adapter/outbound/shadowsocks.go | 4 +++- adapter/outbound/shadowsocksr.go | 4 +++- adapter/outbound/snell.go | 4 +++- adapter/outbound/socks5.go | 8 ++++++-- adapter/outbound/trojan.go | 12 +++++++++--- adapter/outbound/vmess.go | 16 ++++++++++++---- 7 files changed, 39 insertions(+), 13 deletions(-) diff --git a/adapter/outbound/http.go b/adapter/outbound/http.go index 480710dc..b7425100 100644 --- a/adapter/outbound/http.go +++ b/adapter/outbound/http.go @@ -63,7 +63,9 @@ func (h *Http) DialContext(ctx context.Context, metadata *C.Metadata, opts ...di } tcpKeepAlive(c) - defer safeConnClose(c, err) + defer func(c net.Conn) { + safeConnClose(c, err) + }(c) c, err = h.StreamConn(c, metadata) if err != nil { diff --git a/adapter/outbound/shadowsocks.go b/adapter/outbound/shadowsocks.go index 6ed78050..e1878dfa 100644 --- a/adapter/outbound/shadowsocks.go +++ b/adapter/outbound/shadowsocks.go @@ -81,7 +81,9 @@ func (ss *ShadowSocks) DialContext(ctx context.Context, metadata *C.Metadata, op } tcpKeepAlive(c) - defer safeConnClose(c, err) + defer func(c net.Conn) { + safeConnClose(c, err) + }(c) c, err = ss.StreamConn(c, metadata) return NewConn(c, ss), err diff --git a/adapter/outbound/shadowsocksr.go b/adapter/outbound/shadowsocksr.go index 57ef5604..4542eebc 100644 --- a/adapter/outbound/shadowsocksr.go +++ b/adapter/outbound/shadowsocksr.go @@ -66,7 +66,9 @@ func (ssr *ShadowSocksR) DialContext(ctx context.Context, metadata *C.Metadata, } tcpKeepAlive(c) - defer safeConnClose(c, err) + defer func(c net.Conn) { + safeConnClose(c, err) + }(c) c, err = ssr.StreamConn(c, metadata) return NewConn(c, ssr), err diff --git a/adapter/outbound/snell.go b/adapter/outbound/snell.go index 07f3d89d..ab791908 100644 --- a/adapter/outbound/snell.go +++ b/adapter/outbound/snell.go @@ -80,7 +80,9 @@ func (s *Snell) DialContext(ctx context.Context, metadata *C.Metadata, opts ...d } tcpKeepAlive(c) - defer safeConnClose(c, err) + defer func(c net.Conn) { + safeConnClose(c, err) + }(c) c, err = s.StreamConn(c, metadata) return NewConn(c, s), err diff --git a/adapter/outbound/socks5.go b/adapter/outbound/socks5.go index 3269457d..4f816211 100644 --- a/adapter/outbound/socks5.go +++ b/adapter/outbound/socks5.go @@ -69,7 +69,9 @@ func (ss *Socks5) DialContext(ctx context.Context, metadata *C.Metadata, opts .. } tcpKeepAlive(c) - defer safeConnClose(c, err) + defer func(c net.Conn) { + safeConnClose(c, err) + }(c) c, err = ss.StreamConn(c, metadata) if err != nil { @@ -95,7 +97,9 @@ func (ss *Socks5) ListenPacketContext(ctx context.Context, metadata *C.Metadata, c = cc } - defer safeConnClose(c, err) + defer func(c net.Conn) { + safeConnClose(c, err) + }(c) tcpKeepAlive(c) var user *socks5.User diff --git a/adapter/outbound/trojan.go b/adapter/outbound/trojan.go index 064cd3c2..fa549df0 100644 --- a/adapter/outbound/trojan.go +++ b/adapter/outbound/trojan.go @@ -109,7 +109,9 @@ func (t *Trojan) DialContext(ctx context.Context, metadata *C.Metadata, opts ... } tcpKeepAlive(c) - defer safeConnClose(c, err) + defer func(c net.Conn) { + safeConnClose(c, err) + }(c) c, err = t.StreamConn(c, metadata) if err != nil { @@ -129,13 +131,17 @@ func (t *Trojan) ListenPacketContext(ctx context.Context, metadata *C.Metadata, if err != nil { return nil, fmt.Errorf("%s connect error: %w", t.addr, err) } - defer safeConnClose(c, err) + defer func(c net.Conn) { + safeConnClose(c, err) + }(c) } else { c, err = dialer.DialContext(ctx, "tcp", t.addr, t.Base.DialOptions(opts...)...) if err != nil { return nil, fmt.Errorf("%s connect error: %w", t.addr, err) } - defer safeConnClose(c, err) + defer func(c net.Conn) { + safeConnClose(c, err) + }(c) tcpKeepAlive(c) c, err = t.plainStream(c) if err != nil { diff --git a/adapter/outbound/vmess.go b/adapter/outbound/vmess.go index aaa9105d..8fa74fbd 100644 --- a/adapter/outbound/vmess.go +++ b/adapter/outbound/vmess.go @@ -195,7 +195,9 @@ func (v *Vmess) DialContext(ctx context.Context, metadata *C.Metadata, opts ...d if err != nil { return nil, err } - defer safeConnClose(c, err) + defer func(c net.Conn) { + safeConnClose(c, err) + }(c) c, err = v.client.StreamConn(c, parseVmessAddr(metadata)) if err != nil { @@ -210,7 +212,9 @@ func (v *Vmess) DialContext(ctx context.Context, metadata *C.Metadata, opts ...d return nil, fmt.Errorf("%s connect error: %s", v.addr, err.Error()) } tcpKeepAlive(c) - defer safeConnClose(c, err) + defer func(c net.Conn) { + safeConnClose(c, err) + }(c) c, err = v.StreamConn(c, metadata) return NewConn(c, v), err @@ -234,7 +238,9 @@ func (v *Vmess) ListenPacketContext(ctx context.Context, metadata *C.Metadata, o if err != nil { return nil, err } - defer safeConnClose(c, err) + defer func(c net.Conn) { + safeConnClose(c, err) + }(c) c, err = v.client.StreamConn(c, parseVmessAddr(metadata)) } else { @@ -243,7 +249,9 @@ func (v *Vmess) ListenPacketContext(ctx context.Context, metadata *C.Metadata, o return nil, fmt.Errorf("%s connect error: %s", v.addr, err.Error()) } tcpKeepAlive(c) - defer safeConnClose(c, err) + defer func(c net.Conn) { + safeConnClose(c, err) + }(c) c, err = v.StreamConn(c, metadata) }