parsing proxy url setting
This commit is contained in:
parent
f9d01068cf
commit
583cee4607
|
@ -145,6 +145,33 @@ defp do_convert(entity) when is_map(entity) do
|
||||||
for {k, v} <- entity, into: %{}, do: {do_convert(k), do_convert(v)}
|
for {k, v} <- entity, into: %{}, do: {do_convert(k), do_convert(v)}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp do_convert({:proxy_url, {type, :localhost, port}}) do
|
||||||
|
%{"tuple" => [":proxy_url", %{"tuple" => [do_convert(type), "localhost", port]}]}
|
||||||
|
end
|
||||||
|
|
||||||
|
defp do_convert({:proxy_url, {type, host, port}}) when is_tuple(host) do
|
||||||
|
ip =
|
||||||
|
host
|
||||||
|
|> :inet_parse.ntoa()
|
||||||
|
|> to_string()
|
||||||
|
|
||||||
|
%{
|
||||||
|
"tuple" => [
|
||||||
|
":proxy_url",
|
||||||
|
%{"tuple" => [do_convert(type), ip, port]}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
defp do_convert({:proxy_url, {type, host, port}}) do
|
||||||
|
%{
|
||||||
|
"tuple" => [
|
||||||
|
":proxy_url",
|
||||||
|
%{"tuple" => [do_convert(type), to_string(host), port]}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
defp do_convert({:dispatch, [entity]}), do: %{"tuple" => [":dispatch", [inspect(entity)]]}
|
defp do_convert({:dispatch, [entity]}), do: %{"tuple" => [":dispatch", [inspect(entity)]]}
|
||||||
# TODO: will become useless after removing hackney
|
# TODO: will become useless after removing hackney
|
||||||
defp do_convert({:partial_chain, entity}), do: %{"tuple" => [":partial_chain", inspect(entity)]}
|
defp do_convert({:partial_chain, entity}), do: %{"tuple" => [":partial_chain", inspect(entity)]}
|
||||||
|
@ -173,6 +200,10 @@ def to_binary(entity), do: :erlang.term_to_binary(entity)
|
||||||
|
|
||||||
defp do_transform(%Regex{} = entity), do: entity
|
defp do_transform(%Regex{} = entity), do: entity
|
||||||
|
|
||||||
|
defp do_transform(%{"tuple" => [":proxy_url", %{"tuple" => [type, host, port]}]}) do
|
||||||
|
{:proxy_url, {do_transform_string(type), parse_host(host), port}}
|
||||||
|
end
|
||||||
|
|
||||||
defp do_transform(%{"tuple" => [":dispatch", [entity]]}) do
|
defp do_transform(%{"tuple" => [":dispatch", [entity]]}) do
|
||||||
{dispatch_settings, []} = do_eval(entity)
|
{dispatch_settings, []} = do_eval(entity)
|
||||||
{:dispatch, [dispatch_settings]}
|
{:dispatch, [dispatch_settings]}
|
||||||
|
@ -204,6 +235,20 @@ defp do_transform(entity) when is_binary(entity) do
|
||||||
|
|
||||||
defp do_transform(entity), do: entity
|
defp do_transform(entity), do: entity
|
||||||
|
|
||||||
|
defp parse_host("localhost"), do: :localhost
|
||||||
|
|
||||||
|
defp parse_host(host) do
|
||||||
|
charlist = to_charlist(host)
|
||||||
|
|
||||||
|
case :inet.parse_address(charlist) do
|
||||||
|
{:error, :einval} ->
|
||||||
|
charlist
|
||||||
|
|
||||||
|
{:ok, ip} ->
|
||||||
|
ip
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
@delimiters ["/", "|", "\"", "'", {"(", ")"}, {"[", "]"}, {"{", "}"}, {"<", ">"}]
|
@delimiters ["/", "|", "\"", "'", {"(", ")"}, {"[", "]"}, {"{", "}"}, {"<", ">"}]
|
||||||
|
|
||||||
defp find_valid_delimiter([], _string, _),
|
defp find_valid_delimiter([], _string, _),
|
||||||
|
|
|
@ -1997,6 +1997,8 @@ test "POST /api/pleroma/admin/config error" do
|
||||||
setup %{conn: conn} do
|
setup %{conn: conn} do
|
||||||
admin = insert(:user, is_admin: true)
|
admin = insert(:user, is_admin: true)
|
||||||
|
|
||||||
|
http = Application.get_env(:pleroma, :http)
|
||||||
|
|
||||||
on_exit(fn ->
|
on_exit(fn ->
|
||||||
Application.delete_env(:pleroma, :key1)
|
Application.delete_env(:pleroma, :key1)
|
||||||
Application.delete_env(:pleroma, :key2)
|
Application.delete_env(:pleroma, :key2)
|
||||||
|
@ -2006,6 +2008,7 @@ test "POST /api/pleroma/admin/config error" do
|
||||||
Application.delete_env(:pleroma, :keyaa2)
|
Application.delete_env(:pleroma, :keyaa2)
|
||||||
Application.delete_env(:pleroma, Pleroma.Web.Endpoint.NotReal)
|
Application.delete_env(:pleroma, Pleroma.Web.Endpoint.NotReal)
|
||||||
Application.delete_env(:pleroma, Pleroma.Captcha.NotReal)
|
Application.delete_env(:pleroma, Pleroma.Captcha.NotReal)
|
||||||
|
Application.put_env(:pleroma, :http, http)
|
||||||
Application.put_env(:tesla, :adapter, Tesla.Mock)
|
Application.put_env(:tesla, :adapter, Tesla.Mock)
|
||||||
:ok = File.rm("config/test.exported_from_db.secret.exs")
|
:ok = File.rm("config/test.exported_from_db.secret.exs")
|
||||||
end)
|
end)
|
||||||
|
@ -2656,8 +2659,7 @@ test "delete part of settings by atom subkeys", %{conn: conn} do
|
||||||
]
|
]
|
||||||
})
|
})
|
||||||
|
|
||||||
assert(
|
assert json_response(conn, 200) == %{
|
||||||
json_response(conn, 200) == %{
|
|
||||||
"configs" => [
|
"configs" => [
|
||||||
%{
|
%{
|
||||||
"group" => ":pleroma",
|
"group" => ":pleroma",
|
||||||
|
@ -2666,7 +2668,93 @@ test "delete part of settings by atom subkeys", %{conn: conn} do
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
)
|
end
|
||||||
|
|
||||||
|
test "proxy tuple localhost", %{conn: conn} do
|
||||||
|
conn =
|
||||||
|
post(conn, "/api/pleroma/admin/config", %{
|
||||||
|
configs: [
|
||||||
|
%{
|
||||||
|
group: ":pleroma",
|
||||||
|
key: ":http",
|
||||||
|
value: [
|
||||||
|
%{"tuple" => [":proxy_url", %{"tuple" => [":socks5", "localhost", 1234]}]},
|
||||||
|
%{"tuple" => [":send_user_agent", false]}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
})
|
||||||
|
|
||||||
|
assert json_response(conn, 200) == %{
|
||||||
|
"configs" => [
|
||||||
|
%{
|
||||||
|
"group" => ":pleroma",
|
||||||
|
"key" => ":http",
|
||||||
|
"value" => [
|
||||||
|
%{"tuple" => [":proxy_url", %{"tuple" => [":socks5", "localhost", 1234]}]},
|
||||||
|
%{"tuple" => [":send_user_agent", false]}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
test "proxy tuple domain", %{conn: conn} do
|
||||||
|
conn =
|
||||||
|
post(conn, "/api/pleroma/admin/config", %{
|
||||||
|
configs: [
|
||||||
|
%{
|
||||||
|
group: ":pleroma",
|
||||||
|
key: ":http",
|
||||||
|
value: [
|
||||||
|
%{"tuple" => [":proxy_url", %{"tuple" => [":socks5", "domain.com", 1234]}]},
|
||||||
|
%{"tuple" => [":send_user_agent", false]}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
})
|
||||||
|
|
||||||
|
assert json_response(conn, 200) == %{
|
||||||
|
"configs" => [
|
||||||
|
%{
|
||||||
|
"group" => ":pleroma",
|
||||||
|
"key" => ":http",
|
||||||
|
"value" => [
|
||||||
|
%{"tuple" => [":proxy_url", %{"tuple" => [":socks5", "domain.com", 1234]}]},
|
||||||
|
%{"tuple" => [":send_user_agent", false]}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
test "proxy tuple ip", %{conn: conn} do
|
||||||
|
conn =
|
||||||
|
post(conn, "/api/pleroma/admin/config", %{
|
||||||
|
configs: [
|
||||||
|
%{
|
||||||
|
group: ":pleroma",
|
||||||
|
key: ":http",
|
||||||
|
value: [
|
||||||
|
%{"tuple" => [":proxy_url", %{"tuple" => [":socks5", "127.0.0.1", 1234]}]},
|
||||||
|
%{"tuple" => [":send_user_agent", false]}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
})
|
||||||
|
|
||||||
|
assert json_response(conn, 200) == %{
|
||||||
|
"configs" => [
|
||||||
|
%{
|
||||||
|
"group" => ":pleroma",
|
||||||
|
"key" => ":http",
|
||||||
|
"value" => [
|
||||||
|
%{"tuple" => [":proxy_url", %{"tuple" => [":socks5", "127.0.0.1", 1234]}]},
|
||||||
|
%{"tuple" => [":send_user_agent", false]}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -217,6 +217,36 @@ test "2 child tuple" do
|
||||||
assert Config.from_binary(binary) == {"v1", :v2}
|
assert Config.from_binary(binary) == {"v1", :v2}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "proxy tuple with localhost" do
|
||||||
|
binary =
|
||||||
|
Config.transform(%{
|
||||||
|
"tuple" => [":proxy_url", %{"tuple" => [":socks5", "localhost", 1234]}]
|
||||||
|
})
|
||||||
|
|
||||||
|
assert binary == :erlang.term_to_binary({:proxy_url, {:socks5, :localhost, 1234}})
|
||||||
|
assert Config.from_binary(binary) == {:proxy_url, {:socks5, :localhost, 1234}}
|
||||||
|
end
|
||||||
|
|
||||||
|
test "proxy tuple with domain" do
|
||||||
|
binary =
|
||||||
|
Config.transform(%{
|
||||||
|
"tuple" => [":proxy_url", %{"tuple" => [":socks5", "domain.com", 1234]}]
|
||||||
|
})
|
||||||
|
|
||||||
|
assert binary == :erlang.term_to_binary({:proxy_url, {:socks5, 'domain.com', 1234}})
|
||||||
|
assert Config.from_binary(binary) == {:proxy_url, {:socks5, 'domain.com', 1234}}
|
||||||
|
end
|
||||||
|
|
||||||
|
test "proxy tuple with ip" do
|
||||||
|
binary =
|
||||||
|
Config.transform(%{
|
||||||
|
"tuple" => [":proxy_url", %{"tuple" => [":socks5", "127.0.0.1", 1234]}]
|
||||||
|
})
|
||||||
|
|
||||||
|
assert binary == :erlang.term_to_binary({:proxy_url, {:socks5, {127, 0, 0, 1}, 1234}})
|
||||||
|
assert Config.from_binary(binary) == {:proxy_url, {:socks5, {127, 0, 0, 1}, 1234}}
|
||||||
|
end
|
||||||
|
|
||||||
test "tuple with n childs" do
|
test "tuple with n childs" do
|
||||||
binary =
|
binary =
|
||||||
Config.transform(%{
|
Config.transform(%{
|
||||||
|
|
Loading…
Reference in New Issue