shadowsocks_test.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. LegacyOutbounds: []option.LegacyOutbound{
  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. LegacyOutbounds: []option.LegacyOutbound{
  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. Type: C.RuleTypeDefault,
  190. DefaultOptions: option.DefaultRule{
  191. RawDefaultRule: option.RawDefaultRule{
  192. Inbound: []string{"mixed-in"},
  193. },
  194. RuleAction: option.RuleAction{
  195. Action: C.RuleActionTypeRoute,
  196. RouteOptions: option.RouteActionOptions{
  197. Outbound: "ss-out",
  198. },
  199. },
  200. },
  201. },
  202. },
  203. },
  204. })
  205. testSuit(t, clientPort, testPort)
  206. }
  207. func TestShadowsocksUoT(t *testing.T) {
  208. method := shadowaead_2022.List[0]
  209. password := mkBase64(t, 16)
  210. startInstance(t, option.Options{
  211. Inbounds: []option.Inbound{
  212. {
  213. Type: C.TypeMixed,
  214. Tag: "mixed-in",
  215. MixedOptions: option.HTTPMixedInboundOptions{
  216. ListenOptions: option.ListenOptions{
  217. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  218. ListenPort: clientPort,
  219. },
  220. },
  221. },
  222. {
  223. Type: C.TypeShadowsocks,
  224. ShadowsocksOptions: option.ShadowsocksInboundOptions{
  225. ListenOptions: option.ListenOptions{
  226. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  227. ListenPort: serverPort,
  228. },
  229. Method: method,
  230. Password: password,
  231. },
  232. },
  233. },
  234. LegacyOutbounds: []option.LegacyOutbound{
  235. {
  236. Type: C.TypeDirect,
  237. },
  238. {
  239. Type: C.TypeShadowsocks,
  240. Tag: "ss-out",
  241. ShadowsocksOptions: option.ShadowsocksOutboundOptions{
  242. ServerOptions: option.ServerOptions{
  243. Server: "127.0.0.1",
  244. ServerPort: serverPort,
  245. },
  246. Method: method,
  247. Password: password,
  248. UDPOverTCP: &option.UDPOverTCPOptions{
  249. Enabled: true,
  250. },
  251. },
  252. },
  253. },
  254. Route: &option.RouteOptions{
  255. Rules: []option.Rule{
  256. {
  257. Type: C.RuleTypeDefault,
  258. DefaultOptions: option.DefaultRule{
  259. RawDefaultRule: option.RawDefaultRule{
  260. Inbound: []string{"mixed-in"},
  261. },
  262. RuleAction: option.RuleAction{
  263. Action: C.RuleActionTypeRoute,
  264. RouteOptions: option.RouteActionOptions{
  265. Outbound: "ss-out",
  266. },
  267. },
  268. },
  269. },
  270. },
  271. },
  272. })
  273. testSuit(t, clientPort, testPort)
  274. }
  275. func testShadowsocks2022EIH(t *testing.T, method string, password string) {
  276. startInstance(t, option.Options{
  277. Inbounds: []option.Inbound{
  278. {
  279. Type: C.TypeMixed,
  280. Tag: "mixed-in",
  281. MixedOptions: option.HTTPMixedInboundOptions{
  282. ListenOptions: option.ListenOptions{
  283. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  284. ListenPort: clientPort,
  285. },
  286. },
  287. },
  288. {
  289. Type: C.TypeShadowsocks,
  290. ShadowsocksOptions: option.ShadowsocksInboundOptions{
  291. ListenOptions: option.ListenOptions{
  292. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  293. ListenPort: serverPort,
  294. },
  295. Method: method,
  296. Password: password,
  297. Users: []option.ShadowsocksUser{
  298. {
  299. Password: password,
  300. },
  301. },
  302. },
  303. },
  304. },
  305. LegacyOutbounds: []option.LegacyOutbound{
  306. {
  307. Type: C.TypeDirect,
  308. },
  309. {
  310. Type: C.TypeShadowsocks,
  311. Tag: "ss-out",
  312. ShadowsocksOptions: option.ShadowsocksOutboundOptions{
  313. ServerOptions: option.ServerOptions{
  314. Server: "127.0.0.1",
  315. ServerPort: serverPort,
  316. },
  317. Method: method,
  318. Password: password + ":" + password,
  319. },
  320. },
  321. },
  322. Route: &option.RouteOptions{
  323. Rules: []option.Rule{
  324. {
  325. Type: C.RuleTypeDefault,
  326. DefaultOptions: option.DefaultRule{
  327. RawDefaultRule: option.RawDefaultRule{
  328. Inbound: []string{"mixed-in"},
  329. },
  330. RuleAction: option.RuleAction{
  331. Action: C.RuleActionTypeRoute,
  332. RouteOptions: option.RouteActionOptions{
  333. Outbound: "ss-out",
  334. },
  335. },
  336. },
  337. },
  338. },
  339. },
  340. })
  341. testSuit(t, clientPort, testPort)
  342. }
  343. func mkBase64(t *testing.T, length int) string {
  344. psk := make([]byte, length)
  345. _, err := rand.Read(psk)
  346. require.NoError(t, err)
  347. return base64.StdEncoding.EncodeToString(psk)
  348. }