shadowtls_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. package main
  2. import (
  3. "context"
  4. "crypto/tls"
  5. "net"
  6. "net/http"
  7. "net/netip"
  8. "testing"
  9. C "github.com/sagernet/sing-box/constant"
  10. "github.com/sagernet/sing-box/option"
  11. "github.com/sagernet/sing-shadowsocks/shadowaead_2022"
  12. "github.com/sagernet/sing/common"
  13. F "github.com/sagernet/sing/common/format"
  14. "github.com/sagernet/sing/common/json/badoption"
  15. "github.com/stretchr/testify/require"
  16. )
  17. func TestShadowTLS(t *testing.T) {
  18. t.Run("v1", func(t *testing.T) {
  19. testShadowTLS(t, 1, "", false, option.ShadowTLSWildcardSNIOff)
  20. })
  21. t.Run("v2", func(t *testing.T) {
  22. testShadowTLS(t, 2, "hello", false, option.ShadowTLSWildcardSNIOff)
  23. })
  24. t.Run("v3", func(t *testing.T) {
  25. testShadowTLS(t, 3, "hello", false, option.ShadowTLSWildcardSNIOff)
  26. })
  27. t.Run("v2-utls", func(t *testing.T) {
  28. testShadowTLS(t, 2, "hello", true, option.ShadowTLSWildcardSNIOff)
  29. })
  30. t.Run("v3-utls", func(t *testing.T) {
  31. testShadowTLS(t, 3, "hello", true, option.ShadowTLSWildcardSNIOff)
  32. })
  33. t.Run("v3-wildcard-sni-authed", func(t *testing.T) {
  34. testShadowTLS(t, 3, "hello", false, option.ShadowTLSWildcardSNIAuthed)
  35. })
  36. t.Run("v3-wildcard-sni-all", func(t *testing.T) {
  37. testShadowTLS(t, 3, "hello", false, option.ShadowTLSWildcardSNIAll)
  38. })
  39. t.Run("v3-wildcard-sni-authed-utls", func(t *testing.T) {
  40. testShadowTLS(t, 3, "hello", true, option.ShadowTLSWildcardSNIAll)
  41. })
  42. t.Run("v3-wildcard-sni-all-utls", func(t *testing.T) {
  43. testShadowTLS(t, 3, "hello", true, option.ShadowTLSWildcardSNIAll)
  44. })
  45. }
  46. func testShadowTLS(t *testing.T, version int, password string, utlsEanbled bool, wildcardSNI option.WildcardSNI) {
  47. method := shadowaead_2022.List[0]
  48. ssPassword := mkBase64(t, 16)
  49. var clientServerName string
  50. if wildcardSNI != option.ShadowTLSWildcardSNIOff {
  51. clientServerName = "cloudflare.com"
  52. } else {
  53. clientServerName = "google.com"
  54. }
  55. startInstance(t, option.Options{
  56. Inbounds: []option.Inbound{
  57. {
  58. Type: C.TypeMixed,
  59. Options: &option.HTTPMixedInboundOptions{
  60. ListenOptions: option.ListenOptions{
  61. Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
  62. ListenPort: clientPort,
  63. },
  64. },
  65. },
  66. {
  67. Type: C.TypeShadowTLS,
  68. Tag: "in",
  69. Options: &option.ShadowTLSInboundOptions{
  70. ListenOptions: option.ListenOptions{
  71. Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
  72. ListenPort: serverPort,
  73. Detour: "detour",
  74. },
  75. Handshake: option.ShadowTLSHandshakeOptions{
  76. ServerOptions: option.ServerOptions{
  77. Server: "google.com",
  78. ServerPort: 443,
  79. },
  80. },
  81. Version: version,
  82. Password: password,
  83. Users: []option.ShadowTLSUser{{Password: password}},
  84. WildcardSNI: wildcardSNI,
  85. },
  86. },
  87. {
  88. Type: C.TypeShadowsocks,
  89. Tag: "detour",
  90. Options: &option.ShadowsocksInboundOptions{
  91. ListenOptions: option.ListenOptions{
  92. Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
  93. ListenPort: otherPort,
  94. },
  95. Method: method,
  96. Password: ssPassword,
  97. },
  98. },
  99. },
  100. Outbounds: []option.Outbound{
  101. {
  102. Type: C.TypeShadowsocks,
  103. Options: &option.ShadowsocksOutboundOptions{
  104. Method: method,
  105. Password: ssPassword,
  106. DialerOptions: option.DialerOptions{
  107. Detour: "detour",
  108. },
  109. },
  110. },
  111. {
  112. Type: C.TypeShadowTLS,
  113. Tag: "detour",
  114. Options: &option.ShadowTLSOutboundOptions{
  115. ServerOptions: option.ServerOptions{
  116. Server: "127.0.0.1",
  117. ServerPort: serverPort,
  118. },
  119. OutboundTLSOptionsContainer: option.OutboundTLSOptionsContainer{
  120. TLS: &option.OutboundTLSOptions{
  121. Enabled: true,
  122. ServerName: clientServerName,
  123. UTLS: &option.OutboundUTLSOptions{
  124. Enabled: utlsEanbled,
  125. },
  126. },
  127. },
  128. Version: version,
  129. Password: password,
  130. },
  131. },
  132. {
  133. Type: C.TypeDirect,
  134. Tag: "direct",
  135. },
  136. },
  137. Route: &option.RouteOptions{
  138. Rules: []option.Rule{
  139. {
  140. Type: C.RuleTypeDefault,
  141. DefaultOptions: option.DefaultRule{
  142. RawDefaultRule: option.RawDefaultRule{
  143. Inbound: []string{"detour"},
  144. },
  145. RuleAction: option.RuleAction{
  146. Action: C.RuleActionTypeRoute,
  147. RouteOptions: option.RouteActionOptions{
  148. Outbound: "direct",
  149. },
  150. },
  151. },
  152. },
  153. },
  154. },
  155. })
  156. testTCP(t, clientPort, testPort)
  157. }
  158. func TestShadowTLSFallback(t *testing.T) {
  159. startInstance(t, option.Options{
  160. Inbounds: []option.Inbound{
  161. {
  162. Type: C.TypeShadowTLS,
  163. Options: &option.ShadowTLSInboundOptions{
  164. ListenOptions: option.ListenOptions{
  165. Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
  166. ListenPort: serverPort,
  167. },
  168. Handshake: option.ShadowTLSHandshakeOptions{
  169. ServerOptions: option.ServerOptions{
  170. Server: "bing.com",
  171. ServerPort: 443,
  172. },
  173. },
  174. Version: 3,
  175. Users: []option.ShadowTLSUser{
  176. {Password: "hello"},
  177. },
  178. },
  179. },
  180. },
  181. })
  182. client := &http.Client{
  183. Transport: &http.Transport{
  184. DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
  185. var d net.Dialer
  186. return d.DialContext(ctx, network, "127.0.0.1:"+F.ToString(serverPort))
  187. },
  188. },
  189. }
  190. response, err := client.Get("https://bing.com")
  191. require.NoError(t, err)
  192. require.Equal(t, response.StatusCode, 200)
  193. response.Body.Close()
  194. client.CloseIdleConnections()
  195. }
  196. func TestShadowTLSFallbackWildcardAll(t *testing.T) {
  197. startInstance(t, option.Options{
  198. Inbounds: []option.Inbound{
  199. {
  200. Type: C.TypeShadowTLS,
  201. Options: &option.ShadowTLSInboundOptions{
  202. ListenOptions: option.ListenOptions{
  203. Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
  204. ListenPort: serverPort,
  205. },
  206. Version: 3,
  207. Users: []option.ShadowTLSUser{
  208. {Password: "hello"},
  209. },
  210. WildcardSNI: option.ShadowTLSWildcardSNIAll,
  211. },
  212. },
  213. },
  214. })
  215. client := &http.Client{
  216. Transport: &http.Transport{
  217. DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
  218. var d net.Dialer
  219. return d.DialContext(ctx, network, "127.0.0.1:"+F.ToString(serverPort))
  220. },
  221. },
  222. }
  223. response, err := client.Get("https://www.bing.com")
  224. require.NoError(t, err)
  225. require.Equal(t, response.StatusCode, 200)
  226. response.Body.Close()
  227. client.CloseIdleConnections()
  228. }
  229. func TestShadowTLSFallbackWildcardAuthedFail(t *testing.T) {
  230. startInstance(t, option.Options{
  231. Inbounds: []option.Inbound{
  232. {
  233. Type: C.TypeShadowTLS,
  234. Options: &option.ShadowTLSInboundOptions{
  235. ListenOptions: option.ListenOptions{
  236. Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
  237. ListenPort: serverPort,
  238. },
  239. Handshake: option.ShadowTLSHandshakeOptions{
  240. ServerOptions: option.ServerOptions{
  241. Server: "bing.com",
  242. ServerPort: 443,
  243. },
  244. },
  245. Version: 3,
  246. Users: []option.ShadowTLSUser{
  247. {Password: "hello"},
  248. },
  249. WildcardSNI: option.ShadowTLSWildcardSNIAuthed,
  250. },
  251. },
  252. },
  253. })
  254. client := &http.Client{
  255. Transport: &http.Transport{
  256. DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
  257. var d net.Dialer
  258. return d.DialContext(ctx, network, "127.0.0.1:"+F.ToString(serverPort))
  259. },
  260. },
  261. }
  262. _, err := client.Get("https://baidu.com")
  263. expected := &tls.CertificateVerificationError{}
  264. require.ErrorAs(t, err, &expected)
  265. client.CloseIdleConnections()
  266. }
  267. func TestShadowTLSFallbackWildcardOffFail(t *testing.T) {
  268. startInstance(t, option.Options{
  269. Inbounds: []option.Inbound{
  270. {
  271. Type: C.TypeShadowTLS,
  272. Options: &option.ShadowTLSInboundOptions{
  273. ListenOptions: option.ListenOptions{
  274. Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
  275. ListenPort: serverPort,
  276. },
  277. Handshake: option.ShadowTLSHandshakeOptions{
  278. ServerOptions: option.ServerOptions{
  279. Server: "bing.com",
  280. ServerPort: 443,
  281. },
  282. },
  283. Version: 3,
  284. Users: []option.ShadowTLSUser{
  285. {Password: "hello"},
  286. },
  287. WildcardSNI: option.ShadowTLSWildcardSNIOff,
  288. },
  289. },
  290. },
  291. })
  292. client := &http.Client{
  293. Transport: &http.Transport{
  294. DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
  295. var d net.Dialer
  296. return d.DialContext(ctx, network, "127.0.0.1:"+F.ToString(serverPort))
  297. },
  298. },
  299. }
  300. _, err := client.Get("https://baidu.com")
  301. expected := &tls.CertificateVerificationError{}
  302. require.ErrorAs(t, err, &expected)
  303. client.CloseIdleConnections()
  304. }
  305. func TestShadowTLSInbound(t *testing.T) {
  306. method := shadowaead_2022.List[0]
  307. password := mkBase64(t, 16)
  308. startDockerContainer(t, DockerOptions{
  309. Image: ImageShadowTLS,
  310. Ports: []uint16{serverPort, otherPort},
  311. EntryPoint: "shadow-tls",
  312. 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},
  313. })
  314. startInstance(t, option.Options{
  315. Inbounds: []option.Inbound{
  316. {
  317. Type: C.TypeMixed,
  318. Tag: "in",
  319. Options: &option.HTTPMixedInboundOptions{
  320. ListenOptions: option.ListenOptions{
  321. Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
  322. ListenPort: clientPort,
  323. },
  324. },
  325. },
  326. {
  327. Type: C.TypeShadowTLS,
  328. Options: &option.ShadowTLSInboundOptions{
  329. ListenOptions: option.ListenOptions{
  330. Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
  331. ListenPort: serverPort,
  332. Detour: "detour",
  333. },
  334. Handshake: option.ShadowTLSHandshakeOptions{
  335. ServerOptions: option.ServerOptions{
  336. Server: "google.com",
  337. ServerPort: 443,
  338. },
  339. },
  340. Version: 3,
  341. Users: []option.ShadowTLSUser{
  342. {Password: password},
  343. },
  344. },
  345. },
  346. {
  347. Type: C.TypeShadowsocks,
  348. Tag: "detour",
  349. Options: &option.ShadowsocksInboundOptions{
  350. ListenOptions: option.ListenOptions{
  351. Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
  352. },
  353. Method: method,
  354. Password: password,
  355. },
  356. },
  357. },
  358. Outbounds: []option.Outbound{
  359. {
  360. Type: C.TypeDirect,
  361. },
  362. {
  363. Type: C.TypeShadowsocks,
  364. Tag: "out",
  365. Options: &option.ShadowsocksOutboundOptions{
  366. ServerOptions: option.ServerOptions{
  367. Server: "127.0.0.1",
  368. ServerPort: otherPort,
  369. },
  370. Method: method,
  371. Password: password,
  372. },
  373. },
  374. },
  375. Route: &option.RouteOptions{
  376. Rules: []option.Rule{
  377. {
  378. Type: C.RuleTypeDefault,
  379. DefaultOptions: option.DefaultRule{
  380. RawDefaultRule: option.RawDefaultRule{
  381. Inbound: []string{"in"},
  382. },
  383. RuleAction: option.RuleAction{
  384. Action: C.RuleActionTypeRoute,
  385. RouteOptions: option.RouteActionOptions{
  386. Outbound: "out",
  387. },
  388. },
  389. },
  390. },
  391. },
  392. },
  393. })
  394. testTCP(t, clientPort, testPort)
  395. }
  396. func TestShadowTLSOutbound(t *testing.T) {
  397. method := shadowaead_2022.List[0]
  398. password := mkBase64(t, 16)
  399. startDockerContainer(t, DockerOptions{
  400. Image: ImageShadowTLS,
  401. Ports: []uint16{serverPort, otherPort},
  402. EntryPoint: "shadow-tls",
  403. 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"},
  404. Env: []string{"RUST_LOG=trace"},
  405. })
  406. startInstance(t, option.Options{
  407. Inbounds: []option.Inbound{
  408. {
  409. Type: C.TypeMixed,
  410. Options: &option.HTTPMixedInboundOptions{
  411. ListenOptions: option.ListenOptions{
  412. Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
  413. ListenPort: clientPort,
  414. },
  415. },
  416. },
  417. {
  418. Type: C.TypeShadowsocks,
  419. Tag: "detour",
  420. Options: &option.ShadowsocksInboundOptions{
  421. ListenOptions: option.ListenOptions{
  422. Listen: common.Ptr(badoption.Addr(netip.IPv4Unspecified())),
  423. ListenPort: otherPort,
  424. },
  425. Method: method,
  426. Password: password,
  427. },
  428. },
  429. },
  430. Outbounds: []option.Outbound{
  431. {
  432. Type: C.TypeShadowsocks,
  433. Options: &option.ShadowsocksOutboundOptions{
  434. Method: method,
  435. Password: password,
  436. DialerOptions: option.DialerOptions{
  437. Detour: "detour",
  438. },
  439. },
  440. },
  441. {
  442. Type: C.TypeShadowTLS,
  443. Tag: "detour",
  444. Options: &option.ShadowTLSOutboundOptions{
  445. ServerOptions: option.ServerOptions{
  446. Server: "127.0.0.1",
  447. ServerPort: serverPort,
  448. },
  449. OutboundTLSOptionsContainer: option.OutboundTLSOptionsContainer{
  450. TLS: &option.OutboundTLSOptions{
  451. Enabled: true,
  452. ServerName: "google.com",
  453. },
  454. },
  455. Version: 3,
  456. Password: "hello",
  457. },
  458. },
  459. {
  460. Type: C.TypeDirect,
  461. Tag: "direct",
  462. },
  463. },
  464. Route: &option.RouteOptions{
  465. Rules: []option.Rule{
  466. {
  467. Type: C.RuleTypeDefault,
  468. DefaultOptions: option.DefaultRule{
  469. RawDefaultRule: option.RawDefaultRule{
  470. Inbound: []string{"detour"},
  471. },
  472. RuleAction: option.RuleAction{
  473. Action: C.RuleActionTypeRoute,
  474. RouteOptions: option.RouteActionOptions{
  475. Outbound: "direct",
  476. },
  477. },
  478. },
  479. },
  480. },
  481. },
  482. })
  483. testTCP(t, clientPort, testPort)
  484. }