lib/mihari/analyzers/shodan.rb in mihari-5.4.2 vs lib/mihari/analyzers/shodan.rb in mihari-5.4.3

- old
+ new

@@ -16,58 +16,26 @@ @api_key = api_key || Mihari.config.shodan_api_key end def artifacts - results = search - return [] if results.empty? - - results.map(&:to_artifacts).flatten.uniq(&:data) + client.search_with_pagination( + query, + pagination_limit: pagination_limit + ).map(&:artifacts).flatten.uniq(&:data) end def configuration_keys %w[shodan_api_key] end private - PAGE_SIZE = 100 - - def client - @client ||= Clients::Shodan.new(api_key: api_key) - end - # - # Search with pagination + # @return [Clients::Shodan] # - # @param [Integer] page - # - # @return [Structs::Shodan::Result] - # - def search_with_page(page: 1) - client.search(query, page: page) - end - - # - # Search - # - # @return [Array<Structs::Shodan::Result>] - # - def search - responses = [] - (1..pagination_limit).each do |page| - res = search_with_page(page: page) - responses << res - break if res.total <= page * PAGE_SIZE - - # sleep #{interval} seconds to avoid the rate limitation (if it is set) - sleep_interval - rescue JSON::ParserError - # ignore JSON::ParserError - # ref. https://github.com/ninoseki/mihari/issues/197 - next - end - responses + def client + @client ||= Clients::Shodan.new(api_key: api_key, interval: interval) end end end end