shadowtls_test.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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.NewListenAddress(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.NewListenAddress(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.NewListenAddress(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. OutboundTLSOptionsContainer: option.OutboundTLSOptionsContainer{
  98. TLS: &option.OutboundTLSOptions{
  99. Enabled: true,
  100. ServerName: "google.com",
  101. UTLS: &option.OutboundUTLSOptions{
  102. Enabled: utlsEanbled,
  103. },
  104. },
  105. },
  106. Version: version,
  107. Password: password,
  108. },
  109. },
  110. {
  111. Type: C.TypeDirect,
  112. Tag: "direct",
  113. },
  114. },
  115. Route: &option.RouteOptions{
  116. Rules: []option.Rule{{
  117. DefaultOptions: option.DefaultRule{
  118. Inbound: []string{"detour"},
  119. Outbound: "direct",
  120. },
  121. }},
  122. },
  123. })
  124. testTCP(t, clientPort, testPort)
  125. }
  126. func TestShadowTLSFallback(t *testing.T) {
  127. startInstance(t, option.Options{
  128. Inbounds: []option.Inbound{
  129. {
  130. Type: C.TypeShadowTLS,
  131. ShadowTLSOptions: option.ShadowTLSInboundOptions{
  132. ListenOptions: option.ListenOptions{
  133. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  134. ListenPort: serverPort,
  135. },
  136. Handshake: option.ShadowTLSHandshakeOptions{
  137. ServerOptions: option.ServerOptions{
  138. Server: "google.com",
  139. ServerPort: 443,
  140. },
  141. },
  142. Version: 3,
  143. Users: []option.ShadowTLSUser{
  144. {Password: "hello"},
  145. },
  146. },
  147. },
  148. },
  149. })
  150. client := &http.Client{
  151. Transport: &http.Transport{
  152. DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
  153. var d net.Dialer
  154. return d.DialContext(ctx, network, "127.0.0.1:"+F.ToString(serverPort))
  155. },
  156. },
  157. }
  158. response, err := client.Get("https://google.com")
  159. require.NoError(t, err)
  160. require.Equal(t, response.StatusCode, 200)
  161. response.Body.Close()
  162. client.CloseIdleConnections()
  163. }
  164. func TestShadowTLSInbound(t *testing.T) {
  165. method := shadowaead_2022.List[0]
  166. password := mkBase64(t, 16)
  167. startDockerContainer(t, DockerOptions{
  168. Image: ImageShadowTLS,
  169. Ports: []uint16{serverPort, otherPort},
  170. EntryPoint: "shadow-tls",
  171. 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},
  172. })
  173. startInstance(t, option.Options{
  174. Inbounds: []option.Inbound{
  175. {
  176. Type: C.TypeMixed,
  177. Tag: "in",
  178. MixedOptions: option.HTTPMixedInboundOptions{
  179. ListenOptions: option.ListenOptions{
  180. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  181. ListenPort: clientPort,
  182. },
  183. },
  184. },
  185. {
  186. Type: C.TypeShadowTLS,
  187. ShadowTLSOptions: option.ShadowTLSInboundOptions{
  188. ListenOptions: option.ListenOptions{
  189. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  190. ListenPort: serverPort,
  191. Detour: "detour",
  192. },
  193. Handshake: option.ShadowTLSHandshakeOptions{
  194. ServerOptions: option.ServerOptions{
  195. Server: "google.com",
  196. ServerPort: 443,
  197. },
  198. },
  199. Version: 3,
  200. Users: []option.ShadowTLSUser{
  201. {Password: password},
  202. },
  203. },
  204. },
  205. {
  206. Type: C.TypeShadowsocks,
  207. Tag: "detour",
  208. ShadowsocksOptions: option.ShadowsocksInboundOptions{
  209. ListenOptions: option.ListenOptions{
  210. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  211. },
  212. Method: method,
  213. Password: password,
  214. },
  215. },
  216. },
  217. Outbounds: []option.Outbound{
  218. {
  219. Type: C.TypeDirect,
  220. },
  221. {
  222. Type: C.TypeShadowsocks,
  223. Tag: "out",
  224. ShadowsocksOptions: option.ShadowsocksOutboundOptions{
  225. ServerOptions: option.ServerOptions{
  226. Server: "127.0.0.1",
  227. ServerPort: otherPort,
  228. },
  229. Method: method,
  230. Password: password,
  231. },
  232. },
  233. },
  234. Route: &option.RouteOptions{
  235. Rules: []option.Rule{{
  236. DefaultOptions: option.DefaultRule{
  237. Inbound: []string{"in"},
  238. Outbound: "out",
  239. },
  240. }},
  241. },
  242. })
  243. testTCP(t, clientPort, testPort)
  244. }
  245. func TestShadowTLSOutbound(t *testing.T) {
  246. method := shadowaead_2022.List[0]
  247. password := mkBase64(t, 16)
  248. startDockerContainer(t, DockerOptions{
  249. Image: ImageShadowTLS,
  250. Ports: []uint16{serverPort, otherPort},
  251. EntryPoint: "shadow-tls",
  252. 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"},
  253. Env: []string{"RUST_LOG=trace"},
  254. })
  255. startInstance(t, option.Options{
  256. Inbounds: []option.Inbound{
  257. {
  258. Type: C.TypeMixed,
  259. MixedOptions: option.HTTPMixedInboundOptions{
  260. ListenOptions: option.ListenOptions{
  261. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  262. ListenPort: clientPort,
  263. },
  264. },
  265. },
  266. {
  267. Type: C.TypeShadowsocks,
  268. Tag: "detour",
  269. ShadowsocksOptions: option.ShadowsocksInboundOptions{
  270. ListenOptions: option.ListenOptions{
  271. Listen: option.NewListenAddress(netip.IPv4Unspecified()),
  272. ListenPort: otherPort,
  273. },
  274. Method: method,
  275. Password: password,
  276. },
  277. },
  278. },
  279. Outbounds: []option.Outbound{
  280. {
  281. Type: C.TypeShadowsocks,
  282. ShadowsocksOptions: option.ShadowsocksOutboundOptions{
  283. Method: method,
  284. Password: password,
  285. DialerOptions: option.DialerOptions{
  286. Detour: "detour",
  287. },
  288. },
  289. },
  290. {
  291. Type: C.TypeShadowTLS,
  292. Tag: "detour",
  293. ShadowTLSOptions: option.ShadowTLSOutboundOptions{
  294. ServerOptions: option.ServerOptions{
  295. Server: "127.0.0.1",
  296. ServerPort: serverPort,
  297. },
  298. OutboundTLSOptionsContainer: option.OutboundTLSOptionsContainer{
  299. TLS: &option.OutboundTLSOptions{
  300. Enabled: true,
  301. ServerName: "google.com",
  302. },
  303. },
  304. Version: 3,
  305. Password: "hello",
  306. },
  307. },
  308. {
  309. Type: C.TypeDirect,
  310. Tag: "direct",
  311. },
  312. },
  313. Route: &option.RouteOptions{
  314. Rules: []option.Rule{{
  315. DefaultOptions: option.DefaultRule{
  316. Inbound: []string{"detour"},
  317. Outbound: "direct",
  318. },
  319. }},
  320. },
  321. })
  322. testTCP(t, clientPort, testPort)
  323. }