Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

136 rindas
3.5KB

  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Gun.Conn do
  5. alias Pleroma.Gun
  6. require Logger
  7. def open(%URI{} = uri, opts) do
  8. pool_opts = Pleroma.Config.get([:connections_pool], [])
  9. opts =
  10. opts
  11. |> Enum.into(%{})
  12. |> Map.put_new(:connect_timeout, pool_opts[:connect_timeout] || 5_000)
  13. |> Map.put_new(:supervise, false)
  14. |> maybe_add_tls_opts(uri)
  15. do_open(uri, opts)
  16. end
  17. defp maybe_add_tls_opts(opts, %URI{scheme: "http"}), do: opts
  18. defp maybe_add_tls_opts(opts, %URI{scheme: "https"}) do
  19. tls_opts = [
  20. verify: :verify_peer,
  21. cacertfile: CAStore.file_path(),
  22. depth: 20,
  23. reuse_sessions: false,
  24. log_level: :warning,
  25. customize_hostname_check: [match_fun: :public_key.pkix_verify_hostname_match_fun(:https)]
  26. ]
  27. tls_opts =
  28. if Keyword.keyword?(opts[:tls_opts]) do
  29. Keyword.merge(tls_opts, opts[:tls_opts])
  30. else
  31. tls_opts
  32. end
  33. Map.put(opts, :tls_opts, tls_opts)
  34. end
  35. defp do_open(uri, %{proxy: {proxy_host, proxy_port}} = opts) do
  36. connect_opts =
  37. uri
  38. |> destination_opts()
  39. |> add_http2_opts(uri.scheme, Map.get(opts, :tls_opts, []))
  40. with open_opts <- Map.delete(opts, :tls_opts),
  41. {:ok, conn} <- Gun.open(proxy_host, proxy_port, open_opts),
  42. {:ok, protocol} <- Gun.await_up(conn, opts[:connect_timeout]),
  43. stream <- Gun.connect(conn, connect_opts),
  44. {:response, :fin, 200, _} <- Gun.await(conn, stream) do
  45. {:ok, conn, protocol}
  46. else
  47. error ->
  48. Logger.warn(
  49. "Opening proxied connection to #{compose_uri_log(uri)} failed with error #{
  50. inspect(error)
  51. }"
  52. )
  53. error
  54. end
  55. end
  56. defp do_open(uri, %{proxy: {proxy_type, proxy_host, proxy_port}} = opts) do
  57. version =
  58. proxy_type
  59. |> to_string()
  60. |> String.last()
  61. |> case do
  62. "4" -> 4
  63. _ -> 5
  64. end
  65. socks_opts =
  66. uri
  67. |> destination_opts()
  68. |> add_http2_opts(uri.scheme, Map.get(opts, :tls_opts, []))
  69. |> Map.put(:version, version)
  70. opts =
  71. opts
  72. |> Map.put(:protocols, [:socks])
  73. |> Map.put(:socks_opts, socks_opts)
  74. with {:ok, conn} <- Gun.open(proxy_host, proxy_port, opts),
  75. {:ok, protocol} <- Gun.await_up(conn, opts[:connect_timeout]) do
  76. {:ok, conn, protocol}
  77. else
  78. error ->
  79. Logger.warn(
  80. "Opening socks proxied connection to #{compose_uri_log(uri)} failed with error #{
  81. inspect(error)
  82. }"
  83. )
  84. error
  85. end
  86. end
  87. defp do_open(%URI{host: host, port: port} = uri, opts) do
  88. host = Pleroma.HTTP.AdapterHelper.parse_host(host)
  89. with {:ok, conn} <- Gun.open(host, port, opts),
  90. {:ok, protocol} <- Gun.await_up(conn, opts[:connect_timeout]) do
  91. {:ok, conn, protocol}
  92. else
  93. error ->
  94. Logger.warn(
  95. "Opening connection to #{compose_uri_log(uri)} failed with error #{inspect(error)}"
  96. )
  97. error
  98. end
  99. end
  100. defp destination_opts(%URI{host: host, port: port}) do
  101. host = Pleroma.HTTP.AdapterHelper.parse_host(host)
  102. %{host: host, port: port}
  103. end
  104. defp add_http2_opts(opts, "https", tls_opts) do
  105. Map.merge(opts, %{protocols: [:http2], transport: :tls, tls_opts: tls_opts})
  106. end
  107. defp add_http2_opts(opts, _, _), do: opts
  108. def compose_uri_log(%URI{scheme: scheme, host: host, path: path}) do
  109. "#{scheme}://#{host}#{path}"
  110. end
  111. end