From 78eba94dfcf79d6d3e47211f54d38b7fd1f5f6b9 Mon Sep 17 00:00:00 2001 From: Chaska <166928710+chaskayote@users.noreply.github.com> Date: Sun, 12 May 2024 13:31:32 -0500 Subject: [PATCH] Adding Maxmind --- config/runtime.exs | 8 ++++++ docker-compose.yml | 1 + lib/philomena/application.ex | 14 ++++++++++ .../controllers/ip_profile_controller.ex | 6 ++++- .../templates/ip_profile/show.html.slime | 27 +++++++++++++++++++ mix.exs | 1 + mix.lock | 1 + 7 files changed, 57 insertions(+), 1 deletion(-) diff --git a/config/runtime.exs b/config/runtime.exs index 028acfdd..41e6ab19 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -42,6 +42,14 @@ config :philomena, camo_key: System.get_env("CAMO_KEY"), cdn_host: System.fetch_env!("CDN_HOST") +locus_key = + case System.fetch_env("LOCUS_KEY") do + {:ok, locus_key} -> locus_key + _ -> nil + end + +config :locus, license_key: locus_key + app_dir = System.get_env("APP_DIR", File.cwd!()) json_config = diff --git a/docker-compose.yml b/docker-compose.yml index 740d05d4..561564a3 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -45,6 +45,7 @@ services: - S3_BUCKET=philomena - AWS_ACCESS_KEY_ID=local-identity - AWS_SECRET_ACCESS_KEY=local-credential + - LOCUS_KEY= working_dir: /srv/philomena tty: true volumes: diff --git a/lib/philomena/application.ex b/lib/philomena/application.ex index 28d7d645..87a5cbde 100644 --- a/lib/philomena/application.ex +++ b/lib/philomena/application.ex @@ -6,6 +6,20 @@ defmodule Philomena.Application do use Application def start(_type, _args) do + locus_key = Application.get_env(:locus, :license_key) + + if locus_key && locus_key != "" do + :locus.start_loader(:city, {:maxmind, "GeoLite2-City"}, + update_period: 86_400 * 1_000, + error_retries: [backoff: 3600] + ) + + :locus.start_loader(:asn, {:maxmind, "GeoLite2-ASN"}, + update_period: 86_400 * 1_000, + error_retries: [backoff: 3600] + ) + end + # List all child processes to be supervised children = [ # Start the Ecto repository diff --git a/lib/philomena_web/controllers/ip_profile_controller.ex b/lib/philomena_web/controllers/ip_profile_controller.ex index c4909fae..8cf12703 100644 --- a/lib/philomena_web/controllers/ip_profile_controller.ex +++ b/lib/philomena_web/controllers/ip_profile_controller.ex @@ -9,6 +9,8 @@ defmodule PhilomenaWeb.IpProfileController do plug :authorize_ip def show(conn, %{"id" => ip}) do + geo = :locus.lookup(:city, ip) + asn = :locus.lookup(:asn, ip) {:ok, ip} = EctoNetwork.INET.cast(ip) user_ips = @@ -28,7 +30,9 @@ defmodule PhilomenaWeb.IpProfileController do title: "#{ip}'s IP profile", ip: ip, user_ips: user_ips, - subnet_bans: subnet_bans + subnet_bans: subnet_bans, + geo: geo, + asn: asn ) end diff --git a/lib/philomena_web/templates/ip_profile/show.html.slime b/lib/philomena_web/templates/ip_profile/show.html.slime index c752d9ff..a6d05460 100644 --- a/lib/philomena_web/templates/ip_profile/show.html.slime +++ b/lib/philomena_web/templates/ip_profile/show.html.slime @@ -9,6 +9,33 @@ ul = render PhilomenaWeb.BanView, "_bans.html", bans: @subnet_bans, conn: @conn +h4 Geolocation += case @geo do + - {:ok, data} -> + table.table + thead + tr + td Country + td City + td ASN + td Timezone + tbody + tr + td = data["country"]["names"]["en"] || data["registered_country"]["names"]["en"] || "Unavailable" + td = data["city"]["names"]["en"] || "Unavailable" + td + = if {:ok, asn} = @asn do + = asn["autonomous_system_organization"] + td = data["location"]["time_zone"] || "Unavailable" + - :not_found -> + p Could not geolocate IP address. + - {:error, {:invalid_address, _}} -> + p The provided IP address is invalid. + - {:error, :database_unknown} -> + p GeoIP is not configured on this instance. + - _ -> + p An error ocurred while geolocating the IP. + h2 Administration Options ul li = link "View tag changes", to: Routes.ip_profile_tag_change_path(@conn, :index, to_string(@ip)) diff --git a/mix.exs b/mix.exs index 595ac89d..4ef493b3 100644 --- a/mix.exs +++ b/mix.exs @@ -75,6 +75,7 @@ defmodule Philomena.MixProject do {:sweet_xml, "~> 0.7"}, {:inet_cidr, "~> 1.0"}, {:html_sanitize_ex, "~> 1.4"}, + {:locus, "~> 2.3"}, # SMTP {:tls_certificate_check, "~> 1.21"}, diff --git a/mix.lock b/mix.lock index f6767a09..098aebde 100644 --- a/mix.lock +++ b/mix.lock @@ -41,6 +41,7 @@ "idna": {:hex, :idna, "6.1.1", "8a63070e9f7d0c62eb9d9fcb360a7de382448200fbbd1b106cc96d3d8099df8d", [:rebar3], [{:unicode_util_compat, "~> 0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "92376eb7894412ed19ac475e4a86f7b413c1b9fbb5bd16dccd57934157944cea"}, "inet_cidr": {:hex, :inet_cidr, "1.0.8", "d26bb7bdbdf21ae401ead2092bf2bb4bf57fe44a62f5eaa5025280720ace8a40", [:mix], [], "hexpm", "d5b26da66603bb56c933c65214c72152f0de9a6ea53618b56d63302a68f6a90e"}, "jason": {:hex, :jason, "1.4.1", "af1504e35f629ddcdd6addb3513c3853991f694921b1b9368b0bd32beb9f1b63", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "fbb01ecdfd565b56261302f7e1fcc27c4fb8f32d56eab74db621fc154604a7a1"}, + "locus": {:hex, :locus, "2.3.8", "001ad869d1f0903eba0c87597614dec0581dee12933c7fff5348fa58ff32c053", [:rebar3], [{:tls_certificate_check, "~> 1.9", [hex: :tls_certificate_check, repo: "hexpm", optional: false]}], "hexpm", "dc288571ed176f86e5dcbc8fb02fe7cc07c4c958f97b758043f70b2fd3ca208c"}, "metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm", "69b09adddc4f74a40716ae54d140f93beb0fb8978d8636eaded0c31b6f099f16"}, "mime": {:hex, :mime, "1.6.0", "dabde576a497cef4bbdd60aceee8160e02a6c89250d6c0b29e56c0dfb00db3d2", [:mix], [], "hexpm", "31a1a8613f8321143dde1dafc36006a17d28d02bdfecb9e95a880fa7aabd19a7"}, "mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm", "f278585650aa581986264638ebf698f8bb19df297f66ad91b18910dfc6e19323"},