tuic_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. package main
  2. import (
  3. "net/netip"
  4. "testing"
  5. C "github.com/sagernet/sing-box/constant"
  6. "github.com/sagernet/sing-box/option"
  7. "github.com/gofrs/uuid/v5"
  8. )
  9. func TestTUICSelf(t *testing.T) {
  10. t.Run("self", func(t *testing.T) {
  11. testTUICSelf(t, false, false)
  12. })
  13. t.Run("self-udp-stream", func(t *testing.T) {
  14. testTUICSelf(t, true, false)
  15. })
  16. t.Run("self-early", func(t *testing.T) {
  17. testTUICSelf(t, false, true)
  18. })
  19. }
  20. func testTUICSelf(t *testing.T, udpStream bool, zeroRTTHandshake bool) {
  21. _, certPem, keyPem := createSelfSignedCertificate(t, "example.org")
  22. var udpRelayMode string
  23. if udpStream {
  24. udpRelayMode = "quic"
  25. }
  26. startInstance(t, option.Options{
  27. Inbounds: []option.Inbound{
  28. {
  29. Type: C.TypeMixed,
  30. Tag: "mixed-in",
  31. MixedOptions: option.HTTPMixedInboundOptions{
  32. ListenOptions: option.ListenOptions{
  33. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  34. ListenPort: clientPort,
  35. },
  36. },
  37. },
  38. {
  39. Type: C.TypeTUIC,
  40. TUICOptions: option.TUICInboundOptions{
  41. ListenOptions: option.ListenOptions{
  42. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  43. ListenPort: serverPort,
  44. },
  45. Users: []option.TUICUser{{
  46. UUID: uuid.Nil.String(),
  47. }},
  48. ZeroRTTHandshake: zeroRTTHandshake,
  49. TLS: &option.InboundTLSOptions{
  50. Enabled: true,
  51. ServerName: "example.org",
  52. CertificatePath: certPem,
  53. KeyPath: keyPem,
  54. },
  55. },
  56. },
  57. },
  58. Outbounds: []option.Outbound{
  59. {
  60. Type: C.TypeDirect,
  61. },
  62. {
  63. Type: C.TypeTUIC,
  64. Tag: "tuic-out",
  65. TUICOptions: option.TUICOutboundOptions{
  66. ServerOptions: option.ServerOptions{
  67. Server: "127.0.0.1",
  68. ServerPort: serverPort,
  69. },
  70. UUID: uuid.Nil.String(),
  71. UDPRelayMode: udpRelayMode,
  72. ZeroRTTHandshake: zeroRTTHandshake,
  73. TLS: &option.OutboundTLSOptions{
  74. Enabled: true,
  75. ServerName: "example.org",
  76. CertificatePath: certPem,
  77. },
  78. },
  79. },
  80. },
  81. Route: &option.RouteOptions{
  82. Rules: []option.Rule{
  83. {
  84. DefaultOptions: option.DefaultRule{
  85. Inbound: []string{"mixed-in"},
  86. Outbound: "tuic-out",
  87. },
  88. },
  89. },
  90. },
  91. })
  92. testSuitLargeUDP(t, clientPort, testPort)
  93. }
  94. func TestTUICInbound(t *testing.T) {
  95. caPem, certPem, keyPem := createSelfSignedCertificate(t, "example.org")
  96. startInstance(t, option.Options{
  97. Inbounds: []option.Inbound{
  98. {
  99. Type: C.TypeTUIC,
  100. TUICOptions: option.TUICInboundOptions{
  101. ListenOptions: option.ListenOptions{
  102. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  103. ListenPort: serverPort,
  104. },
  105. Users: []option.TUICUser{{
  106. UUID: "FE35D05B-8803-45C4-BAE6-723AD2CD5D3D",
  107. Password: "tuic",
  108. }},
  109. TLS: &option.InboundTLSOptions{
  110. Enabled: true,
  111. ServerName: "example.org",
  112. CertificatePath: certPem,
  113. KeyPath: keyPem,
  114. },
  115. },
  116. },
  117. },
  118. })
  119. startDockerContainer(t, DockerOptions{
  120. Image: ImageTUICClient,
  121. Ports: []uint16{serverPort, clientPort},
  122. Bind: map[string]string{
  123. "tuic-client.json": "/etc/tuic/config.json",
  124. caPem: "/etc/tuic/ca.pem",
  125. },
  126. })
  127. testSuitLargeUDP(t, clientPort, testPort)
  128. }
  129. func TestTUICOutbound(t *testing.T) {
  130. _, certPem, keyPem := createSelfSignedCertificate(t, "example.org")
  131. startDockerContainer(t, DockerOptions{
  132. Image: ImageTUICServer,
  133. Ports: []uint16{testPort},
  134. Bind: map[string]string{
  135. "tuic-server.json": "/etc/tuic/config.json",
  136. certPem: "/etc/tuic/cert.pem",
  137. keyPem: "/etc/tuic/key.pem",
  138. },
  139. })
  140. startInstance(t, option.Options{
  141. Inbounds: []option.Inbound{
  142. {
  143. Type: C.TypeMixed,
  144. MixedOptions: option.HTTPMixedInboundOptions{
  145. ListenOptions: option.ListenOptions{
  146. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  147. ListenPort: clientPort,
  148. },
  149. },
  150. },
  151. },
  152. Outbounds: []option.Outbound{
  153. {
  154. Type: C.TypeTUIC,
  155. TUICOptions: option.TUICOutboundOptions{
  156. ServerOptions: option.ServerOptions{
  157. Server: "127.0.0.1",
  158. ServerPort: serverPort,
  159. },
  160. UUID: "FE35D05B-8803-45C4-BAE6-723AD2CD5D3D",
  161. Password: "tuic",
  162. TLS: &option.OutboundTLSOptions{
  163. Enabled: true,
  164. ServerName: "example.org",
  165. CertificatePath: certPem,
  166. },
  167. },
  168. },
  169. },
  170. })
  171. testSuitLargeUDP(t, clientPort, testPort)
  172. }