lib/tasks/helpers/whois.rb
def _rdap_ip_lookup(ip_address)
    response = http_get_body "https://rdap.arin.net/registry/ip/#{ip_address}"

    begin
      json = JSON.parse(response)
    rescue JSON::ParserError => e
      return nil
    end

    return nil unless json

    # This is just terrible. I am ashamed.
    start_address = json["startAddress"]
    if response =~ /apnic/
      regex = Regexp.new(/rdap.apnic.net\/ip\/\d.\d.\d.\d\/(\d*)\",/)
      match = response.match(regex)
      cidr_length = match.captures.first.strip if match
    else # do something sane
      cidr_length = "#{json["handle"]}".split("/").last
    end

    if json["links"]
      description = "Queried via RDAP: #{json["links"].first["value"]}"
    end
    
    type = "#{json["type"]}"

    # return a standard set of info
    out = {
      "name" => "#{start_address}/#{cidr_length}",
      "start_address" => "#{start_address}",
      "end_address" => "#{json["endAddress"]}",
      "cidr" => "#{cidr_length}",
      "description" => "#{description}",
      "block_type" => "#{type}"
    }

  out
  end
# use RDAP to query an IP
use RDAP to query an IP
use RDAP to query an IP
def _rdap_ip_lookup(ip_address)
    response = http_get_body "https://rdap.arin.net/registry/ip/#{ip_address}"
    begin
      json = JSON.parse(response)
    rescue JSON::ParserError => e
      return nil
    end
    return nil unless json
    start_address = json["startAddress"]
    if response =~ /apnic/
      regex = Regexp.new(/rdap.apnic.net\/ip\/\d.\d.\d.\d\/(\d*)\",/)
      match = response.match(regex)
      cidr_length = match.captures.first.strip if match
    else 
      cidr_length = "#{json["handle"]}".split("/").last
    end
    if json["links"]
      description = "Queried via RDAP: #{json["links"].first["value"]}"
    end
    type = "#{json["type"]}"
    out = {
      "name" => "#{start_address}/#{cidr_length}",
      "start_address" => "#{start_address}",
      "end_address" => "#{json["endAddress"]}",
      "cidr" => "#{cidr_length}",
      "description" => "#{description}",
      "block_type" => "#{type}"
    }
  out
  end
def _rdap_ip_lookup ( ip_address ) response = http_get_body "https://rdap.arin.net/registry/ip/#{ip_address}" begin json = JSON . parse ( response ) rescue JSON :: ParserError => e return nil end return nil unless json start_address = json [ "startAddress" ] if response =~ / apnic / regex = Regexp . new ( / rdap.apnic.net \/ ip \/ \d . \d . \d . \d \/ ( \d *) \" , / ) match = response . match ( regex ) cidr_length = match . captures . first . strip if match else cidr_length = "#{json["handle"]}" . split ( "/" ) . last end if json [ "links" ] description = "Queried via RDAP: #{json["links"].first["value"]}" end type = "#{json["type"]}" out = { "name" => "#{start_address}/#{cidr_length}" , "start_address" => "#{start_address}" , "end_address" => "#{json["endAddress"]}" , "cidr" => "#{cidr_length}" , "description" => "#{description}" , "block_type" => "#{type}" } out end
use RDAP to query an IP
use RDAP to query an IP
# This is just terrible. I am ashamed. # do something sane # return a standard set of info
ip_address