123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535 |
- package main
- import (
- "net/netip"
- "os"
- "testing"
- C "github.com/sagernet/sing-box/constant"
- "github.com/sagernet/sing-box/option"
- "github.com/sagernet/sing-box/transport/vless"
- "github.com/gofrs/uuid/v5"
- "github.com/spyzhov/ajson"
- "github.com/stretchr/testify/require"
- )
- func TestVLESS(t *testing.T) {
- content, err := os.ReadFile("config/vless-server.json")
- require.NoError(t, err)
- config, err := ajson.Unmarshal(content)
- require.NoError(t, err)
- user := newUUID()
- inbound := config.MustKey("inbounds").MustIndex(0)
- inbound.MustKey("port").SetNumeric(float64(serverPort))
- inbound.MustKey("settings").MustKey("clients").MustIndex(0).MustKey("id").SetString(user.String())
- content, err = ajson.Marshal(config)
- require.NoError(t, err)
- startDockerContainer(t, DockerOptions{
- Image: ImageV2RayCore,
- Ports: []uint16{serverPort},
- EntryPoint: "v2ray",
- Cmd: []string{"run"},
- Stdin: content,
- })
- startInstance(t, option.Options{
- Inbounds: []option.Inbound{
- {
- Type: C.TypeMixed,
- MixedOptions: option.HTTPMixedInboundOptions{
- ListenOptions: option.ListenOptions{
- Listen: option.NewListenAddress(netip.IPv4Unspecified()),
- ListenPort: clientPort,
- },
- },
- },
- },
- Outbounds: []option.Outbound{
- {
- Type: C.TypeVLESS,
- VLESSOptions: option.VLESSOutboundOptions{
- ServerOptions: option.ServerOptions{
- Server: "127.0.0.1",
- ServerPort: serverPort,
- },
- UUID: user.String(),
- },
- },
- },
- })
- testTCP(t, clientPort, testPort)
- }
- func TestVLESSXRay(t *testing.T) {
- t.Run("origin", func(t *testing.T) {
- testVLESSXrayOutbound(t, "", "")
- })
- t.Run("xudp", func(t *testing.T) {
- testVLESSXrayOutbound(t, "xudp", "")
- })
- t.Run("vision", func(t *testing.T) {
- testVLESSXrayOutbound(t, "xudp", vless.FlowVision)
- })
- }
- func testVLESSXrayOutbound(t *testing.T, packetEncoding string, flow string) {
- _, certPem, keyPem := createSelfSignedCertificate(t, "example.org")
- content, err := os.ReadFile("config/vless-tls-server.json")
- require.NoError(t, err)
- config, err := ajson.Unmarshal(content)
- require.NoError(t, err)
- userID := newUUID()
- inbound := config.MustKey("inbounds").MustIndex(0)
- inbound.MustKey("port").SetNumeric(float64(serverPort))
- user := inbound.MustKey("settings").MustKey("clients").MustIndex(0)
- user.MustKey("id").SetString(userID.String())
- user.MustKey("flow").SetString(flow)
- content, err = ajson.Marshal(config)
- require.NoError(t, err)
- startDockerContainer(t, DockerOptions{
- Image: ImageXRayCore,
- Ports: []uint16{serverPort},
- EntryPoint: "xray",
- Stdin: content,
- Bind: map[string]string{
- certPem: "/path/to/certificate.crt",
- keyPem: "/path/to/private.key",
- },
- })
- startInstance(t, option.Options{
- Inbounds: []option.Inbound{
- {
- Type: C.TypeMixed,
- MixedOptions: option.HTTPMixedInboundOptions{
- ListenOptions: option.ListenOptions{
- Listen: option.NewListenAddress(netip.IPv4Unspecified()),
- ListenPort: clientPort,
- },
- },
- },
- {
- Type: C.TypeTrojan,
- Tag: "trojan",
- TrojanOptions: option.TrojanInboundOptions{
- ListenOptions: option.ListenOptions{
- Listen: option.NewListenAddress(netip.IPv4Unspecified()),
- ListenPort: otherPort,
- },
- Users: []option.TrojanUser{
- {
- Name: "sekai",
- Password: userID.String(),
- },
- },
- TLS: &option.InboundTLSOptions{
- Enabled: true,
- ServerName: "example.org",
- CertificatePath: certPem,
- KeyPath: keyPem,
- },
- },
- },
- },
- Outbounds: []option.Outbound{
- {
- Type: C.TypeTrojan,
- TrojanOptions: option.TrojanOutboundOptions{
- ServerOptions: option.ServerOptions{
- Server: "host.docker.internal",
- ServerPort: otherPort,
- },
- Password: userID.String(),
- TLS: &option.OutboundTLSOptions{
- Enabled: true,
- ServerName: "example.org",
- CertificatePath: certPem,
- },
- DialerOptions: option.DialerOptions{
- Detour: "vless",
- },
- },
- },
- {
- Type: C.TypeVLESS,
- Tag: "vless",
- VLESSOptions: option.VLESSOutboundOptions{
- ServerOptions: option.ServerOptions{
- Server: "127.0.0.1",
- ServerPort: serverPort,
- },
- UUID: userID.String(),
- Flow: flow,
- PacketEncoding: &packetEncoding,
- TLS: &option.OutboundTLSOptions{
- Enabled: true,
- ServerName: "example.org",
- CertificatePath: certPem,
- },
- },
- },
- {
- Type: C.TypeDirect,
- Tag: "direct",
- },
- },
- Route: &option.RouteOptions{
- Rules: []option.Rule{
- {
- DefaultOptions: option.DefaultRule{
- Inbound: []string{"trojan"},
- Outbound: "direct",
- },
- },
- },
- },
- })
- testSuit(t, clientPort, testPort)
- }
- func TestVLESSSelf(t *testing.T) {
- t.Run("origin", func(t *testing.T) {
- testVLESSSelf(t, "")
- })
- t.Run("vision", func(t *testing.T) {
- testVLESSSelf(t, vless.FlowVision)
- })
- t.Run("vision-tls", func(t *testing.T) {
- testVLESSSelfTLS(t, vless.FlowVision)
- })
- }
- func testVLESSSelf(t *testing.T, flow string) {
- _, certPem, keyPem := createSelfSignedCertificate(t, "example.org")
- userUUID := newUUID()
- startInstance(t, option.Options{
- Inbounds: []option.Inbound{
- {
- Type: C.TypeMixed,
- Tag: "mixed-in",
- MixedOptions: option.HTTPMixedInboundOptions{
- ListenOptions: option.ListenOptions{
- Listen: option.NewListenAddress(netip.IPv4Unspecified()),
- ListenPort: clientPort,
- },
- },
- },
- {
- Type: C.TypeVLESS,
- VLESSOptions: option.VLESSInboundOptions{
- ListenOptions: option.ListenOptions{
- Listen: option.NewListenAddress(netip.IPv4Unspecified()),
- ListenPort: serverPort,
- },
- Users: []option.VLESSUser{
- {
- Name: "sekai",
- UUID: userUUID.String(),
- Flow: flow,
- },
- },
- TLS: &option.InboundTLSOptions{
- Enabled: true,
- ServerName: "example.org",
- CertificatePath: certPem,
- KeyPath: keyPem,
- },
- },
- },
- },
- Outbounds: []option.Outbound{
- {
- Type: C.TypeDirect,
- },
- {
- Type: C.TypeVLESS,
- Tag: "vless-out",
- VLESSOptions: option.VLESSOutboundOptions{
- ServerOptions: option.ServerOptions{
- Server: "127.0.0.1",
- ServerPort: serverPort,
- },
- UUID: userUUID.String(),
- Flow: flow,
- TLS: &option.OutboundTLSOptions{
- Enabled: true,
- ServerName: "example.org",
- CertificatePath: certPem,
- },
- },
- },
- },
- Route: &option.RouteOptions{
- Rules: []option.Rule{
- {
- DefaultOptions: option.DefaultRule{
- Inbound: []string{"mixed-in"},
- Outbound: "vless-out",
- },
- },
- },
- },
- })
- testSuit(t, clientPort, testPort)
- }
- func testVLESSSelfTLS(t *testing.T, flow string) {
- _, certPem, keyPem := createSelfSignedCertificate(t, "example.org")
- userUUID := newUUID()
- startInstance(t, option.Options{
- Inbounds: []option.Inbound{
- {
- Type: C.TypeMixed,
- Tag: "mixed-in",
- MixedOptions: option.HTTPMixedInboundOptions{
- ListenOptions: option.ListenOptions{
- Listen: option.NewListenAddress(netip.IPv4Unspecified()),
- ListenPort: clientPort,
- },
- },
- },
- {
- Type: C.TypeVLESS,
- VLESSOptions: option.VLESSInboundOptions{
- ListenOptions: option.ListenOptions{
- Listen: option.NewListenAddress(netip.IPv4Unspecified()),
- ListenPort: serverPort,
- },
- Users: []option.VLESSUser{
- {
- Name: "sekai",
- UUID: userUUID.String(),
- Flow: flow,
- },
- },
- TLS: &option.InboundTLSOptions{
- Enabled: true,
- ServerName: "example.org",
- CertificatePath: certPem,
- KeyPath: keyPem,
- },
- },
- },
- {
- Type: C.TypeTrojan,
- Tag: "trojan",
- TrojanOptions: option.TrojanInboundOptions{
- ListenOptions: option.ListenOptions{
- Listen: option.NewListenAddress(netip.IPv4Unspecified()),
- ListenPort: otherPort,
- },
- Users: []option.TrojanUser{
- {
- Name: "sekai",
- Password: userUUID.String(),
- },
- },
- TLS: &option.InboundTLSOptions{
- Enabled: true,
- ServerName: "example.org",
- CertificatePath: certPem,
- KeyPath: keyPem,
- },
- },
- },
- },
- Outbounds: []option.Outbound{
- {
- Type: C.TypeDirect,
- },
- {
- Type: C.TypeTrojan,
- Tag: "trojan-out",
- TrojanOptions: option.TrojanOutboundOptions{
- ServerOptions: option.ServerOptions{
- Server: "127.0.0.1",
- ServerPort: otherPort,
- },
- Password: userUUID.String(),
- TLS: &option.OutboundTLSOptions{
- Enabled: true,
- ServerName: "example.org",
- CertificatePath: certPem,
- },
- DialerOptions: option.DialerOptions{
- Detour: "vless-out",
- },
- },
- },
- {
- Type: C.TypeVLESS,
- Tag: "vless-out",
- VLESSOptions: option.VLESSOutboundOptions{
- ServerOptions: option.ServerOptions{
- Server: "127.0.0.1",
- ServerPort: serverPort,
- },
- UUID: userUUID.String(),
- Flow: flow,
- TLS: &option.OutboundTLSOptions{
- Enabled: true,
- ServerName: "example.org",
- CertificatePath: certPem,
- },
- },
- },
- },
- Route: &option.RouteOptions{
- Rules: []option.Rule{
- {
- DefaultOptions: option.DefaultRule{
- Inbound: []string{"mixed-in"},
- Outbound: "trojan-out",
- },
- },
- },
- },
- })
- testSuit(t, clientPort, testPort)
- }
- func TestVLESSXrayInbound(t *testing.T) {
- testVLESSXrayInbound(t, vless.FlowVision)
- }
- func testVLESSXrayInbound(t *testing.T, flow string) {
- userId, err := uuid.DefaultGenerator.NewV4()
- require.NoError(t, err)
- _, certPem, keyPem := createSelfSignedCertificate(t, "example.org")
- startInstance(t, option.Options{
- Inbounds: []option.Inbound{
- {
- Type: C.TypeVLESS,
- VLESSOptions: option.VLESSInboundOptions{
- ListenOptions: option.ListenOptions{
- Listen: option.NewListenAddress(netip.IPv4Unspecified()),
- ListenPort: serverPort,
- },
- Users: []option.VLESSUser{
- {
- Name: "sekai",
- UUID: userId.String(),
- Flow: flow,
- },
- },
- TLS: &option.InboundTLSOptions{
- Enabled: true,
- ServerName: "example.org",
- CertificatePath: certPem,
- KeyPath: keyPem,
- },
- },
- },
- {
- Type: C.TypeTrojan,
- Tag: "trojan",
- TrojanOptions: option.TrojanInboundOptions{
- ListenOptions: option.ListenOptions{
- Listen: option.NewListenAddress(netip.IPv4Unspecified()),
- ListenPort: otherPort,
- },
- Users: []option.TrojanUser{
- {
- Name: "sekai",
- Password: userId.String(),
- },
- },
- TLS: &option.InboundTLSOptions{
- Enabled: true,
- ServerName: "example.org",
- CertificatePath: certPem,
- KeyPath: keyPem,
- },
- },
- },
- },
- })
- startInstance(t, option.Options{
- Inbounds: []option.Inbound{
- {
- Type: C.TypeMixed,
- Tag: "mixed-in",
- MixedOptions: option.HTTPMixedInboundOptions{
- ListenOptions: option.ListenOptions{
- Listen: option.NewListenAddress(netip.IPv4Unspecified()),
- ListenPort: otherClientPort,
- },
- },
- },
- },
- Outbounds: []option.Outbound{
- {
- Type: C.TypeTrojan,
- Tag: "trojan-out",
- TrojanOptions: option.TrojanOutboundOptions{
- ServerOptions: option.ServerOptions{
- Server: "127.0.0.1",
- ServerPort: otherPort,
- },
- Password: userId.String(),
- TLS: &option.OutboundTLSOptions{
- Enabled: true,
- ServerName: "example.org",
- CertificatePath: certPem,
- },
- DialerOptions: option.DialerOptions{
- Detour: "vless-out",
- },
- },
- },
- {
- Type: C.TypeSOCKS,
- Tag: "vless-out",
- SocksOptions: option.SocksOutboundOptions{
- ServerOptions: option.ServerOptions{
- Server: "127.0.0.1",
- ServerPort: clientPort,
- },
- },
- },
- },
- })
- content, err := os.ReadFile("config/vless-tls-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())
- user.MustKey("flow").SetString(flow)
- content, err = ajson.Marshal(config)
- require.NoError(t, err)
- content, err = ajson.Marshal(config)
- require.NoError(t, err)
- startDockerContainer(t, DockerOptions{
- Image: ImageXRayCore,
- Ports: []uint16{clientPort},
- EntryPoint: "xray",
- Stdin: content,
- Bind: map[string]string{
- certPem: "/path/to/certificate.crt",
- keyPem: "/path/to/private.key",
- },
- })
- testTCP(t, otherClientPort, testPort)
- }
|