sing-box/test/vmess_test.go

335 lines
9.4 KiB
Go
Raw Normal View History

2022-07-18 04:32:31 +00:00
package main
import (
"net/netip"
2022-07-18 10:50:19 +00:00
"os"
2022-07-18 04:32:31 +00:00
"testing"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/option"
"github.com/gofrs/uuid"
2022-07-18 10:50:19 +00:00
"github.com/spyzhov/ajson"
2022-07-18 04:32:31 +00:00
"github.com/stretchr/testify/require"
)
2022-08-29 02:10:41 +00:00
func _TestVMessAuto(t *testing.T) {
2022-08-08 01:06:24 +00:00
security := "auto"
user, err := uuid.DefaultGenerator.NewV4()
require.NoError(t, err)
t.Run("self", func(t *testing.T) {
2022-08-27 03:28:01 +00:00
testVMessSelf(t, security, user, 0, false, false, false)
})
t.Run("packetaddr", func(t *testing.T) {
testVMessSelf(t, security, user, 0, false, false, true)
2022-08-08 01:06:24 +00:00
})
t.Run("inbound", func(t *testing.T) {
testVMessInboundWithV2Ray(t, security, user, 0, false)
})
t.Run("outbound", func(t *testing.T) {
testVMessOutboundWithV2Ray(t, security, user, false, false, 0)
})
}
2022-08-29 02:10:41 +00:00
func TestVMess(t *testing.T) {
2022-07-18 04:32:31 +00:00
for _, security := range []string{
"zero",
} {
t.Run(security, func(t *testing.T) {
2022-07-18 10:50:19 +00:00
testVMess0(t, security)
2022-07-18 04:32:31 +00:00
})
}
for _, security := range []string{
2022-08-29 02:10:41 +00:00
"none",
2022-07-18 04:32:31 +00:00
} {
t.Run(security, func(t *testing.T) {
2022-07-18 10:50:19 +00:00
testVMess1(t, security)
2022-07-18 04:32:31 +00:00
})
}
2022-08-29 02:10:41 +00: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 04:32:31 +00:00
}
2022-07-18 10:50:19 +00:00
func testVMess0(t *testing.T, security string) {
2022-07-18 04:32:31 +00:00
user, err := uuid.DefaultGenerator.NewV4()
require.NoError(t, err)
2022-07-18 10:50:19 +00:00
t.Run("self", func(t *testing.T) {
2022-08-27 03:28:01 +00:00
testVMessSelf(t, security, user, 0, false, false, false)
2022-07-18 04:32:31 +00:00
})
2022-08-01 04:23:34 +00:00
t.Run("self-legacy", func(t *testing.T) {
2022-08-27 03:28:01 +00:00
testVMessSelf(t, security, user, 1, false, false, false)
})
t.Run("packetaddr", func(t *testing.T) {
testVMessSelf(t, security, user, 0, false, false, true)
2022-08-01 04:23:34 +00:00
})
2022-07-18 10:50:19 +00:00
t.Run("outbound", func(t *testing.T) {
testVMessOutboundWithV2Ray(t, security, user, false, false, 0)
})
t.Run("outbound-legacy", func(t *testing.T) {
testVMessOutboundWithV2Ray(t, security, user, false, false, 1)
})
2022-07-18 04:32:31 +00:00
}
2022-07-18 10:50:19 +00:00
func testVMess1(t *testing.T, security string) {
2022-08-29 02:10:41 +00:00
user, err := uuid.DefaultGenerator.NewV4()
require.NoError(t, err)
t.Run("self", func(t *testing.T) {
testVMessSelf(t, security, user, 0, false, false, false)
})
t.Run("self-legacy", func(t *testing.T) {
testVMessSelf(t, security, user, 1, false, false, false)
})
t.Run("packetaddr", func(t *testing.T) {
testVMessSelf(t, security, user, 0, false, false, true)
})
t.Run("inbound", func(t *testing.T) {
testVMessInboundWithV2Ray(t, security, user, 0, false)
})
t.Run("outbound", func(t *testing.T) {
testVMessOutboundWithV2Ray(t, security, user, false, false, 0)
})
t.Run("outbound-legacy", func(t *testing.T) {
testVMessOutboundWithV2Ray(t, security, user, false, false, 1)
})
}
func testVMess2(t *testing.T, security string) {
2022-07-18 04:32:31 +00:00
user, err := uuid.DefaultGenerator.NewV4()
require.NoError(t, err)
2022-07-18 10:50:19 +00:00
t.Run("self", func(t *testing.T) {
2022-08-27 03:28:01 +00:00
testVMessSelf(t, security, user, 0, false, false, false)
2022-07-18 10:50:19 +00:00
})
t.Run("self-padding", func(t *testing.T) {
2022-08-27 03:28:01 +00:00
testVMessSelf(t, security, user, 0, true, false, false)
2022-07-18 10:50:19 +00:00
})
t.Run("self-authid", func(t *testing.T) {
2022-08-27 03:28:01 +00:00
testVMessSelf(t, security, user, 0, false, true, false)
2022-07-18 10:50:19 +00:00
})
t.Run("self-padding-authid", func(t *testing.T) {
2022-08-27 03:28:01 +00:00
testVMessSelf(t, security, user, 0, true, true, false)
2022-08-01 04:23:34 +00:00
})
t.Run("self-legacy", func(t *testing.T) {
2022-08-27 03:28:01 +00:00
testVMessSelf(t, security, user, 1, false, false, false)
2022-08-01 04:23:34 +00:00
})
t.Run("self-legacy-padding", func(t *testing.T) {
2022-08-27 03:28:01 +00:00
testVMessSelf(t, security, user, 1, true, false, false)
})
t.Run("packetaddr", func(t *testing.T) {
testVMessSelf(t, security, user, 0, false, false, true)
2022-07-18 10:50:19 +00:00
})
t.Run("inbound", func(t *testing.T) {
2022-08-01 04:23:34 +00:00
testVMessInboundWithV2Ray(t, security, user, 0, false)
2022-07-18 10:50:19 +00:00
})
t.Run("inbound-authid", func(t *testing.T) {
2022-08-01 04:23:34 +00:00
testVMessInboundWithV2Ray(t, security, user, 0, true)
})
t.Run("inbound-legacy", func(t *testing.T) {
testVMessInboundWithV2Ray(t, security, user, 64, false)
2022-07-18 10:50:19 +00:00
})
t.Run("outbound", func(t *testing.T) {
testVMessOutboundWithV2Ray(t, security, user, false, false, 0)
})
t.Run("outbound-padding", func(t *testing.T) {
testVMessOutboundWithV2Ray(t, security, user, true, false, 0)
})
t.Run("outbound-authid", func(t *testing.T) {
testVMessOutboundWithV2Ray(t, security, user, false, true, 0)
})
t.Run("outbound-padding-authid", func(t *testing.T) {
testVMessOutboundWithV2Ray(t, security, user, true, true, 0)
2022-07-18 04:32:31 +00:00
})
2022-07-18 10:50:19 +00:00
t.Run("outbound-legacy", func(t *testing.T) {
testVMessOutboundWithV2Ray(t, security, user, false, false, 1)
2022-07-18 04:32:31 +00:00
})
2022-07-18 10:50:19 +00:00
t.Run("outbound-legacy-padding", func(t *testing.T) {
testVMessOutboundWithV2Ray(t, security, user, true, false, 1)
2022-07-18 04:32:31 +00:00
})
2022-07-18 10:50:19 +00:00
}
2022-08-01 04:23:34 +00:00
func testVMessInboundWithV2Ray(t *testing.T, security string, uuid uuid.UUID, alterId int, authenticatedLength bool) {
2022-07-18 10:50:19 +00: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)
user.MustKey("id").SetString(uuid.String())
2022-08-01 04:23:34 +00:00
user.MustKey("alterId").SetNumeric(float64(alterId))
2022-07-18 10:50:19 +00: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",
Stdin: content,
Env: []string{"V2RAY_VMESS_AEAD_FORCED=false"},
})
startInstance(t, option.Options{
2022-07-19 14:16:49 +00:00
Log: &option.LogOptions{
2022-07-18 10:50:19 +00:00
Level: "error",
},
Inbounds: []option.Inbound{
{
Type: C.TypeVMess,
VMessOptions: option.VMessInboundOptions{
ListenOptions: option.ListenOptions{
Listen: option.ListenAddress(netip.IPv4Unspecified()),
ListenPort: serverPort,
},
Users: []option.VMessUser{
{
2022-08-01 04:23:34 +00:00
Name: "sekai",
UUID: uuid.String(),
AlterId: alterId,
2022-07-18 10:50:19 +00:00
},
},
},
},
},
})
2022-08-29 02:10:41 +00:00
testSuitSimple(t, clientPort, testPort)
2022-07-18 10:50:19 +00:00
}
func testVMessOutboundWithV2Ray(t *testing.T, security string, uuid uuid.UUID, globalPadding bool, authenticatedLength bool, alterId int) {
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))
inbound.MustKey("settings").MustKey("clients").MustIndex(0).MustKey("id").SetString(uuid.String())
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",
Stdin: content,
Env: []string{"V2RAY_VMESS_AEAD_FORCED=false"},
})
startInstance(t, option.Options{
2022-07-19 14:16:49 +00:00
Log: &option.LogOptions{
2022-07-18 10:50:19 +00:00
Level: "error",
},
Inbounds: []option.Inbound{
{
Type: C.TypeMixed,
MixedOptions: option.HTTPMixedInboundOptions{
ListenOptions: option.ListenOptions{
Listen: option.ListenAddress(netip.IPv4Unspecified()),
ListenPort: clientPort,
},
},
},
},
Outbounds: []option.Outbound{
{
Type: C.TypeVMess,
VMessOptions: option.VMessOutboundOptions{
ServerOptions: option.ServerOptions{
Server: "127.0.0.1",
ServerPort: serverPort,
},
Security: security,
UUID: uuid.String(),
GlobalPadding: globalPadding,
AuthenticatedLength: authenticatedLength,
AlterId: alterId,
},
},
},
})
2022-08-29 02:10:41 +00:00
testSuitSimple(t, clientPort, testPort)
2022-07-18 04:32:31 +00:00
}
2022-08-27 03:28:01 +00:00
func testVMessSelf(t *testing.T, security string, uuid uuid.UUID, alterId int, globalPadding bool, authenticatedLength bool, packetAddr bool) {
2022-07-18 04:32:31 +00:00
startInstance(t, option.Options{
2022-07-19 14:16:49 +00:00
Log: &option.LogOptions{
2022-08-29 02:10:41 +00:00
Level: "error",
2022-07-18 04:32:31 +00:00
},
Inbounds: []option.Inbound{
{
Type: C.TypeMixed,
Tag: "mixed-in",
MixedOptions: option.HTTPMixedInboundOptions{
ListenOptions: option.ListenOptions{
Listen: option.ListenAddress(netip.IPv4Unspecified()),
ListenPort: clientPort,
},
},
},
{
Type: C.TypeVMess,
VMessOptions: option.VMessInboundOptions{
ListenOptions: option.ListenOptions{
Listen: option.ListenAddress(netip.IPv4Unspecified()),
ListenPort: serverPort,
},
Users: []option.VMessUser{
{
2022-08-01 04:23:34 +00:00
Name: "sekai",
UUID: uuid.String(),
AlterId: alterId,
2022-07-18 04:32:31 +00:00
},
},
},
},
},
Outbounds: []option.Outbound{
{
Type: C.TypeDirect,
},
{
Type: C.TypeVMess,
Tag: "vmess-out",
VMessOptions: option.VMessOutboundOptions{
ServerOptions: option.ServerOptions{
Server: "127.0.0.1",
ServerPort: serverPort,
},
Security: security,
2022-07-18 10:50:19 +00:00
UUID: uuid.String(),
2022-08-01 04:23:34 +00:00
AlterId: alterId,
2022-07-18 04:32:31 +00:00
GlobalPadding: globalPadding,
AuthenticatedLength: authenticatedLength,
2022-08-27 03:28:01 +00:00
PacketAddr: packetAddr,
2022-07-18 04:32:31 +00:00
},
},
},
Route: &option.RouteOptions{
Rules: []option.Rule{
{
DefaultOptions: option.DefaultRule{
Inbound: []string{"mixed-in"},
Outbound: "vmess-out",
},
},
},
},
})
testSuit(t, clientPort, testPort)
}