sing-box/test/v2ray_transport_test.go

385 lines
9.3 KiB
Go
Raw Normal View History

2022-08-22 18:53:47 +08:00
package main
import (
"net/netip"
"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-08-22 18:53:47 +08:00
2023-04-09 15:37:06 +08:00
"github.com/gofrs/uuid/v5"
2022-08-22 18:53:47 +08:00
"github.com/stretchr/testify/require"
)
2022-08-23 12:11:56 +08:00
func TestV2RayHTTPSelf(t *testing.T) {
testV2RayTransportSelf(t, &option.V2RayTransportOptions{
2022-08-22 22:19:25 +08:00
Type: C.V2RayTransportTypeHTTP,
2022-08-27 21:05:15 +08:00
HTTPOptions: option.V2RayHTTPOptions{
Method: "POST",
},
2022-08-22 21:20:05 +08:00
})
}
2022-08-23 12:11:56 +08:00
func TestV2RayHTTPPlainSelf(t *testing.T) {
testV2RayTransportNOTLSSelf(t, &option.V2RayTransportOptions{
Type: C.V2RayTransportTypeHTTP,
})
}
func testV2RayTransportSelf(t *testing.T, transport *option.V2RayTransportOptions) {
2022-08-27 21:05:15 +08:00
testV2RayTransportSelfWith(t, transport, transport)
}
func testV2RayTransportSelfWith(t *testing.T, server, client *option.V2RayTransportOptions) {
2022-08-23 12:11:56 +08:00
t.Run("vmess", func(t *testing.T) {
2022-08-27 21:05:15 +08:00
testVMessTransportSelf(t, server, client)
2022-08-23 12:11:56 +08:00
})
t.Run("trojan", func(t *testing.T) {
2022-08-27 21:05:15 +08:00
testTrojanTransportSelf(t, server, client)
2022-08-23 12:11:56 +08:00
})
}
2022-08-27 21:05:15 +08:00
func testVMessTransportSelf(t *testing.T, server *option.V2RayTransportOptions, client *option.V2RayTransportOptions) {
2022-08-22 22:19:25 +08:00
user, 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-08-22 22:19:25 +08:00
{
Type: C.TypeMixed,
Tag: "mixed-in",
2024-11-18 18:59:19 +08:00
Options: &option.HTTPMixedInboundOptions{
2022-08-22 22:19:25 +08:00
ListenOptions: option.ListenOptions{
2024-11-18 18:59:19 +08:00
Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
2022-08-22 22:19:25 +08:00
ListenPort: clientPort,
},
},
},
{
Type: C.TypeVMess,
2024-11-18 18:59:19 +08:00
Options: &option.VMessInboundOptions{
2022-08-22 22:19:25 +08:00
ListenOptions: option.ListenOptions{
2024-11-18 18:59:19 +08:00
Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
2022-08-22 22:19:25 +08: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 22:19:25 +08:00
},
2022-08-27 21:05:15 +08:00
Transport: server,
2022-08-22 22:19:25 +08:00
},
},
},
2024-11-18 18:59:19 +08:00
Outbounds: []option.Outbound{
2022-08-22 22:19:25 +08:00
{
Type: C.TypeDirect,
},
{
Type: C.TypeVMess,
Tag: "vmess-out",
2024-11-18 18:59:19 +08:00
Options: &option.VMessOutboundOptions{
2022-08-22 22:19:25 +08:00
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 22:19:25 +08:00
},
2022-08-27 21:05:15 +08:00
Transport: client,
2022-08-22 22:19:25 +08:00
},
},
},
Route: &option.RouteOptions{
Rules: []option.Rule{
{
2024-10-21 23:38:34 +08:00
Type: C.RuleTypeDefault,
2022-08-22 22:19:25 +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-08-22 22:19:25 +08:00
},
},
},
},
})
testSuit(t, clientPort, testPort)
}
2022-08-27 21:05:15 +08:00
func testTrojanTransportSelf(t *testing.T, server *option.V2RayTransportOptions, client *option.V2RayTransportOptions) {
2022-08-23 12:11:56 +08:00
user, 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-08-23 12:11:56 +08:00
{
Type: C.TypeMixed,
Tag: "mixed-in",
2024-11-18 18:59:19 +08:00
Options: &option.HTTPMixedInboundOptions{
2022-08-23 12:11:56 +08:00
ListenOptions: option.ListenOptions{
2024-11-18 18:59:19 +08:00
Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
2022-08-23 12:11:56 +08:00
ListenPort: clientPort,
},
},
},
{
Type: C.TypeTrojan,
2024-11-18 18:59:19 +08:00
Options: &option.TrojanInboundOptions{
2022-08-23 12:11:56 +08:00
ListenOptions: option.ListenOptions{
2024-11-18 18:59:19 +08:00
Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
2022-08-23 12:11:56 +08: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 12:11:56 +08:00
},
2022-08-27 21:05:15 +08:00
Transport: server,
2022-08-23 12:11:56 +08:00
},
},
},
2024-11-18 18:59:19 +08:00
Outbounds: []option.Outbound{
2022-08-23 12:11:56 +08:00
{
Type: C.TypeDirect,
},
{
Type: C.TypeTrojan,
Tag: "vmess-out",
2024-11-18 18:59:19 +08:00
Options: &option.TrojanOutboundOptions{
2022-08-23 12:11:56 +08:00
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 12:11:56 +08:00
},
2022-08-27 21:05:15 +08:00
Transport: client,
2022-08-23 12:11:56 +08:00
},
},
},
Route: &option.RouteOptions{
Rules: []option.Rule{
{
2024-10-21 23:38:34 +08:00
Type: C.RuleTypeDefault,
2022-08-23 12:11:56 +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-08-23 12:11:56 +08:00
},
},
},
},
})
testSuit(t, clientPort, testPort)
}
2022-08-22 22:19:25 +08:00
func TestVMessQUICSelf(t *testing.T) {
transport := &option.V2RayTransportOptions{
Type: C.V2RayTransportTypeQUIC,
}
2022-08-22 18:53:47 +08:00
user, 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-08-22 18:53:47 +08:00
{
Type: C.TypeMixed,
Tag: "mixed-in",
2024-11-18 18:59:19 +08:00
Options: &option.HTTPMixedInboundOptions{
2022-08-22 18:53:47 +08:00
ListenOptions: option.ListenOptions{
2024-11-18 18:59:19 +08:00
Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
2022-08-22 18:53:47 +08:00
ListenPort: clientPort,
},
},
},
{
Type: C.TypeVMess,
2024-11-18 18:59:19 +08:00
Options: &option.VMessInboundOptions{
2022-08-22 18:53:47 +08:00
ListenOptions: option.ListenOptions{
2024-11-18 18:59:19 +08:00
Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
2022-08-22 18:53:47 +08: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 18:53:47 +08:00
},
2022-08-22 20:20:56 +08:00
Transport: transport,
2022-08-22 18:53:47 +08:00
},
},
},
2024-11-18 18:59:19 +08:00
Outbounds: []option.Outbound{
2022-08-22 18:53:47 +08:00
{
Type: C.TypeDirect,
},
{
Type: C.TypeVMess,
Tag: "vmess-out",
2024-11-18 18:59:19 +08:00
Options: &option.VMessOutboundOptions{
2022-08-22 18:53:47 +08:00
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 18:53:47 +08:00
},
2022-08-22 20:20:56 +08:00
Transport: transport,
2022-08-22 18:53:47 +08:00
},
},
},
Route: &option.RouteOptions{
Rules: []option.Rule{
{
2024-10-21 23:38:34 +08:00
Type: C.RuleTypeDefault,
2022-08-22 18:53:47 +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-08-22 18:53:47 +08:00
},
},
},
},
})
2022-09-14 16:07:52 +08:00
testSuitSimple1(t, clientPort, testPort)
2022-08-22 18:53:47 +08:00
}
2022-08-22 22:51:08 +08:00
2022-08-23 12:11:56 +08:00
func testV2RayTransportNOTLSSelf(t *testing.T, transport *option.V2RayTransportOptions) {
2022-08-22 22:51:08 +08:00
user, err := uuid.DefaultGenerator.NewV4()
require.NoError(t, err)
startInstance(t, option.Options{
2024-11-18 18:59:19 +08:00
Inbounds: []option.Inbound{
2022-08-22 22:51:08 +08:00
{
Type: C.TypeMixed,
Tag: "mixed-in",
2024-11-18 18:59:19 +08:00
Options: &option.HTTPMixedInboundOptions{
2022-08-22 22:51:08 +08:00
ListenOptions: option.ListenOptions{
2024-11-18 18:59:19 +08:00
Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
2022-08-22 22:51:08 +08:00
ListenPort: clientPort,
},
},
},
{
Type: C.TypeVMess,
2024-11-18 18:59:19 +08:00
Options: &option.VMessInboundOptions{
2022-08-22 22:51:08 +08:00
ListenOptions: option.ListenOptions{
2024-11-18 18:59:19 +08:00
Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
2022-08-22 22:51:08 +08:00
ListenPort: serverPort,
},
Users: []option.VMessUser{
{
Name: "sekai",
UUID: user.String(),
},
},
Transport: transport,
},
},
},
2024-11-18 18:59:19 +08:00
Outbounds: []option.Outbound{
2022-08-22 22:51:08 +08:00
{
Type: C.TypeDirect,
},
{
Type: C.TypeVMess,
Tag: "vmess-out",
2024-11-18 18:59:19 +08:00
Options: &option.VMessOutboundOptions{
2022-08-22 22:51:08 +08:00
ServerOptions: option.ServerOptions{
Server: "127.0.0.1",
ServerPort: serverPort,
},
UUID: user.String(),
Security: "zero",
Transport: transport,
},
},
},
Route: &option.RouteOptions{
Rules: []option.Rule{
{
2024-10-21 23:38:34 +08:00
Type: C.RuleTypeDefault,
2022-08-22 22:51:08 +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-08-22 22:51:08 +08:00
},
},
},
},
})
2022-09-13 10:41:10 +08:00
testSuit(t, clientPort, testPort)
2022-08-22 22:51:08 +08:00
}