diff --git a/adapters/outbound/trojan.go b/adapters/outbound/trojan.go index 99f67c30..a87bac7f 100644 --- a/adapters/outbound/trojan.go +++ b/adapters/outbound/trojan.go @@ -104,15 +104,15 @@ func (t *Trojan) DialUDP(metadata *C.Metadata) (_ C.PacketConn, err error) { return nil, fmt.Errorf("%s connect error: %w", t.addr, 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) - c, err = t.instance.StreamConn(c) - if err != nil { - return nil, fmt.Errorf("%s connect error: %w", t.addr, err) - } - err = t.instance.WriteHeader(c, trojan.CommandUDP, serializesSocksAddr(metadata)) if err != nil { return nil, err