shadowsocks_test.go 6.3 KB

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