From f23df47c095c8f2dbacdaccd4931453760bc02ed Mon Sep 17 00:00:00 2001 From: evazion Date: Sun, 22 Mar 2020 23:28:40 -0500 Subject: [PATCH] ip addresses: add ip address show page. * Add IP address show page at /ip_addresses/1.2.3.4. * Add "Details", "Ban IP", "Ban User" options to the /ip_addresses listing. --- app/controllers/ip_addresses_controller.rb | 6 ++ app/helpers/ip_addresses_helper.rb | 11 +++ .../src/styles/specific/ip_addresses.scss | 7 ++ app/views/ip_addresses/_index.html.erb | 12 +-- .../ip_addresses/_index_by_ip_addr.html.erb | 2 +- app/views/ip_addresses/_info.html.erb | 75 +++++++++++++++++++ app/views/ip_addresses/index.html.erb | 6 ++ app/views/ip_addresses/show.html.erb | 7 ++ app/views/users/_statistics.html.erb | 5 +- config/danbooru_default_config.rb | 6 ++ config/routes.rb | 2 +- 11 files changed, 130 insertions(+), 9 deletions(-) create mode 100644 app/helpers/ip_addresses_helper.rb create mode 100644 app/javascript/src/styles/specific/ip_addresses.scss create mode 100644 app/views/ip_addresses/_info.html.erb create mode 100644 app/views/ip_addresses/show.html.erb diff --git a/app/controllers/ip_addresses_controller.rb b/app/controllers/ip_addresses_controller.rb index 357b32b58..98c070278 100644 --- a/app/controllers/ip_addresses_controller.rb +++ b/app/controllers/ip_addresses_controller.rb @@ -14,4 +14,10 @@ class IpAddressesController < ApplicationController respond_with(@ip_addresses) end + + def show + @ip_address = IpAddress.new(ip_addr: params[:id]) + @ip_info = @ip_address.lookup.info + respond_with(@ip_info) + end end diff --git a/app/helpers/ip_addresses_helper.rb b/app/helpers/ip_addresses_helper.rb new file mode 100644 index 000000000..61a833f22 --- /dev/null +++ b/app/helpers/ip_addresses_helper.rb @@ -0,0 +1,11 @@ +module IpAddressesHelper + def embedded_google_map(location, width, height, api_key: Danbooru.config.google_maps_api_key) + tag.iframe( + width: width, + height: height, + frameborder: 0, + allowfullscreen: true, + src: "https://www.google.com/maps/embed/v1/search?q=#{location}&key=#{api_key}" + ) + end +end diff --git a/app/javascript/src/styles/specific/ip_addresses.scss b/app/javascript/src/styles/specific/ip_addresses.scss new file mode 100644 index 000000000..5bb7c0ca0 --- /dev/null +++ b/app/javascript/src/styles/specific/ip_addresses.scss @@ -0,0 +1,7 @@ +#c-ip-addresses { + .ip-address-details, .ip-address-map { + display: inline-block; + margin-right: 2em; + vertical-align: top; + } +} diff --git a/app/views/ip_addresses/_index.html.erb b/app/views/ip_addresses/_index.html.erb index 2466a3885..cb7179777 100644 --- a/app/views/ip_addresses/_index.html.erb +++ b/app/views/ip_addresses/_index.html.erb @@ -1,14 +1,14 @@ <%= table_for @ip_addresses, {class: "striped autofit"} do |t| %> + <% t.column "IP Address" do |ip| %> + <%= link_to ip.ip_addr, ip_addresses_path(search: { ip_addr: ip.ip_addr }) %> + <%= link_to "»", ip_addresses_path(search: { ip_addr: ip.ip_addr, group_by: "user" }) %> + <% end %> <% t.column "Source" do |ip| %> <%= link_to ip.model_type.underscore.humanize, ip_addresses_path(search: { model_type: ip.model_type }) %> <% end %> <% t.column "ID" do |ip| %> <%= link_to "##{ip.model_id}", ip.model %> <% end %> - <% t.column "IP Address" do |ip| %> - <%= link_to ip.ip_addr, ip_addresses_path(search: { ip_addr: ip.ip_addr }) %> - <%= link_to "»", ip_addresses_path(search: { ip_addr: ip.ip_addr, group_by: "user" }) %> - <% end %> <% t.column "User" do |ip| %> <%= link_to_user ip.user %> <%= link_to "»", ip_addresses_path(search: { user_id: ip.user_id, group_by: "ip_addr" }) %> @@ -17,6 +17,8 @@ <%= time_ago_in_words_tagged ip.created_at %> <% end %> <% t.column column: "ip-info" do |ip| %> - <%= link_to "IP info", "https://ipinfo.io/#{ip.ip_addr}" %> + <%= link_to "Details", ip_address_path(ip.ip_addr.to_s) %> + | <%= link_to "Ban IP", new_ip_ban_path(ip_ban: { ip_addr: ip.ip_addr.to_s }) %> + | <%= link_to "Ban User", new_ban_path(ban: { user_name: ip.user.name, duration: 999999 }) %> <% end %> <% end %> diff --git a/app/views/ip_addresses/_index_by_ip_addr.html.erb b/app/views/ip_addresses/_index_by_ip_addr.html.erb index a51822654..5c827a60d 100644 --- a/app/views/ip_addresses/_index_by_ip_addr.html.erb +++ b/app/views/ip_addresses/_index_by_ip_addr.html.erb @@ -11,6 +11,6 @@ <%= link_to ip.count_all, ip_addresses_path(search: { ip_addr: ip.to_s }) %> <% end %> <% t.column column: "ip-info" do |ip| %> - <%= link_to "IP info", "https://ipinfo.io/#{ip.ip_addr}" %> + <%= link_to "Details", ip_address_path(ip.ip_addr.to_s) %> <% end %> <% end %> diff --git a/app/views/ip_addresses/_info.html.erb b/app/views/ip_addresses/_info.html.erb new file mode 100644 index 000000000..adf31775f --- /dev/null +++ b/app/views/ip_addresses/_info.html.erb @@ -0,0 +1,75 @@ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + <% if ip_info.dig(:carrier, :name).present? %> + + + + + <% end %> + + + + + + + + +
Location + <% if ip_info.dig(:location, :city) %> + <%= ip_info.dig(:location, :city) %>, + <% end %> + <% if ip_info.dig(:location, :region, :name).present? %> + <%= ip_info.dig(:location, :region, :name) %>, + <% end %> + <%= ip_info.dig(:location, :country, :name) %> +
Network + <%= link_to ip_info.dig(:connection, :route), ip_addresses_path(search: { ip_addr: ip_info.dig(:connection, :route) }) %> + (<%= link_to "info", "https://ipinfo.io/AS#{ip_info.dig(:connection, :asn)}/#{ip_info.dig(:connection, :route)}" %>) +
Proxy<%= ip_address.lookup.is_proxy? ? "yes" : "no" %>
IP Banned + <% if IpBan.is_banned?(ip_address.ip_addr.to_s) %> + yes (<%= link_to "info", ip_bans_path(search: { ip_addr: ip_address.to_s }) %>) + <% else %> + no + <% end %> +
Type<%= ip_info.dig(:connection, :type) %>
ASN + <%= link_to "AS#{ip_info.dig(:connection, :asn)}", "https://ipinfo.io/AS#{ip_info.dig(:connection, :asn)}" %> +
Organization + <%= ip_info.dig(:connection, :organization) %> +
Mobile Carrier<%= ip_info.dig(:carrier, :name) %>
Website<%= external_link_to("http://#{ip_info.dig(:connection, :domain)}") %>
Details +
+
<%= JSON.pretty_generate(ip_info) %>
+
+
+
+ +
+ <%= embedded_google_map("#{ip_info.dig(:location, :latitude)},#{ip_info.dig(:location, :longitude)}", 300, 200) %> +
diff --git a/app/views/ip_addresses/index.html.erb b/app/views/ip_addresses/index.html.erb index e9a64b4b5..8d0c7261b 100644 --- a/app/views/ip_addresses/index.html.erb +++ b/app/views/ip_addresses/index.html.erb @@ -1,5 +1,11 @@
+ <% if @ip_address %> +

