hysteria2_test.go 5.2 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-quic/hysteria2"
  8. )
  9. func TestHysteria2Self(t *testing.T) {
  10. t.Run("self", func(t *testing.T) {
  11. testHysteria2Self(t, "")
  12. })
  13. t.Run("self-salamander", func(t *testing.T) {
  14. testHysteria2Self(t, "password")
  15. })
  16. }
  17. func testHysteria2Self(t *testing.T, salamanderPassword string) {
  18. _, certPem, keyPem := createSelfSignedCertificate(t, "example.org")
  19. var obfs *option.Hysteria2Obfs
  20. if salamanderPassword != "" {
  21. obfs = &option.Hysteria2Obfs{
  22. Type: hysteria2.ObfsTypeSalamander,
  23. Password: salamanderPassword,
  24. }
  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.TypeHysteria2,
  40. Hysteria2Options: option.Hysteria2InboundOptions{
  41. ListenOptions: option.ListenOptions{
  42. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  43. ListenPort: serverPort,
  44. },
  45. UpMbps: 100,
  46. DownMbps: 100,
  47. Obfs: obfs,
  48. Users: []option.Hysteria2User{{
  49. Password: "password",
  50. }},
  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.TypeHysteria2,
  68. Tag: "hy2-out",
  69. Hysteria2Options: option.Hysteria2OutboundOptions{
  70. ServerOptions: option.ServerOptions{
  71. Server: "127.0.0.1",
  72. ServerPort: serverPort,
  73. },
  74. UpMbps: 100,
  75. DownMbps: 100,
  76. Obfs: obfs,
  77. Password: "password",
  78. OutboundTLSOptionsContainer: option.OutboundTLSOptionsContainer{
  79. TLS: &option.OutboundTLSOptions{
  80. Enabled: true,
  81. ServerName: "example.org",
  82. CertificatePath: certPem,
  83. },
  84. },
  85. },
  86. },
  87. },
  88. Route: &option.RouteOptions{
  89. Rules: []option.Rule{
  90. {
  91. Type: C.RuleTypeDefault,
  92. DefaultOptions: option.DefaultRule{
  93. Inbound: []string{"mixed-in"},
  94. Outbound: "hy2-out",
  95. },
  96. },
  97. },
  98. },
  99. })
  100. testSuitLargeUDP(t, clientPort, testPort)
  101. }
  102. func TestHysteria2Inbound(t *testing.T) {
  103. caPem, certPem, keyPem := createSelfSignedCertificate(t, "example.org")
  104. startInstance(t, option.Options{
  105. Inbounds: []option.Inbound{
  106. {
  107. Type: C.TypeHysteria2,
  108. Hysteria2Options: option.Hysteria2InboundOptions{
  109. ListenOptions: option.ListenOptions{
  110. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  111. ListenPort: serverPort,
  112. },
  113. Obfs: &option.Hysteria2Obfs{
  114. Type: hysteria2.ObfsTypeSalamander,
  115. Password: "cry_me_a_r1ver",
  116. },
  117. Users: []option.Hysteria2User{{
  118. Password: "password",
  119. }},
  120. InboundTLSOptionsContainer: option.InboundTLSOptionsContainer{
  121. TLS: &option.InboundTLSOptions{
  122. Enabled: true,
  123. ServerName: "example.org",
  124. CertificatePath: certPem,
  125. KeyPath: keyPem,
  126. },
  127. },
  128. },
  129. },
  130. },
  131. })
  132. startDockerContainer(t, DockerOptions{
  133. Image: ImageHysteria2,
  134. Ports: []uint16{serverPort, clientPort},
  135. Cmd: []string{"client", "-c", "/etc/hysteria/config.yml", "--disable-update-check", "--log-level", "debug"},
  136. Bind: map[string]string{
  137. "hysteria2-client.yml": "/etc/hysteria/config.yml",
  138. caPem: "/etc/hysteria/ca.pem",
  139. },
  140. })
  141. testSuit(t, clientPort, testPort)
  142. }
  143. func TestHysteria2Outbound(t *testing.T) {
  144. _, certPem, keyPem := createSelfSignedCertificate(t, "example.org")
  145. startDockerContainer(t, DockerOptions{
  146. Image: ImageHysteria2,
  147. Ports: []uint16{testPort},
  148. Cmd: []string{"server", "-c", "/etc/hysteria/config.yml", "--disable-update-check", "--log-level", "debug"},
  149. Bind: map[string]string{
  150. "hysteria2-server.yml": "/etc/hysteria/config.yml",
  151. certPem: "/etc/hysteria/cert.pem",
  152. keyPem: "/etc/hysteria/key.pem",
  153. },
  154. })
  155. startInstance(t, option.Options{
  156. Inbounds: []option.Inbound{
  157. {
  158. Type: C.TypeMixed,
  159. MixedOptions: option.HTTPMixedInboundOptions{
  160. ListenOptions: option.ListenOptions{
  161. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  162. ListenPort: clientPort,
  163. },
  164. },
  165. },
  166. },
  167. Outbounds: []option.Outbound{
  168. {
  169. Type: C.TypeHysteria2,
  170. Hysteria2Options: option.Hysteria2OutboundOptions{
  171. ServerOptions: option.ServerOptions{
  172. Server: "127.0.0.1",
  173. ServerPort: serverPort,
  174. },
  175. Obfs: &option.Hysteria2Obfs{
  176. Type: hysteria2.ObfsTypeSalamander,
  177. Password: "cry_me_a_r1ver",
  178. },
  179. Password: "password",
  180. OutboundTLSOptionsContainer: option.OutboundTLSOptionsContainer{
  181. TLS: &option.OutboundTLSOptions{
  182. Enabled: true,
  183. ServerName: "example.org",
  184. CertificatePath: certPem,
  185. },
  186. },
  187. },
  188. },
  189. },
  190. })
  191. testSuitSimple1(t, clientPort, testPort)
  192. }