shadowsocks_test.go 6.4 KB

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