From bffb0573a65d33397cdb7dddfc743e2b923517b7 Mon Sep 17 00:00:00 2001 From: wwqgtxx Date: Tue, 13 Dec 2022 13:20:40 +0800 Subject: [PATCH] fix: safeConnClose not working --- 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/vless.go | 16 ++++++++++++---- adapter/outbound/vmess.go | 16 ++++++++++++---- 8 files changed, 51 insertions(+), 17 deletions(-) diff --git a/adapter/outbound/http.go b/adapter/outbound/http.go index 67cafd7e..bdcf5c9f 100644 --- a/adapter/outbound/http.go +++ b/adapter/outbound/http.go @@ -67,7 +67,9 @@ func (h *Http) DialContext(ctx context.Context, metadata *C.Metadata, opts ...di } tcpKeepAlive(c) - defer safeConnClose(c, err) + defer func() { + safeConnClose(c, err) + }() c, err = h.StreamConn(c, metadata) if err != nil { diff --git a/adapter/outbound/shadowsocks.go b/adapter/outbound/shadowsocks.go index 74400301..263284b0 100644 --- a/adapter/outbound/shadowsocks.go +++ b/adapter/outbound/shadowsocks.go @@ -90,7 +90,9 @@ func (ss *ShadowSocks) DialContext(ctx context.Context, metadata *C.Metadata, op } tcpKeepAlive(c) - defer safeConnClose(c, err) + defer func() { + safeConnClose(c, err) + }() c, err = ss.StreamConn(c, metadata) return NewConn(c, ss), err diff --git a/adapter/outbound/shadowsocksr.go b/adapter/outbound/shadowsocksr.go index 5d71ed65..c4112e6e 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() { + safeConnClose(c, err) + }() c, err = ssr.StreamConn(c, metadata) return NewConn(c, ssr), err diff --git a/adapter/outbound/snell.go b/adapter/outbound/snell.go index 0aadb1c8..f2bc6116 100644 --- a/adapter/outbound/snell.go +++ b/adapter/outbound/snell.go @@ -84,7 +84,9 @@ func (s *Snell) DialContext(ctx context.Context, metadata *C.Metadata, opts ...d } tcpKeepAlive(c) - defer safeConnClose(c, err) + defer func() { + safeConnClose(c, err) + }() c, err = s.StreamConn(c, metadata) return NewConn(c, s), err diff --git a/adapter/outbound/socks5.go b/adapter/outbound/socks5.go index 0da864b6..fe3b9bdc 100644 --- a/adapter/outbound/socks5.go +++ b/adapter/outbound/socks5.go @@ -71,7 +71,9 @@ func (ss *Socks5) DialContext(ctx context.Context, metadata *C.Metadata, opts .. } tcpKeepAlive(c) - defer safeConnClose(c, err) + defer func() { + safeConnClose(c, err) + }() c, err = ss.StreamConn(c, metadata) if err != nil { @@ -97,7 +99,9 @@ func (ss *Socks5) ListenPacketContext(ctx context.Context, metadata *C.Metadata, c = cc } - defer safeConnClose(c, err) + defer func() { + safeConnClose(c, err) + }() tcpKeepAlive(c) var user *socks5.User diff --git a/adapter/outbound/trojan.go b/adapter/outbound/trojan.go index 946a0101..9d3dcd4c 100644 --- a/adapter/outbound/trojan.go +++ b/adapter/outbound/trojan.go @@ -127,7 +127,9 @@ func (t *Trojan) DialContext(ctx context.Context, metadata *C.Metadata, opts ... } tcpKeepAlive(c) - defer safeConnClose(c, err) + defer func() { + safeConnClose(c, err) + }() c, err = t.StreamConn(c, metadata) if err != nil { @@ -147,13 +149,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() { + safeConnClose(c, err) + }() } 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() { + safeConnClose(c, err) + }() tcpKeepAlive(c) c, err = t.plainStream(c) if err != nil { diff --git a/adapter/outbound/vless.go b/adapter/outbound/vless.go index b86a9d6f..f1e62829 100644 --- a/adapter/outbound/vless.go +++ b/adapter/outbound/vless.go @@ -208,7 +208,9 @@ func (v *Vless) DialContext(ctx context.Context, metadata *C.Metadata, opts ...d if err != nil { return nil, err } - defer safeConnClose(c, err) + defer func() { + safeConnClose(c, err) + }() c, err = v.client.StreamConn(c, parseVlessAddr(metadata)) if err != nil { @@ -223,7 +225,9 @@ func (v *Vless) 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() { + safeConnClose(c, err) + }() c, err = v.StreamConn(c, metadata) return NewConn(c, v), err @@ -247,7 +251,9 @@ func (v *Vless) ListenPacketContext(ctx context.Context, metadata *C.Metadata, o if err != nil { return nil, err } - defer safeConnClose(c, err) + defer func() { + safeConnClose(c, err) + }() c, err = v.client.StreamConn(c, parseVlessAddr(metadata)) } else { @@ -256,7 +262,9 @@ func (v *Vless) 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() { + safeConnClose(c, err) + }() c, err = v.StreamConn(c, metadata) } diff --git a/adapter/outbound/vmess.go b/adapter/outbound/vmess.go index 6d135b62..65d3073b 100644 --- a/adapter/outbound/vmess.go +++ b/adapter/outbound/vmess.go @@ -221,7 +221,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() { + safeConnClose(c, err) + }() c, err = v.client.DialConn(c, M.ParseSocksaddr(metadata.RemoteAddress())) if err != nil { @@ -236,7 +238,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() { + safeConnClose(c, err) + }() c, err = v.StreamConn(c, metadata) return NewConn(c, v), err @@ -267,7 +271,9 @@ func (v *Vmess) ListenPacketContext(ctx context.Context, metadata *C.Metadata, o if err != nil { return nil, err } - defer safeConnClose(c, err) + defer func() { + safeConnClose(c, err) + }() if v.option.XUDP { c, err = v.client.DialXUDPPacketConn(c, M.ParseSocksaddr(metadata.RemoteAddress())) @@ -280,7 +286,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() { + safeConnClose(c, err) + }() c, err = v.StreamConn(c, metadata) }