| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531 |
- package main
- import (
- "net/netip"
- "os"
- "strings"
- "testing"
- "github.com/sagernet/sing-box/common/tls"
- C "github.com/sagernet/sing-box/constant"
- "github.com/sagernet/sing-box/option"
- "github.com/sagernet/sing-box/protocol/naive"
- "github.com/sagernet/sing/common"
- "github.com/sagernet/sing/common/auth"
- "github.com/sagernet/sing/common/json/badoption"
- "github.com/sagernet/sing/common/network"
- "github.com/stretchr/testify/require"
- )
- func TestNaiveSelf(t *testing.T) {
- caPem, certPem, keyPem := createSelfSignedCertificate(t, "example.org")
- caPemContent, err := os.ReadFile(caPem)
- require.NoError(t, err)
- startInstance(t, option.Options{
- Inbounds: []option.Inbound{
- {
- Type: C.TypeMixed,
- Tag: "mixed-in",
- Options: &option.HTTPMixedInboundOptions{
- ListenOptions: option.ListenOptions{
- Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
- ListenPort: clientPort,
- },
- },
- },
- {
- Type: C.TypeNaive,
- Tag: "naive-in",
- Options: &option.NaiveInboundOptions{
- ListenOptions: option.ListenOptions{
- Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
- ListenPort: serverPort,
- },
- Users: []auth.User{
- {
- Username: "sekai",
- Password: "password",
- },
- },
- Network: network.NetworkTCP,
- InboundTLSOptionsContainer: option.InboundTLSOptionsContainer{
- TLS: &option.InboundTLSOptions{
- Enabled: true,
- ServerName: "example.org",
- CertificatePath: certPem,
- KeyPath: keyPem,
- },
- },
- },
- },
- },
- Outbounds: []option.Outbound{
- {
- Type: C.TypeDirect,
- },
- {
- Type: C.TypeNaive,
- Tag: "naive-out",
- Options: &option.NaiveOutboundOptions{
- ServerOptions: option.ServerOptions{
- Server: "127.0.0.1",
- ServerPort: serverPort,
- },
- Username: "sekai",
- Password: "password",
- OutboundTLSOptionsContainer: option.OutboundTLSOptionsContainer{
- TLS: &option.OutboundTLSOptions{
- Enabled: true,
- ServerName: "example.org",
- Certificate: []string{string(caPemContent)},
- },
- },
- },
- },
- },
- Route: &option.RouteOptions{
- Rules: []option.Rule{
- {
- Type: C.RuleTypeDefault,
- DefaultOptions: option.DefaultRule{
- RawDefaultRule: option.RawDefaultRule{
- Inbound: []string{"mixed-in"},
- },
- RuleAction: option.RuleAction{
- Action: C.RuleActionTypeRoute,
- RouteOptions: option.RouteActionOptions{
- Outbound: "naive-out",
- },
- },
- },
- },
- },
- },
- })
- testTCP(t, clientPort, testPort)
- }
- func TestNaiveSelfECH(t *testing.T) {
- caPem, certPem, keyPem := createSelfSignedCertificate(t, "example.org")
- caPemContent, err := os.ReadFile(caPem)
- require.NoError(t, err)
- echConfig, echKey := common.Must2(tls.ECHKeygenDefault("not.example.org"))
- instance := startInstance(t, option.Options{
- Inbounds: []option.Inbound{
- {
- Type: C.TypeMixed,
- Tag: "mixed-in",
- Options: &option.HTTPMixedInboundOptions{
- ListenOptions: option.ListenOptions{
- Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
- ListenPort: clientPort,
- },
- },
- },
- {
- Type: C.TypeNaive,
- Tag: "naive-in",
- Options: &option.NaiveInboundOptions{
- ListenOptions: option.ListenOptions{
- Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
- ListenPort: serverPort,
- },
- Users: []auth.User{
- {
- Username: "sekai",
- Password: "password",
- },
- },
- Network: network.NetworkTCP,
- InboundTLSOptionsContainer: option.InboundTLSOptionsContainer{
- TLS: &option.InboundTLSOptions{
- Enabled: true,
- ServerName: "example.org",
- CertificatePath: certPem,
- KeyPath: keyPem,
- ECH: &option.InboundECHOptions{
- Enabled: true,
- Key: []string{echKey},
- },
- },
- },
- },
- },
- },
- Outbounds: []option.Outbound{
- {
- Type: C.TypeDirect,
- },
- {
- Type: C.TypeNaive,
- Tag: "naive-out",
- Options: &option.NaiveOutboundOptions{
- ServerOptions: option.ServerOptions{
- Server: "127.0.0.1",
- ServerPort: serverPort,
- },
- Username: "sekai",
- Password: "password",
- OutboundTLSOptionsContainer: option.OutboundTLSOptionsContainer{
- TLS: &option.OutboundTLSOptions{
- Enabled: true,
- ServerName: "example.org",
- Certificate: []string{string(caPemContent)},
- ECH: &option.OutboundECHOptions{
- Enabled: true,
- Config: []string{echConfig},
- },
- },
- },
- },
- },
- },
- Route: &option.RouteOptions{
- Rules: []option.Rule{
- {
- Type: C.RuleTypeDefault,
- DefaultOptions: option.DefaultRule{
- RawDefaultRule: option.RawDefaultRule{
- Inbound: []string{"mixed-in"},
- },
- RuleAction: option.RuleAction{
- Action: C.RuleActionTypeRoute,
- RouteOptions: option.RouteActionOptions{
- Outbound: "naive-out",
- },
- },
- },
- },
- },
- },
- })
- naiveOut, ok := instance.Outbound().Outbound("naive-out")
- require.True(t, ok)
- naiveOutbound := naiveOut.(*naive.Outbound)
- netLogPath := "/tmp/naive_ech_netlog.json"
- require.True(t, naiveOutbound.Client().Engine().StartNetLogToFile(netLogPath, true))
- defer naiveOutbound.Client().Engine().StopNetLog()
- testTCP(t, clientPort, testPort)
- naiveOutbound.Client().Engine().StopNetLog()
- logContent, err := os.ReadFile(netLogPath)
- require.NoError(t, err)
- logStr := string(logContent)
- require.True(t, strings.Contains(logStr, `"encrypted_client_hello":true`),
- "ECH should be accepted in TLS handshake. NetLog saved to: %s", netLogPath)
- }
- func TestNaiveSelfInsecureConcurrency(t *testing.T) {
- caPem, certPem, keyPem := createSelfSignedCertificate(t, "example.org")
- caPemContent, err := os.ReadFile(caPem)
- require.NoError(t, err)
- instance := startInstance(t, option.Options{
- Inbounds: []option.Inbound{
- {
- Type: C.TypeMixed,
- Tag: "mixed-in",
- Options: &option.HTTPMixedInboundOptions{
- ListenOptions: option.ListenOptions{
- Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
- ListenPort: clientPort,
- },
- },
- },
- {
- Type: C.TypeNaive,
- Tag: "naive-in",
- Options: &option.NaiveInboundOptions{
- ListenOptions: option.ListenOptions{
- Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
- ListenPort: serverPort,
- },
- Users: []auth.User{
- {
- Username: "sekai",
- Password: "password",
- },
- },
- Network: network.NetworkTCP,
- InboundTLSOptionsContainer: option.InboundTLSOptionsContainer{
- TLS: &option.InboundTLSOptions{
- Enabled: true,
- ServerName: "example.org",
- CertificatePath: certPem,
- KeyPath: keyPem,
- },
- },
- },
- },
- },
- Outbounds: []option.Outbound{
- {
- Type: C.TypeDirect,
- },
- {
- Type: C.TypeNaive,
- Tag: "naive-out",
- Options: &option.NaiveOutboundOptions{
- ServerOptions: option.ServerOptions{
- Server: "127.0.0.1",
- ServerPort: serverPort,
- },
- Username: "sekai",
- Password: "password",
- InsecureConcurrency: 3,
- OutboundTLSOptionsContainer: option.OutboundTLSOptionsContainer{
- TLS: &option.OutboundTLSOptions{
- Enabled: true,
- ServerName: "example.org",
- Certificate: []string{string(caPemContent)},
- },
- },
- },
- },
- },
- Route: &option.RouteOptions{
- Rules: []option.Rule{
- {
- Type: C.RuleTypeDefault,
- DefaultOptions: option.DefaultRule{
- RawDefaultRule: option.RawDefaultRule{
- Inbound: []string{"mixed-in"},
- },
- RuleAction: option.RuleAction{
- Action: C.RuleActionTypeRoute,
- RouteOptions: option.RouteActionOptions{
- Outbound: "naive-out",
- },
- },
- },
- },
- },
- },
- })
- naiveOut, ok := instance.Outbound().Outbound("naive-out")
- require.True(t, ok)
- naiveOutbound := naiveOut.(*naive.Outbound)
- netLogPath := "/tmp/naive_concurrency_netlog.json"
- require.True(t, naiveOutbound.Client().Engine().StartNetLogToFile(netLogPath, true))
- defer naiveOutbound.Client().Engine().StopNetLog()
- // Send multiple sequential connections to trigger round-robin
- // With insecure_concurrency=3, connections will be distributed to 3 pools
- for i := 0; i < 6; i++ {
- testTCP(t, clientPort, testPort)
- }
- naiveOutbound.Client().Engine().StopNetLog()
- // Verify NetLog contains multiple independent HTTP/2 sessions
- logContent, err := os.ReadFile(netLogPath)
- require.NoError(t, err)
- logStr := string(logContent)
- // Count HTTP2_SESSION_INITIALIZED events to verify connection pool isolation
- // NetLog stores event types as numeric IDs, HTTP2_SESSION_INITIALIZED = 249
- sessionCount := strings.Count(logStr, `"type":249`)
- require.GreaterOrEqual(t, sessionCount, 3,
- "Expected at least 3 HTTP/2 sessions with insecure_concurrency=3. NetLog: %s", netLogPath)
- }
- func TestNaiveSelfQUIC(t *testing.T) {
- caPem, certPem, keyPem := createSelfSignedCertificate(t, "example.org")
- caPemContent, err := os.ReadFile(caPem)
- require.NoError(t, err)
- startInstance(t, option.Options{
- Inbounds: []option.Inbound{
- {
- Type: C.TypeMixed,
- Tag: "mixed-in",
- Options: &option.HTTPMixedInboundOptions{
- ListenOptions: option.ListenOptions{
- Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
- ListenPort: clientPort,
- },
- },
- },
- {
- Type: C.TypeNaive,
- Tag: "naive-in",
- Options: &option.NaiveInboundOptions{
- ListenOptions: option.ListenOptions{
- Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
- ListenPort: serverPort,
- },
- Users: []auth.User{
- {
- Username: "sekai",
- Password: "password",
- },
- },
- Network: network.NetworkUDP,
- InboundTLSOptionsContainer: option.InboundTLSOptionsContainer{
- TLS: &option.InboundTLSOptions{
- Enabled: true,
- ServerName: "example.org",
- CertificatePath: certPem,
- KeyPath: keyPem,
- },
- },
- },
- },
- },
- Outbounds: []option.Outbound{
- {
- Type: C.TypeDirect,
- },
- {
- Type: C.TypeNaive,
- Tag: "naive-out",
- Options: &option.NaiveOutboundOptions{
- ServerOptions: option.ServerOptions{
- Server: "127.0.0.1",
- ServerPort: serverPort,
- },
- Username: "sekai",
- Password: "password",
- QUIC: true,
- OutboundTLSOptionsContainer: option.OutboundTLSOptionsContainer{
- TLS: &option.OutboundTLSOptions{
- Enabled: true,
- ServerName: "example.org",
- Certificate: []string{string(caPemContent)},
- },
- },
- },
- },
- },
- Route: &option.RouteOptions{
- Rules: []option.Rule{
- {
- Type: C.RuleTypeDefault,
- DefaultOptions: option.DefaultRule{
- RawDefaultRule: option.RawDefaultRule{
- Inbound: []string{"mixed-in"},
- },
- RuleAction: option.RuleAction{
- Action: C.RuleActionTypeRoute,
- RouteOptions: option.RouteActionOptions{
- Outbound: "naive-out",
- },
- },
- },
- },
- },
- },
- })
- testTCP(t, clientPort, testPort)
- }
- func TestNaiveSelfQUICCongestionControl(t *testing.T) {
- testCases := []struct {
- name string
- congestionControl string
- }{
- {"BBR", "bbr"},
- {"BBR2", "bbr2"},
- {"Cubic", "cubic"},
- {"Reno", "reno"},
- }
- for _, tc := range testCases {
- t.Run(tc.name, func(t *testing.T) {
- caPem, certPem, keyPem := createSelfSignedCertificate(t, "example.org")
- caPemContent, err := os.ReadFile(caPem)
- require.NoError(t, err)
- startInstance(t, option.Options{
- Inbounds: []option.Inbound{
- {
- Type: C.TypeMixed,
- Tag: "mixed-in",
- Options: &option.HTTPMixedInboundOptions{
- ListenOptions: option.ListenOptions{
- Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
- ListenPort: clientPort,
- },
- },
- },
- {
- Type: C.TypeNaive,
- Tag: "naive-in",
- Options: &option.NaiveInboundOptions{
- ListenOptions: option.ListenOptions{
- Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
- ListenPort: serverPort,
- },
- Users: []auth.User{
- {
- Username: "sekai",
- Password: "password",
- },
- },
- Network: network.NetworkUDP,
- QUICCongestionControl: tc.congestionControl,
- InboundTLSOptionsContainer: option.InboundTLSOptionsContainer{
- TLS: &option.InboundTLSOptions{
- Enabled: true,
- ServerName: "example.org",
- CertificatePath: certPem,
- KeyPath: keyPem,
- },
- },
- },
- },
- },
- Outbounds: []option.Outbound{
- {
- Type: C.TypeDirect,
- },
- {
- Type: C.TypeNaive,
- Tag: "naive-out",
- Options: &option.NaiveOutboundOptions{
- ServerOptions: option.ServerOptions{
- Server: "127.0.0.1",
- ServerPort: serverPort,
- },
- Username: "sekai",
- Password: "password",
- QUIC: true,
- QUICCongestionControl: tc.congestionControl,
- OutboundTLSOptionsContainer: option.OutboundTLSOptionsContainer{
- TLS: &option.OutboundTLSOptions{
- Enabled: true,
- ServerName: "example.org",
- Certificate: []string{string(caPemContent)},
- },
- },
- },
- },
- },
- Route: &option.RouteOptions{
- Rules: []option.Rule{
- {
- Type: C.RuleTypeDefault,
- DefaultOptions: option.DefaultRule{
- RawDefaultRule: option.RawDefaultRule{
- Inbound: []string{"mixed-in"},
- },
- RuleAction: option.RuleAction{
- Action: C.RuleActionTypeRoute,
- RouteOptions: option.RouteActionOptions{
- Outbound: "naive-out",
- },
- },
- },
- },
- },
- },
- })
- testTCP(t, clientPort, testPort)
- })
- }
- }
|