tuic_test.go 5.0 KB

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