From 7d7f5c89806dd545ddb763ab9682f3a6764f136c Mon Sep 17 00:00:00 2001 From: wwqgtxx Date: Thu, 17 Apr 2025 10:02:48 +0800 Subject: [PATCH] chore: add inbound test for tuic --- listener/inbound/common_test.go | 5 ++ listener/inbound/shadowsocks_test.go | 1 + listener/inbound/tuic_test.go | 79 ++++++++++++++++++++++++++++ listener/tuic/server.go | 8 +++ 4 files changed, 93 insertions(+) create mode 100644 listener/inbound/tuic_test.go diff --git a/listener/inbound/common_test.go b/listener/inbound/common_test.go index 3fabc1d1..80ec006b 100644 --- a/listener/inbound/common_test.go +++ b/listener/inbound/common_test.go @@ -211,6 +211,11 @@ func NewHttpTestTunnel() *TestTunnel { }, CloseFn: ln.Close, DoTestFn: func(t *testing.T, proxy C.ProxyAdapter) { + // Sequential testing for debugging + testFn(t, proxy, "http") + testFn(t, proxy, "https") + + // Concurrent testing to detect stress wg := sync.WaitGroup{} num := 50 for i := 0; i < num; i++ { diff --git a/listener/inbound/shadowsocks_test.go b/listener/inbound/shadowsocks_test.go index 044898a3..34772a21 100644 --- a/listener/inbound/shadowsocks_test.go +++ b/listener/inbound/shadowsocks_test.go @@ -35,6 +35,7 @@ func testInboundShadowSocks(t *testing.T, inboundOptions inbound.ShadowSocksOpti for _, cipher := range shadowsocksCipherList { t.Run(cipher, func(t *testing.T) { t.Parallel() + inboundOptions, outboundOptions := inboundOptions, outboundOptions // don't modify outside options value inboundOptions.Cipher = cipher outboundOptions.Cipher = cipher testInboundShadowSocks0(t, inboundOptions, outboundOptions) diff --git a/listener/inbound/tuic_test.go b/listener/inbound/tuic_test.go new file mode 100644 index 00000000..236d3f81 --- /dev/null +++ b/listener/inbound/tuic_test.go @@ -0,0 +1,79 @@ +package inbound_test + +import ( + "net/netip" + "testing" + + "github.com/metacubex/mihomo/adapter/outbound" + "github.com/metacubex/mihomo/listener/inbound" + + "github.com/stretchr/testify/assert" +) + +var tuicCCs = []string{"cubic", "new_reno", "bbr"} + +func testInboundTuic(t *testing.T, inboundOptions inbound.TuicOption, outboundOptions outbound.TuicOption) { + inboundOptions.Users = map[string]string{userUUID: userUUID} + inboundOptions.Token = []string{userUUID} + + for _, tuicCC := range tuicCCs { + t.Run("v4", func(t *testing.T) { + t.Parallel() + inboundOptions, outboundOptions := inboundOptions, outboundOptions // don't modify outside options value + outboundOptions.Token = userUUID + outboundOptions.CongestionController = tuicCC + inboundOptions.CongestionController = tuicCC + testInboundTuic0(t, inboundOptions, outboundOptions) + }) + t.Run("v5", func(t *testing.T) { + t.Parallel() + inboundOptions, outboundOptions := inboundOptions, outboundOptions // don't modify outside options value + outboundOptions.UUID = userUUID + outboundOptions.Password = userUUID + outboundOptions.CongestionController = tuicCC + inboundOptions.CongestionController = tuicCC + testInboundTuic0(t, inboundOptions, outboundOptions) + }) + } +} + +func testInboundTuic0(t *testing.T, inboundOptions inbound.TuicOption, outboundOptions outbound.TuicOption) { + inboundOptions.BaseOption = inbound.BaseOption{ + NameStr: "tuic_inbound", + Listen: "127.0.0.1", + Port: "0", + } + in, err := inbound.NewTuic(&inboundOptions) + assert.NoError(t, err) + + tunnel := NewHttpTestTunnel() + defer tunnel.Close() + + err = in.Listen(tunnel) + assert.NoError(t, err) + defer in.Close() + + addrPort, err := netip.ParseAddrPort(in.Address()) + assert.NoError(t, err) + + outboundOptions.Name = "tuic_outbound" + outboundOptions.Server = addrPort.Addr().String() + outboundOptions.Port = int(addrPort.Port()) + + out, err := outbound.NewTuic(outboundOptions) + assert.NoError(t, err) + defer out.Close() + + tunnel.DoTest(t, out) +} + +func TestInboundTuic_TLS(t *testing.T) { + inboundOptions := inbound.TuicOption{ + Certificate: tlsCertificate, + PrivateKey: tlsPrivateKey, + } + outboundOptions := outbound.TuicOption{ + Fingerprint: tlsFingerprint, + } + testInboundTuic(t, inboundOptions, outboundOptions) +} diff --git a/listener/tuic/server.go b/listener/tuic/server.go index 3425cb04..ea4aed8f 100644 --- a/listener/tuic/server.go +++ b/listener/tuic/server.go @@ -60,6 +60,14 @@ func New(config LC.TuicServer, tunnel C.Tunnel, additions ...inbound.Addition) ( } else { tlsConfig.NextProtos = []string{"h3"} } + + if config.MaxIdleTime == 0 { + config.MaxIdleTime = 15000 + } + if config.AuthenticationTimeout == 0 { + config.AuthenticationTimeout = 1000 + } + quicConfig := &quic.Config{ MaxIdleTimeout: time.Duration(config.MaxIdleTime) * time.Millisecond, MaxIncomingStreams: ServerMaxIncomingStreams,