Sha256: 552dc5dab037e2c6917a973260d5f5165112c21b543862e74437eccf2eb05f3d

Contents?: true

Size: 1.79 KB

Versions: 4

Compression:

Stored size: 1.79 KB

Contents

# frozen_string_literal: true

module Mihari
  module Structs
    module Shodan
      class Location < Dry::Struct
        attribute :country_code, Types::String.optional
        attribute :country_name, Types::String.optional

        def self.from_dynamic!(d)
          d = Types::Hash[d]
          new(
            country_code: d["country_code"],
            country_name: d["country_name"]
          )
        end
      end

      class Match < Dry::Struct
        attribute :asn, Types::String.optional
        attribute :hostnames, Types.Array(Types::String)
        attribute :location, Location
        attribute :domains, Types.Array(Types::String)
        attribute :ip_str, Types::String
        attribute :metadata, Types::Hash

        def self.from_dynamic!(d)
          d = Types::Hash[d]

          # hostnames should be an array of string but sometimes Shodan returns a string
          # e.g. "hostnames": "set(['149.28.146.131.vultr.com', 'rebs.ga'])",
          # https://github.com/ninoseki/mihari/issues/424
          # so use an empty array if hostnames is a string
          hostnames = d.fetch("hostnames")
          hostnames = [] if hostnames.is_a?(String)

          new(
            asn: d["asn"],
            hostnames: hostnames,
            location: Location.from_dynamic!(d.fetch("location")),
            domains: d.fetch("domains"),
            ip_str: d.fetch("ip_str"),
            metadata: d
          )
        end
      end

      class Result < Dry::Struct
        attribute :matches, Types.Array(Match)
        attribute :total, Types::Int

        def self.from_dynamic!(d)
          d = Types::Hash[d]
          new(
            matches: d.fetch("matches", []).map { |x| Match.from_dynamic!(x) },
            total: d.fetch("total")
          )
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
mihari-4.4.1 lib/mihari/structs/shodan.rb
mihari-4.4.0 lib/mihari/structs/shodan.rb
mihari-4.3.0 lib/mihari/structs/shodan.rb
mihari-4.2.0 lib/mihari/structs/shodan.rb