hysteria2_test.go 5.5 KB

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