From 575c25215e2d9f627068d7beb9ea8cf094b62be1 Mon Sep 17 00:00:00 2001 From: gVisor bot Date: Sun, 11 Sep 2022 15:24:56 +0800 Subject: [PATCH] Fix: wechat protocol is not working if no obfs string is configured --- transport/hysteria/obfs/dummy.go | 18 ++++++++++++++++++ transport/hysteria/transport/client.go | 14 ++++++-------- 2 files changed, 24 insertions(+), 8 deletions(-) create mode 100644 transport/hysteria/obfs/dummy.go diff --git a/transport/hysteria/obfs/dummy.go b/transport/hysteria/obfs/dummy.go new file mode 100644 index 00000000..3edff101 --- /dev/null +++ b/transport/hysteria/obfs/dummy.go @@ -0,0 +1,18 @@ +package obfs + +type DummyObfuscator struct{} + +func NewDummyObfuscator() *DummyObfuscator { + return &DummyObfuscator{} +} + +func (x *DummyObfuscator) Deobfuscate(in []byte, out []byte) int { + if len(out) < len(in) { + return 0 + } + return copy(out, in) +} + +func (x *DummyObfuscator) Obfuscate(in []byte, out []byte) int { + return copy(out, in) +} diff --git a/transport/hysteria/transport/client.go b/transport/hysteria/transport/client.go index d350b4ad..43330cd3 100644 --- a/transport/hysteria/transport/client.go +++ b/transport/hysteria/transport/client.go @@ -7,7 +7,7 @@ import ( "github.com/Dreamacro/clash/transport/hysteria/conns/faketcp" "github.com/Dreamacro/clash/transport/hysteria/conns/udp" "github.com/Dreamacro/clash/transport/hysteria/conns/wechat" - "github.com/Dreamacro/clash/transport/hysteria/obfs" + obfsPkg "github.com/Dreamacro/clash/transport/hysteria/obfs" "github.com/lucas-clemente/quic-go" "net" ) @@ -16,7 +16,7 @@ type ClientTransport struct { Dialer *net.Dialer } -func (ct *ClientTransport) quicPacketConn(proto string, server string, obfs obfs.Obfuscator, dialer PacketDialer) (net.PacketConn, error) { +func (ct *ClientTransport) quicPacketConn(proto string, server string, obfs obfsPkg.Obfuscator, dialer PacketDialer) (net.PacketConn, error) { if len(proto) == 0 || proto == "udp" { conn, err := dialer.ListenPacket() if err != nil { @@ -33,12 +33,10 @@ func (ct *ClientTransport) quicPacketConn(proto string, server string, obfs obfs if err != nil { return nil, err } - if obfs != nil { - oc := wechat.NewObfsWeChatUDPConn(conn, obfs) - return oc, nil - } else { - return conn, nil + if obfs == nil { + obfs = obfsPkg.NewDummyObfuscator() } + return wechat.NewObfsWeChatUDPConn(conn, obfs), nil } else if proto == "faketcp" { var conn *faketcp.TCPConn conn, err := faketcp.Dial("tcp", server) @@ -62,7 +60,7 @@ type PacketDialer interface { RemoteAddr(host string) (net.Addr, error) } -func (ct *ClientTransport) QUICDial(proto string, server string, tlsConfig *tls.Config, quicConfig *quic.Config, obfs obfs.Obfuscator, dialer PacketDialer) (quic.Connection, error) { +func (ct *ClientTransport) QUICDial(proto string, server string, tlsConfig *tls.Config, quicConfig *quic.Config, obfs obfsPkg.Obfuscator, dialer PacketDialer) (quic.Connection, error) { serverUDPAddr, err := dialer.RemoteAddr(server) if err != nil { return nil, err