shadowsocks_test.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. "github.com/sagernet/sing-shadowsocks/shadowaead_2022"
  10. F "github.com/sagernet/sing/common/format"
  11. "github.com/stretchr/testify/require"
  12. )
  13. const (
  14. serverPort uint16 = 10000 + iota
  15. clientPort
  16. testPort
  17. otherPort
  18. otherClientPort
  19. )
  20. func TestShadowsocks(t *testing.T) {
  21. for _, method := range []string{
  22. "aes-128-gcm",
  23. "aes-256-gcm",
  24. "chacha20-ietf-poly1305",
  25. } {
  26. t.Run(method+"-inbound", func(t *testing.T) {
  27. testShadowsocksInboundWithShadowsocksRust(t, method, mkBase64(t, 16))
  28. })
  29. t.Run(method+"-outbound", func(t *testing.T) {
  30. testShadowsocksOutboundWithShadowsocksRust(t, method, mkBase64(t, 16))
  31. })
  32. t.Run(method+"-self", func(t *testing.T) {
  33. testShadowsocksSelf(t, method, mkBase64(t, 16))
  34. })
  35. }
  36. }
  37. func TestShadowsocksNone(t *testing.T) {
  38. testShadowsocksSelf(t, "none", "")
  39. }
  40. func TestShadowsocks2022(t *testing.T) {
  41. for _, method16 := range []string{
  42. "2022-blake3-aes-128-gcm",
  43. } {
  44. t.Run(method16+"-inbound", func(t *testing.T) {
  45. testShadowsocksInboundWithShadowsocksRust(t, method16, mkBase64(t, 16))
  46. })
  47. t.Run(method16+"-outbound", func(t *testing.T) {
  48. testShadowsocksOutboundWithShadowsocksRust(t, method16, mkBase64(t, 16))
  49. })
  50. t.Run(method16+"-self", func(t *testing.T) {
  51. testShadowsocksSelf(t, method16, mkBase64(t, 16))
  52. })
  53. }
  54. for _, method32 := range []string{
  55. "2022-blake3-aes-256-gcm",
  56. "2022-blake3-chacha20-poly1305",
  57. } {
  58. t.Run(method32+"-inbound", func(t *testing.T) {
  59. testShadowsocksInboundWithShadowsocksRust(t, method32, mkBase64(t, 32))
  60. })
  61. t.Run(method32+"-outbound", func(t *testing.T) {
  62. testShadowsocksOutboundWithShadowsocksRust(t, method32, mkBase64(t, 32))
  63. })
  64. t.Run(method32+"-self", func(t *testing.T) {
  65. testShadowsocksSelf(t, method32, mkBase64(t, 32))
  66. })
  67. }
  68. }
  69. func testShadowsocksInboundWithShadowsocksRust(t *testing.T, method string, password string) {
  70. startDockerContainer(t, DockerOptions{
  71. Image: ImageShadowsocksRustClient,
  72. EntryPoint: "sslocal",
  73. Ports: []uint16{serverPort, clientPort},
  74. Cmd: []string{"-s", F.ToString("127.0.0.1:", serverPort), "-b", F.ToString("0.0.0.0:", clientPort), "-m", method, "-k", password, "-U"},
  75. })
  76. startInstance(t, option.Options{
  77. Inbounds: []option.Inbound{
  78. {
  79. Type: C.TypeShadowsocks,
  80. ShadowsocksOptions: option.ShadowsocksInboundOptions{
  81. ListenOptions: option.ListenOptions{
  82. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  83. ListenPort: serverPort,
  84. },
  85. Method: method,
  86. Password: password,
  87. },
  88. },
  89. },
  90. })
  91. testSuit(t, clientPort, testPort)
  92. }
  93. func testShadowsocksOutboundWithShadowsocksRust(t *testing.T, method string, password string) {
  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. Inbounds: []option.Inbound{
  102. {
  103. Type: C.TypeMixed,
  104. MixedOptions: option.HTTPMixedInboundOptions{
  105. ListenOptions: option.ListenOptions{
  106. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  107. ListenPort: clientPort,
  108. },
  109. },
  110. },
  111. },
  112. Outbounds: []option.Outbound{
  113. {
  114. Type: C.TypeShadowsocks,
  115. ShadowsocksOptions: option.ShadowsocksOutboundOptions{
  116. ServerOptions: option.ServerOptions{
  117. Server: "127.0.0.1",
  118. ServerPort: serverPort,
  119. },
  120. Method: method,
  121. Password: password,
  122. },
  123. },
  124. },
  125. })
  126. testSuit(t, clientPort, testPort)
  127. }
  128. func testShadowsocksSelf(t *testing.T, method string, password string) {
  129. startInstance(t, option.Options{
  130. Inbounds: []option.Inbound{
  131. {
  132. Type: C.TypeMixed,
  133. Tag: "mixed-in",
  134. MixedOptions: option.HTTPMixedInboundOptions{
  135. ListenOptions: option.ListenOptions{
  136. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  137. ListenPort: clientPort,
  138. },
  139. },
  140. },
  141. {
  142. Type: C.TypeShadowsocks,
  143. ShadowsocksOptions: option.ShadowsocksInboundOptions{
  144. ListenOptions: option.ListenOptions{
  145. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  146. ListenPort: serverPort,
  147. },
  148. Method: method,
  149. Password: password,
  150. },
  151. },
  152. },
  153. Outbounds: []option.Outbound{
  154. {
  155. Type: C.TypeDirect,
  156. },
  157. {
  158. Type: C.TypeShadowsocks,
  159. Tag: "ss-out",
  160. ShadowsocksOptions: option.ShadowsocksOutboundOptions{
  161. ServerOptions: option.ServerOptions{
  162. Server: "127.0.0.1",
  163. ServerPort: serverPort,
  164. },
  165. Method: method,
  166. Password: password,
  167. },
  168. },
  169. },
  170. Route: &option.RouteOptions{
  171. Rules: []option.Rule{
  172. {
  173. DefaultOptions: option.DefaultRule{
  174. Inbound: []string{"mixed-in"},
  175. Outbound: "ss-out",
  176. },
  177. },
  178. },
  179. },
  180. })
  181. testSuit(t, clientPort, testPort)
  182. }
  183. func TestShadowsocksUoT(t *testing.T) {
  184. method := shadowaead_2022.List[0]
  185. password := mkBase64(t, 16)
  186. startInstance(t, option.Options{
  187. Inbounds: []option.Inbound{
  188. {
  189. Type: C.TypeMixed,
  190. Tag: "mixed-in",
  191. MixedOptions: option.HTTPMixedInboundOptions{
  192. ListenOptions: option.ListenOptions{
  193. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  194. ListenPort: clientPort,
  195. },
  196. },
  197. },
  198. {
  199. Type: C.TypeShadowsocks,
  200. ShadowsocksOptions: option.ShadowsocksInboundOptions{
  201. ListenOptions: option.ListenOptions{
  202. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  203. ListenPort: serverPort,
  204. },
  205. Method: method,
  206. Password: password,
  207. },
  208. },
  209. },
  210. Outbounds: []option.Outbound{
  211. {
  212. Type: C.TypeDirect,
  213. },
  214. {
  215. Type: C.TypeShadowsocks,
  216. Tag: "ss-out",
  217. ShadowsocksOptions: option.ShadowsocksOutboundOptions{
  218. ServerOptions: option.ServerOptions{
  219. Server: "127.0.0.1",
  220. ServerPort: serverPort,
  221. },
  222. Method: method,
  223. Password: password,
  224. UDPOverTCPOptions: &option.UDPOverTCPOptions{
  225. Enabled: true,
  226. },
  227. },
  228. },
  229. },
  230. Route: &option.RouteOptions{
  231. Rules: []option.Rule{
  232. {
  233. DefaultOptions: option.DefaultRule{
  234. Inbound: []string{"mixed-in"},
  235. Outbound: "ss-out",
  236. },
  237. },
  238. },
  239. },
  240. })
  241. testSuit(t, clientPort, testPort)
  242. }
  243. func mkBase64(t *testing.T, length int) string {
  244. psk := make([]byte, length)
  245. _, err := rand.Read(psk)
  246. require.NoError(t, err)
  247. return base64.StdEncoding.EncodeToString(psk)
  248. }