Skip to content

Commit 7d7f5c8

Browse files
committed
chore: add inbound test for tuic
1 parent e79465d commit 7d7f5c8

File tree

4 files changed

+93
-0
lines changed

4 files changed

+93
-0
lines changed

listener/inbound/common_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,11 @@ func NewHttpTestTunnel() *TestTunnel {
211211
},
212212
CloseFn: ln.Close,
213213
DoTestFn: func(t *testing.T, proxy C.ProxyAdapter) {
214+
// Sequential testing for debugging
215+
testFn(t, proxy, "http")
216+
testFn(t, proxy, "https")
217+
218+
// Concurrent testing to detect stress
214219
wg := sync.WaitGroup{}
215220
num := 50
216221
for i := 0; i < num; i++ {

listener/inbound/shadowsocks_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ func testInboundShadowSocks(t *testing.T, inboundOptions inbound.ShadowSocksOpti
3535
for _, cipher := range shadowsocksCipherList {
3636
t.Run(cipher, func(t *testing.T) {
3737
t.Parallel()
38+
inboundOptions, outboundOptions := inboundOptions, outboundOptions // don't modify outside options value
3839
inboundOptions.Cipher = cipher
3940
outboundOptions.Cipher = cipher
4041
testInboundShadowSocks0(t, inboundOptions, outboundOptions)

listener/inbound/tuic_test.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package inbound_test
2+
3+
import (
4+
"net/netip"
5+
"testing"
6+
7+
"github.com/metacubex/mihomo/adapter/outbound"
8+
"github.com/metacubex/mihomo/listener/inbound"
9+
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
var tuicCCs = []string{"cubic", "new_reno", "bbr"}
14+
15+
func testInboundTuic(t *testing.T, inboundOptions inbound.TuicOption, outboundOptions outbound.TuicOption) {
16+
inboundOptions.Users = map[string]string{userUUID: userUUID}
17+
inboundOptions.Token = []string{userUUID}
18+
19+
for _, tuicCC := range tuicCCs {
20+
t.Run("v4", func(t *testing.T) {
21+
t.Parallel()
22+
inboundOptions, outboundOptions := inboundOptions, outboundOptions // don't modify outside options value
23+
outboundOptions.Token = userUUID
24+
outboundOptions.CongestionController = tuicCC
25+
inboundOptions.CongestionController = tuicCC
26+
testInboundTuic0(t, inboundOptions, outboundOptions)
27+
})
28+
t.Run("v5", func(t *testing.T) {
29+
t.Parallel()
30+
inboundOptions, outboundOptions := inboundOptions, outboundOptions // don't modify outside options value
31+
outboundOptions.UUID = userUUID
32+
outboundOptions.Password = userUUID
33+
outboundOptions.CongestionController = tuicCC
34+
inboundOptions.CongestionController = tuicCC
35+
testInboundTuic0(t, inboundOptions, outboundOptions)
36+
})
37+
}
38+
}
39+
40+
func testInboundTuic0(t *testing.T, inboundOptions inbound.TuicOption, outboundOptions outbound.TuicOption) {
41+
inboundOptions.BaseOption = inbound.BaseOption{
42+
NameStr: "tuic_inbound",
43+
Listen: "127.0.0.1",
44+
Port: "0",
45+
}
46+
in, err := inbound.NewTuic(&inboundOptions)
47+
assert.NoError(t, err)
48+
49+
tunnel := NewHttpTestTunnel()
50+
defer tunnel.Close()
51+
52+
err = in.Listen(tunnel)
53+
assert.NoError(t, err)
54+
defer in.Close()
55+
56+
addrPort, err := netip.ParseAddrPort(in.Address())
57+
assert.NoError(t, err)
58+
59+
outboundOptions.Name = "tuic_outbound"
60+
outboundOptions.Server = addrPort.Addr().String()
61+
outboundOptions.Port = int(addrPort.Port())
62+
63+
out, err := outbound.NewTuic(outboundOptions)
64+
assert.NoError(t, err)
65+
defer out.Close()
66+
67+
tunnel.DoTest(t, out)
68+
}
69+
70+
func TestInboundTuic_TLS(t *testing.T) {
71+
inboundOptions := inbound.TuicOption{
72+
Certificate: tlsCertificate,
73+
PrivateKey: tlsPrivateKey,
74+
}
75+
outboundOptions := outbound.TuicOption{
76+
Fingerprint: tlsFingerprint,
77+
}
78+
testInboundTuic(t, inboundOptions, outboundOptions)
79+
}

listener/tuic/server.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ func New(config LC.TuicServer, tunnel C.Tunnel, additions ...inbound.Addition) (
6060
} else {
6161
tlsConfig.NextProtos = []string{"h3"}
6262
}
63+
64+
if config.MaxIdleTime == 0 {
65+
config.MaxIdleTime = 15000
66+
}
67+
if config.AuthenticationTimeout == 0 {
68+
config.AuthenticationTimeout = 1000
69+
}
70+
6371
quicConfig := &quic.Config{
6472
MaxIdleTimeout: time.Duration(config.MaxIdleTime) * time.Millisecond,
6573
MaxIncomingStreams: ServerMaxIncomingStreams,

0 commit comments

Comments
 (0)