-
-
Save shahryarjb/9c082b4c2394f90f6ed68c6e89dec490 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule TrangellUsersService.Login.Plugs.BanIpChecker do | |
import Plug.Conn | |
import Phoenix.Controller | |
alias TrangellUsersService.Login.Security.Security | |
def init(_params) do | |
end | |
def call(conn, params) do | |
# IO.inspect conn.params["last_ip"] | |
# ip = to_string(:inet_parse.ntoa(conn.remote_ip)) | |
user_banned_ip = case Security.convert_redis_number(conn.params["last_ip"]) do | |
[nil] -> | |
assign(conn, :user_ip_checked, params) | |
n when n >= 4 -> | |
conn | |
|> put_status(403) | |
|> json(%{error_code: "403", error_msg: "your IP is banned, Please try again later."}) | |
|> halt() | |
_ -> | |
assign(conn, :user_ip_checked, params) | |
end | |
user_banned_ip | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule TrangellUsersService.Login.Plugs.BanIpChecker do | |
import Plug.Conn | |
import Phoenix.Controller | |
alias TrangellUsersService.Login.Security.Security | |
def init(_params) do | |
end | |
def call(%Plug.Conn{params: %{"last_ip" => last_ip}} = conn, params) do | |
# IO.inspect conn.params["last_ip"] | |
# ip = to_string(:inet_parse.ntoa(conn.remote_ip)) | |
# user_banned_ip = case Security.convert_redis_number(conn.params["last_ip"]) do | |
user_banned_ip = case Security.convert_redis_number(last_ip) do | |
[nil] -> | |
assign(conn, :user_ip_checked, params) | |
n when n >= 4 -> | |
conn | |
|> put_status(403) | |
|> json(%{error_code: "403", error_msg: "your IP is banned, Please try again later."}) | |
|> halt() | |
_ -> | |
assign(conn, :user_ip_checked, params) | |
end | |
user_banned_ip | |
end | |
def call(conn, _params) do | |
conn | |
|> put_status(403) | |
|> json(%{error_code: "403", error_msg: "You don't have an access."}) | |
|> halt() | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment