shadowtls_test.go 7.8 KB

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