tuic_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. InboundTLSOptionsContainer: option.InboundTLSOptionsContainer{
  50. TLS: &option.InboundTLSOptions{
  51. Enabled: true,
  52. ServerName: "example.org",
  53. CertificatePath: certPem,
  54. KeyPath: keyPem,
  55. },
  56. },
  57. },
  58. },
  59. },
  60. LegacyOutbounds: []option.LegacyOutbound{
  61. {
  62. Type: C.TypeDirect,
  63. },
  64. {
  65. Type: C.TypeTUIC,
  66. Tag: "tuic-out",
  67. TUICOptions: option.TUICOutboundOptions{
  68. ServerOptions: option.ServerOptions{
  69. Server: "127.0.0.1",
  70. ServerPort: serverPort,
  71. },
  72. UUID: uuid.Nil.String(),
  73. UDPRelayMode: udpRelayMode,
  74. ZeroRTTHandshake: zeroRTTHandshake,
  75. OutboundTLSOptionsContainer: option.OutboundTLSOptionsContainer{
  76. TLS: &option.OutboundTLSOptions{
  77. Enabled: true,
  78. ServerName: "example.org",
  79. CertificatePath: certPem,
  80. },
  81. },
  82. },
  83. },
  84. },
  85. Route: &option.RouteOptions{
  86. Rules: []option.Rule{
  87. {
  88. Type: C.RuleTypeDefault,
  89. DefaultOptions: option.DefaultRule{
  90. RawDefaultRule: option.RawDefaultRule{
  91. Inbound: []string{"mixed-in"},
  92. },
  93. RuleAction: option.RuleAction{
  94. Action: C.RuleActionTypeRoute,
  95. RouteOptions: option.RouteActionOptions{
  96. Outbound: "tuic-out",
  97. },
  98. },
  99. },
  100. },
  101. },
  102. },
  103. })
  104. testSuitLargeUDP(t, clientPort, testPort)
  105. }
  106. func TestTUICInbound(t *testing.T) {
  107. caPem, certPem, keyPem := createSelfSignedCertificate(t, "example.org")
  108. startInstance(t, option.Options{
  109. Inbounds: []option.Inbound{
  110. {
  111. Type: C.TypeTUIC,
  112. TUICOptions: option.TUICInboundOptions{
  113. ListenOptions: option.ListenOptions{
  114. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  115. ListenPort: serverPort,
  116. },
  117. Users: []option.TUICUser{{
  118. UUID: "FE35D05B-8803-45C4-BAE6-723AD2CD5D3D",
  119. Password: "tuic",
  120. }},
  121. InboundTLSOptionsContainer: option.InboundTLSOptionsContainer{
  122. TLS: &option.InboundTLSOptions{
  123. Enabled: true,
  124. ServerName: "example.org",
  125. CertificatePath: certPem,
  126. KeyPath: keyPem,
  127. },
  128. },
  129. },
  130. },
  131. },
  132. })
  133. startDockerContainer(t, DockerOptions{
  134. Image: ImageTUICClient,
  135. Ports: []uint16{serverPort, clientPort},
  136. Bind: map[string]string{
  137. "tuic-client.json": "/etc/tuic/config.json",
  138. caPem: "/etc/tuic/ca.pem",
  139. },
  140. })
  141. testSuitLargeUDP(t, clientPort, testPort)
  142. }
  143. func TestTUICOutbound(t *testing.T) {
  144. _, certPem, keyPem := createSelfSignedCertificate(t, "example.org")
  145. startDockerContainer(t, DockerOptions{
  146. Image: ImageTUICServer,
  147. Ports: []uint16{testPort},
  148. Bind: map[string]string{
  149. "tuic-server.json": "/etc/tuic/config.json",
  150. certPem: "/etc/tuic/cert.pem",
  151. keyPem: "/etc/tuic/key.pem",
  152. },
  153. })
  154. startInstance(t, option.Options{
  155. Inbounds: []option.Inbound{
  156. {
  157. Type: C.TypeMixed,
  158. MixedOptions: option.HTTPMixedInboundOptions{
  159. ListenOptions: option.ListenOptions{
  160. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  161. ListenPort: clientPort,
  162. },
  163. },
  164. },
  165. },
  166. LegacyOutbounds: []option.LegacyOutbound{
  167. {
  168. Type: C.TypeTUIC,
  169. TUICOptions: option.TUICOutboundOptions{
  170. ServerOptions: option.ServerOptions{
  171. Server: "127.0.0.1",
  172. ServerPort: serverPort,
  173. },
  174. UUID: "FE35D05B-8803-45C4-BAE6-723AD2CD5D3D",
  175. Password: "tuic",
  176. OutboundTLSOptionsContainer: option.OutboundTLSOptionsContainer{
  177. TLS: &option.OutboundTLSOptions{
  178. Enabled: true,
  179. ServerName: "example.org",
  180. CertificatePath: certPem,
  181. },
  182. },
  183. },
  184. },
  185. },
  186. })
  187. testSuitLargeUDP(t, clientPort, testPort)
  188. }