mirror of
https://github.com/SagerNet/sing-box.git
synced 2025-01-11 18:39:47 +00:00
Add vmess legacy server and test
This commit is contained in:
parent
b526ab2746
commit
7966b80476
2
go.mod
2
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
|
||||
|
|
4
go.sum
4
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=
|
||||
|
|
|
@ -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),
|
||||
)
|
||||
|
|
|
@ -9,6 +9,7 @@ type VMessInboundOptions struct {
|
|||
type VMessUser struct {
|
||||
Name string `json:"name"`
|
||||
UUID string `json:"uuid"`
|
||||
AlterId int `json:"alterId,omitempty"`
|
||||
}
|
||||
|
||||
type VMessOutboundOptions struct {
|
||||
|
|
|
@ -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))
|
||||
|
|
|
@ -25,6 +25,7 @@
|
|||
"users": [
|
||||
{
|
||||
"id": "",
|
||||
"alterId": 0,
|
||||
"security": "",
|
||||
"experiments": ""
|
||||
}
|
||||
|
|
|
@ -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,8 +85,8 @@ func startDockerContainer(t *testing.T, options DockerOptions) {
|
|||
require.NoError(t, err)
|
||||
stdinAttach.Close()
|
||||
}
|
||||
|
||||
/*attach, err := dockerClient.ContainerAttach(context.Background(), dockerContainer.ID, types.ContainerAttachOptions{
|
||||
if debug.Enabled {
|
||||
attach, err := dockerClient.ContainerAttach(context.Background(), dockerContainer.ID, types.ContainerAttachOptions{
|
||||
Stdout: true,
|
||||
Stderr: true,
|
||||
Logs: true,
|
||||
|
@ -92,7 +95,8 @@ func startDockerContainer(t *testing.T, options DockerOptions) {
|
|||
require.NoError(t, err)
|
||||
go func() {
|
||||
stdcopy.StdCopy(os.Stderr, os.Stderr, attach.Reader)
|
||||
}()*/
|
||||
}()
|
||||
}
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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=
|
||||
|
|
|
@ -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 {
|
||||
|
@ -145,6 +155,7 @@ func testVMessInboundWithV2Ray(t *testing.T, security string, uuid uuid.UUID, au
|
|||
{
|
||||
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",
|
||||
Output: "stderr",
|
||||
},
|
||||
Inbounds: []option.Inbound{
|
||||
{
|
||||
|
@ -239,6 +251,7 @@ func testVMessSelf(t *testing.T, security string, uuid uuid.UUID, globalPadding
|
|||
{
|
||||
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,
|
||||
},
|
||||
|
|
Loading…
Reference in a new issue