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"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestVMessGRPCSelf(t *testing.T) {
|
2022-08-22 12:20:56 +00:00
|
|
|
testVMessWebscoketSelf(t, &option.V2RayTransportOptions{
|
|
|
|
Type: C.V2RayTransportTypeGRPC,
|
|
|
|
GRPCOptions: option.V2RayGRPCOptions{
|
|
|
|
ServiceName: "TunService",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestVMessWebscoketSelf(t *testing.T) {
|
|
|
|
t.Run("basic", func(t *testing.T) {
|
|
|
|
testVMessWebscoketSelf(t, &option.V2RayTransportOptions{
|
|
|
|
Type: C.V2RayTransportTypeWebsocket,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
t.Run("v2ray early data", func(t *testing.T) {
|
|
|
|
testVMessWebscoketSelf(t, &option.V2RayTransportOptions{
|
|
|
|
Type: C.V2RayTransportTypeWebsocket,
|
|
|
|
WebsocketOptions: option.V2RayWebsocketOptions{
|
|
|
|
MaxEarlyData: 2048,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
t.Run("xray early data", func(t *testing.T) {
|
|
|
|
testVMessWebscoketSelf(t, &option.V2RayTransportOptions{
|
|
|
|
Type: C.V2RayTransportTypeWebsocket,
|
|
|
|
WebsocketOptions: option.V2RayWebsocketOptions{
|
|
|
|
MaxEarlyData: 2048,
|
|
|
|
EarlyDataHeaderName: "Sec-WebSocket-Protocol",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-08-22 14:19:25 +00:00
|
|
|
func TestVMessHTTPSelf(t *testing.T) {
|
2022-08-22 13:20:05 +00:00
|
|
|
testVMessWebscoketSelf(t, &option.V2RayTransportOptions{
|
2022-08-22 14:19:25 +00:00
|
|
|
Type: C.V2RayTransportTypeHTTP,
|
2022-08-22 13:20:05 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-08-22 12:20:56 +00:00
|
|
|
func testVMessWebscoketSelf(t *testing.T, transport *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,
|
|
|
|
},
|
|
|
|
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",
|
|
|
|
TLS: &option.OutboundTLSOptions{
|
|
|
|
Enabled: true,
|
|
|
|
ServerName: "example.org",
|
|
|
|
CertificatePath: certPem,
|
|
|
|
},
|
|
|
|
Transport: transport,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Route: &option.RouteOptions{
|
|
|
|
Rules: []option.Rule{
|
|
|
|
{
|
|
|
|
DefaultOptions: option.DefaultRule{
|
|
|
|
Inbound: []string{"mixed-in"},
|
|
|
|
Outbound: "vmess-out",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
testSuit(t, clientPort, testPort)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
func TestVMessHTTPNoTLSSelf(t *testing.T) {
|
|
|
|
transport := &option.V2RayTransportOptions{
|
|
|
|
Type: C.V2RayTransportTypeHTTP,
|
|
|
|
}
|
|
|
|
user, err := uuid.DefaultGenerator.NewV4()
|
|
|
|
require.NoError(t, err)
|
|
|
|
startInstance(t, option.Options{
|
|
|
|
Log: &option.LogOptions{
|
|
|
|
Level: "trace",
|
|
|
|
},
|
|
|
|
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)
|
|
|
|
}
|