Skip to content

Commit

Permalink
RateLimiterLive : cast des IPs en string (#3659)
Browse files Browse the repository at this point in the history
* RateLimiterLive : cast des IPs en string

* Add test

* bail_ip_from_jail: convert to charlist

* Add test: removing IP from jail

* PR comment
  • Loading branch information
AntoineAugusti committed Dec 12, 2023
1 parent 2f288fb commit 008e6c6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,13 @@ defmodule TransportWeb.Backoffice.RateLimiterLive do
{:noreply, socket}
end

defp ips_in_jail do
def ips_in_jail do
# See https://github.com/xward/phoenix_ddos/blob/master/lib/phoenix_ddos/core/jail.ex
# The dependency does not a method for this feature at the moment, we're calling an
# internal API.
# https://github.com/xward/phoenix_ddos/issues/2
{:ok, keys} = Cachex.keys(:phoenix_ddos_jail)
keys |> Enum.reject(&String.starts_with?(&1, "suspicious_"))
keys |> Enum.map(&to_string/1) |> Enum.reject(&String.starts_with?(&1, "suspicious_"))
end

defp env_value(env_value), do: System.get_env(env_value)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule TransportWeb.Backoffice.RateLimiterLiveTest do
use ExUnit.Case, async: true
use TransportWeb.LiveCase
import Phoenix.LiveViewTest
import TransportWeb.ConnCase, only: [setup_admin_in_session: 1]

@endpoint TransportWeb.Endpoint
Expand All @@ -22,4 +23,29 @@ defmodule TransportWeb.Backoffice.RateLimiterLiveTest do
|> get(@url)
|> html_response(200)
end

test "ips_in_jail" do
assert [] == TransportWeb.Backoffice.RateLimiterLive.ips_in_jail()
PhoenixDDoS.Jail.send(~c"108.128.238.17", {nil, %{jail_time: {1, :hour}}})
assert ["108.128.238.17"] == TransportWeb.Backoffice.RateLimiterLive.ips_in_jail()
end

test "bail out IP", %{conn: conn} do
ip = "108.128.238.17"
ip |> to_charlist() |> PhoenixDDoS.Jail.send({nil, %{jail_time: {1, :hour}}})
{:ok, view, html} = conn |> setup_admin_in_session() |> live(@url)
assert [ip] == TransportWeb.Backoffice.RateLimiterLive.ips_in_jail()

assert html =~ ip

assert view
|> element("button", "Retirer de la jail")
|> render_click()

assert [] == TransportWeb.Backoffice.RateLimiterLive.ips_in_jail()

send(view.pid, :update_data)

refute render(view) =~ ip
end
end

0 comments on commit 008e6c6

Please sign in to comment.