GamendWeb.Plugs.IpBan (gamend_web v1.0.1215)

Copy Markdown View Source

Plug that blocks requests from banned IP addresses.

The hot-path check reads a dedicated ETS table (:ip_bans). Bans are also persisted via Gamend.IpBans, so they survive restarts, and broadcast on PubSub so every app instance applies them (see GamendWeb.IpBanSync, which loads persisted bans at boot and mirrors remote changes into ETS).

Banning / unbanning an IP

GamendWeb.Plugs.IpBan.ban("1.2.3.4")                    # permanent
GamendWeb.Plugs.IpBan.ban("1.2.3.4", :timer.hours(24))  # 24h ban
GamendWeb.Plugs.IpBan.unban("1.2.3.4")
GamendWeb.Plugs.IpBan.banned?("1.2.3.4")
GamendWeb.Plugs.IpBan.list_bans()

This plug runs early in the endpoint pipeline, after RealIp extracts the true client address.

Summary

Functions

Apply a ban/unban event that originated on another app instance.

Ban an IP address. Pass ttl_ms for a temporary ban (milliseconds) or :infinity (default) for a permanent ban.

Check if an IP is currently banned.

Ensure the ETS tables exist (called once at app startup).

List all currently active bans as [{ip, expires_at}].

Return recent ban/unban log entries as a list of maps, newest first.

Load persisted bans from the database into ETS and drop expired rows.

PubSub topic on which ban/unban events are broadcast.

Remove a ban for the given IP (locally, persisted, and cluster-wide).

Functions

apply_remote(atom, ip, expires_at_utc)

Apply a ban/unban event that originated on another app instance.

Only touches ETS — the originating instance already persisted the change.

ban(ip, ttl_ms \\ :infinity)

Ban an IP address. Pass ttl_ms for a temporary ban (milliseconds) or :infinity (default) for a permanent ban.

The ban takes effect locally right away, is persisted to the database, and is broadcast to the other app instances.

banned?(ip)

Check if an IP is currently banned.

init_table()

Ensure the ETS tables exist (called once at app startup).

list_bans()

List all currently active bans as [{ip, expires_at}].

list_log()

Return recent ban/unban log entries as a list of maps, newest first.

Each entry: %{action: :ban | :unban, ip: String.t(), ttl: term(), at: DateTime.t()}

load_persisted()

Load persisted bans from the database into ETS and drop expired rows.

Called at boot by GamendWeb.IpBanSync.

topic()

PubSub topic on which ban/unban events are broadcast.

unban(ip)

Remove a ban for the given IP (locally, persisted, and cluster-wide).