shadowtls_test.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. package main
  2. import (
  3. "context"
  4. "net"
  5. "net/http"
  6. "net/netip"
  7. "testing"
  8. C "github.com/sagernet/sing-box/constant"
  9. "github.com/sagernet/sing-box/option"
  10. "github.com/sagernet/sing-shadowsocks/shadowaead_2022"
  11. F "github.com/sagernet/sing/common/format"
  12. "github.com/stretchr/testify/require"
  13. )
  14. func TestShadowTLS(t *testing.T) {
  15. t.Run("v1", func(t *testing.T) {
  16. testShadowTLS(t, 1, "", false)
  17. })
  18. t.Run("v2", func(t *testing.T) {
  19. testShadowTLS(t, 2, "hello", false)
  20. })
  21. t.Run("v3", func(t *testing.T) {
  22. testShadowTLS(t, 3, "hello", false)
  23. })
  24. t.Run("v2-utls", func(t *testing.T) {
  25. testShadowTLS(t, 2, "hello", true)
  26. })
  27. t.Run("v3-utls", func(t *testing.T) {
  28. testShadowTLS(t, 3, "hello", true)
  29. })
  30. }
  31. func testShadowTLS(t *testing.T, version int, password string, utlsEanbled bool) {
  32. method := shadowaead_2022.List[0]
  33. ssPassword := mkBase64(t, 16)
  34. startInstance(t, option.Options{
  35. Inbounds: []option.Inbound{
  36. {
  37. Type: C.TypeMixed,
  38. MixedOptions: option.HTTPMixedInboundOptions{
  39. ListenOptions: option.ListenOptions{
  40. Listen: option.ListenAddress(netip.IPv4Unspecified()),
  41. ListenPort: clientPort,
  42. },
  43. },
  44. },
  45. {
  46. Type: C.TypeShadowTLS,
  47. Tag: "in",
  48. ShadowTLSOptions: option.ShadowTLSInboundOptions{
  49. ListenOptions: option.ListenOptions{
  50. Listen: option.ListenAddress(netip.IPv4Unspecified()),
  51. ListenPort: serverPort,
  52. Detour: "detour",
  53. },
  54. Handshake: option.ShadowTLSHandshakeOptions{
  55. ServerOptions: option.ServerOptions{
  56. Server: "google.com",
  57. ServerPort: 443,
  58. },
  59. },
  60. Version: version,
  61. Password: password,
  62. Users: []option.ShadowTLSUser{{Password: password}},
  63. },
  64. },
  65. {
  66. Type: C.TypeShadowsocks,
  67. Tag: "detour",
  68. ShadowsocksOptions: option.ShadowsocksInboundOptions{
  69. ListenOptions: option.ListenOptions{
  70. Listen: option.ListenAddress(netip.IPv4Unspecified()),
  71. ListenPort: otherPort,
  72. },
  73. Method: method,
  74. Password: ssPassword,
  75. },
  76. },
  77. },
  78. Outbounds: []option.Outbound{
  79. {
  80. Type: C.TypeShadowsocks,
  81. ShadowsocksOptions: option.ShadowsocksOutboundOptions{
  82. Method: method,
  83. Password: ssPassword,
  84. DialerOptions: option.DialerOptions{
  85. Detour: "detour",
  86. },
  87. },
  88. },
  89. {
  90. Type: C.TypeShadowTLS,
  91. Tag: "detour",
  92. ShadowTLSOptions: option.ShadowTLSOutboundOptions{
  93. ServerOptions: option.ServerOptions{
  94. Server: "127.0.0.1",
  95. ServerPort: serverPort,
  96. },
  97. TLS: &option.OutboundTLSOptions{
  98. Enabled: true,
  99. ServerName: "google.com",
  100. UTLS: &option.OutboundUTLSOptions{
  101. Enabled: utlsEanbled,
  102. },
  103. },
  104. Version: version,
  105. Password: password,
  106. },
  107. },
  108. {
  109. Type: C.TypeDirect,
  110. Tag: "direct",
  111. },
  112. },
  113. Route: &option.RouteOptions{
  114. Rules: []option.Rule{{
  115. DefaultOptions: option.DefaultRule{
  116. Inbound: []string{"detour"},
  117. Outbound: "direct",
  118. },
  119. }},
  120. },
  121. })
  122. testTCP(t, clientPort, testPort)
  123. }
  124. func TestShadowTLSFallback(t *testing.T) {
  125. startInstance(t, option.Options{
  126. Inbounds: []option.Inbound{
  127. {
  128. Type: C.TypeShadowTLS,
  129. ShadowTLSOptions: option.ShadowTLSInboundOptions{
  130. ListenOptions: option.ListenOptions{
  131. Listen: option.ListenAddress(netip.IPv4Unspecified()),
  132. ListenPort: serverPort,
  133. },
  134. Handshake: option.ShadowTLSHandshakeOptions{
  135. ServerOptions: option.ServerOptions{
  136. Server: "google.com",
  137. ServerPort: 443,
  138. },
  139. },
  140. Version: 3,
  141. Password: "hello",
  142. },
  143. },
  144. },
  145. })
  146. client := &http.Client{
  147. Transport: &http.Transport{
  148. DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
  149. var d net.Dialer
  150. return d.DialContext(ctx, network, "127.0.0.1:"+F.ToString(serverPort))
  151. },
  152. },
  153. }
  154. response, err := client.Get("https://google.com")
  155. require.NoError(t, err)
  156. require.Equal(t, response.StatusCode, 200)
  157. response.Body.Close()
  158. client.CloseIdleConnections()
  159. }
  160. func TestShadowTLSInbound(t *testing.T) {
  161. method := shadowaead_2022.List[0]
  162. password := mkBase64(t, 16)
  163. startDockerContainer(t, DockerOptions{
  164. Image: ImageShadowTLS,
  165. Ports: []uint16{serverPort, otherPort},
  166. EntryPoint: "shadow-tls",
  167. Cmd: []string{"--v3", "--threads", "1", "client", "--listen", "0.0.0.0:" + F.ToString(otherPort), "--server", "127.0.0.1:" + F.ToString(serverPort), "--sni", "google.com", "--password", password},
  168. })
  169. startInstance(t, option.Options{
  170. Inbounds: []option.Inbound{
  171. {
  172. Type: C.TypeMixed,
  173. Tag: "in",
  174. MixedOptions: option.HTTPMixedInboundOptions{
  175. ListenOptions: option.ListenOptions{
  176. Listen: option.ListenAddress(netip.IPv4Unspecified()),
  177. ListenPort: clientPort,
  178. },
  179. },
  180. },
  181. {
  182. Type: C.TypeShadowTLS,
  183. ShadowTLSOptions: option.ShadowTLSInboundOptions{
  184. ListenOptions: option.ListenOptions{
  185. Listen: option.ListenAddress(netip.IPv4Unspecified()),
  186. ListenPort: serverPort,
  187. Detour: "detour",
  188. },
  189. Handshake: option.ShadowTLSHandshakeOptions{
  190. ServerOptions: option.ServerOptions{
  191. Server: "google.com",
  192. ServerPort: 443,
  193. },
  194. },
  195. Version: 3,
  196. Password: password,
  197. },
  198. },
  199. {
  200. Type: C.TypeShadowsocks,
  201. Tag: "detour",
  202. ShadowsocksOptions: option.ShadowsocksInboundOptions{
  203. ListenOptions: option.ListenOptions{
  204. Listen: option.ListenAddress(netip.IPv4Unspecified()),
  205. },
  206. Method: method,
  207. Password: password,
  208. },
  209. },
  210. },
  211. Outbounds: []option.Outbound{
  212. {
  213. Type: C.TypeDirect,
  214. },
  215. {
  216. Type: C.TypeShadowsocks,
  217. Tag: "out",
  218. ShadowsocksOptions: option.ShadowsocksOutboundOptions{
  219. ServerOptions: option.ServerOptions{
  220. Server: "127.0.0.1",
  221. ServerPort: otherPort,
  222. },
  223. Method: method,
  224. Password: password,
  225. },
  226. },
  227. },
  228. Route: &option.RouteOptions{
  229. Rules: []option.Rule{{
  230. DefaultOptions: option.DefaultRule{
  231. Inbound: []string{"in"},
  232. Outbound: "out",
  233. },
  234. }},
  235. },
  236. })
  237. testTCP(t, clientPort, testPort)
  238. }
  239. func TestShadowTLSOutbound(t *testing.T) {
  240. method := shadowaead_2022.List[0]
  241. password := mkBase64(t, 16)
  242. startDockerContainer(t, DockerOptions{
  243. Image: ImageShadowTLS,
  244. Ports: []uint16{serverPort, otherPort},
  245. EntryPoint: "shadow-tls",
  246. Cmd: []string{"--v3", "--threads", "1", "server", "--listen", "0.0.0.0:" + F.ToString(serverPort), "--server", "127.0.0.1:" + F.ToString(otherPort), "--tls", "google.com:443", "--password", "hello"},
  247. Env: []string{"RUST_LOG=trace"},
  248. })
  249. startInstance(t, option.Options{
  250. Inbounds: []option.Inbound{
  251. {
  252. Type: C.TypeMixed,
  253. MixedOptions: option.HTTPMixedInboundOptions{
  254. ListenOptions: option.ListenOptions{
  255. Listen: option.ListenAddress(netip.IPv4Unspecified()),
  256. ListenPort: clientPort,
  257. },
  258. },
  259. },
  260. {
  261. Type: C.TypeShadowsocks,
  262. Tag: "detour",
  263. ShadowsocksOptions: option.ShadowsocksInboundOptions{
  264. ListenOptions: option.ListenOptions{
  265. Listen: option.ListenAddress(netip.IPv4Unspecified()),
  266. ListenPort: otherPort,
  267. },
  268. Method: method,
  269. Password: password,
  270. },
  271. },
  272. },
  273. Outbounds: []option.Outbound{
  274. {
  275. Type: C.TypeShadowsocks,
  276. ShadowsocksOptions: option.ShadowsocksOutboundOptions{
  277. Method: method,
  278. Password: password,
  279. DialerOptions: option.DialerOptions{
  280. Detour: "detour",
  281. },
  282. },
  283. },
  284. {
  285. Type: C.TypeShadowTLS,
  286. Tag: "detour",
  287. ShadowTLSOptions: option.ShadowTLSOutboundOptions{
  288. ServerOptions: option.ServerOptions{
  289. Server: "127.0.0.1",
  290. ServerPort: serverPort,
  291. },
  292. TLS: &option.OutboundTLSOptions{
  293. Enabled: true,
  294. ServerName: "google.com",
  295. },
  296. Version: 3,
  297. Password: "hello",
  298. },
  299. },
  300. {
  301. Type: C.TypeDirect,
  302. Tag: "direct",
  303. },
  304. },
  305. Route: &option.RouteOptions{
  306. Rules: []option.Rule{{
  307. DefaultOptions: option.DefaultRule{
  308. Inbound: []string{"detour"},
  309. Outbound: "direct",
  310. },
  311. }},
  312. },
  313. })
  314. testTCP(t, clientPort, testPort)
  315. }