hysteria_test.go 4.9 KB

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