|
| 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 | +} |
0 commit comments