ech_test.go 5.9 KB

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