shadowsocks_test.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 TestShadowsocks2022EIH(t *testing.T) {
  70. for _, method16 := range []string{
  71. "2022-blake3-aes-128-gcm",
  72. } {
  73. t.Run(method16, func(t *testing.T) {
  74. testShadowsocks2022EIH(t, method16, mkBase64(t, 16))
  75. })
  76. }
  77. for _, method32 := range []string{
  78. "2022-blake3-aes-256-gcm",
  79. } {
  80. t.Run(method32, func(t *testing.T) {
  81. testShadowsocks2022EIH(t, method32, mkBase64(t, 32))
  82. })
  83. }
  84. }
  85. func testShadowsocksInboundWithShadowsocksRust(t *testing.T, method string, password string) {
  86. startDockerContainer(t, DockerOptions{
  87. Image: ImageShadowsocksRustClient,
  88. EntryPoint: "sslocal",
  89. Ports: []uint16{serverPort, clientPort},
  90. Cmd: []string{"-s", F.ToString("127.0.0.1:", serverPort), "-b", F.ToString("0.0.0.0:", clientPort), "-m", method, "-k", password, "-U"},
  91. })
  92. startInstance(t, option.Options{
  93. Inbounds: []option.Inbound{
  94. {
  95. Type: C.TypeShadowsocks,
  96. ShadowsocksOptions: option.ShadowsocksInboundOptions{
  97. ListenOptions: option.ListenOptions{
  98. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  99. ListenPort: serverPort,
  100. },
  101. Method: method,
  102. Password: password,
  103. },
  104. },
  105. },
  106. })
  107. testSuit(t, clientPort, testPort)
  108. }
  109. func testShadowsocksOutboundWithShadowsocksRust(t *testing.T, method string, password string) {
  110. startDockerContainer(t, DockerOptions{
  111. Image: ImageShadowsocksRustServer,
  112. EntryPoint: "ssserver",
  113. Ports: []uint16{serverPort, testPort},
  114. Cmd: []string{"-s", F.ToString("0.0.0.0:", serverPort), "-m", method, "-k", password, "-U"},
  115. })
  116. startInstance(t, option.Options{
  117. Inbounds: []option.Inbound{
  118. {
  119. Type: C.TypeMixed,
  120. MixedOptions: option.HTTPMixedInboundOptions{
  121. ListenOptions: option.ListenOptions{
  122. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  123. ListenPort: clientPort,
  124. },
  125. },
  126. },
  127. },
  128. Outbounds: []option.Outbound{
  129. {
  130. Type: C.TypeShadowsocks,
  131. ShadowsocksOptions: option.ShadowsocksOutboundOptions{
  132. ServerOptions: option.ServerOptions{
  133. Server: "127.0.0.1",
  134. ServerPort: serverPort,
  135. },
  136. Method: method,
  137. Password: password,
  138. },
  139. },
  140. },
  141. })
  142. testSuit(t, clientPort, testPort)
  143. }
  144. func testShadowsocksSelf(t *testing.T, method string, password string) {
  145. startInstance(t, option.Options{
  146. Inbounds: []option.Inbound{
  147. {
  148. Type: C.TypeMixed,
  149. Tag: "mixed-in",
  150. MixedOptions: option.HTTPMixedInboundOptions{
  151. ListenOptions: option.ListenOptions{
  152. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  153. ListenPort: clientPort,
  154. },
  155. },
  156. },
  157. {
  158. Type: C.TypeShadowsocks,
  159. ShadowsocksOptions: option.ShadowsocksInboundOptions{
  160. ListenOptions: option.ListenOptions{
  161. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  162. ListenPort: serverPort,
  163. },
  164. Method: method,
  165. Password: password,
  166. },
  167. },
  168. },
  169. Outbounds: []option.Outbound{
  170. {
  171. Type: C.TypeDirect,
  172. },
  173. {
  174. Type: C.TypeShadowsocks,
  175. Tag: "ss-out",
  176. ShadowsocksOptions: option.ShadowsocksOutboundOptions{
  177. ServerOptions: option.ServerOptions{
  178. Server: "127.0.0.1",
  179. ServerPort: serverPort,
  180. },
  181. Method: method,
  182. Password: password,
  183. },
  184. },
  185. },
  186. Route: &option.RouteOptions{
  187. Rules: []option.Rule{
  188. {
  189. DefaultOptions: option.DefaultRule{
  190. Inbound: []string{"mixed-in"},
  191. Outbound: "ss-out",
  192. },
  193. },
  194. },
  195. },
  196. })
  197. testSuit(t, clientPort, testPort)
  198. }
  199. func TestShadowsocksUoT(t *testing.T) {
  200. method := shadowaead_2022.List[0]
  201. password := mkBase64(t, 16)
  202. startInstance(t, option.Options{
  203. Inbounds: []option.Inbound{
  204. {
  205. Type: C.TypeMixed,
  206. Tag: "mixed-in",
  207. MixedOptions: option.HTTPMixedInboundOptions{
  208. ListenOptions: option.ListenOptions{
  209. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  210. ListenPort: clientPort,
  211. },
  212. },
  213. },
  214. {
  215. Type: C.TypeShadowsocks,
  216. ShadowsocksOptions: option.ShadowsocksInboundOptions{
  217. ListenOptions: option.ListenOptions{
  218. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  219. ListenPort: serverPort,
  220. },
  221. Method: method,
  222. Password: password,
  223. },
  224. },
  225. },
  226. Outbounds: []option.Outbound{
  227. {
  228. Type: C.TypeDirect,
  229. },
  230. {
  231. Type: C.TypeShadowsocks,
  232. Tag: "ss-out",
  233. ShadowsocksOptions: option.ShadowsocksOutboundOptions{
  234. ServerOptions: option.ServerOptions{
  235. Server: "127.0.0.1",
  236. ServerPort: serverPort,
  237. },
  238. Method: method,
  239. Password: password,
  240. UDPOverTCP: &option.UDPOverTCPOptions{
  241. Enabled: true,
  242. },
  243. },
  244. },
  245. },
  246. Route: &option.RouteOptions{
  247. Rules: []option.Rule{
  248. {
  249. DefaultOptions: option.DefaultRule{
  250. Inbound: []string{"mixed-in"},
  251. Outbound: "ss-out",
  252. },
  253. },
  254. },
  255. },
  256. })
  257. testSuit(t, clientPort, testPort)
  258. }
  259. func testShadowsocks2022EIH(t *testing.T, method string, password string) {
  260. startInstance(t, option.Options{
  261. Inbounds: []option.Inbound{
  262. {
  263. Type: C.TypeMixed,
  264. Tag: "mixed-in",
  265. MixedOptions: option.HTTPMixedInboundOptions{
  266. ListenOptions: option.ListenOptions{
  267. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  268. ListenPort: clientPort,
  269. },
  270. },
  271. },
  272. {
  273. Type: C.TypeShadowsocks,
  274. ShadowsocksOptions: option.ShadowsocksInboundOptions{
  275. ListenOptions: option.ListenOptions{
  276. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  277. ListenPort: serverPort,
  278. },
  279. Method: method,
  280. Password: password,
  281. Users: []option.ShadowsocksUser{
  282. {
  283. Password: password,
  284. },
  285. },
  286. },
  287. },
  288. },
  289. Outbounds: []option.Outbound{
  290. {
  291. Type: C.TypeDirect,
  292. },
  293. {
  294. Type: C.TypeShadowsocks,
  295. Tag: "ss-out",
  296. ShadowsocksOptions: option.ShadowsocksOutboundOptions{
  297. ServerOptions: option.ServerOptions{
  298. Server: "127.0.0.1",
  299. ServerPort: serverPort,
  300. },
  301. Method: method,
  302. Password: password + ":" + password,
  303. },
  304. },
  305. },
  306. Route: &option.RouteOptions{
  307. Rules: []option.Rule{
  308. {
  309. DefaultOptions: option.DefaultRule{
  310. Inbound: []string{"mixed-in"},
  311. Outbound: "ss-out",
  312. },
  313. },
  314. },
  315. },
  316. })
  317. testSuit(t, clientPort, testPort)
  318. }
  319. func mkBase64(t *testing.T, length int) string {
  320. psk := make([]byte, length)
  321. _, err := rand.Read(psk)
  322. require.NoError(t, err)
  323. return base64.StdEncoding.EncodeToString(psk)
  324. }