From 131e9d38b6313e465e28845be131567409629207 Mon Sep 17 00:00:00 2001 From: yaling888 <73897884+yaling888@users.noreply.github.com> Date: Tue, 29 Mar 2022 07:18:09 +0800 Subject: [PATCH] Fix: Vless UDP --- README.md | 1 + adapter/outbound/vless.go | 17 +++++++++++++---- test/vless_test.go | 18 +++++++++--------- 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 5df2420b..d7af8a41 100644 --- a/README.md +++ b/README.md @@ -140,6 +140,7 @@ proxies: network: tcp servername: example.com # flow: xtls-rprx-direct # xtls-rprx-origin # enable XTLS + # udp: true # skip-cert-verify: true ``` diff --git a/adapter/outbound/vless.go b/adapter/outbound/vless.go index df7ce4a1..876613b2 100644 --- a/adapter/outbound/vless.go +++ b/adapter/outbound/vless.go @@ -3,6 +3,7 @@ package outbound import ( "context" "crypto/tls" + "encoding/binary" "errors" "fmt" "net" @@ -128,8 +129,9 @@ func (v *Vless) StreamConn(c net.Conn, metadata *C.Metadata) (net.Conn, error) { c, err = gun.StreamGunWithConn(c, v.gunTLSConfig, v.gunConfig) } default: + // default tcp network // handle TLS And XTLS - c, err = v.streamTLSOrXTLSConn(c, true) + c, err = v.streamTLSOrXTLSConn(c, false) } if err != nil { @@ -213,7 +215,7 @@ func (v *Vless) DialContext(ctx context.Context, metadata *C.Metadata, opts ...d // ListenPacketContext implements C.ProxyAdapter func (v *Vless) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.PacketConn, err error) { - // vmess use stream-oriented udp with a special address, so we needs a net.UDPAddr + // vless use stream-oriented udp with a special address, so we needs a net.UDPAddr if !metadata.Resolved() { ip, err := resolver.ResolveIP(metadata.Host) if err != nil { @@ -269,7 +271,7 @@ func parseVlessAddr(metadata *C.Metadata) *vless.DstAddr { copy(addr[1:], []byte(metadata.Host)) } - port, _ := strconv.Atoi(metadata.DstPort) + port, _ := strconv.ParseUint(metadata.DstPort, 10, 16) return &vless.DstAddr{ UDP: metadata.NetWork == C.UDP, AddrType: addrType, @@ -281,14 +283,21 @@ func parseVlessAddr(metadata *C.Metadata) *vless.DstAddr { type vlessPacketConn struct { net.Conn rAddr net.Addr + cache [2]byte } func (uc *vlessPacketConn) WriteTo(b []byte, addr net.Addr) (int, error) { + binary.BigEndian.PutUint16(uc.cache[:], uint16(len(b))) + _, _ = uc.Conn.Write(uc.cache[:]) return uc.Conn.Write(b) } func (uc *vlessPacketConn) ReadFrom(b []byte) (int, net.Addr, error) { - n, err := uc.Conn.Read(b) + n, err := uc.Conn.Read(uc.cache[:]) + if err != nil { + return n, uc.rAddr, err + } + n, err = uc.Conn.Read(b) return n, uc.rAddr, err } diff --git a/test/vless_test.go b/test/vless_test.go index 8bce51d6..b5fbec86 100644 --- a/test/vless_test.go +++ b/test/vless_test.go @@ -40,7 +40,7 @@ func TestClash_VlessTLS(t *testing.T) { TLS: true, SkipCertVerify: true, ServerName: "example.org", - UDP: false, + UDP: true, }) if err != nil { assert.FailNow(t, err.Error()) @@ -71,16 +71,16 @@ func TestClash_VlessXTLS(t *testing.T) { defer cleanContainer(id) proxy, err := outbound.NewVless(outbound.VlessOption{ - Name: "vless", - Server: localIP.String(), - Port: 10002, - UUID: "b831381d-6324-4d53-ad4f-8cda48b30811", - TLS: true, - Flow: "xtls-rprx-direct", - //FlowShow: true, + Name: "vless", + Server: localIP.String(), + Port: 10002, + UUID: "b831381d-6324-4d53-ad4f-8cda48b30811", + TLS: true, SkipCertVerify: true, ServerName: "example.org", - UDP: false, + UDP: true, + Flow: "xtls-rprx-direct", + FlowShow: true, }) if err != nil { assert.FailNow(t, err.Error())