Skip to content

Commit

Permalink
Merge branch 'feat/jail_accessors'
Browse files Browse the repository at this point in the history
  • Loading branch information
xward committed Dec 16, 2023
2 parents feb0746 + 855fb46 commit 4c95bd6
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# PhoenixDDoS changelog

## 1.1.14

- small refactors on Jail module, documentation and accessors (list banned ip functions, ...)

## 1.1.13

- remove router requirement, this was not a good idea !
Expand Down
4 changes: 4 additions & 0 deletions lib/phoenix_ddos/application/supervisor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ defmodule PhoenixDDoS.Supervisor do
%{
id: :phoenix_ddos_jail,
start: {Cachex, :start_link, [:phoenix_ddos_jail, []]}
},
%{
id: :phoenix_ddos_suspicious_ips,
start: {Cachex, :start_link, [:phoenix_ddos_suspicious_ips, []]}
}
]

Expand Down
42 changes: 35 additions & 7 deletions lib/phoenix_ddos/core/jail.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
defmodule PhoenixDDoS.Jail do
@moduledoc false
@moduledoc """
Ip got caught, go to jail ! Further request will be rejected and won't be included in rate limits
# Ip got caught, go to jail ! Skipping request count
iex> PhoenixDDoS.Jail.send('1.2.3.4', Enum.at(Application.get_env( :phoenix_ddos,:_prots),0))
:ok
iex> PhoenixDDoS.Jail.ips_in_jail()
["1.2.3.4"]
iex> PhoenixDDoS.Jail.in_jail?("1.2.3.4")
true
iex> PhoenixDDoS.Jail.in_jail?("1.2.3.5")
false
iex> PhoenixDDoS.Jail.bail_out("1.2.3.4")
:ok
iex> PhoenixDDoS.Jail.in_jail?("1.2.3.4")
false
"""

alias PhoenixDDoS.Monitoring.AlertSentry
alias PhoenixDDoS.Telemetry
Expand All @@ -10,9 +23,8 @@ defmodule PhoenixDDoS.Jail do
@sentry Application.compile_env(:phoenix_ddos, :on_jail_alert_to_sentry)

def send(ip, {_module, cfg} = prot) do
Cachex.put(:phoenix_ddos_jail, ip, true, ttl: Time.period_to_msec(cfg.jail_time))

{:ok, _} = Cachex.put(:phoenix_ddos_jail, "suspicious_#{ip}", ttl: :timer.hours(6))
{:ok, _} = Cachex.put(:phoenix_ddos_jail, ip, true, ttl: Time.period_to_msec(cfg.jail_time))
{:ok, _} = Cachex.put(:phoenix_ddos_suspicious_ips, ip, true, ttl: :timer.hours(6))

Telemetry.push([:jail, :new], %{}, %{ip: ip, protection: prot})

Expand All @@ -22,19 +34,35 @@ defmodule PhoenixDDoS.Jail do
Telemetry.push([:jail, :count], %{total: total}, %{})
end

@doc "list all ips in jail"
def ips_in_jail do
{:ok, keys} = Cachex.keys(:phoenix_ddos_jail)
keys |> Enum.map(&to_string/1)
end

@doc "check if an ip is in jail"
def in_jail?(ip) when is_binary(ip), do: ip |> String.to_charlist() |> in_jail?()

def in_jail?(ip) do
{:ok, exist} = Cachex.exists?(:phoenix_ddos_jail, ip)
exist
end

@doc false
def suspicious_ip?(ip) when is_binary(ip), do: ip |> String.to_charlist() |> in_jail?()

def suspicious_ip?(ip) do
{:ok, exist} = Cachex.exists?(:phoenix_ddos_jail, "suspicious_#{ip}")
{:ok, exist} = Cachex.exists?(:phoenix_ddos_suspicious_ips, ip)
exist
end

@doc "remove ip from jail"
# you have a powerful friend !
def bail_out(ip) when is_binary(ip), do: ip |> String.to_charlist() |> bail_out()

def bail_out(ip) do
Cachex.del(:phoenix_ddos_jail, ip)
Cachex.del(:phoenix_ddos_jail, "suspicious_#{ip}")
Cachex.del(:phoenix_ddos_suspicious_ips, ip)
:ok
end
end
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule PhoenixDDoS.MixProject do
use Mix.Project

@version "1.1.13"
@version "1.1.14"
@source_url "https://github.com/xward/phoenix_ddos"

def project do
Expand Down
8 changes: 8 additions & 0 deletions test/jail_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
defmodule PhoenixDDoS.JailTest do
@moduledoc false

use ExUnit.Case, async: true
# use Plug.Test

doctest PhoenixDDoS.Jail
end

0 comments on commit 4c95bd6

Please sign in to comment.