hysteria2_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. TLS: &option.InboundTLSOptions{
  52. Enabled: true,
  53. ServerName: "example.org",
  54. CertificatePath: certPem,
  55. KeyPath: keyPem,
  56. },
  57. },
  58. },
  59. },
  60. Outbounds: []option.Outbound{
  61. {
  62. Type: C.TypeDirect,
  63. },
  64. {
  65. Type: C.TypeHysteria2,
  66. Tag: "hy2-out",
  67. Hysteria2Options: option.Hysteria2OutboundOptions{
  68. ServerOptions: option.ServerOptions{
  69. Server: "127.0.0.1",
  70. ServerPort: serverPort,
  71. },
  72. UpMbps: 100,
  73. DownMbps: 100,
  74. Obfs: obfs,
  75. Password: "password",
  76. TLS: &option.OutboundTLSOptions{
  77. Enabled: true,
  78. ServerName: "example.org",
  79. CertificatePath: certPem,
  80. },
  81. },
  82. },
  83. },
  84. Route: &option.RouteOptions{
  85. Rules: []option.Rule{
  86. {
  87. Type: C.RuleTypeDefault,
  88. DefaultOptions: option.DefaultRule{
  89. Inbound: []string{"mixed-in"},
  90. Outbound: "hy2-out",
  91. },
  92. },
  93. },
  94. },
  95. })
  96. testSuitLargeUDP(t, clientPort, testPort)
  97. }
  98. func TestHysteria2Inbound(t *testing.T) {
  99. caPem, certPem, keyPem := createSelfSignedCertificate(t, "example.org")
  100. startInstance(t, option.Options{
  101. Inbounds: []option.Inbound{
  102. {
  103. Type: C.TypeHysteria2,
  104. Hysteria2Options: option.Hysteria2InboundOptions{
  105. ListenOptions: option.ListenOptions{
  106. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  107. ListenPort: serverPort,
  108. },
  109. Obfs: &option.Hysteria2Obfs{
  110. Type: hysteria2.ObfsTypeSalamander,
  111. Password: "cry_me_a_r1ver",
  112. },
  113. Users: []option.Hysteria2User{{
  114. Password: "password",
  115. }},
  116. TLS: &option.InboundTLSOptions{
  117. Enabled: true,
  118. ServerName: "example.org",
  119. CertificatePath: certPem,
  120. KeyPath: keyPem,
  121. },
  122. },
  123. },
  124. },
  125. })
  126. startDockerContainer(t, DockerOptions{
  127. Image: ImageHysteria2,
  128. Ports: []uint16{serverPort, clientPort},
  129. Cmd: []string{"client", "-c", "/etc/hysteria/config.yml", "--disable-update-check", "--log-level", "debug"},
  130. Bind: map[string]string{
  131. "hysteria2-client.yml": "/etc/hysteria/config.yml",
  132. caPem: "/etc/hysteria/ca.pem",
  133. },
  134. })
  135. testSuit(t, clientPort, testPort)
  136. }
  137. func TestHysteria2Outbound(t *testing.T) {
  138. _, certPem, keyPem := createSelfSignedCertificate(t, "example.org")
  139. startDockerContainer(t, DockerOptions{
  140. Image: ImageHysteria2,
  141. Ports: []uint16{testPort},
  142. Cmd: []string{"server", "-c", "/etc/hysteria/config.yml", "--disable-update-check", "--log-level", "debug"},
  143. Bind: map[string]string{
  144. "hysteria2-server.yml": "/etc/hysteria/config.yml",
  145. certPem: "/etc/hysteria/cert.pem",
  146. keyPem: "/etc/hysteria/key.pem",
  147. },
  148. })
  149. startInstance(t, option.Options{
  150. Inbounds: []option.Inbound{
  151. {
  152. Type: C.TypeMixed,
  153. MixedOptions: option.HTTPMixedInboundOptions{
  154. ListenOptions: option.ListenOptions{
  155. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  156. ListenPort: clientPort,
  157. },
  158. },
  159. },
  160. },
  161. Outbounds: []option.Outbound{
  162. {
  163. Type: C.TypeHysteria2,
  164. Hysteria2Options: option.Hysteria2OutboundOptions{
  165. ServerOptions: option.ServerOptions{
  166. Server: "127.0.0.1",
  167. ServerPort: serverPort,
  168. },
  169. Obfs: &option.Hysteria2Obfs{
  170. Type: hysteria2.ObfsTypeSalamander,
  171. Password: "cry_me_a_r1ver",
  172. },
  173. Password: "password",
  174. TLS: &option.OutboundTLSOptions{
  175. Enabled: true,
  176. ServerName: "example.org",
  177. CertificatePath: certPem,
  178. },
  179. },
  180. },
  181. },
  182. })
  183. testSuitSimple1(t, clientPort, testPort)
  184. }