hysteria2_test.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. LegacyOutbounds: []option.LegacyOutbound{
  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. RawDefaultRule: option.RawDefaultRule{
  94. Inbound: []string{"mixed-in"},
  95. },
  96. RuleAction: option.RuleAction{
  97. Action: C.RuleActionTypeRoute,
  98. RouteOptions: option.RouteActionOptions{
  99. Outbound: "hy2-out",
  100. },
  101. },
  102. },
  103. },
  104. },
  105. },
  106. })
  107. testSuitLargeUDP(t, clientPort, testPort)
  108. }
  109. func TestHysteria2Inbound(t *testing.T) {
  110. caPem, certPem, keyPem := createSelfSignedCertificate(t, "example.org")
  111. startInstance(t, option.Options{
  112. Inbounds: []option.Inbound{
  113. {
  114. Type: C.TypeHysteria2,
  115. Hysteria2Options: option.Hysteria2InboundOptions{
  116. ListenOptions: option.ListenOptions{
  117. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  118. ListenPort: serverPort,
  119. },
  120. Obfs: &option.Hysteria2Obfs{
  121. Type: hysteria2.ObfsTypeSalamander,
  122. Password: "cry_me_a_r1ver",
  123. },
  124. Users: []option.Hysteria2User{{
  125. Password: "password",
  126. }},
  127. InboundTLSOptionsContainer: option.InboundTLSOptionsContainer{
  128. TLS: &option.InboundTLSOptions{
  129. Enabled: true,
  130. ServerName: "example.org",
  131. CertificatePath: certPem,
  132. KeyPath: keyPem,
  133. },
  134. },
  135. },
  136. },
  137. },
  138. })
  139. startDockerContainer(t, DockerOptions{
  140. Image: ImageHysteria2,
  141. Ports: []uint16{serverPort, clientPort},
  142. Cmd: []string{"client", "-c", "/etc/hysteria/config.yml", "--disable-update-check", "--log-level", "debug"},
  143. Bind: map[string]string{
  144. "hysteria2-client.yml": "/etc/hysteria/config.yml",
  145. caPem: "/etc/hysteria/ca.pem",
  146. },
  147. })
  148. testSuit(t, clientPort, testPort)
  149. }
  150. func TestHysteria2Outbound(t *testing.T) {
  151. _, certPem, keyPem := createSelfSignedCertificate(t, "example.org")
  152. startDockerContainer(t, DockerOptions{
  153. Image: ImageHysteria2,
  154. Ports: []uint16{testPort},
  155. Cmd: []string{"server", "-c", "/etc/hysteria/config.yml", "--disable-update-check", "--log-level", "debug"},
  156. Bind: map[string]string{
  157. "hysteria2-server.yml": "/etc/hysteria/config.yml",
  158. certPem: "/etc/hysteria/cert.pem",
  159. keyPem: "/etc/hysteria/key.pem",
  160. },
  161. })
  162. startInstance(t, option.Options{
  163. Inbounds: []option.Inbound{
  164. {
  165. Type: C.TypeMixed,
  166. MixedOptions: option.HTTPMixedInboundOptions{
  167. ListenOptions: option.ListenOptions{
  168. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  169. ListenPort: clientPort,
  170. },
  171. },
  172. },
  173. },
  174. LegacyOutbounds: []option.LegacyOutbound{
  175. {
  176. Type: C.TypeHysteria2,
  177. Hysteria2Options: option.Hysteria2OutboundOptions{
  178. ServerOptions: option.ServerOptions{
  179. Server: "127.0.0.1",
  180. ServerPort: serverPort,
  181. },
  182. Obfs: &option.Hysteria2Obfs{
  183. Type: hysteria2.ObfsTypeSalamander,
  184. Password: "cry_me_a_r1ver",
  185. },
  186. Password: "password",
  187. OutboundTLSOptionsContainer: option.OutboundTLSOptionsContainer{
  188. TLS: &option.OutboundTLSOptions{
  189. Enabled: true,
  190. ServerName: "example.org",
  191. CertificatePath: certPem,
  192. },
  193. },
  194. },
  195. },
  196. },
  197. })
  198. testSuitSimple1(t, clientPort, testPort)
  199. }