From 7966b80476b04ad74a459a6128319d03440209d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Mon, 1 Aug 2022 12:23:34 +0800 Subject: [PATCH] Add vmess legacy server and test --- go.mod | 2 +- go.sum | 4 +-- inbound/vmess.go | 14 ++++++--- option/vmess.go | 5 ++-- test/box_test.go | 17 +++++++---- test/config/vmess-client.json | 1 + test/docker_test.go | 26 +++++++++------- test/go.mod | 2 +- test/go.sum | 4 +-- test/vmess_test.go | 56 ++++++++++++++++++++++------------- 10 files changed, 81 insertions(+), 50 deletions(-) diff --git a/go.mod b/go.mod index cb9caaab..b9ffaa56 100644 --- a/go.mod +++ b/go.mod @@ -17,7 +17,7 @@ require ( github.com/sagernet/sing-dns v0.0.0-20220730061139-c8e0fb296da9 github.com/sagernet/sing-shadowsocks v0.0.0-20220730132258-5c45f99276b8 github.com/sagernet/sing-tun v0.0.0-20220731115551-4a805410a2e8 - github.com/sagernet/sing-vmess v0.0.0-20220730132251-bb961241a5d0 + github.com/sagernet/sing-vmess v0.0.0-20220801041428-8f2785c629c3 github.com/spf13/cobra v1.5.0 github.com/stretchr/testify v1.8.0 go.uber.org/atomic v1.9.0 diff --git a/go.sum b/go.sum index c1fa9349..57408fcf 100644 --- a/go.sum +++ b/go.sum @@ -157,8 +157,8 @@ github.com/sagernet/sing-shadowsocks v0.0.0-20220730132258-5c45f99276b8 h1:d6Sda github.com/sagernet/sing-shadowsocks v0.0.0-20220730132258-5c45f99276b8/go.mod h1:aNTIkd+bCIUgr5C43aZBGXDxIc0Y9NOozLLzH2vUydM= github.com/sagernet/sing-tun v0.0.0-20220731115551-4a805410a2e8 h1:xXpkIml1GbkNlQwlmdLWvXBmOBPHDEIjv/QSrKPsLgY= github.com/sagernet/sing-tun v0.0.0-20220731115551-4a805410a2e8/go.mod h1:1V/Scct3DGHi925AasPCj1k+6SRWIcg0TvRHM0ZXB8I= -github.com/sagernet/sing-vmess v0.0.0-20220730132251-bb961241a5d0 h1:BE+4nxv3zXp+BLmnkWyZRKo5fpoADZKRSw095FgdH3M= -github.com/sagernet/sing-vmess v0.0.0-20220730132251-bb961241a5d0/go.mod h1:NnKMgOr0WvyHsLICWTIz4xW3+yd3Ft2BtU8N5ycT4OU= +github.com/sagernet/sing-vmess v0.0.0-20220801041428-8f2785c629c3 h1:2ZTNZND525FxPkal2hiibrsVjtT8DRjAKA15PXkvggE= +github.com/sagernet/sing-vmess v0.0.0-20220801041428-8f2785c629c3/go.mod h1:NnKMgOr0WvyHsLICWTIz4xW3+yd3Ft2BtU8N5ycT4OU= github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= github.com/shurcooL/component v0.0.0-20170202220835-f88ec8f54cc4/go.mod h1:XhFIlyj5a1fBNx5aJTbKoIq0mNaPvOagO+HjB3EtxrY= github.com/shurcooL/events v0.0.0-20181021180414-410e4ca65f48/go.mod h1:5u70Mqkb5O5cxEA8nxTsgrgLehJeAw6Oc4Ab1c/P1HM= diff --git a/inbound/vmess.go b/inbound/vmess.go index 88b19495..28e4a53b 100644 --- a/inbound/vmess.go +++ b/inbound/vmess.go @@ -41,10 +41,12 @@ func NewVMess(ctx context.Context, router adapter.Router, logger log.ContextLogg users: options.Users, } service := vmess.NewService[int](adapter.NewUpstreamContextHandler(inbound.newConnection, inbound.newPacketConnection, inbound)) - err := service.UpdateUsers(common.MapIndexed(options.Users, func(index int, user option.VMessUser) int { + err := service.UpdateUsers(common.MapIndexed(options.Users, func(index int, it option.VMessUser) int { return index - }), common.Map(options.Users, func(user option.VMessUser) string { - return user.UUID + }), common.Map(options.Users, func(it option.VMessUser) string { + return it.UUID + }), common.Map(options.Users, func(it option.VMessUser) int { + return it.AlterId })) if err != nil { return nil, err @@ -68,11 +70,15 @@ func (h *VMess) Start() error { return E.Cause(err, "create TLS config") } } - return h.myInboundAdapter.Start() + return common.Start( + h.service, + &h.myInboundAdapter, + ) } func (h *VMess) Close() error { return common.Close( + h.service, &h.myInboundAdapter, common.PtrOrNil(h.tlsConfig), ) diff --git a/option/vmess.go b/option/vmess.go index 8899dab6..c1fe667d 100644 --- a/option/vmess.go +++ b/option/vmess.go @@ -7,8 +7,9 @@ type VMessInboundOptions struct { } type VMessUser struct { - Name string `json:"name"` - UUID string `json:"uuid"` + Name string `json:"name"` + UUID string `json:"uuid"` + AlterId int `json:"alterId,omitempty"` } type VMessOutboundOptions struct { diff --git a/test/box_test.go b/test/box_test.go index 02ac0da4..a2edad7a 100644 --- a/test/box_test.go +++ b/test/box_test.go @@ -35,20 +35,23 @@ func mkPort(t *testing.T) uint16 { } func startInstance(t *testing.T, options option.Options) { + var instance *box.Box var err error for retry := 0; retry < 3; retry++ { - instance, err := box.New(context.Background(), options) + instance, err = box.New(context.Background(), options) require.NoError(t, err) err = instance.Start() if err != nil { time.Sleep(5 * time.Millisecond) continue } - t.Cleanup(func() { - instance.Close() - }) + break } require.NoError(t, err) + t.Cleanup(func() { + time.Sleep(500 * time.Millisecond) + instance.Close() + }) } func testSuit(t *testing.T, clientPort uint16, testPort uint16) { @@ -59,7 +62,7 @@ func testSuit(t *testing.T, clientPort uint16, testPort uint16) { dialUDP := func() (net.PacketConn, error) { return dialer.ListenPacket(context.Background(), M.ParseSocksaddrHostPort("127.0.0.1", testPort)) } - t.Run("tcp", func(t *testing.T) { + /*t.Run("tcp", func(t *testing.T) { t.Parallel() var err error for retry := 0; retry < 3; retry++ { @@ -80,7 +83,9 @@ func testSuit(t *testing.T, clientPort uint16, testPort uint16) { } } require.NoError(t, err) - }) + })*/ + require.NoError(t, testLargeDataWithConn(t, testPort, dialTCP)) + require.NoError(t, testLargeDataWithPacketConn(t, testPort, dialUDP)) // require.NoError(t, testPingPongWithConn(t, testPort, dialTCP)) // require.NoError(t, testPingPongWithPacketConn(t, testPort, dialUDP)) // require.NoError(t, testPacketConnTimeout(t, dialUDP)) diff --git a/test/config/vmess-client.json b/test/config/vmess-client.json index b2787e2c..868b8b08 100644 --- a/test/config/vmess-client.json +++ b/test/config/vmess-client.json @@ -25,6 +25,7 @@ "users": [ { "id": "", + "alterId": 0, "security": "", "experiments": "" } diff --git a/test/docker_test.go b/test/docker_test.go index 2ed6c29f..dc203ca5 100644 --- a/test/docker_test.go +++ b/test/docker_test.go @@ -2,15 +2,18 @@ package main import ( "context" + "os" "testing" "time" C "github.com/sagernet/sing-box/constant" + "github.com/sagernet/sing/common/debug" F "github.com/sagernet/sing/common/format" "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/container" "github.com/docker/docker/client" + "github.com/docker/docker/pkg/stdcopy" "github.com/docker/go-connections/nat" "github.com/stretchr/testify/require" ) @@ -82,17 +85,18 @@ func startDockerContainer(t *testing.T, options DockerOptions) { require.NoError(t, err) stdinAttach.Close() } - - /*attach, err := dockerClient.ContainerAttach(context.Background(), dockerContainer.ID, types.ContainerAttachOptions{ - Stdout: true, - Stderr: true, - Logs: true, - Stream: true, - }) - require.NoError(t, err) - go func() { - stdcopy.StdCopy(os.Stderr, os.Stderr, attach.Reader) - }()*/ + if debug.Enabled { + attach, err := dockerClient.ContainerAttach(context.Background(), dockerContainer.ID, types.ContainerAttachOptions{ + Stdout: true, + Stderr: true, + Logs: true, + Stream: true, + }) + require.NoError(t, err) + go func() { + stdcopy.StdCopy(os.Stderr, os.Stderr, attach.Reader) + }() + } time.Sleep(time.Second) } diff --git a/test/go.mod b/test/go.mod index bc3a7be9..ae19ca0a 100644 --- a/test/go.mod +++ b/test/go.mod @@ -54,7 +54,7 @@ require ( github.com/pmezard/go-difflib v1.0.0 // indirect github.com/sagernet/sing-dns v0.0.0-20220730061139-c8e0fb296da9 // indirect github.com/sagernet/sing-tun v0.0.0-20220731115551-4a805410a2e8 // indirect - github.com/sagernet/sing-vmess v0.0.0-20220730132251-bb961241a5d0 // indirect + github.com/sagernet/sing-vmess v0.0.0-20220801041428-8f2785c629c3 // indirect github.com/sirupsen/logrus v1.8.1 // indirect github.com/vishvananda/netlink v1.1.0 // indirect github.com/vishvananda/netns v0.0.0-20211101163701-50045581ed74 // indirect diff --git a/test/go.sum b/test/go.sum index a7e6e289..21c9d61d 100644 --- a/test/go.sum +++ b/test/go.sum @@ -180,8 +180,8 @@ github.com/sagernet/sing-shadowsocks v0.0.0-20220730132258-5c45f99276b8 h1:d6Sda github.com/sagernet/sing-shadowsocks v0.0.0-20220730132258-5c45f99276b8/go.mod h1:aNTIkd+bCIUgr5C43aZBGXDxIc0Y9NOozLLzH2vUydM= github.com/sagernet/sing-tun v0.0.0-20220731115551-4a805410a2e8 h1:xXpkIml1GbkNlQwlmdLWvXBmOBPHDEIjv/QSrKPsLgY= github.com/sagernet/sing-tun v0.0.0-20220731115551-4a805410a2e8/go.mod h1:1V/Scct3DGHi925AasPCj1k+6SRWIcg0TvRHM0ZXB8I= -github.com/sagernet/sing-vmess v0.0.0-20220730132251-bb961241a5d0 h1:BE+4nxv3zXp+BLmnkWyZRKo5fpoADZKRSw095FgdH3M= -github.com/sagernet/sing-vmess v0.0.0-20220730132251-bb961241a5d0/go.mod h1:NnKMgOr0WvyHsLICWTIz4xW3+yd3Ft2BtU8N5ycT4OU= +github.com/sagernet/sing-vmess v0.0.0-20220801041428-8f2785c629c3 h1:2ZTNZND525FxPkal2hiibrsVjtT8DRjAKA15PXkvggE= +github.com/sagernet/sing-vmess v0.0.0-20220801041428-8f2785c629c3/go.mod h1:NnKMgOr0WvyHsLICWTIz4xW3+yd3Ft2BtU8N5ycT4OU= github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= github.com/shurcooL/component v0.0.0-20170202220835-f88ec8f54cc4/go.mod h1:XhFIlyj5a1fBNx5aJTbKoIq0mNaPvOagO+HjB3EtxrY= github.com/shurcooL/events v0.0.0-20181021180414-410e4ca65f48/go.mod h1:5u70Mqkb5O5cxEA8nxTsgrgLehJeAw6Oc4Ab1c/P1HM= diff --git a/test/vmess_test.go b/test/vmess_test.go index 00ee69ca..1e1a1fcf 100644 --- a/test/vmess_test.go +++ b/test/vmess_test.go @@ -34,10 +34,16 @@ func testVMess0(t *testing.T, security string) { user, err := uuid.DefaultGenerator.NewV4() require.NoError(t, err) t.Run("self", func(t *testing.T) { - testVMessSelf(t, security, user, false, false) + testVMessSelf(t, security, user, 0, false, false) }) t.Run("self-padding", func(t *testing.T) { - testVMessSelf(t, security, user, true, false) + testVMessSelf(t, security, user, 0, true, false) + }) + t.Run("self-legacy", func(t *testing.T) { + testVMessSelf(t, security, user, 1, false, false) + }) + t.Run("self-legacy-padding", func(t *testing.T) { + testVMessSelf(t, security, user, 1, true, false) }) t.Run("outbound", func(t *testing.T) { testVMessOutboundWithV2Ray(t, security, user, false, false, 0) @@ -57,22 +63,31 @@ func testVMess1(t *testing.T, security string) { user, err := uuid.DefaultGenerator.NewV4() require.NoError(t, err) t.Run("self", func(t *testing.T) { - testVMessSelf(t, security, user, false, false) + testVMessSelf(t, security, user, 0, false, false) }) t.Run("self-padding", func(t *testing.T) { - testVMessSelf(t, security, user, true, false) + testVMessSelf(t, security, user, 0, true, false) }) t.Run("self-authid", func(t *testing.T) { - testVMessSelf(t, security, user, false, true) + testVMessSelf(t, security, user, 0, false, true) }) t.Run("self-padding-authid", func(t *testing.T) { - testVMessSelf(t, security, user, true, true) + testVMessSelf(t, security, user, 0, true, true) + }) + t.Run("self-legacy", func(t *testing.T) { + testVMessSelf(t, security, user, 1, false, false) + }) + t.Run("self-legacy-padding", func(t *testing.T) { + testVMessSelf(t, security, user, 1, true, false) }) t.Run("inbound", func(t *testing.T) { - testVMessInboundWithV2Ray(t, security, user, false) + testVMessInboundWithV2Ray(t, security, user, 0, false) }) t.Run("inbound-authid", func(t *testing.T) { - testVMessInboundWithV2Ray(t, security, user, true) + testVMessInboundWithV2Ray(t, security, user, 0, true) + }) + t.Run("inbound-legacy", func(t *testing.T) { + testVMessInboundWithV2Ray(t, security, user, 64, false) }) t.Run("outbound", func(t *testing.T) { testVMessOutboundWithV2Ray(t, security, user, false, false, 0) @@ -92,15 +107,9 @@ func testVMess1(t *testing.T, security string) { t.Run("outbound-legacy-padding", func(t *testing.T) { testVMessOutboundWithV2Ray(t, security, user, true, false, 1) }) - t.Run("outbound-legacy-authid", func(t *testing.T) { - testVMessOutboundWithV2Ray(t, security, user, false, true, 1) - }) - t.Run("outbound-legacy-padding-authid", func(t *testing.T) { - testVMessOutboundWithV2Ray(t, security, user, true, true, 1) - }) } -func testVMessInboundWithV2Ray(t *testing.T, security string, uuid uuid.UUID, authenticatedLength bool) { +func testVMessInboundWithV2Ray(t *testing.T, security string, uuid uuid.UUID, alterId int, authenticatedLength bool) { content, err := os.ReadFile("config/vmess-client.json") require.NoError(t, err) config, err := ajson.Unmarshal(content) @@ -111,6 +120,7 @@ func testVMessInboundWithV2Ray(t *testing.T, security string, uuid uuid.UUID, au outbound.MustKey("port").SetNumeric(float64(serverPort)) user := outbound.MustKey("users").MustIndex(0) user.MustKey("id").SetString(uuid.String()) + user.MustKey("alterId").SetNumeric(float64(alterId)) user.MustKey("security").SetString(security) var experiments string if authenticatedLength { @@ -143,8 +153,9 @@ func testVMessInboundWithV2Ray(t *testing.T, security string, uuid uuid.UUID, au }, Users: []option.VMessUser{ { - Name: "sekai", - UUID: uuid.String(), + Name: "sekai", + UUID: uuid.String(), + AlterId: alterId, }, }, }, @@ -212,10 +223,11 @@ func testVMessOutboundWithV2Ray(t *testing.T, security string, uuid uuid.UUID, g testSuit(t, clientPort, testPort) } -func testVMessSelf(t *testing.T, security string, uuid uuid.UUID, globalPadding bool, authenticatedLength bool) { +func testVMessSelf(t *testing.T, security string, uuid uuid.UUID, alterId int, globalPadding bool, authenticatedLength bool) { startInstance(t, option.Options{ Log: &option.LogOptions{ - Level: "error", + Level: "error", + Output: "stderr", }, Inbounds: []option.Inbound{ { @@ -237,8 +249,9 @@ func testVMessSelf(t *testing.T, security string, uuid uuid.UUID, globalPadding }, Users: []option.VMessUser{ { - Name: "sekai", - UUID: uuid.String(), + Name: "sekai", + UUID: uuid.String(), + AlterId: alterId, }, }, }, @@ -258,6 +271,7 @@ func testVMessSelf(t *testing.T, security string, uuid uuid.UUID, globalPadding }, Security: security, UUID: uuid.String(), + AlterId: alterId, GlobalPadding: globalPadding, AuthenticatedLength: authenticatedLength, },