shadowsocks_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. package main
  2. import (
  3. "crypto/rand"
  4. "encoding/base64"
  5. "net/netip"
  6. "testing"
  7. C "github.com/sagernet/sing-box/constant"
  8. "github.com/sagernet/sing-box/option"
  9. F "github.com/sagernet/sing/common/format"
  10. "github.com/stretchr/testify/require"
  11. )
  12. func TestShadowsocks(t *testing.T) {
  13. for _, method := range []string{
  14. "aes-128-gcm",
  15. "aes-256-gcm",
  16. "chacha20-ietf-poly1305",
  17. } {
  18. t.Run(method+"-inbound", func(t *testing.T) {
  19. testShadowsocksInboundWithShadowsocksRust(t, method, mkBase64(t, 16))
  20. })
  21. t.Run(method+"-outbound", func(t *testing.T) {
  22. testShadowsocksOutboundWithShadowsocksRust(t, method, mkBase64(t, 16))
  23. })
  24. t.Run(method+"-self", func(t *testing.T) {
  25. testShadowsocksSelf(t, method, mkBase64(t, 16))
  26. })
  27. }
  28. }
  29. func TestShadowsocks2022(t *testing.T) {
  30. for _, method16 := range []string{
  31. "2022-blake3-aes-128-gcm",
  32. } {
  33. t.Run(method16+"-inbound", func(t *testing.T) {
  34. testShadowsocksInboundWithShadowsocksRust(t, method16, mkBase64(t, 16))
  35. })
  36. t.Run(method16+"-outbound", func(t *testing.T) {
  37. testShadowsocksOutboundWithShadowsocksRust(t, method16, mkBase64(t, 16))
  38. })
  39. t.Run(method16+"-self", func(t *testing.T) {
  40. testShadowsocksSelf(t, method16, mkBase64(t, 16))
  41. })
  42. }
  43. for _, method32 := range []string{
  44. "2022-blake3-aes-256-gcm",
  45. "2022-blake3-chacha20-poly1305",
  46. } {
  47. t.Run(method32+"-inbound", func(t *testing.T) {
  48. testShadowsocksInboundWithShadowsocksRust(t, method32, mkBase64(t, 32))
  49. })
  50. t.Run(method32+"-outbound", func(t *testing.T) {
  51. testShadowsocksOutboundWithShadowsocksRust(t, method32, mkBase64(t, 32))
  52. })
  53. t.Run(method32+"-self", func(t *testing.T) {
  54. testShadowsocksSelf(t, method32, mkBase64(t, 32))
  55. })
  56. }
  57. }
  58. func testShadowsocksInboundWithShadowsocksRust(t *testing.T, method string, password string) {
  59. t.Parallel()
  60. serverPort := mkPort(t)
  61. clientPort := mkPort(t)
  62. testPort := mkPort(t)
  63. startDockerContainer(t, DockerOptions{
  64. Image: ImageShadowsocksRustClient,
  65. EntryPoint: "sslocal",
  66. Ports: []uint16{serverPort, clientPort},
  67. Cmd: []string{"-s", F.ToString("127.0.0.1:", serverPort), "-b", F.ToString("0.0.0.0:", clientPort), "-m", method, "-k", password, "-U"},
  68. })
  69. startInstance(t, option.Options{
  70. Log: &option.LogOption{
  71. Disabled: true,
  72. },
  73. Inbounds: []option.Inbound{
  74. {
  75. Type: C.TypeShadowsocks,
  76. ShadowsocksOptions: option.ShadowsocksInboundOptions{
  77. ListenOptions: option.ListenOptions{
  78. Listen: option.ListenAddress(netip.IPv4Unspecified()),
  79. ListenPort: serverPort,
  80. },
  81. Method: method,
  82. Password: password,
  83. },
  84. },
  85. },
  86. })
  87. testSuit(t, clientPort, testPort)
  88. }
  89. func testShadowsocksOutboundWithShadowsocksRust(t *testing.T, method string, password string) {
  90. t.Parallel()
  91. serverPort := mkPort(t)
  92. clientPort := mkPort(t)
  93. testPort := mkPort(t)
  94. startDockerContainer(t, DockerOptions{
  95. Image: ImageShadowsocksRustServer,
  96. EntryPoint: "ssserver",
  97. Ports: []uint16{serverPort, testPort},
  98. Cmd: []string{"-s", F.ToString("0.0.0.0:", serverPort), "-m", method, "-k", password, "-U"},
  99. })
  100. startInstance(t, option.Options{
  101. Log: &option.LogOption{
  102. Disabled: true,
  103. },
  104. Inbounds: []option.Inbound{
  105. {
  106. Type: C.TypeMixed,
  107. MixedOptions: option.HTTPMixedInboundOptions{
  108. ListenOptions: option.ListenOptions{
  109. Listen: option.ListenAddress(netip.IPv4Unspecified()),
  110. ListenPort: clientPort,
  111. },
  112. },
  113. },
  114. },
  115. Outbounds: []option.Outbound{
  116. {
  117. Type: C.TypeShadowsocks,
  118. ShadowsocksOptions: option.ShadowsocksOutboundOptions{
  119. ServerOptions: option.ServerOptions{
  120. Server: "127.0.0.1",
  121. ServerPort: serverPort,
  122. },
  123. Method: method,
  124. Password: password,
  125. },
  126. },
  127. },
  128. })
  129. testSuit(t, clientPort, testPort)
  130. }
  131. func testShadowsocksSelf(t *testing.T, method string, password string) {
  132. t.Parallel()
  133. serverPort := mkPort(t)
  134. clientPort := mkPort(t)
  135. testPort := mkPort(t)
  136. startInstance(t, option.Options{
  137. Log: &option.LogOption{
  138. Disabled: true,
  139. },
  140. Inbounds: []option.Inbound{
  141. {
  142. Type: C.TypeMixed,
  143. Tag: "mixed-in",
  144. MixedOptions: option.HTTPMixedInboundOptions{
  145. ListenOptions: option.ListenOptions{
  146. Listen: option.ListenAddress(netip.IPv4Unspecified()),
  147. ListenPort: clientPort,
  148. },
  149. },
  150. },
  151. {
  152. Type: C.TypeShadowsocks,
  153. ShadowsocksOptions: option.ShadowsocksInboundOptions{
  154. ListenOptions: option.ListenOptions{
  155. Listen: option.ListenAddress(netip.IPv4Unspecified()),
  156. ListenPort: serverPort,
  157. },
  158. Method: method,
  159. Password: password,
  160. },
  161. },
  162. },
  163. Outbounds: []option.Outbound{
  164. {
  165. Type: C.TypeDirect,
  166. },
  167. {
  168. Type: C.TypeShadowsocks,
  169. Tag: "ss-out",
  170. ShadowsocksOptions: option.ShadowsocksOutboundOptions{
  171. ServerOptions: option.ServerOptions{
  172. Server: "127.0.0.1",
  173. ServerPort: serverPort,
  174. },
  175. Method: method,
  176. Password: password,
  177. },
  178. },
  179. },
  180. Route: &option.RouteOptions{
  181. Rules: []option.Rule{
  182. {
  183. DefaultOptions: option.DefaultRule{
  184. Inbound: []string{"mixed-in"},
  185. Outbound: "ss-out",
  186. },
  187. },
  188. },
  189. },
  190. })
  191. testSuit(t, clientPort, testPort)
  192. }
  193. func mkBase64(t *testing.T, length int) string {
  194. psk := make([]byte, length)
  195. _, err := rand.Read(psk)
  196. require.NoError(t, err)
  197. return base64.StdEncoding.EncodeToString(psk)
  198. }