diff --git a/ui/lib/ui_web/controllers/page_controller.ex b/ui/lib/ui_web/controllers/page_controller.ex index 7bb9da1..ef56340 100644 --- a/ui/lib/ui_web/controllers/page_controller.ex +++ b/ui/lib/ui_web/controllers/page_controller.ex @@ -11,7 +11,10 @@ defmodule UiWeb.PageController do end def morse(conn, _params) do - ip = conn |> Plug.Conn.get_req_header("x-real-ip") |> hd() + ip = case Plug.Conn.get_req_header(conn, "x-real-ip") do + [h|_tl] -> h + _ -> "0.0.0.0" + end LiveView.Controller.live_render(conn, UiWeb.MorseLive, session: %{ip: ip}) end end diff --git a/ui/lib/ui_web/live/morse_live.ex b/ui/lib/ui_web/live/morse_live.ex index 5831da5..34f9204 100644 --- a/ui/lib/ui_web/live/morse_live.ex +++ b/ui/lib/ui_web/live/morse_live.ex @@ -13,8 +13,8 @@ defmodule UiWeb.MorseLive do end def handle_event("toggle_morse", _value, %{assigns: %{ip: ip}} = socket) do - if not Morse.Server.in_progress?() and ip != "127.0.0.1" do - spawn fn -> Ui.TelegramBot.message("#{ip} pressed the button!") end + if not Morse.Server.in_progress?() and ip_send_message?(ip) do + spawn(fn -> Ui.TelegramBot.message("#{ip} pressed the button!") end) end Morse.Server.toggle_morse() @@ -38,4 +38,9 @@ defmodule UiWeb.MorseLive do ip: ip ] end + + defp ip_send_message?(ip) do + not (String.starts_with?(ip, "192.168.") or String.starts_with?(ip, "10.") + or ip == "127.0.0.1" or ip == "localhost" or ip == "0.0.0.0") + end end