Skip to content

Commit 7966b80

Browse files
committed
Add vmess legacy server and test
1 parent b526ab2 commit 7966b80

File tree

10 files changed

+81
-50
lines changed

10 files changed

+81
-50
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ require (
1717
github.com/sagernet/sing-dns v0.0.0-20220730061139-c8e0fb296da9
1818
github.com/sagernet/sing-shadowsocks v0.0.0-20220730132258-5c45f99276b8
1919
github.com/sagernet/sing-tun v0.0.0-20220731115551-4a805410a2e8
20-
github.com/sagernet/sing-vmess v0.0.0-20220730132251-bb961241a5d0
20+
github.com/sagernet/sing-vmess v0.0.0-20220801041428-8f2785c629c3
2121
github.com/spf13/cobra v1.5.0
2222
github.com/stretchr/testify v1.8.0
2323
go.uber.org/atomic v1.9.0

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,8 @@ github.com/sagernet/sing-shadowsocks v0.0.0-20220730132258-5c45f99276b8 h1:d6Sda
157157
github.com/sagernet/sing-shadowsocks v0.0.0-20220730132258-5c45f99276b8/go.mod h1:aNTIkd+bCIUgr5C43aZBGXDxIc0Y9NOozLLzH2vUydM=
158158
github.com/sagernet/sing-tun v0.0.0-20220731115551-4a805410a2e8 h1:xXpkIml1GbkNlQwlmdLWvXBmOBPHDEIjv/QSrKPsLgY=
159159
github.com/sagernet/sing-tun v0.0.0-20220731115551-4a805410a2e8/go.mod h1:1V/Scct3DGHi925AasPCj1k+6SRWIcg0TvRHM0ZXB8I=
160-
github.com/sagernet/sing-vmess v0.0.0-20220730132251-bb961241a5d0 h1:BE+4nxv3zXp+BLmnkWyZRKo5fpoADZKRSw095FgdH3M=
161-
github.com/sagernet/sing-vmess v0.0.0-20220730132251-bb961241a5d0/go.mod h1:NnKMgOr0WvyHsLICWTIz4xW3+yd3Ft2BtU8N5ycT4OU=
160+
github.com/sagernet/sing-vmess v0.0.0-20220801041428-8f2785c629c3 h1:2ZTNZND525FxPkal2hiibrsVjtT8DRjAKA15PXkvggE=
161+
github.com/sagernet/sing-vmess v0.0.0-20220801041428-8f2785c629c3/go.mod h1:NnKMgOr0WvyHsLICWTIz4xW3+yd3Ft2BtU8N5ycT4OU=
162162
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
163163
github.com/shurcooL/component v0.0.0-20170202220835-f88ec8f54cc4/go.mod h1:XhFIlyj5a1fBNx5aJTbKoIq0mNaPvOagO+HjB3EtxrY=
164164
github.com/shurcooL/events v0.0.0-20181021180414-410e4ca65f48/go.mod h1:5u70Mqkb5O5cxEA8nxTsgrgLehJeAw6Oc4Ab1c/P1HM=

inbound/vmess.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,12 @@ func NewVMess(ctx context.Context, router adapter.Router, logger log.ContextLogg
4141
users: options.Users,
4242
}
4343
service := vmess.NewService[int](adapter.NewUpstreamContextHandler(inbound.newConnection, inbound.newPacketConnection, inbound))
44-
err := service.UpdateUsers(common.MapIndexed(options.Users, func(index int, user option.VMessUser) int {
44+
err := service.UpdateUsers(common.MapIndexed(options.Users, func(index int, it option.VMessUser) int {
4545
return index
46-
}), common.Map(options.Users, func(user option.VMessUser) string {
47-
return user.UUID
46+
}), common.Map(options.Users, func(it option.VMessUser) string {
47+
return it.UUID
48+
}), common.Map(options.Users, func(it option.VMessUser) int {
49+
return it.AlterId
4850
}))
4951
if err != nil {
5052
return nil, err
@@ -68,11 +70,15 @@ func (h *VMess) Start() error {
6870
return E.Cause(err, "create TLS config")
6971
}
7072
}
71-
return h.myInboundAdapter.Start()
73+
return common.Start(
74+
h.service,
75+
&h.myInboundAdapter,
76+
)
7277
}
7378

7479
func (h *VMess) Close() error {
7580
return common.Close(
81+
h.service,
7682
&h.myInboundAdapter,
7783
common.PtrOrNil(h.tlsConfig),
7884
)

option/vmess.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ type VMessInboundOptions struct {
77
}
88

99
type VMessUser struct {
10-
Name string `json:"name"`
11-
UUID string `json:"uuid"`
10+
Name string `json:"name"`
11+
UUID string `json:"uuid"`
12+
AlterId int `json:"alterId,omitempty"`
1213
}
1314

1415
type VMessOutboundOptions struct {

test/box_test.go

+11-6
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,23 @@ func mkPort(t *testing.T) uint16 {
3535
}
3636

3737
func startInstance(t *testing.T, options option.Options) {
38+
var instance *box.Box
3839
var err error
3940
for retry := 0; retry < 3; retry++ {
40-
instance, err := box.New(context.Background(), options)
41+
instance, err = box.New(context.Background(), options)
4142
require.NoError(t, err)
4243
err = instance.Start()
4344
if err != nil {
4445
time.Sleep(5 * time.Millisecond)
4546
continue
4647
}
47-
t.Cleanup(func() {
48-
instance.Close()
49-
})
48+
break
5049
}
5150
require.NoError(t, err)
51+
t.Cleanup(func() {
52+
time.Sleep(500 * time.Millisecond)
53+
instance.Close()
54+
})
5255
}
5356

5457
func testSuit(t *testing.T, clientPort uint16, testPort uint16) {
@@ -59,7 +62,7 @@ func testSuit(t *testing.T, clientPort uint16, testPort uint16) {
5962
dialUDP := func() (net.PacketConn, error) {
6063
return dialer.ListenPacket(context.Background(), M.ParseSocksaddrHostPort("127.0.0.1", testPort))
6164
}
62-
t.Run("tcp", func(t *testing.T) {
65+
/*t.Run("tcp", func(t *testing.T) {
6366
t.Parallel()
6467
var err error
6568
for retry := 0; retry < 3; retry++ {
@@ -80,7 +83,9 @@ func testSuit(t *testing.T, clientPort uint16, testPort uint16) {
8083
}
8184
}
8285
require.NoError(t, err)
83-
})
86+
})*/
87+
require.NoError(t, testLargeDataWithConn(t, testPort, dialTCP))
88+
require.NoError(t, testLargeDataWithPacketConn(t, testPort, dialUDP))
8489
// require.NoError(t, testPingPongWithConn(t, testPort, dialTCP))
8590
// require.NoError(t, testPingPongWithPacketConn(t, testPort, dialUDP))
8691
// require.NoError(t, testPacketConnTimeout(t, dialUDP))

test/config/vmess-client.json

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"users": [
2626
{
2727
"id": "",
28+
"alterId": 0,
2829
"security": "",
2930
"experiments": ""
3031
}

test/docker_test.go

+15-11
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@ package main
22

33
import (
44
"context"
5+
"os"
56
"testing"
67
"time"
78

89
C "github.com/sagernet/sing-box/constant"
10+
"github.com/sagernet/sing/common/debug"
911
F "github.com/sagernet/sing/common/format"
1012

1113
"github.com/docker/docker/api/types"
1214
"github.com/docker/docker/api/types/container"
1315
"github.com/docker/docker/client"
16+
"github.com/docker/docker/pkg/stdcopy"
1417
"github.com/docker/go-connections/nat"
1518
"github.com/stretchr/testify/require"
1619
)
@@ -82,17 +85,18 @@ func startDockerContainer(t *testing.T, options DockerOptions) {
8285
require.NoError(t, err)
8386
stdinAttach.Close()
8487
}
85-
86-
/*attach, err := dockerClient.ContainerAttach(context.Background(), dockerContainer.ID, types.ContainerAttachOptions{
87-
Stdout: true,
88-
Stderr: true,
89-
Logs: true,
90-
Stream: true,
91-
})
92-
require.NoError(t, err)
93-
go func() {
94-
stdcopy.StdCopy(os.Stderr, os.Stderr, attach.Reader)
95-
}()*/
88+
if debug.Enabled {
89+
attach, err := dockerClient.ContainerAttach(context.Background(), dockerContainer.ID, types.ContainerAttachOptions{
90+
Stdout: true,
91+
Stderr: true,
92+
Logs: true,
93+
Stream: true,
94+
})
95+
require.NoError(t, err)
96+
go func() {
97+
stdcopy.StdCopy(os.Stderr, os.Stderr, attach.Reader)
98+
}()
99+
}
96100
time.Sleep(time.Second)
97101
}
98102

test/go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ require (
5454
github.com/pmezard/go-difflib v1.0.0 // indirect
5555
github.com/sagernet/sing-dns v0.0.0-20220730061139-c8e0fb296da9 // indirect
5656
github.com/sagernet/sing-tun v0.0.0-20220731115551-4a805410a2e8 // indirect
57-
github.com/sagernet/sing-vmess v0.0.0-20220730132251-bb961241a5d0 // indirect
57+
github.com/sagernet/sing-vmess v0.0.0-20220801041428-8f2785c629c3 // indirect
5858
github.com/sirupsen/logrus v1.8.1 // indirect
5959
github.com/vishvananda/netlink v1.1.0 // indirect
6060
github.com/vishvananda/netns v0.0.0-20211101163701-50045581ed74 // indirect

test/go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,8 @@ github.com/sagernet/sing-shadowsocks v0.0.0-20220730132258-5c45f99276b8 h1:d6Sda
180180
github.com/sagernet/sing-shadowsocks v0.0.0-20220730132258-5c45f99276b8/go.mod h1:aNTIkd+bCIUgr5C43aZBGXDxIc0Y9NOozLLzH2vUydM=
181181
github.com/sagernet/sing-tun v0.0.0-20220731115551-4a805410a2e8 h1:xXpkIml1GbkNlQwlmdLWvXBmOBPHDEIjv/QSrKPsLgY=
182182
github.com/sagernet/sing-tun v0.0.0-20220731115551-4a805410a2e8/go.mod h1:1V/Scct3DGHi925AasPCj1k+6SRWIcg0TvRHM0ZXB8I=
183-
github.com/sagernet/sing-vmess v0.0.0-20220730132251-bb961241a5d0 h1:BE+4nxv3zXp+BLmnkWyZRKo5fpoADZKRSw095FgdH3M=
184-
github.com/sagernet/sing-vmess v0.0.0-20220730132251-bb961241a5d0/go.mod h1:NnKMgOr0WvyHsLICWTIz4xW3+yd3Ft2BtU8N5ycT4OU=
183+
github.com/sagernet/sing-vmess v0.0.0-20220801041428-8f2785c629c3 h1:2ZTNZND525FxPkal2hiibrsVjtT8DRjAKA15PXkvggE=
184+
github.com/sagernet/sing-vmess v0.0.0-20220801041428-8f2785c629c3/go.mod h1:NnKMgOr0WvyHsLICWTIz4xW3+yd3Ft2BtU8N5ycT4OU=
185185
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
186186
github.com/shurcooL/component v0.0.0-20170202220835-f88ec8f54cc4/go.mod h1:XhFIlyj5a1fBNx5aJTbKoIq0mNaPvOagO+HjB3EtxrY=
187187
github.com/shurcooL/events v0.0.0-20181021180414-410e4ca65f48/go.mod h1:5u70Mqkb5O5cxEA8nxTsgrgLehJeAw6Oc4Ab1c/P1HM=

test/vmess_test.go

+35-21
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,16 @@ func testVMess0(t *testing.T, security string) {
3434
user, err := uuid.DefaultGenerator.NewV4()
3535
require.NoError(t, err)
3636
t.Run("self", func(t *testing.T) {
37-
testVMessSelf(t, security, user, false, false)
37+
testVMessSelf(t, security, user, 0, false, false)
3838
})
3939
t.Run("self-padding", func(t *testing.T) {
40-
testVMessSelf(t, security, user, true, false)
40+
testVMessSelf(t, security, user, 0, true, false)
41+
})
42+
t.Run("self-legacy", func(t *testing.T) {
43+
testVMessSelf(t, security, user, 1, false, false)
44+
})
45+
t.Run("self-legacy-padding", func(t *testing.T) {
46+
testVMessSelf(t, security, user, 1, true, false)
4147
})
4248
t.Run("outbound", func(t *testing.T) {
4349
testVMessOutboundWithV2Ray(t, security, user, false, false, 0)
@@ -57,22 +63,31 @@ func testVMess1(t *testing.T, security string) {
5763
user, err := uuid.DefaultGenerator.NewV4()
5864
require.NoError(t, err)
5965
t.Run("self", func(t *testing.T) {
60-
testVMessSelf(t, security, user, false, false)
66+
testVMessSelf(t, security, user, 0, false, false)
6167
})
6268
t.Run("self-padding", func(t *testing.T) {
63-
testVMessSelf(t, security, user, true, false)
69+
testVMessSelf(t, security, user, 0, true, false)
6470
})
6571
t.Run("self-authid", func(t *testing.T) {
66-
testVMessSelf(t, security, user, false, true)
72+
testVMessSelf(t, security, user, 0, false, true)
6773
})
6874
t.Run("self-padding-authid", func(t *testing.T) {
69-
testVMessSelf(t, security, user, true, true)
75+
testVMessSelf(t, security, user, 0, true, true)
76+
})
77+
t.Run("self-legacy", func(t *testing.T) {
78+
testVMessSelf(t, security, user, 1, false, false)
79+
})
80+
t.Run("self-legacy-padding", func(t *testing.T) {
81+
testVMessSelf(t, security, user, 1, true, false)
7082
})
7183
t.Run("inbound", func(t *testing.T) {
72-
testVMessInboundWithV2Ray(t, security, user, false)
84+
testVMessInboundWithV2Ray(t, security, user, 0, false)
7385
})
7486
t.Run("inbound-authid", func(t *testing.T) {
75-
testVMessInboundWithV2Ray(t, security, user, true)
87+
testVMessInboundWithV2Ray(t, security, user, 0, true)
88+
})
89+
t.Run("inbound-legacy", func(t *testing.T) {
90+
testVMessInboundWithV2Ray(t, security, user, 64, false)
7691
})
7792
t.Run("outbound", func(t *testing.T) {
7893
testVMessOutboundWithV2Ray(t, security, user, false, false, 0)
@@ -92,15 +107,9 @@ func testVMess1(t *testing.T, security string) {
92107
t.Run("outbound-legacy-padding", func(t *testing.T) {
93108
testVMessOutboundWithV2Ray(t, security, user, true, false, 1)
94109
})
95-
t.Run("outbound-legacy-authid", func(t *testing.T) {
96-
testVMessOutboundWithV2Ray(t, security, user, false, true, 1)
97-
})
98-
t.Run("outbound-legacy-padding-authid", func(t *testing.T) {
99-
testVMessOutboundWithV2Ray(t, security, user, true, true, 1)
100-
})
101110
}
102111

103-
func testVMessInboundWithV2Ray(t *testing.T, security string, uuid uuid.UUID, authenticatedLength bool) {
112+
func testVMessInboundWithV2Ray(t *testing.T, security string, uuid uuid.UUID, alterId int, authenticatedLength bool) {
104113
content, err := os.ReadFile("config/vmess-client.json")
105114
require.NoError(t, err)
106115
config, err := ajson.Unmarshal(content)
@@ -111,6 +120,7 @@ func testVMessInboundWithV2Ray(t *testing.T, security string, uuid uuid.UUID, au
111120
outbound.MustKey("port").SetNumeric(float64(serverPort))
112121
user := outbound.MustKey("users").MustIndex(0)
113122
user.MustKey("id").SetString(uuid.String())
123+
user.MustKey("alterId").SetNumeric(float64(alterId))
114124
user.MustKey("security").SetString(security)
115125
var experiments string
116126
if authenticatedLength {
@@ -143,8 +153,9 @@ func testVMessInboundWithV2Ray(t *testing.T, security string, uuid uuid.UUID, au
143153
},
144154
Users: []option.VMessUser{
145155
{
146-
Name: "sekai",
147-
UUID: uuid.String(),
156+
Name: "sekai",
157+
UUID: uuid.String(),
158+
AlterId: alterId,
148159
},
149160
},
150161
},
@@ -212,10 +223,11 @@ func testVMessOutboundWithV2Ray(t *testing.T, security string, uuid uuid.UUID, g
212223
testSuit(t, clientPort, testPort)
213224
}
214225

215-
func testVMessSelf(t *testing.T, security string, uuid uuid.UUID, globalPadding bool, authenticatedLength bool) {
226+
func testVMessSelf(t *testing.T, security string, uuid uuid.UUID, alterId int, globalPadding bool, authenticatedLength bool) {
216227
startInstance(t, option.Options{
217228
Log: &option.LogOptions{
218-
Level: "error",
229+
Level: "error",
230+
Output: "stderr",
219231
},
220232
Inbounds: []option.Inbound{
221233
{
@@ -237,8 +249,9 @@ func testVMessSelf(t *testing.T, security string, uuid uuid.UUID, globalPadding
237249
},
238250
Users: []option.VMessUser{
239251
{
240-
Name: "sekai",
241-
UUID: uuid.String(),
252+
Name: "sekai",
253+
UUID: uuid.String(),
254+
AlterId: alterId,
242255
},
243256
},
244257
},
@@ -258,6 +271,7 @@ func testVMessSelf(t *testing.T, security string, uuid uuid.UUID, globalPadding
258271
},
259272
Security: security,
260273
UUID: uuid.String(),
274+
AlterId: alterId,
261275
GlobalPadding: globalPadding,
262276
AuthenticatedLength: authenticatedLength,
263277
},

0 commit comments

Comments
 (0)