sing-box/test/v2ray_transport_test.go

347 lines
8.4 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"
2023-04-09 07:37:06 +00:00
"github.com/gofrs/uuid/v5"
2022-08-22 10:53:47 +00:00
"github.com/stretchr/testify/require"
)
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{
Inbounds: []option.Inbound{
{
Type: C.TypeMixed,
Tag: "mixed-in",
MixedOptions: option.HTTPMixedInboundOptions{
ListenOptions: option.ListenOptions{
2023-03-19 12:46:22 +00:00
Listen: option.NewListenAddress(netip.IPv4Unspecified()),
2022-08-22 14:19:25 +00:00
ListenPort: clientPort,
},
},
},
{
Type: C.TypeVMess,
VMessOptions: option.VMessInboundOptions{
ListenOptions: option.ListenOptions{
2023-03-19 12:46:22 +00:00
Listen: option.NewListenAddress(netip.IPv4Unspecified()),
2022-08-22 14:19:25 +00:00
ListenPort: serverPort,
},
Users: []option.VMessUser{
{
Name: "sekai",
UUID: user.String(),
},
},
InboundTLSOptionsContainer: option.InboundTLSOptionsContainer{
TLS: &option.InboundTLSOptions{
Enabled: true,
ServerName: "example.org",
CertificatePath: certPem,
KeyPath: keyPem,
},
2022-08-22 14:19:25 +00:00
},
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",
OutboundTLSOptionsContainer: option.OutboundTLSOptionsContainer{
TLS: &option.OutboundTLSOptions{
Enabled: true,
ServerName: "example.org",
CertificatePath: certPem,
},
2022-08-22 14:19:25 +00:00
},
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{
Inbounds: []option.Inbound{
{
Type: C.TypeMixed,
Tag: "mixed-in",
MixedOptions: option.HTTPMixedInboundOptions{
ListenOptions: option.ListenOptions{
2023-03-19 12:46:22 +00:00
Listen: option.NewListenAddress(netip.IPv4Unspecified()),
2022-08-23 04:11:56 +00:00
ListenPort: clientPort,
},
},
},
{
Type: C.TypeTrojan,
TrojanOptions: option.TrojanInboundOptions{
ListenOptions: option.ListenOptions{
2023-03-19 12:46:22 +00:00
Listen: option.NewListenAddress(netip.IPv4Unspecified()),
2022-08-23 04:11:56 +00:00
ListenPort: serverPort,
},
Users: []option.TrojanUser{
{
Name: "sekai",
Password: user.String(),
},
},
InboundTLSOptionsContainer: option.InboundTLSOptionsContainer{
TLS: &option.InboundTLSOptions{
Enabled: true,
ServerName: "example.org",
CertificatePath: certPem,
KeyPath: keyPem,
},
2022-08-23 04:11:56 +00:00
},
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(),
OutboundTLSOptionsContainer: option.OutboundTLSOptionsContainer{
TLS: &option.OutboundTLSOptions{
Enabled: true,
ServerName: "example.org",
CertificatePath: certPem,
},
2022-08-23 04:11:56 +00:00
},
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{
Inbounds: []option.Inbound{
{
Type: C.TypeMixed,
Tag: "mixed-in",
MixedOptions: option.HTTPMixedInboundOptions{
ListenOptions: option.ListenOptions{
2023-03-19 12:46:22 +00:00
Listen: option.NewListenAddress(netip.IPv4Unspecified()),
2022-08-22 10:53:47 +00:00
ListenPort: clientPort,
},
},
},
{
Type: C.TypeVMess,
VMessOptions: option.VMessInboundOptions{
ListenOptions: option.ListenOptions{
2023-03-19 12:46:22 +00:00
Listen: option.NewListenAddress(netip.IPv4Unspecified()),
2022-08-22 10:53:47 +00:00
ListenPort: serverPort,
},
Users: []option.VMessUser{
{
Name: "sekai",
UUID: user.String(),
},
},
InboundTLSOptionsContainer: option.InboundTLSOptionsContainer{
TLS: &option.InboundTLSOptions{
Enabled: true,
ServerName: "example.org",
CertificatePath: certPem,
KeyPath: keyPem,
},
2022-08-22 10:53:47 +00:00
},
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",
OutboundTLSOptionsContainer: option.OutboundTLSOptionsContainer{
TLS: &option.OutboundTLSOptions{
Enabled: true,
ServerName: "example.org",
CertificatePath: certPem,
},
2022-08-22 10:53:47 +00:00
},
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-09-14 08:07:52 +00:00
testSuitSimple1(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{
Inbounds: []option.Inbound{
{
Type: C.TypeMixed,
Tag: "mixed-in",
MixedOptions: option.HTTPMixedInboundOptions{
ListenOptions: option.ListenOptions{
2023-03-19 12:46:22 +00:00
Listen: option.NewListenAddress(netip.IPv4Unspecified()),
2022-08-22 14:51:08 +00:00
ListenPort: clientPort,
},
},
},
{
Type: C.TypeVMess,
VMessOptions: option.VMessInboundOptions{
ListenOptions: option.ListenOptions{
2023-03-19 12:46:22 +00:00
Listen: option.NewListenAddress(netip.IPv4Unspecified()),
2022-08-22 14:51:08 +00:00
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",
},
},
},
},
})
2022-09-13 02:41:10 +00:00
testSuit(t, clientPort, testPort)
2022-08-22 14:51:08 +00:00
}