sing-box/test/vmess_test.go

338 lines
9.2 KiB
Go
Raw Normal View History

2022-07-18 12:32:31 +08:00
package main
import (
"net/netip"
2022-07-18 18:50:19 +08:00
"os"
2022-07-18 12:32:31 +08:00
"testing"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/option"
2024-11-18 18:59:19 +08:00
"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/json/badoption"
2022-07-18 12:32:31 +08:00
2023-04-09 15:37:06 +08:00
"github.com/gofrs/uuid/v5"
2022-07-18 18:50:19 +08:00
"github.com/spyzhov/ajson"
2022-07-18 12:32:31 +08:00
"github.com/stretchr/testify/require"
)
2022-09-12 21:59:27 +08:00
func newUUID() uuid.UUID {
user, _ := uuid.DefaultGenerator.NewV4()
return user
}
2022-09-23 13:14:31 +08:00
func TestVMessAuto(t *testing.T) {
2022-08-08 09:06:24 +08:00
security := "auto"
t.Run("self", func(t *testing.T) {
2022-09-12 21:59:27 +08:00
testVMessSelf(t, security, 0, false, false, false)
2022-08-27 11:28:01 +08:00
})
t.Run("packetaddr", func(t *testing.T) {
2022-09-12 21:59:27 +08:00
testVMessSelf(t, security, 0, false, false, true)
2022-08-08 09:06:24 +08:00
})
t.Run("inbound", func(t *testing.T) {
2022-09-12 21:59:27 +08:00
testVMessInboundWithV2Ray(t, security, 0, false)
2022-08-08 09:06:24 +08:00
})
t.Run("outbound", func(t *testing.T) {
2022-09-12 21:59:27 +08:00
testVMessOutboundWithV2Ray(t, security, false, false, 0)
2022-08-08 09:06:24 +08:00
})
}
2022-08-29 10:10:41 +08:00
func TestVMess(t *testing.T) {
2022-07-18 12:32:31 +08:00
for _, security := range []string{
"zero",
} {
t.Run(security, func(t *testing.T) {
2022-07-18 18:50:19 +08:00
testVMess0(t, security)
2022-07-18 12:32:31 +08:00
})
}
for _, security := range []string{
2022-08-29 10:10:41 +08:00
"none",
2022-07-18 12:32:31 +08:00
} {
t.Run(security, func(t *testing.T) {
2022-07-18 18:50:19 +08:00
testVMess1(t, security)
2022-07-18 12:32:31 +08:00
})
}
2022-08-29 10:10:41 +08:00
for _, security := range []string{
"aes-128-gcm", "chacha20-poly1305", "aes-128-cfb",
} {
t.Run(security, func(t *testing.T) {
testVMess2(t, security)
})
}
2022-07-18 12:32:31 +08:00
}
2022-07-18 18:50:19 +08:00
func testVMess0(t *testing.T, security string) {
t.Run("self", func(t *testing.T) {
2022-09-12 21:59:27 +08:00
testVMessSelf(t, security, 0, false, false, false)
2022-07-18 12:32:31 +08:00
})
2022-08-01 12:23:34 +08:00
t.Run("self-legacy", func(t *testing.T) {
2022-09-12 21:59:27 +08:00
testVMessSelf(t, security, 1, false, false, false)
2022-08-27 11:28:01 +08:00
})
t.Run("packetaddr", func(t *testing.T) {
2022-09-12 21:59:27 +08:00
testVMessSelf(t, security, 0, false, false, true)
2022-08-01 12:23:34 +08:00
})
2022-07-18 18:50:19 +08:00
t.Run("outbound", func(t *testing.T) {
2022-09-12 21:59:27 +08:00
testVMessOutboundWithV2Ray(t, security, false, false, 0)
2022-07-18 18:50:19 +08:00
})
t.Run("outbound-legacy", func(t *testing.T) {
2022-09-12 21:59:27 +08:00
testVMessOutboundWithV2Ray(t, security, false, false, 1)
2022-07-18 18:50:19 +08:00
})
2022-07-18 12:32:31 +08:00
}
2022-07-18 18:50:19 +08:00
func testVMess1(t *testing.T, security string) {
2022-08-29 10:10:41 +08:00
t.Run("self", func(t *testing.T) {
2022-09-12 21:59:27 +08:00
testVMessSelf(t, security, 0, false, false, false)
2022-08-29 10:10:41 +08:00
})
t.Run("self-legacy", func(t *testing.T) {
2022-09-12 21:59:27 +08:00
testVMessSelf(t, security, 1, false, false, false)
2022-08-29 10:10:41 +08:00
})
t.Run("packetaddr", func(t *testing.T) {
2022-09-12 21:59:27 +08:00
testVMessSelf(t, security, 0, false, false, true)
2022-08-29 10:10:41 +08:00
})
t.Run("inbound", func(t *testing.T) {
2022-09-12 21:59:27 +08:00
testVMessInboundWithV2Ray(t, security, 0, false)
2022-08-29 10:10:41 +08:00
})
t.Run("outbound", func(t *testing.T) {
2022-09-12 21:59:27 +08:00
testVMessOutboundWithV2Ray(t, security, false, false, 0)
2022-08-29 10:10:41 +08:00
})
t.Run("outbound-legacy", func(t *testing.T) {
2022-09-12 21:59:27 +08:00
testVMessOutboundWithV2Ray(t, security, false, false, 1)
2022-08-29 10:10:41 +08:00
})
}
func testVMess2(t *testing.T, security string) {
2022-07-18 18:50:19 +08:00
t.Run("self", func(t *testing.T) {
2022-09-12 21:59:27 +08:00
testVMessSelf(t, security, 0, false, false, false)
2022-07-18 18:50:19 +08:00
})
t.Run("self-padding", func(t *testing.T) {
2022-09-12 21:59:27 +08:00
testVMessSelf(t, security, 0, true, false, false)
2022-07-18 18:50:19 +08:00
})
t.Run("self-authid", func(t *testing.T) {
2022-09-12 21:59:27 +08:00
testVMessSelf(t, security, 0, false, true, false)
2022-07-18 18:50:19 +08:00
})
t.Run("self-padding-authid", func(t *testing.T) {
2022-09-12 21:59:27 +08:00
testVMessSelf(t, security, 0, true, true, false)
2022-08-01 12:23:34 +08:00
})
t.Run("self-legacy", func(t *testing.T) {
2022-09-12 21:59:27 +08:00
testVMessSelf(t, security, 1, false, false, false)
2022-08-01 12:23:34 +08:00
})
t.Run("self-legacy-padding", func(t *testing.T) {
2022-09-12 21:59:27 +08:00
testVMessSelf(t, security, 1, true, false, false)
2022-08-27 11:28:01 +08:00
})
t.Run("packetaddr", func(t *testing.T) {
2022-09-12 21:59:27 +08:00
testVMessSelf(t, security, 0, false, false, true)
2022-07-18 18:50:19 +08:00
})
t.Run("inbound", func(t *testing.T) {
2022-09-12 21:59:27 +08:00
testVMessInboundWithV2Ray(t, security, 0, false)
2022-07-18 18:50:19 +08:00
})
t.Run("inbound-authid", func(t *testing.T) {
2022-09-12 21:59:27 +08:00
testVMessInboundWithV2Ray(t, security, 0, true)
2022-08-01 12:23:34 +08:00
})
t.Run("inbound-legacy", func(t *testing.T) {
2022-09-12 21:59:27 +08:00
testVMessInboundWithV2Ray(t, security, 64, false)
2022-07-18 18:50:19 +08:00
})
t.Run("outbound", func(t *testing.T) {
2022-09-12 21:59:27 +08:00
testVMessOutboundWithV2Ray(t, security, false, false, 0)
2022-07-18 18:50:19 +08:00
})
t.Run("outbound-padding", func(t *testing.T) {
2022-09-12 21:59:27 +08:00
testVMessOutboundWithV2Ray(t, security, true, false, 0)
2022-07-18 18:50:19 +08:00
})
t.Run("outbound-authid", func(t *testing.T) {
2022-09-12 21:59:27 +08:00
testVMessOutboundWithV2Ray(t, security, false, true, 0)
2022-07-18 18:50:19 +08:00
})
t.Run("outbound-padding-authid", func(t *testing.T) {
2022-09-12 21:59:27 +08:00
testVMessOutboundWithV2Ray(t, security, true, true, 0)
2022-07-18 12:32:31 +08:00
})
2022-07-18 18:50:19 +08:00
t.Run("outbound-legacy", func(t *testing.T) {
2022-09-12 21:59:27 +08:00
testVMessOutboundWithV2Ray(t, security, false, false, 1)
2022-07-18 12:32:31 +08:00
})
2022-07-18 18:50:19 +08:00
t.Run("outbound-legacy-padding", func(t *testing.T) {
2022-09-12 21:59:27 +08:00
testVMessOutboundWithV2Ray(t, security, true, false, 1)
2022-07-18 12:32:31 +08:00
})
2022-07-18 18:50:19 +08:00
}
2022-09-12 21:59:27 +08:00
func testVMessInboundWithV2Ray(t *testing.T, security string, alterId int, authenticatedLength bool) {
userId := newUUID()
2022-07-18 18:50:19 +08:00
content, err := os.ReadFile("config/vmess-client.json")
require.NoError(t, err)
config, err := ajson.Unmarshal(content)
require.NoError(t, err)
config.MustKey("inbounds").MustIndex(0).MustKey("port").SetNumeric(float64(clientPort))
outbound := config.MustKey("outbounds").MustIndex(0).MustKey("settings").MustKey("vnext").MustIndex(0)
outbound.MustKey("port").SetNumeric(float64(serverPort))
user := outbound.MustKey("users").MustIndex(0)
2022-09-12 21:59:27 +08:00
user.MustKey("id").SetString(userId.String())
2022-08-01 12:23:34 +08:00
user.MustKey("alterId").SetNumeric(float64(alterId))
2022-07-18 18:50:19 +08:00
user.MustKey("security").SetString(security)
var experiments string
if authenticatedLength {
experiments += "AuthenticatedLength"
}
user.MustKey("experiments").SetString(experiments)
content, err = ajson.Marshal(config)
require.NoError(t, err)
startDockerContainer(t, DockerOptions{
Image: ImageV2RayCore,
Ports: []uint16{serverPort, testPort},
EntryPoint: "v2ray",
2023-07-11 14:03:55 +08:00
Cmd: []string{"run"},
2022-07-18 18:50:19 +08:00
Stdin: content,
Env: []string{"V2RAY_VMESS_AEAD_FORCED=false"},
})
startInstance(t, option.Options{
2024-11-18 18:59:19 +08:00
Inbounds: []option.Inbound{
2022-07-18 18:50:19 +08:00
{
Type: C.TypeVMess,
2024-11-18 18:59:19 +08:00
Options: &option.VMessInboundOptions{
2022-07-18 18:50:19 +08:00
ListenOptions: option.ListenOptions{
2024-11-18 18:59:19 +08:00
Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
2022-07-18 18:50:19 +08:00
ListenPort: serverPort,
},
Users: []option.VMessUser{
{
2022-08-01 12:23:34 +08:00
Name: "sekai",
2022-09-12 21:59:27 +08:00
UUID: userId.String(),
2022-08-01 12:23:34 +08:00
AlterId: alterId,
2022-07-18 18:50:19 +08:00
},
},
},
},
},
})
2022-08-29 10:10:41 +08:00
testSuitSimple(t, clientPort, testPort)
2022-07-18 18:50:19 +08:00
}
2022-09-12 21:59:27 +08:00
func testVMessOutboundWithV2Ray(t *testing.T, security string, globalPadding bool, authenticatedLength bool, alterId int) {
user := newUUID()
2022-07-18 18:50:19 +08:00
content, err := os.ReadFile("config/vmess-server.json")
require.NoError(t, err)
config, err := ajson.Unmarshal(content)
require.NoError(t, err)
inbound := config.MustKey("inbounds").MustIndex(0)
inbound.MustKey("port").SetNumeric(float64(serverPort))
2022-09-12 21:59:27 +08:00
inbound.MustKey("settings").MustKey("clients").MustIndex(0).MustKey("id").SetString(user.String())
2022-07-18 18:50:19 +08:00
inbound.MustKey("settings").MustKey("clients").MustIndex(0).MustKey("alterId").SetNumeric(float64(alterId))
content, err = ajson.Marshal(config)
require.NoError(t, err)
startDockerContainer(t, DockerOptions{
Image: ImageV2RayCore,
Ports: []uint16{serverPort, testPort},
EntryPoint: "v2ray",
2023-07-11 14:03:55 +08:00
Cmd: []string{"run"},
2022-07-18 18:50:19 +08:00
Stdin: content,
Env: []string{"V2RAY_VMESS_AEAD_FORCED=false"},
})
startInstance(t, option.Options{
2024-11-18 18:59:19 +08:00
Inbounds: []option.Inbound{
2022-07-18 18:50:19 +08:00
{
Type: C.TypeMixed,
2024-11-18 18:59:19 +08:00
Options: &option.HTTPMixedInboundOptions{
2022-07-18 18:50:19 +08:00
ListenOptions: option.ListenOptions{
2024-11-18 18:59:19 +08:00
Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
2022-07-18 18:50:19 +08:00
ListenPort: clientPort,
},
},
},
},
2024-11-18 18:59:19 +08:00
Outbounds: []option.Outbound{
2022-07-18 18:50:19 +08:00
{
Type: C.TypeVMess,
2024-11-18 18:59:19 +08:00
Options: &option.VMessOutboundOptions{
2022-07-18 18:50:19 +08:00
ServerOptions: option.ServerOptions{
Server: "127.0.0.1",
ServerPort: serverPort,
},
Security: security,
2022-09-12 21:59:27 +08:00
UUID: user.String(),
2022-07-18 18:50:19 +08:00
GlobalPadding: globalPadding,
AuthenticatedLength: authenticatedLength,
AlterId: alterId,
},
},
},
})
2022-09-13 10:41:10 +08:00
testSuit(t, clientPort, testPort)
2022-07-18 12:32:31 +08:00
}
2022-09-12 21:59:27 +08:00
func testVMessSelf(t *testing.T, security string, alterId int, globalPadding bool, authenticatedLength bool, packetAddr bool) {
user := newUUID()
2022-07-18 12:32:31 +08:00
startInstance(t, option.Options{
2024-11-18 18:59:19 +08:00
Inbounds: []option.Inbound{
2022-07-18 12:32:31 +08:00
{
Type: C.TypeMixed,
Tag: "mixed-in",
2024-11-18 18:59:19 +08:00
Options: &option.HTTPMixedInboundOptions{
2022-07-18 12:32:31 +08:00
ListenOptions: option.ListenOptions{
2024-11-18 18:59:19 +08:00
Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
2022-07-18 12:32:31 +08:00
ListenPort: clientPort,
},
},
},
{
Type: C.TypeVMess,
2024-11-18 18:59:19 +08:00
Options: &option.VMessInboundOptions{
2022-07-18 12:32:31 +08:00
ListenOptions: option.ListenOptions{
2024-11-18 18:59:19 +08:00
Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
2022-07-18 12:32:31 +08:00
ListenPort: serverPort,
},
Users: []option.VMessUser{
{
2022-08-01 12:23:34 +08:00
Name: "sekai",
2022-09-12 21:59:27 +08:00
UUID: user.String(),
2022-08-01 12:23:34 +08:00
AlterId: alterId,
2022-07-18 12:32:31 +08:00
},
},
},
},
},
2024-11-18 18:59:19 +08:00
Outbounds: []option.Outbound{
2022-07-18 12:32:31 +08:00
{
Type: C.TypeDirect,
},
{
Type: C.TypeVMess,
Tag: "vmess-out",
2024-11-18 18:59:19 +08:00
Options: &option.VMessOutboundOptions{
2022-07-18 12:32:31 +08:00
ServerOptions: option.ServerOptions{
Server: "127.0.0.1",
ServerPort: serverPort,
},
Security: security,
2022-09-12 21:59:27 +08:00
UUID: user.String(),
2022-08-01 12:23:34 +08:00
AlterId: alterId,
2022-07-18 12:32:31 +08:00
GlobalPadding: globalPadding,
AuthenticatedLength: authenticatedLength,
PacketEncoding: "packetaddr",
2022-07-18 12:32:31 +08:00
},
},
},
Route: &option.RouteOptions{
Rules: []option.Rule{
{
2024-10-21 23:38:34 +08:00
Type: C.RuleTypeDefault,
2022-07-18 12:32:31 +08:00
DefaultOptions: option.DefaultRule{
2024-10-21 23:38:34 +08:00
RawDefaultRule: option.RawDefaultRule{
Inbound: []string{"mixed-in"},
},
RuleAction: option.RuleAction{
Action: C.RuleActionTypeRoute,
RouteOptions: option.RouteActionOptions{
Outbound: "vmess-out",
},
},
2022-07-18 12:32:31 +08:00
},
},
},
},
})
testSuit(t, clientPort, testPort)
}