hysteria_test.go 4.9 KB

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