shadowsocks_test.go 6.5 KB

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