Page MenuHomePhorge

No OneTemporary

Authored By
Unknown
Size
4 KB
Referenced Files
None
Subscribers
None
diff --git a/changelog.d/hackney-proxy-ip-host.fix b/changelog.d/hackney-proxy-ip-host.fix
new file mode 100644
index 000000000..5d11aadd5
--- /dev/null
+++ b/changelog.d/hackney-proxy-ip-host.fix
@@ -0,0 +1 @@
+Hackney: use the configured proxy when its host is an IP address; Hackney 4 ignored such proxies and connected directly
\ No newline at end of file
diff --git a/lib/pleroma/http/adapter_helper/hackney.ex b/lib/pleroma/http/adapter_helper/hackney.ex
index 314f9e8b1..e9cc04361 100644
--- a/lib/pleroma/http/adapter_helper/hackney.ex
+++ b/lib/pleroma/http/adapter_helper/hackney.ex
@@ -1,44 +1,60 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.HTTP.AdapterHelper.Hackney do
@behaviour Pleroma.HTTP.AdapterHelper
@defaults [
follow_redirect: false,
force_redirect: false,
with_body: true
]
@spec options(keyword(), URI.t()) :: keyword()
def options(connection_opts \\ [], %URI{} = uri) do
proxy = Pleroma.Config.get([:http, :proxy_url])
config_opts = Pleroma.Config.get([:http, :adapter], [])
url_encoding =
Keyword.new()
|> Keyword.put(:path_encode_fun, fn path -> path end)
@defaults
|> Keyword.merge(url_encoding)
|> Keyword.merge(config_opts)
|> Keyword.merge(connection_opts)
|> add_scheme_opts(uri)
|> maybe_add_with_body()
|> Pleroma.HTTP.AdapterHelper.maybe_add_proxy(proxy)
|> Keyword.put(:follow_redirect, false)
|> Keyword.put(:force_redirect, false)
+ |> normalize_proxy()
+ end
+
+ # Hackney 4 ignores a proxy whose host is an IP tuple and connects directly.
+ # ConfigDB turns a numeric proxy host into such a tuple.
+ defp normalize_proxy(opts) do
+ case Keyword.fetch(opts, :proxy) do
+ {:ok, {type, host, port}} when is_tuple(host) ->
+ Keyword.put(opts, :proxy, {type, :inet.ntoa(host), port})
+
+ {:ok, {host, port}} when is_tuple(host) ->
+ Keyword.put(opts, :proxy, {:inet.ntoa(host), port})
+
+ _ ->
+ opts
+ end
end
defp add_scheme_opts(opts, _), do: opts
defp maybe_add_with_body(opts) do
if opts[:max_body] do
Keyword.put(opts, :with_body, true)
else
opts
end
end
end
diff --git a/test/pleroma/http/adapter_helper/hackney_test.exs b/test/pleroma/http/adapter_helper/hackney_test.exs
index cf60d994b..22fcc93d1 100644
--- a/test/pleroma/http/adapter_helper/hackney_test.exs
+++ b/test/pleroma/http/adapter_helper/hackney_test.exs
@@ -1,50 +1,63 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.HTTP.AdapterHelper.HackneyTest do
use ExUnit.Case
use Pleroma.Tests.Helpers
alias Pleroma.HTTP.AdapterHelper.Hackney
setup_all do
uri = URI.parse("http://domain.com")
{:ok, uri: uri}
end
describe "options/2" do
setup do: clear_config([:http, :adapter], a: 1, b: 2)
test "uses redirect-safe defaults", %{uri: uri} do
opts = Hackney.options([], uri)
assert opts[:follow_redirect] == false
assert opts[:force_redirect] == false
assert opts[:with_body] == true
end
test "does not allow connection options to enable Hackney redirects", %{uri: uri} do
opts = Hackney.options([follow_redirect: true, force_redirect: true], uri)
assert opts[:follow_redirect] == false
assert opts[:force_redirect] == false
end
test "add proxy and opts from config", %{uri: uri} do
opts = Hackney.options([proxy: "localhost:8123"], uri)
assert opts[:a] == 1
assert opts[:b] == 2
assert opts[:proxy] == "localhost:8123"
end
+ test "gives Hackney a string host for a proxy with an IP address", %{uri: uri} do
+ clear_config([:http, :proxy_url], {:socks5, {127, 0, 0, 1}, 9050})
+ assert Hackney.options([], uri)[:proxy] == {:socks5, ~c"127.0.0.1", 9050}
+
+ clear_config([:http, :proxy_url], {{10, 0, 0, 1}, 8123})
+ assert Hackney.options([], uri)[:proxy] == {~c"10.0.0.1", 8123}
+
+ clear_config([:http, :proxy_url], nil)
+
+ assert Hackney.options([proxy: {:socks5, {0, 0, 0, 0, 0, 0, 0, 1}, 9050}], uri)[:proxy] ==
+ {:socks5, ~c"::1", 9050}
+ end
+
test "respect connection opts and no proxy", %{uri: uri} do
opts = Hackney.options([a: 2, b: 1], uri)
assert opts[:a] == 2
assert opts[:b] == 1
refute Keyword.has_key?(opts, :proxy)
end
end
end

File Metadata

Mime Type
text/x-diff
Expires
Thu, Oct 29, 7:15 PM (1 d, 20 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1798550
Default Alt Text
(4 KB)

Event Timeline