shadowtls_test.go 8.1 KB

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