2022-09-01 20:04:04 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/netip"
|
|
|
|
"os"
|
|
|
|
"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-09-01 20:04:04 +08:00
|
|
|
|
2023-04-09 15:37:06 +08:00
|
|
|
"github.com/gofrs/uuid/v5"
|
2022-09-01 20:04:04 +08:00
|
|
|
"github.com/spyzhov/ajson"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestV2RayWebsocket(t *testing.T) {
|
|
|
|
t.Run("self", func(t *testing.T) {
|
|
|
|
testV2RayTransportSelf(t, &option.V2RayTransportOptions{
|
|
|
|
Type: C.V2RayTransportTypeWebsocket,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
t.Run("self-early-data", func(t *testing.T) {
|
|
|
|
testV2RayTransportSelf(t, &option.V2RayTransportOptions{
|
|
|
|
Type: C.V2RayTransportTypeWebsocket,
|
|
|
|
WebsocketOptions: option.V2RayWebsocketOptions{
|
|
|
|
MaxEarlyData: 2048,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
t.Run("self-xray-early-data", func(t *testing.T) {
|
|
|
|
testV2RayTransportSelf(t, &option.V2RayTransportOptions{
|
|
|
|
Type: C.V2RayTransportTypeWebsocket,
|
|
|
|
WebsocketOptions: option.V2RayWebsocketOptions{
|
|
|
|
MaxEarlyData: 2048,
|
|
|
|
EarlyDataHeaderName: "Sec-WebSocket-Protocol",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
t.Run("inbound", func(t *testing.T) {
|
|
|
|
testV2RayWebsocketInbound(t, 0, "")
|
|
|
|
})
|
|
|
|
t.Run("inbound-early-data", func(t *testing.T) {
|
|
|
|
testV2RayWebsocketInbound(t, 2048, "")
|
|
|
|
})
|
|
|
|
t.Run("inbound-xray-early-data", func(t *testing.T) {
|
|
|
|
testV2RayWebsocketInbound(t, 2048, "Sec-WebSocket-Protocol")
|
|
|
|
})
|
|
|
|
t.Run("outbound", func(t *testing.T) {
|
|
|
|
testV2RayWebsocketOutbound(t, 0, "")
|
|
|
|
})
|
|
|
|
t.Run("outbound-early-data", func(t *testing.T) {
|
|
|
|
testV2RayWebsocketOutbound(t, 2048, "")
|
|
|
|
})
|
|
|
|
t.Run("outbound-xray-early-data", func(t *testing.T) {
|
|
|
|
testV2RayWebsocketOutbound(t, 2048, "Sec-WebSocket-Protocol")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testV2RayWebsocketInbound(t *testing.T, maxEarlyData uint32, earlyDataHeaderName string) {
|
|
|
|
userId, err := uuid.DefaultGenerator.NewV4()
|
|
|
|
require.NoError(t, err)
|
|
|
|
_, certPem, keyPem := createSelfSignedCertificate(t, "example.org")
|
|
|
|
startInstance(t, option.Options{
|
2024-11-18 18:59:19 +08:00
|
|
|
Inbounds: []option.Inbound{
|
2022-09-01 20:04:04 +08:00
|
|
|
{
|
|
|
|
Type: C.TypeVMess,
|
2024-11-18 18:59:19 +08:00
|
|
|
Options: &option.VMessInboundOptions{
|
2022-09-01 20:04:04 +08:00
|
|
|
ListenOptions: option.ListenOptions{
|
2024-11-18 18:59:19 +08:00
|
|
|
Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
|
2022-09-01 20:04:04 +08:00
|
|
|
ListenPort: serverPort,
|
|
|
|
},
|
|
|
|
Users: []option.VMessUser{
|
|
|
|
{
|
|
|
|
Name: "sekai",
|
|
|
|
UUID: userId.String(),
|
|
|
|
},
|
|
|
|
},
|
2023-12-11 18:36:06 +08:00
|
|
|
InboundTLSOptionsContainer: option.InboundTLSOptionsContainer{
|
|
|
|
TLS: &option.InboundTLSOptions{
|
|
|
|
Enabled: true,
|
|
|
|
ServerName: "example.org",
|
|
|
|
CertificatePath: certPem,
|
|
|
|
KeyPath: keyPem,
|
|
|
|
},
|
2022-09-01 20:04:04 +08:00
|
|
|
},
|
|
|
|
Transport: &option.V2RayTransportOptions{
|
|
|
|
Type: C.V2RayTransportTypeWebsocket,
|
|
|
|
WebsocketOptions: option.V2RayWebsocketOptions{
|
|
|
|
MaxEarlyData: maxEarlyData,
|
|
|
|
EarlyDataHeaderName: earlyDataHeaderName,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
content, err := os.ReadFile("config/vmess-ws-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)
|
|
|
|
settings := outbound.MustKey("settings").MustKey("vnext").MustIndex(0)
|
|
|
|
settings.MustKey("port").SetNumeric(float64(serverPort))
|
|
|
|
user := settings.MustKey("users").MustIndex(0)
|
|
|
|
user.MustKey("id").SetString(userId.String())
|
|
|
|
wsSettings := outbound.MustKey("streamSettings").MustKey("wsSettings")
|
|
|
|
wsSettings.MustKey("maxEarlyData").SetNumeric(float64(maxEarlyData))
|
|
|
|
wsSettings.MustKey("earlyDataHeaderName").SetString(earlyDataHeaderName)
|
|
|
|
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-09-01 20:04:04 +08:00
|
|
|
Stdin: content,
|
|
|
|
Bind: map[string]string{
|
|
|
|
certPem: "/path/to/certificate.crt",
|
|
|
|
keyPem: "/path/to/private.key",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
testSuitSimple(t, clientPort, testPort)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testV2RayWebsocketOutbound(t *testing.T, maxEarlyData uint32, earlyDataHeaderName string) {
|
|
|
|
userId, err := uuid.DefaultGenerator.NewV4()
|
|
|
|
require.NoError(t, err)
|
|
|
|
_, certPem, keyPem := createSelfSignedCertificate(t, "example.org")
|
|
|
|
|
|
|
|
content, err := os.ReadFile("config/vmess-ws-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(userId.String())
|
|
|
|
wsSettings := inbound.MustKey("streamSettings").MustKey("wsSettings")
|
|
|
|
wsSettings.MustKey("maxEarlyData").SetNumeric(float64(maxEarlyData))
|
|
|
|
wsSettings.MustKey("earlyDataHeaderName").SetString(earlyDataHeaderName)
|
|
|
|
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-09-01 20:04:04 +08:00
|
|
|
Stdin: content,
|
|
|
|
Env: []string{"V2RAY_VMESS_AEAD_FORCED=false"},
|
|
|
|
Bind: map[string]string{
|
|
|
|
certPem: "/path/to/certificate.crt",
|
|
|
|
keyPem: "/path/to/private.key",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
startInstance(t, option.Options{
|
2024-11-18 18:59:19 +08:00
|
|
|
Inbounds: []option.Inbound{
|
2022-09-01 20:04:04 +08:00
|
|
|
{
|
|
|
|
Type: C.TypeMixed,
|
|
|
|
Tag: "mixed-in",
|
2024-11-18 18:59:19 +08:00
|
|
|
Options: &option.HTTPMixedInboundOptions{
|
2022-09-01 20:04:04 +08:00
|
|
|
ListenOptions: option.ListenOptions{
|
2024-11-18 18:59:19 +08:00
|
|
|
Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
|
2022-09-01 20:04:04 +08:00
|
|
|
ListenPort: clientPort,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2024-11-18 18:59:19 +08:00
|
|
|
Outbounds: []option.Outbound{
|
2022-09-01 20:04:04 +08:00
|
|
|
{
|
|
|
|
Type: C.TypeVMess,
|
|
|
|
Tag: "vmess-out",
|
2024-11-18 18:59:19 +08:00
|
|
|
Options: &option.VMessOutboundOptions{
|
2022-09-01 20:04:04 +08:00
|
|
|
ServerOptions: option.ServerOptions{
|
|
|
|
Server: "127.0.0.1",
|
|
|
|
ServerPort: serverPort,
|
|
|
|
},
|
|
|
|
UUID: userId.String(),
|
|
|
|
Security: "zero",
|
2023-12-11 18:36:06 +08:00
|
|
|
OutboundTLSOptionsContainer: option.OutboundTLSOptionsContainer{
|
|
|
|
TLS: &option.OutboundTLSOptions{
|
|
|
|
Enabled: true,
|
|
|
|
ServerName: "example.org",
|
|
|
|
CertificatePath: certPem,
|
|
|
|
},
|
2022-09-01 20:04:04 +08:00
|
|
|
},
|
|
|
|
Transport: &option.V2RayTransportOptions{
|
|
|
|
Type: C.V2RayTransportTypeWebsocket,
|
|
|
|
WebsocketOptions: option.V2RayWebsocketOptions{
|
|
|
|
MaxEarlyData: maxEarlyData,
|
|
|
|
EarlyDataHeaderName: earlyDataHeaderName,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2022-09-13 10:41:10 +08:00
|
|
|
testSuit(t, clientPort, testPort)
|
2022-09-01 20:04:04 +08:00
|
|
|
}
|