ech_test.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. package main
  2. import (
  3. "net/netip"
  4. "testing"
  5. "github.com/sagernet/sing-box/common/tls"
  6. C "github.com/sagernet/sing-box/constant"
  7. "github.com/sagernet/sing-box/option"
  8. "github.com/sagernet/sing/common"
  9. "github.com/gofrs/uuid/v5"
  10. )
  11. func TestECH(t *testing.T) {
  12. _, certPem, keyPem := createSelfSignedCertificate(t, "example.org")
  13. echConfig, echKey := common.Must2(tls.ECHKeygenDefault("not.example.org", false))
  14. startInstance(t, option.Options{
  15. Inbounds: []option.Inbound{
  16. {
  17. Type: C.TypeMixed,
  18. Tag: "mixed-in",
  19. MixedOptions: option.HTTPMixedInboundOptions{
  20. ListenOptions: option.ListenOptions{
  21. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  22. ListenPort: clientPort,
  23. },
  24. },
  25. },
  26. {
  27. Type: C.TypeTrojan,
  28. TrojanOptions: option.TrojanInboundOptions{
  29. ListenOptions: option.ListenOptions{
  30. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  31. ListenPort: serverPort,
  32. },
  33. Users: []option.TrojanUser{
  34. {
  35. Name: "sekai",
  36. Password: "password",
  37. },
  38. },
  39. InboundTLSOptionsContainer: option.InboundTLSOptionsContainer{
  40. TLS: &option.InboundTLSOptions{
  41. Enabled: true,
  42. ServerName: "example.org",
  43. CertificatePath: certPem,
  44. KeyPath: keyPem,
  45. ECH: &option.InboundECHOptions{
  46. Enabled: true,
  47. Key: []string{echKey},
  48. },
  49. },
  50. },
  51. },
  52. },
  53. },
  54. Outbounds: []option.Outbound{
  55. {
  56. Type: C.TypeDirect,
  57. },
  58. {
  59. Type: C.TypeTrojan,
  60. Tag: "trojan-out",
  61. TrojanOptions: option.TrojanOutboundOptions{
  62. ServerOptions: option.ServerOptions{
  63. Server: "127.0.0.1",
  64. ServerPort: serverPort,
  65. },
  66. Password: "password",
  67. OutboundTLSOptionsContainer: option.OutboundTLSOptionsContainer{
  68. TLS: &option.OutboundTLSOptions{
  69. Enabled: true,
  70. ServerName: "example.org",
  71. CertificatePath: certPem,
  72. ECH: &option.OutboundECHOptions{
  73. Enabled: true,
  74. Config: []string{echConfig},
  75. },
  76. },
  77. },
  78. },
  79. },
  80. },
  81. Route: &option.RouteOptions{
  82. Rules: []option.Rule{
  83. {
  84. DefaultOptions: option.DefaultRule{
  85. Inbound: []string{"mixed-in"},
  86. Outbound: "trojan-out",
  87. },
  88. },
  89. },
  90. },
  91. })
  92. testSuit(t, clientPort, testPort)
  93. }
  94. func TestECHQUIC(t *testing.T) {
  95. _, certPem, keyPem := createSelfSignedCertificate(t, "example.org")
  96. echConfig, echKey := common.Must2(tls.ECHKeygenDefault("not.example.org", false))
  97. startInstance(t, option.Options{
  98. Inbounds: []option.Inbound{
  99. {
  100. Type: C.TypeMixed,
  101. Tag: "mixed-in",
  102. MixedOptions: option.HTTPMixedInboundOptions{
  103. ListenOptions: option.ListenOptions{
  104. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  105. ListenPort: clientPort,
  106. },
  107. },
  108. },
  109. {
  110. Type: C.TypeTUIC,
  111. TUICOptions: option.TUICInboundOptions{
  112. ListenOptions: option.ListenOptions{
  113. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  114. ListenPort: serverPort,
  115. },
  116. Users: []option.TUICUser{{
  117. UUID: uuid.Nil.String(),
  118. }},
  119. InboundTLSOptionsContainer: option.InboundTLSOptionsContainer{
  120. TLS: &option.InboundTLSOptions{
  121. Enabled: true,
  122. ServerName: "example.org",
  123. CertificatePath: certPem,
  124. KeyPath: keyPem,
  125. ECH: &option.InboundECHOptions{
  126. Enabled: true,
  127. Key: []string{echKey},
  128. },
  129. },
  130. },
  131. },
  132. },
  133. },
  134. Outbounds: []option.Outbound{
  135. {
  136. Type: C.TypeDirect,
  137. },
  138. {
  139. Type: C.TypeTUIC,
  140. Tag: "tuic-out",
  141. TUICOptions: option.TUICOutboundOptions{
  142. ServerOptions: option.ServerOptions{
  143. Server: "127.0.0.1",
  144. ServerPort: serverPort,
  145. },
  146. UUID: uuid.Nil.String(),
  147. OutboundTLSOptionsContainer: option.OutboundTLSOptionsContainer{
  148. TLS: &option.OutboundTLSOptions{
  149. Enabled: true,
  150. ServerName: "example.org",
  151. CertificatePath: certPem,
  152. ECH: &option.OutboundECHOptions{
  153. Enabled: true,
  154. Config: []string{echConfig},
  155. },
  156. },
  157. },
  158. },
  159. },
  160. },
  161. Route: &option.RouteOptions{
  162. Rules: []option.Rule{
  163. {
  164. DefaultOptions: option.DefaultRule{
  165. Inbound: []string{"mixed-in"},
  166. Outbound: "tuic-out",
  167. },
  168. },
  169. },
  170. },
  171. })
  172. testSuitLargeUDP(t, clientPort, testPort)
  173. }
  174. func TestECHHysteria2(t *testing.T) {
  175. _, certPem, keyPem := createSelfSignedCertificate(t, "example.org")
  176. echConfig, echKey := common.Must2(tls.ECHKeygenDefault("not.example.org", false))
  177. startInstance(t, option.Options{
  178. Inbounds: []option.Inbound{
  179. {
  180. Type: C.TypeMixed,
  181. Tag: "mixed-in",
  182. MixedOptions: option.HTTPMixedInboundOptions{
  183. ListenOptions: option.ListenOptions{
  184. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  185. ListenPort: clientPort,
  186. },
  187. },
  188. },
  189. {
  190. Type: C.TypeHysteria2,
  191. Hysteria2Options: option.Hysteria2InboundOptions{
  192. ListenOptions: option.ListenOptions{
  193. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  194. ListenPort: serverPort,
  195. },
  196. Users: []option.Hysteria2User{{
  197. Password: "password",
  198. }},
  199. InboundTLSOptionsContainer: option.InboundTLSOptionsContainer{
  200. TLS: &option.InboundTLSOptions{
  201. Enabled: true,
  202. ServerName: "example.org",
  203. CertificatePath: certPem,
  204. KeyPath: keyPem,
  205. ECH: &option.InboundECHOptions{
  206. Enabled: true,
  207. Key: []string{echKey},
  208. },
  209. },
  210. },
  211. },
  212. },
  213. },
  214. Outbounds: []option.Outbound{
  215. {
  216. Type: C.TypeDirect,
  217. },
  218. {
  219. Type: C.TypeHysteria2,
  220. Tag: "hy2-out",
  221. Hysteria2Options: option.Hysteria2OutboundOptions{
  222. ServerOptions: option.ServerOptions{
  223. Server: "127.0.0.1",
  224. ServerPort: serverPort,
  225. },
  226. Password: "password",
  227. OutboundTLSOptionsContainer: option.OutboundTLSOptionsContainer{
  228. TLS: &option.OutboundTLSOptions{
  229. Enabled: true,
  230. ServerName: "example.org",
  231. CertificatePath: certPem,
  232. ECH: &option.OutboundECHOptions{
  233. Enabled: true,
  234. Config: []string{echConfig},
  235. },
  236. },
  237. },
  238. },
  239. },
  240. },
  241. Route: &option.RouteOptions{
  242. Rules: []option.Rule{
  243. {
  244. Type: C.RuleTypeDefault,
  245. DefaultOptions: option.DefaultRule{
  246. Inbound: []string{"mixed-in"},
  247. Outbound: "hy2-out",
  248. },
  249. },
  250. },
  251. },
  252. })
  253. testSuitLargeUDP(t, clientPort, testPort)
  254. }