From d9341a49eae9b4f8e991efd4493353728a39a0d5 Mon Sep 17 00:00:00 2001 From: Dreamacro <8615343+Dreamacro@users.noreply.github.com> Date: Mon, 19 Apr 2021 12:20:37 +0800 Subject: [PATCH] Fix: trojan should safe close connection --- adapters/outbound/trojan.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/adapters/outbound/trojan.go b/adapters/outbound/trojan.go index a87bac7fa..92fcbeeb0 100644 --- a/adapters/outbound/trojan.go +++ b/adapters/outbound/trojan.go @@ -96,6 +96,7 @@ func (t *Trojan) DialUDP(metadata *C.Metadata) (_ C.PacketConn, err error) { if err != nil { return nil, fmt.Errorf("%s connect error: %w", t.addr, err) } + defer safeConnClose(c, err) } else { ctx, cancel := context.WithTimeout(context.Background(), tcpTimeout) defer cancel() @@ -103,16 +104,14 @@ func (t *Trojan) DialUDP(metadata *C.Metadata) (_ C.PacketConn, err error) { if err != nil { return nil, fmt.Errorf("%s connect error: %w", t.addr, err) } + defer safeConnClose(c, err) tcpKeepAlive(c) c, err = t.instance.StreamConn(c) if err != nil { - c.Close() return nil, fmt.Errorf("%s connect error: %w", t.addr, err) } } - defer safeConnClose(c, err) - err = t.instance.WriteHeader(c, trojan.CommandUDP, serializesSocksAddr(metadata)) if err != nil { return nil, err