From 132a6a6a2f45cd1db3dc1f0b756944b78adf4a24 Mon Sep 17 00:00:00 2001 From: Kaming Chan Date: Wed, 23 Feb 2022 11:22:46 +0800 Subject: [PATCH 1/2] Fix: listener tcp keepalive & reuse net.BufferedConn (#1987) --- listener/http/proxy.go | 7 ++++++- listener/mixed/mixed.go | 2 ++ listener/socks/tcp.go | 7 +------ 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/listener/http/proxy.go b/listener/http/proxy.go index 23a73739..becb3673 100644 --- a/listener/http/proxy.go +++ b/listener/http/proxy.go @@ -19,7 +19,12 @@ func HandleConn(c net.Conn, in chan<- C.ConnContext, cache *cache.Cache) { client := newClient(c.RemoteAddr(), in) defer client.CloseIdleConnections() - conn := N.NewBufferedConn(c) + var conn *N.BufferedConn + if bufConn, ok := c.(*N.BufferedConn); ok { + conn = bufConn + } else { + conn = N.NewBufferedConn(c) + } keepAlive := true trusted := cache == nil // disable authenticate if cache is nil diff --git a/listener/mixed/mixed.go b/listener/mixed/mixed.go index 8fd4f990..57fd055e 100644 --- a/listener/mixed/mixed.go +++ b/listener/mixed/mixed.go @@ -64,6 +64,8 @@ func New(addr string, in chan<- C.ConnContext) (*Listener, error) { } func handleConn(conn net.Conn, in chan<- C.ConnContext, cache *cache.Cache) { + conn.(*net.TCPConn).SetKeepAlive(true) + bufConn := N.NewBufferedConn(conn) head, err := bufConn.Peek(1) if err != nil { diff --git a/listener/socks/tcp.go b/listener/socks/tcp.go index 29016f5b..7cce32ee 100644 --- a/listener/socks/tcp.go +++ b/listener/socks/tcp.go @@ -61,6 +61,7 @@ func New(addr string, in chan<- C.ConnContext) (*Listener, error) { } func handleSocks(conn net.Conn, in chan<- C.ConnContext) { + conn.(*net.TCPConn).SetKeepAlive(true) bufConn := N.NewBufferedConn(conn) head, err := bufConn.Peek(1) if err != nil { @@ -84,9 +85,6 @@ func HandleSocks4(conn net.Conn, in chan<- C.ConnContext) { conn.Close() return } - if c, ok := conn.(*net.TCPConn); ok { - c.SetKeepAlive(true) - } in <- inbound.NewSocket(socks5.ParseAddr(addr), conn, C.SOCKS4) } @@ -96,9 +94,6 @@ func HandleSocks5(conn net.Conn, in chan<- C.ConnContext) { conn.Close() return } - if c, ok := conn.(*net.TCPConn); ok { - c.SetKeepAlive(true) - } if command == socks5.CmdUDPAssociate { defer conn.Close() io.Copy(io.Discard, conn) From b52d0c16e9d936c28a9a8b4bbb279b941d1424d0 Mon Sep 17 00:00:00 2001 From: Dreamacro <8615343+Dreamacro@users.noreply.github.com> Date: Sun, 27 Feb 2022 18:00:04 +0800 Subject: [PATCH 2/2] Chore: vmess test remove all alterid --- test/config/vmess-aead.json | 28 ---------------- test/config/vmess-grpc.json | 3 +- test/config/vmess-http.json | 3 +- test/config/vmess-http2.json | 3 +- test/config/vmess-tls.json | 3 +- test/config/vmess-ws-0rtt.json | 3 +- test/config/vmess-ws-tls.json | 3 +- test/config/vmess-ws.json | 3 +- test/config/vmess.json | 3 +- test/vmess_test.go | 59 ++++------------------------------ 10 files changed, 14 insertions(+), 97 deletions(-) delete mode 100644 test/config/vmess-aead.json diff --git a/test/config/vmess-aead.json b/test/config/vmess-aead.json deleted file mode 100644 index 27bc4757..00000000 --- a/test/config/vmess-aead.json +++ /dev/null @@ -1,28 +0,0 @@ -{ - "inbounds": [ - { - "port": 10002, - "listen": "0.0.0.0", - "protocol": "vmess", - "settings": { - "clients": [ - { - "id": "b831381d-6324-4d53-ad4f-8cda48b30811", - "alterId": 0 - } - ] - }, - "streamSettings": { - "network": "tcp" - } - } - ], - "outbounds": [ - { - "protocol": "freedom" - } - ], - "log": { - "loglevel": "debug" - } -} \ No newline at end of file diff --git a/test/config/vmess-grpc.json b/test/config/vmess-grpc.json index 22e11763..178e0685 100644 --- a/test/config/vmess-grpc.json +++ b/test/config/vmess-grpc.json @@ -7,8 +7,7 @@ "settings": { "clients": [ { - "id": "b831381d-6324-4d53-ad4f-8cda48b30811", - "alterId": 32 + "id": "b831381d-6324-4d53-ad4f-8cda48b30811" } ] }, diff --git a/test/config/vmess-http.json b/test/config/vmess-http.json index aa6c6c8e..90550c35 100644 --- a/test/config/vmess-http.json +++ b/test/config/vmess-http.json @@ -7,8 +7,7 @@ "settings": { "clients": [ { - "id": "b831381d-6324-4d53-ad4f-8cda48b30811", - "alterId": 32 + "id": "b831381d-6324-4d53-ad4f-8cda48b30811" } ] }, diff --git a/test/config/vmess-http2.json b/test/config/vmess-http2.json index 48873731..c6916a1b 100644 --- a/test/config/vmess-http2.json +++ b/test/config/vmess-http2.json @@ -7,8 +7,7 @@ "settings": { "clients": [ { - "id": "b831381d-6324-4d53-ad4f-8cda48b30811", - "alterId": 32 + "id": "b831381d-6324-4d53-ad4f-8cda48b30811" } ] }, diff --git a/test/config/vmess-tls.json b/test/config/vmess-tls.json index ae2fa489..17e87d66 100644 --- a/test/config/vmess-tls.json +++ b/test/config/vmess-tls.json @@ -7,8 +7,7 @@ "settings": { "clients": [ { - "id": "b831381d-6324-4d53-ad4f-8cda48b30811", - "alterId": 32 + "id": "b831381d-6324-4d53-ad4f-8cda48b30811" } ] }, diff --git a/test/config/vmess-ws-0rtt.json b/test/config/vmess-ws-0rtt.json index 7e2876d0..c22909bf 100644 --- a/test/config/vmess-ws-0rtt.json +++ b/test/config/vmess-ws-0rtt.json @@ -7,8 +7,7 @@ "settings": { "clients": [ { - "id": "b831381d-6324-4d53-ad4f-8cda48b30811", - "alterId": 32 + "id": "b831381d-6324-4d53-ad4f-8cda48b30811" } ] }, diff --git a/test/config/vmess-ws-tls.json b/test/config/vmess-ws-tls.json index dda1e0c9..14278f3d 100644 --- a/test/config/vmess-ws-tls.json +++ b/test/config/vmess-ws-tls.json @@ -7,8 +7,7 @@ "settings": { "clients": [ { - "id": "b831381d-6324-4d53-ad4f-8cda48b30811", - "alterId": 32 + "id": "b831381d-6324-4d53-ad4f-8cda48b30811" } ] }, diff --git a/test/config/vmess-ws.json b/test/config/vmess-ws.json index 94ace4e3..2bcb604d 100644 --- a/test/config/vmess-ws.json +++ b/test/config/vmess-ws.json @@ -7,8 +7,7 @@ "settings": { "clients": [ { - "id": "b831381d-6324-4d53-ad4f-8cda48b30811", - "alterId": 32 + "id": "b831381d-6324-4d53-ad4f-8cda48b30811" } ] }, diff --git a/test/config/vmess.json b/test/config/vmess.json index bcf53cd8..1a8f9355 100644 --- a/test/config/vmess.json +++ b/test/config/vmess.json @@ -7,8 +7,7 @@ "settings": { "clients": [ { - "id": "b831381d-6324-4d53-ad4f-8cda48b30811", - "alterId": 32 + "id": "b831381d-6324-4d53-ad4f-8cda48b30811" } ] }, diff --git a/test/vmess_test.go b/test/vmess_test.go index 1483cf0a..bf66fcd8 100644 --- a/test/vmess_test.go +++ b/test/vmess_test.go @@ -34,51 +34,12 @@ func TestClash_Vmess(t *testing.T) { }) proxy, err := outbound.NewVmess(outbound.VmessOption{ - Name: "vmess", - Server: localIP.String(), - Port: 10002, - UUID: "b831381d-6324-4d53-ad4f-8cda48b30811", - Cipher: "auto", - AlterID: 32, - UDP: true, - }) - if err != nil { - assert.FailNow(t, err.Error()) - } - - time.Sleep(waitTime) - testSuit(t, proxy) -} - -func TestClash_VmessAEAD(t *testing.T) { - configPath := C.Path.Resolve("vmess-aead.json") - - cfg := &container.Config{ - Image: ImageVmess, - ExposedPorts: defaultExposedPorts, - } - hostCfg := &container.HostConfig{ - PortBindings: defaultPortBindings, - Binds: []string{fmt.Sprintf("%s:/etc/v2ray/config.json", configPath)}, - } - - id, err := startContainer(cfg, hostCfg, "vmess-aead") - if err != nil { - assert.FailNow(t, err.Error()) - } - - t.Cleanup(func() { - cleanContainer(id) - }) - - proxy, err := outbound.NewVmess(outbound.VmessOption{ - Name: "vmess", - Server: localIP.String(), - Port: 10002, - UUID: "b831381d-6324-4d53-ad4f-8cda48b30811", - Cipher: "auto", - AlterID: 0, - UDP: true, + Name: "vmess", + Server: localIP.String(), + Port: 10002, + UUID: "b831381d-6324-4d53-ad4f-8cda48b30811", + Cipher: "auto", + UDP: true, }) if err != nil { assert.FailNow(t, err.Error()) @@ -114,7 +75,6 @@ func TestClash_VmessTLS(t *testing.T) { Port: 10002, UUID: "b831381d-6324-4d53-ad4f-8cda48b30811", Cipher: "auto", - AlterID: 32, TLS: true, SkipCertVerify: true, ServerName: "example.org", @@ -154,7 +114,6 @@ func TestClash_VmessHTTP2(t *testing.T) { Port: 10002, UUID: "b831381d-6324-4d53-ad4f-8cda48b30811", Cipher: "auto", - AlterID: 32, Network: "h2", TLS: true, SkipCertVerify: true, @@ -197,7 +156,6 @@ func TestClash_VmessHTTP(t *testing.T) { Port: 10002, UUID: "b831381d-6324-4d53-ad4f-8cda48b30811", Cipher: "auto", - AlterID: 32, Network: "http", UDP: true, HTTPOpts: outbound.HTTPOptions{ @@ -250,7 +208,6 @@ func TestClash_VmessWebsocket(t *testing.T) { Port: 10002, UUID: "b831381d-6324-4d53-ad4f-8cda48b30811", Cipher: "auto", - AlterID: 32, Network: "ws", UDP: true, }) @@ -288,7 +245,6 @@ func TestClash_VmessWebsocketTLS(t *testing.T) { Port: 10002, UUID: "b831381d-6324-4d53-ad4f-8cda48b30811", Cipher: "auto", - AlterID: 32, Network: "ws", TLS: true, SkipCertVerify: true, @@ -328,7 +284,6 @@ func TestClash_VmessGrpc(t *testing.T) { Port: 10002, UUID: "b831381d-6324-4d53-ad4f-8cda48b30811", Cipher: "auto", - AlterID: 32, Network: "grpc", TLS: true, SkipCertVerify: true, @@ -370,7 +325,6 @@ func TestClash_VmessWebsocket0RTT(t *testing.T) { Port: 10002, UUID: "b831381d-6324-4d53-ad4f-8cda48b30811", Cipher: "auto", - AlterID: 32, Network: "ws", UDP: true, ServerName: "example.org", @@ -411,7 +365,6 @@ func TestClash_VmessWebsocketXray0RTT(t *testing.T) { Port: 10002, UUID: "b831381d-6324-4d53-ad4f-8cda48b30811", Cipher: "auto", - AlterID: 32, Network: "ws", UDP: true, ServerName: "example.org",