shadowsocks_test.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. "github.com/sagernet/sing/common"
  11. F "github.com/sagernet/sing/common/format"
  12. "github.com/sagernet/sing/common/json/badoption"
  13. "github.com/stretchr/testify/require"
  14. )
  15. const (
  16. serverPort uint16 = 10000 + iota
  17. clientPort
  18. testPort
  19. otherPort
  20. otherClientPort
  21. )
  22. func TestShadowsocks(t *testing.T) {
  23. for _, method := range []string{
  24. "aes-128-gcm",
  25. "aes-256-gcm",
  26. "chacha20-ietf-poly1305",
  27. } {
  28. t.Run(method+"-inbound", func(t *testing.T) {
  29. testShadowsocksInboundWithShadowsocksRust(t, method, mkBase64(t, 16))
  30. })
  31. t.Run(method+"-outbound", func(t *testing.T) {
  32. testShadowsocksOutboundWithShadowsocksRust(t, method, mkBase64(t, 16))
  33. })
  34. t.Run(method+"-self", func(t *testing.T) {
  35. testShadowsocksSelf(t, method, mkBase64(t, 16))
  36. })
  37. }
  38. }
  39. func TestShadowsocksNone(t *testing.T) {
  40. testShadowsocksSelf(t, "none", "")
  41. }
  42. func TestShadowsocks2022(t *testing.T) {
  43. for _, method16 := range []string{
  44. "2022-blake3-aes-128-gcm",
  45. } {
  46. t.Run(method16+"-inbound", func(t *testing.T) {
  47. testShadowsocksInboundWithShadowsocksRust(t, method16, mkBase64(t, 16))
  48. })
  49. t.Run(method16+"-outbound", func(t *testing.T) {
  50. testShadowsocksOutboundWithShadowsocksRust(t, method16, mkBase64(t, 16))
  51. })
  52. t.Run(method16+"-self", func(t *testing.T) {
  53. testShadowsocksSelf(t, method16, mkBase64(t, 16))
  54. })
  55. }
  56. for _, method32 := range []string{
  57. "2022-blake3-aes-256-gcm",
  58. "2022-blake3-chacha20-poly1305",
  59. } {
  60. t.Run(method32+"-inbound", func(t *testing.T) {
  61. testShadowsocksInboundWithShadowsocksRust(t, method32, mkBase64(t, 32))
  62. })
  63. t.Run(method32+"-outbound", func(t *testing.T) {
  64. testShadowsocksOutboundWithShadowsocksRust(t, method32, mkBase64(t, 32))
  65. })
  66. t.Run(method32+"-self", func(t *testing.T) {
  67. testShadowsocksSelf(t, method32, mkBase64(t, 32))
  68. })
  69. }
  70. }
  71. func TestShadowsocks2022EIH(t *testing.T) {
  72. for _, method16 := range []string{
  73. "2022-blake3-aes-128-gcm",
  74. } {
  75. t.Run(method16, func(t *testing.T) {
  76. testShadowsocks2022EIH(t, method16, mkBase64(t, 16))
  77. })
  78. }
  79. for _, method32 := range []string{
  80. "2022-blake3-aes-256-gcm",
  81. } {
  82. t.Run(method32, func(t *testing.T) {
  83. testShadowsocks2022EIH(t, method32, mkBase64(t, 32))
  84. })
  85. }
  86. }
  87. func testShadowsocksInboundWithShadowsocksRust(t *testing.T, method string, password string) {
  88. startDockerContainer(t, DockerOptions{
  89. Image: ImageShadowsocksRustClient,
  90. EntryPoint: "sslocal",
  91. Ports: []uint16{serverPort, clientPort},
  92. Cmd: []string{"-s", F.ToString("127.0.0.1:", serverPort), "-b", F.ToString("0.0.0.0:", clientPort), "-m", method, "-k", password, "-U"},
  93. })
  94. startInstance(t, option.Options{
  95. Inbounds: []option.Inbound{
  96. {
  97. Type: C.TypeShadowsocks,
  98. Options: &option.ShadowsocksInboundOptions{
  99. ListenOptions: option.ListenOptions{
  100. Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
  101. ListenPort: serverPort,
  102. },
  103. Method: method,
  104. Password: password,
  105. },
  106. },
  107. },
  108. })
  109. testSuit(t, clientPort, testPort)
  110. }
  111. func testShadowsocksOutboundWithShadowsocksRust(t *testing.T, method string, password string) {
  112. startDockerContainer(t, DockerOptions{
  113. Image: ImageShadowsocksRustServer,
  114. EntryPoint: "ssserver",
  115. Ports: []uint16{serverPort, testPort},
  116. Cmd: []string{"-s", F.ToString("0.0.0.0:", serverPort), "-m", method, "-k", password, "-U"},
  117. })
  118. startInstance(t, option.Options{
  119. Inbounds: []option.Inbound{
  120. {
  121. Type: C.TypeMixed,
  122. Options: &option.HTTPMixedInboundOptions{
  123. ListenOptions: option.ListenOptions{
  124. Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
  125. ListenPort: clientPort,
  126. },
  127. },
  128. },
  129. },
  130. Outbounds: []option.Outbound{
  131. {
  132. Type: C.TypeShadowsocks,
  133. Options: &option.ShadowsocksOutboundOptions{
  134. ServerOptions: option.ServerOptions{
  135. Server: "127.0.0.1",
  136. ServerPort: serverPort,
  137. },
  138. Method: method,
  139. Password: password,
  140. },
  141. },
  142. },
  143. })
  144. testSuit(t, clientPort, testPort)
  145. }
  146. func testShadowsocksSelf(t *testing.T, method string, password string) {
  147. startInstance(t, option.Options{
  148. Inbounds: []option.Inbound{
  149. {
  150. Type: C.TypeMixed,
  151. Tag: "mixed-in",
  152. Options: &option.HTTPMixedInboundOptions{
  153. ListenOptions: option.ListenOptions{
  154. Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
  155. ListenPort: clientPort,
  156. },
  157. },
  158. },
  159. {
  160. Type: C.TypeShadowsocks,
  161. Options: &option.ShadowsocksInboundOptions{
  162. ListenOptions: option.ListenOptions{
  163. Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
  164. ListenPort: serverPort,
  165. },
  166. Method: method,
  167. Password: password,
  168. },
  169. },
  170. },
  171. Outbounds: []option.Outbound{
  172. {
  173. Type: C.TypeDirect,
  174. },
  175. {
  176. Type: C.TypeShadowsocks,
  177. Tag: "ss-out",
  178. Options: &option.ShadowsocksOutboundOptions{
  179. ServerOptions: option.ServerOptions{
  180. Server: "127.0.0.1",
  181. ServerPort: serverPort,
  182. },
  183. Method: method,
  184. Password: password,
  185. },
  186. },
  187. },
  188. Route: &option.RouteOptions{
  189. Rules: []option.Rule{
  190. {
  191. Type: C.RuleTypeDefault,
  192. DefaultOptions: option.DefaultRule{
  193. RawDefaultRule: option.RawDefaultRule{
  194. Inbound: []string{"mixed-in"},
  195. },
  196. RuleAction: option.RuleAction{
  197. Action: C.RuleActionTypeRoute,
  198. RouteOptions: option.RouteActionOptions{
  199. Outbound: "ss-out",
  200. },
  201. },
  202. },
  203. },
  204. },
  205. },
  206. })
  207. testSuit(t, clientPort, testPort)
  208. }
  209. func TestShadowsocksUoT(t *testing.T) {
  210. method := shadowaead_2022.List[0]
  211. password := mkBase64(t, 16)
  212. startInstance(t, option.Options{
  213. Inbounds: []option.Inbound{
  214. {
  215. Type: C.TypeMixed,
  216. Tag: "mixed-in",
  217. Options: &option.HTTPMixedInboundOptions{
  218. ListenOptions: option.ListenOptions{
  219. Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
  220. ListenPort: clientPort,
  221. },
  222. },
  223. },
  224. {
  225. Type: C.TypeShadowsocks,
  226. Options: &option.ShadowsocksInboundOptions{
  227. ListenOptions: option.ListenOptions{
  228. Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
  229. ListenPort: serverPort,
  230. },
  231. Method: method,
  232. Password: password,
  233. },
  234. },
  235. },
  236. Outbounds: []option.Outbound{
  237. {
  238. Type: C.TypeDirect,
  239. },
  240. {
  241. Type: C.TypeShadowsocks,
  242. Tag: "ss-out",
  243. Options: &option.ShadowsocksOutboundOptions{
  244. ServerOptions: option.ServerOptions{
  245. Server: "127.0.0.1",
  246. ServerPort: serverPort,
  247. },
  248. Method: method,
  249. Password: password,
  250. UDPOverTCP: &option.UDPOverTCPOptions{
  251. Enabled: true,
  252. },
  253. },
  254. },
  255. },
  256. Route: &option.RouteOptions{
  257. Rules: []option.Rule{
  258. {
  259. Type: C.RuleTypeDefault,
  260. DefaultOptions: option.DefaultRule{
  261. RawDefaultRule: option.RawDefaultRule{
  262. Inbound: []string{"mixed-in"},
  263. },
  264. RuleAction: option.RuleAction{
  265. Action: C.RuleActionTypeRoute,
  266. RouteOptions: option.RouteActionOptions{
  267. Outbound: "ss-out",
  268. },
  269. },
  270. },
  271. },
  272. },
  273. },
  274. })
  275. testSuit(t, clientPort, testPort)
  276. }
  277. func testShadowsocks2022EIH(t *testing.T, method string, password string) {
  278. startInstance(t, option.Options{
  279. Inbounds: []option.Inbound{
  280. {
  281. Type: C.TypeMixed,
  282. Tag: "mixed-in",
  283. Options: &option.HTTPMixedInboundOptions{
  284. ListenOptions: option.ListenOptions{
  285. Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
  286. ListenPort: clientPort,
  287. },
  288. },
  289. },
  290. {
  291. Type: C.TypeShadowsocks,
  292. Options: &option.ShadowsocksInboundOptions{
  293. ListenOptions: option.ListenOptions{
  294. Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
  295. ListenPort: serverPort,
  296. },
  297. Method: method,
  298. Password: password,
  299. Users: []option.ShadowsocksUser{
  300. {
  301. Password: password,
  302. },
  303. },
  304. },
  305. },
  306. },
  307. Outbounds: []option.Outbound{
  308. {
  309. Type: C.TypeDirect,
  310. },
  311. {
  312. Type: C.TypeShadowsocks,
  313. Tag: "ss-out",
  314. Options: &option.ShadowsocksOutboundOptions{
  315. ServerOptions: option.ServerOptions{
  316. Server: "127.0.0.1",
  317. ServerPort: serverPort,
  318. },
  319. Method: method,
  320. Password: password + ":" + password,
  321. },
  322. },
  323. },
  324. Route: &option.RouteOptions{
  325. Rules: []option.Rule{
  326. {
  327. Type: C.RuleTypeDefault,
  328. DefaultOptions: option.DefaultRule{
  329. RawDefaultRule: option.RawDefaultRule{
  330. Inbound: []string{"mixed-in"},
  331. },
  332. RuleAction: option.RuleAction{
  333. Action: C.RuleActionTypeRoute,
  334. RouteOptions: option.RouteActionOptions{
  335. Outbound: "ss-out",
  336. },
  337. },
  338. },
  339. },
  340. },
  341. },
  342. })
  343. testSuit(t, clientPort, testPort)
  344. }
  345. func mkBase64(t *testing.T, length int) string {
  346. psk := make([]byte, length)
  347. _, err := rand.Read(psk)
  348. require.NoError(t, err)
  349. return base64.StdEncoding.EncodeToString(psk)
  350. }