Sha256: 71b9185136c03961c9857632136b6bf30c90247e874da0c9c2192a6f4cec0c74

Contents?: true

Size: 1.96 KB

Versions: 19

Compression:

Stored size: 1.96 KB

Contents

# frozen_string_literal: true

module Mihari
  module Clients
    #
    # Shodan API client
    #
    class Shodan < Base
      PAGE_SIZE = 100

      # @return [String]
      attr_reader :api_key

      #
      # @param [String] base_url
      # @param [String, nil] api_key
      # @param [Hash] headers
      # @param [Integer] pagination_interval
      # @param [Integer, nil] timeout
      #
      def initialize(
        base_url = "https://api.shodan.io",
        api_key:,
        headers: {},
        pagination_interval: Mihari.config.pagination_interval,
        timeout: nil
      )
        raise(ArgumentError, "api_key is required") unless api_key

        super(base_url, headers: headers, pagination_interval: pagination_interval, timeout: timeout)

        @api_key = api_key
      end

      #
      # @param [String] query
      # @param [Integer] page
      # @param [Boolean] minify
      #
      # @return [Mihari::Structs::Shodan::Result]
      #
      def search(query, page: 1, minify: true)
        params = {
          query: query,
          page: page,
          minify: minify,
          key: api_key
        }
        Structs::Shodan::Response.from_dynamic! get_json("/shodan/host/search", params: params)
      end

      #
      # @param [String] query
      # @param [Boolean] minify
      # @param [Integer] pagination_limit
      #
      # @return [Enumerable<Mihari::Structs::Shodan::Response>]
      #
      def search_with_pagination(query, minify: true, pagination_limit: Mihari.config.pagination_limit)
        Enumerator.new do |y|
          (1..pagination_limit).each do |page|
            res = search(query, page: page, minify: minify)

            y.yield res

            break if res.total <= page * PAGE_SIZE

            sleep_pagination_interval
          rescue JSON::ParserError
            # ignore JSON::ParserError
            # ref. https://github.com/ninoseki/mihari/issues/197
            next
          end
        end
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
mihari-7.3.2 lib/mihari/clients/shodan.rb
mihari-7.3.1 lib/mihari/clients/shodan.rb
mihari-7.3.0 lib/mihari/clients/shodan.rb
mihari-7.2.0 lib/mihari/clients/shodan.rb
mihari-7.1.3 lib/mihari/clients/shodan.rb
mihari-7.1.2 lib/mihari/clients/shodan.rb
mihari-7.1.1 lib/mihari/clients/shodan.rb
mihari-7.1.0 lib/mihari/clients/shodan.rb
mihari-7.0.5 lib/mihari/clients/shodan.rb
mihari-7.0.4 lib/mihari/clients/shodan.rb
mihari-7.0.3 lib/mihari/clients/shodan.rb
mihari-7.0.2 lib/mihari/clients/shodan.rb
mihari-7.0.1 lib/mihari/clients/shodan.rb
mihari-7.0.0 lib/mihari/clients/shodan.rb
mihari-6.3.0 lib/mihari/clients/shodan.rb
mihari-6.2.0 lib/mihari/clients/shodan.rb
mihari-6.1.0 lib/mihari/clients/shodan.rb
mihari-6.0.0 lib/mihari/clients/shodan.rb
mihari-5.7.2 lib/mihari/clients/shodan.rb