IP Address: <%= @ip_address.ip_addr %>

+ <% else %> +

IP Addresses

+ <% end %> + <%= search_form_for(ip_addresses_path) do |f| %> <%= f.input :user_id, label: "User ID", input_html: { value: params[:search][:user_id] }, hint: "Separate with spaces" %> <%= f.input :user_name, label: "User Name", input_html: { "data-autocomplete": "user", value: params[:search][:user_name] }, hint: "Use * for wildcard" %> diff --git a/app/views/ip_addresses/show.html.erb b/app/views/ip_addresses/show.html.erb new file mode 100644 index 000000000..a1776945d --- /dev/null +++ b/app/views/ip_addresses/show.html.erb @@ -0,0 +1,7 @@ +
+
+

IP Address: <%= @ip_address.ip_addr %>

+ + <%= render "info", ip_address: @ip_address, ip_info: @ip_info %> +
+
diff --git a/app/views/users/_statistics.html.erb b/app/views/users/_statistics.html.erb index 8a96ba485..618703e8f 100644 --- a/app/views/users/_statistics.html.erb +++ b/app/views/users/_statistics.html.erb @@ -16,8 +16,9 @@ <% if user.last_ip_addr.present? %> <%= link_to user.last_ip_addr, ip_addresses_path(search: { ip_addr: user.last_ip_addr }) %> - (<%= link_to "users", ip_addresses_path(search: { ip_addr: user.last_ip_addr, group_by: "user" }) %>, - <%= link_to "IPs", ip_addresses_path(search: { user_id: user.id, group_by: "ip_addr" }) %>) + (<%= link_to "info", ip_address_path(user.last_ip_addr) %>, + <%= link_to "users", ip_addresses_path(search: { ip_addr: user.last_ip_addr, group_by: "user" }) %>, + <%= link_to "IPs", ip_addresses_path(search: { user_id: user.id, group_by: "ip_addr" }) %>) <% else %> unknown (<%= link_to "IPs", ip_addresses_path(search: { user_id: user.id, group_by: "ip_addr" }) %>) diff --git a/config/danbooru_default_config.rb b/config/danbooru_default_config.rb index 16f3a441d..a9283db02 100644 --- a/config/danbooru_default_config.rb +++ b/config/danbooru_default_config.rb @@ -519,6 +519,12 @@ module Danbooru nil end + # API key for Google Maps. Used for embedding maps on IP address lookup pages. + # Generate at https://console.developers.google.com/apis/credentials + def google_maps_api_key + nil + end + # Cloudflare API token. Used to purge URLs from Cloudflare's cache when a # post is replaced. The token must have 'zone.cache_purge' permissions. # https://support.cloudflare.com/hc/en-us/articles/200167836-Managing-API-Tokens-and-Keys diff --git a/config/routes.rb b/config/routes.rb index a99d0a43f..012c737f6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -130,7 +130,7 @@ Rails.application.routes.draw do end resources :forum_topic_visits, only: [:index] resources :ip_bans - resources :ip_addresses, only: [:index] + resources :ip_addresses, only: [:show, :index], id: /.+?(?=\.json|\.xml|\.html)|.+/ resource :iqdb_queries, :only => [:show, :create] do collection do get :preview