View Source Paraxial (Paraxial v2.8.0)

Paraxial.io functions for use by users.

Link to this section Summary

Functions

Ban an IP address, both locally and on the Paraxial.io backend.

Given an email, bulk action (such as :email), and count, return true or fase.any()

Rate limiter that will also ban the relevant IP address via Paraxial.io.

Link to this section Functions

Link to this function

ban_ip(ip, length, message)

View Source

Ban an IP address, both locally and on the Paraxial.io backend.

Returns the result of an HTTP request, for example:

- returned on successful ban

- returned if you attempt to ban an IP that is already banned

If you are using this function in a blocking content, call with Task.start, https://hexdocs.pm/elixir/1.12/Task.html#start/1

  • ip - Format should match conn.remote_ip, which is a list
  • length - Valid options are :hour, :day, :week, :infinity
  • message - A text comment, for example "Submitted honeypot HTML form"
Link to this function

bulk_allowed?(email, bulk_action, count)

View Source

Given an email, bulk action (such as :email), and count, return true or fase.any()

Example config:

config :paraxial,
  # ...
  bulk: %{email: %{trusted: 100, untrusted: 3}},
  trusted_domains: MapSet.new(["paraxial.io", "blackcatprojects.xyz"])

examples

Examples

iex> Paraxial.bulk_allowed?("mike@blackcatprojects.xyz", :email, 3)
true

iex> Paraxial.bulk_allowed?("mike@blackcatprojects.xyz", :email, 100)
true

iex> Paraxial.bulk_allowed?("mike@test.xyz", :email, 4)
false
Link to this function

check_rate(key, seconds, count, ban_length, ip, msg)

View Source

Rate limiter that will also ban the relevant IP address via Paraxial.io.

Returns {:allow, n} or {:deny, n}

  • key: String to rate limit on, ex: "login-96.56.162.210", "send-email-michael@paraxial.io"
  • seconds: Length of the rate limit rule
  • count: Number of times the action can be performed in the seconds time limit
  • ban_length: Valid strings are "alert_only", "hour", "day", "week", "infinity"
  • ip: Tuple, you can pass conn.remote_ip directly here
  • msg: Human-readable string, ex: "> 5 requests in 10 seconds to blackcatprojects.xyz/users/log_in from #{ip}"
ip_string = conn.remote_ip |> :inet.ntoa() |> to_string()
key = "user-register-get-#{ip_string}"
seconds = 5
count = 5
ban_length = "hour"
ip = conn.remote_ip
msg = "> 5 requests in 10 seconds to #{conn.host}/users/log_in from #{ip_string}"

case Paraxial.check_rate(key, seconds, count, ban_length, ip, msg) do
  {:allow, _} ->
    # Allow code here
  {:deny, _} ->
    conn
    |> put_resp_content_type("text/html")
    |> send_resp(401, "Banned")
end
Link to this function

email_trusted?(email, trusted_domains)

View Source