sing-box/test/v2ray_transport_test.go

427 lines
10 KiB
Go
Raw Normal View History

2022-08-22 10:53:47 +00:00
package main
import (
"net/netip"
"testing"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/option"
"github.com/gofrs/uuid"
"github.com/stretchr/testify/require"
)
2022-08-23 04:11:56 +00:00
func TestV2RayGRPCSelf(t *testing.T) {
testV2RayTransportSelf(t, &option.V2RayTransportOptions{
2022-08-22 12:20:56 +00:00
Type: C.V2RayTransportTypeGRPC,
GRPCOptions: option.V2RayGRPCOptions{
ServiceName: "TunService",
},
})
}
2022-08-27 13:05:15 +00:00
func TestV2RayGRPCLite(t *testing.T) {
t.Run("server", func(t *testing.T) {
testV2RayTransportSelfWith(t, &option.V2RayTransportOptions{
Type: C.V2RayTransportTypeGRPC,
GRPCOptions: option.V2RayGRPCOptions{
ServiceName: "TunService",
ForceLite: true,
},
}, &option.V2RayTransportOptions{
Type: C.V2RayTransportTypeGRPC,
GRPCOptions: option.V2RayGRPCOptions{
ServiceName: "TunService",
},
})
})
t.Run("client", func(t *testing.T) {
testV2RayTransportSelfWith(t, &option.V2RayTransportOptions{
Type: C.V2RayTransportTypeGRPC,
GRPCOptions: option.V2RayGRPCOptions{
ServiceName: "TunService",
},
}, &option.V2RayTransportOptions{
Type: C.V2RayTransportTypeGRPC,
GRPCOptions: option.V2RayGRPCOptions{
ServiceName: "TunService",
ForceLite: true,
},
})
})
t.Run("self", func(t *testing.T) {
testV2RayTransportSelfWith(t, &option.V2RayTransportOptions{
Type: C.V2RayTransportTypeGRPC,
GRPCOptions: option.V2RayGRPCOptions{
ServiceName: "TunService",
ForceLite: true,
},
}, &option.V2RayTransportOptions{
Type: C.V2RayTransportTypeGRPC,
GRPCOptions: option.V2RayGRPCOptions{
ServiceName: "TunService",
ForceLite: true,
},
})
})
}
2022-08-23 04:11:56 +00:00
func TestV2RayWebscoketSelf(t *testing.T) {
2022-08-22 12:20:56 +00:00
t.Run("basic", func(t *testing.T) {
2022-08-23 04:11:56 +00:00
testV2RayTransportSelf(t, &option.V2RayTransportOptions{
2022-08-22 12:20:56 +00:00
Type: C.V2RayTransportTypeWebsocket,
})
})
t.Run("v2ray early data", func(t *testing.T) {
2022-08-23 04:11:56 +00:00
testV2RayTransportSelf(t, &option.V2RayTransportOptions{
2022-08-22 12:20:56 +00:00
Type: C.V2RayTransportTypeWebsocket,
WebsocketOptions: option.V2RayWebsocketOptions{
MaxEarlyData: 2048,
},
})
})
t.Run("xray early data", func(t *testing.T) {
2022-08-23 04:11:56 +00:00
testV2RayTransportSelf(t, &option.V2RayTransportOptions{
2022-08-22 12:20:56 +00:00
Type: C.V2RayTransportTypeWebsocket,
WebsocketOptions: option.V2RayWebsocketOptions{
MaxEarlyData: 2048,
EarlyDataHeaderName: "Sec-WebSocket-Protocol",
},
})
})
}
2022-08-23 04:11:56 +00:00
func TestV2RayHTTPSelf(t *testing.T) {
testV2RayTransportSelf(t, &option.V2RayTransportOptions{
2022-08-22 14:19:25 +00:00
Type: C.V2RayTransportTypeHTTP,
2022-08-27 13:05:15 +00:00
HTTPOptions: option.V2RayHTTPOptions{
Method: "POST",
},
2022-08-22 13:20:05 +00:00
})
}
2022-08-23 04:11:56 +00:00
func TestV2RayHTTPPlainSelf(t *testing.T) {
testV2RayTransportNOTLSSelf(t, &option.V2RayTransportOptions{
Type: C.V2RayTransportTypeHTTP,
})
}
func testV2RayTransportSelf(t *testing.T, transport *option.V2RayTransportOptions) {
2022-08-27 13:05:15 +00:00
testV2RayTransportSelfWith(t, transport, transport)
}
func testV2RayTransportSelfWith(t *testing.T, server, client *option.V2RayTransportOptions) {
2022-08-23 04:11:56 +00:00
t.Run("vmess", func(t *testing.T) {
2022-08-27 13:05:15 +00:00
testVMessTransportSelf(t, server, client)
2022-08-23 04:11:56 +00:00
})
t.Run("trojan", func(t *testing.T) {
2022-08-27 13:05:15 +00:00
testTrojanTransportSelf(t, server, client)
2022-08-23 04:11:56 +00:00
})
}
2022-08-27 13:05:15 +00:00
func testVMessTransportSelf(t *testing.T, server *option.V2RayTransportOptions, client *option.V2RayTransportOptions) {
2022-08-22 14:19:25 +00:00
user, err := uuid.DefaultGenerator.NewV4()
require.NoError(t, err)
_, certPem, keyPem := createSelfSignedCertificate(t, "example.org")
startInstance(t, option.Options{
Log: &option.LogOptions{
Level: "error",
},
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{
{
Name: "sekai",
UUID: user.String(),
},
},
TLS: &option.InboundTLSOptions{
Enabled: true,
ServerName: "example.org",
CertificatePath: certPem,
KeyPath: keyPem,
},
2022-08-27 13:05:15 +00:00
Transport: server,
2022-08-22 14:19:25 +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,
},
UUID: user.String(),
Security: "zero",
TLS: &option.OutboundTLSOptions{
Enabled: true,
ServerName: "example.org",
CertificatePath: certPem,
},
2022-08-27 13:05:15 +00:00
Transport: client,
2022-08-22 14:19:25 +00:00
},
},
},
Route: &option.RouteOptions{
Rules: []option.Rule{
{
DefaultOptions: option.DefaultRule{
Inbound: []string{"mixed-in"},
Outbound: "vmess-out",
},
},
},
},
})
testSuit(t, clientPort, testPort)
}
2022-08-27 13:05:15 +00:00
func testTrojanTransportSelf(t *testing.T, server *option.V2RayTransportOptions, client *option.V2RayTransportOptions) {
2022-08-23 04:11:56 +00:00
user, err := uuid.DefaultGenerator.NewV4()
require.NoError(t, err)
_, certPem, keyPem := createSelfSignedCertificate(t, "example.org")
startInstance(t, option.Options{
Log: &option.LogOptions{
Level: "error",
},
Inbounds: []option.Inbound{
{
Type: C.TypeMixed,
Tag: "mixed-in",
MixedOptions: option.HTTPMixedInboundOptions{
ListenOptions: option.ListenOptions{
Listen: option.ListenAddress(netip.IPv4Unspecified()),
ListenPort: clientPort,
},
},
},
{
Type: C.TypeTrojan,
TrojanOptions: option.TrojanInboundOptions{
ListenOptions: option.ListenOptions{
Listen: option.ListenAddress(netip.IPv4Unspecified()),
ListenPort: serverPort,
},
Users: []option.TrojanUser{
{
Name: "sekai",
Password: user.String(),
},
},
TLS: &option.InboundTLSOptions{
Enabled: true,
ServerName: "example.org",
CertificatePath: certPem,
KeyPath: keyPem,
},
2022-08-27 13:05:15 +00:00
Transport: server,
2022-08-23 04:11:56 +00:00
},
},
},
Outbounds: []option.Outbound{
{
Type: C.TypeDirect,
},
{
Type: C.TypeTrojan,
Tag: "vmess-out",
TrojanOptions: option.TrojanOutboundOptions{
ServerOptions: option.ServerOptions{
Server: "127.0.0.1",
ServerPort: serverPort,
},
Password: user.String(),
TLS: &option.OutboundTLSOptions{
Enabled: true,
ServerName: "example.org",
CertificatePath: certPem,
},
2022-08-27 13:05:15 +00:00
Transport: client,
2022-08-23 04:11:56 +00:00
},
},
},
Route: &option.RouteOptions{
Rules: []option.Rule{
{
DefaultOptions: option.DefaultRule{
Inbound: []string{"mixed-in"},
Outbound: "vmess-out",
},
},
},
},
})
testSuit(t, clientPort, testPort)
}
2022-08-22 14:19:25 +00:00
func TestVMessQUICSelf(t *testing.T) {
transport := &option.V2RayTransportOptions{
Type: C.V2RayTransportTypeQUIC,
}
2022-08-22 10:53:47 +00:00
user, err := uuid.DefaultGenerator.NewV4()
require.NoError(t, err)
_, certPem, keyPem := createSelfSignedCertificate(t, "example.org")
startInstance(t, option.Options{
Log: &option.LogOptions{
2022-08-22 13:20:05 +00:00
Level: "error",
2022-08-22 10:53:47 +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{
{
Name: "sekai",
UUID: user.String(),
},
},
TLS: &option.InboundTLSOptions{
Enabled: true,
ServerName: "example.org",
CertificatePath: certPem,
KeyPath: keyPem,
},
2022-08-22 12:20:56 +00:00
Transport: transport,
2022-08-22 10:53:47 +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,
},
UUID: user.String(),
Security: "zero",
TLS: &option.OutboundTLSOptions{
Enabled: true,
ServerName: "example.org",
CertificatePath: certPem,
},
2022-08-22 12:20:56 +00:00
Transport: transport,
2022-08-22 10:53:47 +00:00
},
},
},
Route: &option.RouteOptions{
Rules: []option.Rule{
{
DefaultOptions: option.DefaultRule{
Inbound: []string{"mixed-in"},
Outbound: "vmess-out",
},
},
},
},
})
2022-08-22 13:20:05 +00:00
testSuitQUIC(t, clientPort, testPort)
2022-08-22 10:53:47 +00:00
}
2022-08-22 14:51:08 +00:00
2022-08-23 04:11:56 +00:00
func testV2RayTransportNOTLSSelf(t *testing.T, transport *option.V2RayTransportOptions) {
2022-08-22 14:51:08 +00:00
user, err := uuid.DefaultGenerator.NewV4()
require.NoError(t, err)
startInstance(t, option.Options{
Log: &option.LogOptions{
2022-08-23 05:22:03 +00:00
Level: "error",
2022-08-22 14:51:08 +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{
{
Name: "sekai",
UUID: user.String(),
},
},
Transport: transport,
},
},
},
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,
},
UUID: user.String(),
Security: "zero",
Transport: transport,
},
},
},
Route: &option.RouteOptions{
Rules: []option.Rule{
{
DefaultOptions: option.DefaultRule{
Inbound: []string{"mixed-in"},
Outbound: "vmess-out",
},
},
},
},
})
testSuitQUIC(t, clientPort, testPort)
}