logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

conn.ex (3580B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 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.warning(
  49. "Opening proxied connection to #{compose_uri_log(uri)} failed with error #{inspect(error)}"
  50. )
  51. error
  52. end
  53. end
  54. defp do_open(uri, %{proxy: {proxy_type, proxy_host, proxy_port}} = opts) do
  55. version =
  56. proxy_type
  57. |> to_string()
  58. |> String.last()
  59. |> case do
  60. "4" -> 4
  61. _ -> 5
  62. end
  63. socks_opts =
  64. uri
  65. |> destination_opts()
  66. |> add_http2_opts(uri.scheme, Map.get(opts, :tls_opts, []))
  67. |> Map.put(:version, version)
  68. opts =
  69. opts
  70. |> Map.put(:protocols, [:socks])
  71. |> Map.put(:socks_opts, socks_opts)
  72. with {:ok, conn} <- Gun.open(proxy_host, proxy_port, opts),
  73. {:ok, protocol} <- Gun.await_up(conn, opts[:connect_timeout]) do
  74. {:ok, conn, protocol}
  75. else
  76. error ->
  77. Logger.warning(
  78. "Opening socks proxied connection to #{compose_uri_log(uri)} failed with error #{inspect(error)}"
  79. )
  80. error
  81. end
  82. end
  83. defp do_open(%URI{host: host, port: port} = uri, opts) do
  84. host = Pleroma.HTTP.AdapterHelper.parse_host(host)
  85. with {:ok, conn} <- Gun.open(host, port, opts),
  86. {:ok, protocol} <- Gun.await_up(conn, opts[:connect_timeout]) do
  87. {:ok, conn, protocol}
  88. else
  89. error ->
  90. Logger.warning(
  91. "Opening connection to #{compose_uri_log(uri)} failed with error #{inspect(error)}"
  92. )
  93. error
  94. end
  95. end
  96. defp destination_opts(%URI{host: host, port: port}) do
  97. host = Pleroma.HTTP.AdapterHelper.parse_host(host)
  98. %{host: host, port: port}
  99. end
  100. defp add_http2_opts(opts, "https", tls_opts) do
  101. Map.merge(opts, %{protocols: [:http2], transport: :tls, tls_opts: tls_opts})
  102. end
  103. defp add_http2_opts(opts, _, _), do: opts
  104. def compose_uri_log(%URI{scheme: scheme, host: host, path: path}) do
  105. "#{scheme}://#{host}#{path}"
  106. end
  107. end