passivedns-client 2.1.12 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,10 +1,12 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'sqlite3'
2
4
  require 'yaml'
3
5
  require 'structformatter'
4
6
 
5
7
  module PassiveDNS # :nodoc:
6
8
  # struct to hold pending entries for query
7
- class PDNSQueueEntry < Struct.new(:query, :state, :level); end
9
+ PDNSQueueEntry = Struct.new :query, :state, :level
8
10
 
9
11
  # holds state in memory of the queue to be queried, records returned, and the level of recursion
10
12
  class PDNSToolState
@@ -19,62 +21,55 @@ module PassiveDNS # :nodoc:
19
21
  @recs = []
20
22
  @level = 0
21
23
  end
22
-
23
- # returns the next record
24
- def next_result
25
- @recs.each do |rec|
26
- yield rec
27
- end
24
+
25
+ # returns the next record
26
+ def next_result(&)
27
+ @recs.each(&)
28
28
  end
29
29
 
30
30
  # adds the record to the list of records received and tries to add the answer and query back to the queue for future query
31
31
  def add_result(res)
32
32
  @recs << res
33
- add_query(res.answer,'pending')
34
- add_query(res.query,'pending')
33
+ add_query res.answer, 'pending'
34
+ add_query res.query, 'pending'
35
35
  end
36
36
 
37
37
  # sets the state of a given query
38
- def update_query(query,state)
38
+ def update_query(query, state)
39
39
  @queue.each do |q|
40
- if q.query == query
41
- puts "update_query: #{query} (#{q.state}) -> (#{state})" if @debug
42
- q.state = state
43
- break
44
- end
40
+ next unless q.query == query
41
+
42
+ puts "update_query: #{query} (#{q.state}) -> (#{state})" if @debug
43
+ q.state = state
44
+ break
45
45
  end
46
46
  end
47
47
 
48
48
  # returns the state of a provided query
49
49
  def get_state(query)
50
50
  @queue.each do |q|
51
- if q.query == query
52
- return q.state
53
- end
51
+ return q.state if q.query == query
54
52
  end
55
53
  false
56
54
  end
57
55
 
58
56
  # adding a query to the queue of things to be queried, but only if the query isn't already queued or answered
59
- def add_query(query,state,level=@level+1)
60
- if query =~ /^\d+ \w+\./
61
- query = query.split(/ /,2)[1]
62
- end
63
- return if get_state(query)
57
+ def add_query(query, state, level = @level + 1)
58
+ query = query.split(/ /, 2)[1] if /^\d+ \w+\./.match?(query)
59
+ return if get_state query
60
+
64
61
  puts "Adding query: #{query}, #{state}, #{level}" if @debug
65
- @queue << PDNSQueueEntry.new(query,state,level)
62
+ @queue << PDNSQueueEntry.new(query, state, level)
66
63
  end
67
64
 
68
65
  # returns each query waiting on the queue
69
- def each_query(max_level=20)
66
+ def each_query(max_level = 20)
70
67
  @queue.each do |q|
71
- if q.state == 'pending' or q.state == 'failed'
72
- @level = q.level
73
- q.state = 'queried'
74
- if q.level < max_level
75
- yield q.query
76
- end
77
- end
68
+ next unless (q.state == 'pending') || (q.state == 'failed')
69
+
70
+ @level = q.level
71
+ q.state = 'queried'
72
+ yield q.query if q.level < max_level
78
73
  end
79
74
  end
80
75
 
@@ -84,46 +79,46 @@ module PassiveDNS # :nodoc:
84
79
  # IP "$node2,,white,1"
85
80
  # domain "$node2,,gray,2"
86
81
  # Struct.new(:query, :answer, :rrtype, :ttl, :firstseen, :lastseen)
87
- colors = {"MX" => "green", "A" => "blue", "CNAME" => "pink", "NS" => "red", "SOA" => "white", "PTR" => "purple", "TXT" => "brown"}
82
+ colors = { 'MX' => 'green', 'A' => 'blue', 'CNAME' => 'pink', 'NS' => 'red', 'SOA' => 'white', 'PTR' => 'purple', 'TXT' => 'brown' }
88
83
  nodes = {}
89
84
  edges = {}
90
85
  next_result do |i|
91
- if i
92
- nodes[i.query + ",,gray,2"] = true
93
- if i.answer =~ /[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/ then
94
- nodes[i.answer + ",,white,1"] = true
95
- else
96
- nodes[i.answer + ",,gray,2"] = true
86
+ if i
87
+ nodes["#{i.query},,gray,2"] = true
88
+ if /[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/.match?(i.answer)
89
+ nodes["#{i.answer},,white,1"] = true
90
+ else
91
+ nodes["#{i.answer},,gray,2"] = true
97
92
  end
98
93
  color = colors[i.rrtype]
99
- color ||= "blue"
100
- edges[i.query + "," + i.answer + "," + color] = true
94
+ color ||= 'blue'
95
+ edges["#{i.query},#{i.answer},#{color}"] = true
101
96
  end
102
97
  end
103
- nodes.each do |i,j|
104
- output += i+"\n"
98
+ nodes.each_key do |i|
99
+ output += "#{i}\n"
105
100
  end
106
101
  output += "edgedef> node1,node2,color\n"
107
- edges.each do |i,j|
108
- output += i+"\n"
102
+ edges.each_key do |i|
103
+ output += "#{i}\n"
109
104
  end
110
105
  output
111
106
  end
112
107
 
113
108
  # transforms a set of results into graphviz syntax
114
109
  def to_graphviz
115
- colors = {"MX" => "green", "A" => "blue", "CNAME" => "pink", "NS" => "red", "SOA" => "white", "PTR" => "purple", "TXT" => "brown"}
110
+ colors = { 'MX' => 'green', 'A' => 'blue', 'CNAME' => 'pink', 'NS' => 'red', 'SOA' => 'white', 'PTR' => 'purple', 'TXT' => 'brown' }
116
111
  output = "graph pdns {\n"
117
112
  nodes = {}
118
113
  next_result do |l|
119
114
  if l
120
115
  unless nodes[l.query]
121
116
  output += " \"#{l.query}\" [shape=ellipse, style=filled, color=gray];\n"
122
- if l.answer =~ /^\d{3}\.\d{3}\.\d{3}\.\d{3}$/
123
- output += " \"#{l.answer}\" [shape=box, style=filled, color=white];\n"
124
- else
125
- output += " \"#{l.answer}\" [shape=ellipse, style=filled, color=gray];\n"
126
- end
117
+ output += if /^\d{3}\.\d{3}\.\d{3}\.\d{3}$/.match?(l.answer)
118
+ " \"#{l.answer}\" [shape=box, style=filled, color=white];\n"
119
+ else
120
+ " \"#{l.answer}\" [shape=ellipse, style=filled, color=gray];\n"
121
+ end
127
122
  nodes[l.query] = true
128
123
  end
129
124
  output += " \"#{l.query}\" -- \"#{l.answer}\" [color=#{colors[l.rrtype]}];\n"
@@ -135,7 +130,7 @@ module PassiveDNS # :nodoc:
135
130
  # transforms a set of results into graphml syntax
136
131
  def to_graphml
137
132
  output = '<?xml version="1.0" encoding="UTF-8"?>
138
- <graphml xmlns="http://graphml.graphdrawing.org/xmlns"
133
+ <graphml xmlns="http://graphml.graphdrawing.org/xmlns"
139
134
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
140
135
  xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns
141
136
  http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
@@ -145,41 +140,41 @@ module PassiveDNS # :nodoc:
145
140
  edges = {}
146
141
  next_result do |r|
147
142
  if r
148
- output += " <node id='#{r.query}'/>\n" unless nodes["#{r.query}"]
143
+ output += " <node id='#{r.query}'/>\n" unless nodes[r.query.to_s]
149
144
  nodes[r.query] = true
150
- output += " <node id='#{r.answer}'/>\n" unless nodes["#{r.answer}"]
145
+ output += " <node id='#{r.answer}'/>\n" unless nodes[r.answer.to_s]
151
146
  nodes[r.answer] = true
152
147
  output += " <edge source='#{r.query}' target='#{r.answer}'/>\n" unless edges["#{r.query}|#{r.answer}"]
153
148
  end
154
149
  end
155
- output += '</graph></graphml>'+"\n"
150
+ output += "</graph></graphml>\n"
156
151
  end
157
152
 
158
153
  # transforms a set of results into XML
159
154
  def to_xml
160
- output = '<?xml version="1.0" encoding="UTF-8" ?>'+"\n"
161
- output += "<report>\n"
162
- output += " <results>\n"
155
+ output = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
156
+ output += "<report>\n"
157
+ output += " <results>\n"
163
158
  next_result do |rec|
164
- output += " "+rec.to_xml+"\n"
159
+ output += " #{rec.to_xml}\n"
165
160
  end
166
- output += " </results>\n"
167
- output += "</report>\n"
161
+ output += " </results>\n"
162
+ output += "</report>\n"
168
163
  end
169
164
 
170
165
  # transforms a set of results into YAML
171
166
  def to_yaml
172
- output = ""
167
+ output = ''
173
168
  next_result do |rec|
174
- output += rec.to_yaml+"\n"
169
+ output += "#{rec.to_yaml}\n"
175
170
  end
176
171
  output
177
172
  end
178
173
 
179
174
  # transforms a set of results into JSON
180
- def to_json
175
+ def to_json(*_args)
181
176
  output = "[\n"
182
- sep = ""
177
+ sep = ''
183
178
  next_result do |rec|
184
179
  output += sep
185
180
  output += rec.to_json
@@ -189,113 +184,109 @@ module PassiveDNS # :nodoc:
189
184
  end
190
185
 
191
186
  # transforms a set of results into a text string
192
- def to_s(sep="\t")
193
- output = ""
187
+ def to_s(sep = "\t")
188
+ output = ''
194
189
  next_result do |rec|
195
- output += rec.to_s(sep)+"\n"
190
+ output += "#{rec.to_s sep}\n"
196
191
  end
197
192
  output
198
193
  end
199
- end # class PDNSToolState
200
-
194
+ end
201
195
 
202
196
  # creates persistence to the tool state by leveraging SQLite3
203
197
  class PDNSToolStateDB < PDNSToolState
204
198
  attr_reader :level
199
+
205
200
  # creates an SQLite3-based Passive DNS Client state
206
201
  # only argument is the filename of the sqlite3 database
207
- def initialize(sqlitedb=nil)
202
+ def initialize(sqlitedb = nil)
203
+ super()
208
204
  @debug = false
209
205
  puts "PDNSToolState initialize #{sqlitedb}" if @debug
210
206
  @level = 0
211
207
  @sqlitedb = sqlitedb
212
- raise "Cannot use this class without a database file" unless @sqlitedb
213
- unless File.exist?(@sqlitedb)
214
- newdb = true
215
- end
216
- @sqlitedbh = SQLite3::Database.new(@sqlitedb)
217
- if newdb
218
- create_tables
219
- end
220
- res = @sqlitedbh.execute("select min(level) from queue where state = 'pending'")
221
- if res
222
- res.each do |row|
223
- @level = row[0].to_i
224
- puts "changed @level = #{@level}" if @debug
225
- end
208
+ raise 'Cannot use this class without a database file' unless @sqlitedb
209
+
210
+ newdb = true unless File.exist? @sqlitedb
211
+ @sqlitedbh = SQLite3::Database.new @sqlitedb
212
+ create_tables if newdb
213
+ res = @sqlitedbh.execute "select min(level) from queue where state = 'pending'"
214
+ return unless res
215
+
216
+ res.each do |row|
217
+ @level = row[0].to_i
218
+ puts "changed @level = #{@level}" if @debug
226
219
  end
227
220
  end
228
221
 
229
222
  # creates the sqlite3 tables needed to track the state of this tool as itqueries and recurses
230
223
  def create_tables
231
- puts "creating tables" if @debug
232
- @sqlitedbh.execute("create table results (query, answer, rrtype, ttl, firstseen, lastseen, ts REAL)")
233
- @sqlitedbh.execute("create table queue (query, state, level INTEGER, ts REAL)")
234
- @sqlitedbh.execute("create index residx on results (ts)")
235
- @sqlitedbh.execute("create unique index queue_unique on queue (query)")
236
- @sqlitedbh.execute("create index queue_level_idx on queue (level)")
237
- @sqlitedbh.execute("create index queue_state_idx on queue (state)")
224
+ puts 'creating tables' if @debug
225
+ @sqlitedbh.execute 'create table results (query, answer, rrtype, ttl, firstseen, lastseen, ts REAL)'
226
+ @sqlitedbh.execute 'create table queue (query, state, level INTEGER, ts REAL)'
227
+ @sqlitedbh.execute 'create index residx on results (ts)'
228
+ @sqlitedbh.execute 'create unique index queue_unique on queue (query)'
229
+ @sqlitedbh.execute 'create index queue_level_idx on queue (level)'
230
+ @sqlitedbh.execute 'create index queue_state_idx on queue (state)'
238
231
  end
239
232
 
240
- # returns the next record
233
+ # returns the next record
241
234
  def next_result
242
- rows = @sqlitedbh.execute("select query, answer, rrtype, ttl, firstseen, lastseen from results order by ts")
235
+ rows = @sqlitedbh.execute 'select query, answer, rrtype, ttl, firstseen, lastseen from results order by ts'
243
236
  rows.each do |row|
244
- yield PDNSResult.new(*row)
237
+ # the database does not keep the source or the response time
238
+ yield PDNSResult.new(nil, nil, *row)
245
239
  end
246
240
  end
247
241
 
248
242
  # adds the record to the list of records received and tries to add the answer and query back to the queue for future query
249
243
  def add_result(res)
250
- puts "adding result: #{res.to_s}" if @debug
251
- curtime = Time.now().to_f
252
- @sqlitedbh.execute("insert into results values ('#{res.query}','#{res.answer}','#{res.rrtype}','#{res.ttl}','#{res.firstseen}','#{res.lastseen}',#{curtime})")
244
+ puts "adding result: #{res}" if @debug
245
+ curtime = Time.now.to_f
246
+ @sqlitedbh.execute('insert into results values (?, ?, ?, ?, ?, ?, ?)',
247
+ [res.query, res.answer, res.rrtype, res.ttl, res.firstseen, res.lastseen, curtime].map do |v|
248
+ v.nil? || v.is_a?(Float) ? v : v.to_s
249
+ end)
253
250
 
254
- add_query(res.answer,'pending')
255
- add_query(res.query,'pending')
251
+ add_query res.answer, 'pending'
252
+ add_query res.query, 'pending'
256
253
  end
257
254
 
258
255
  # adding a query to the queue of things to be queried, but only if the query isn't already queued or answered
259
- def add_query(query,state,level=@level+1)
260
- return if get_state(query)
261
- curtime = Time.now().to_f
262
- begin
263
- puts "add_query(#{query},#{state},level=#{level})" if @debug
264
- @sqlitedbh.execute("insert into queue values ('#{query}','#{state}',#{level},#{curtime})")
265
- rescue
266
- end
256
+ def add_query(query, state, level = @level + 1)
257
+ return if get_state query
258
+
259
+ curtime = Time.now.to_f
260
+ puts "add_query(#{query},#{state},level=#{level})" if @debug
261
+ @sqlitedbh.execute 'insert into queue values (?, ?, ?, ?)', [query.to_s, state, level, curtime]
262
+ rescue SQLite3::ConstraintException
263
+ # the query is already queued
267
264
  end
268
265
 
269
266
  # sets the state of a given query
270
- def update_query(query,state)
271
- @sqlitedbh.execute("update queue set state = '#{state}' where query = '#{query}'")
267
+ def update_query(query, state)
268
+ @sqlitedbh.execute 'update queue set state = ? where query = ?', [state, query.to_s]
272
269
  end
273
270
 
274
271
  # returns each query waiting on the queue
275
272
  def get_state(query)
276
- rows = @sqlitedbh.execute("select state from queue where query = '#{query}'")
277
- if rows
278
- rows.each do |row|
279
- return row[0]
280
- end
281
- end
282
- false
273
+ row = @sqlitedbh.get_first_row 'select state from queue where query = ?', [query.to_s]
274
+ row ? row[0] : false
283
275
  end
284
276
 
285
277
  # returns each query waiting on the queue
286
- def each_query(max_level=20)
278
+ def each_query(max_level = 20)
287
279
  puts "each_query max_level=#{max_level} curlevel=#{@level}" if @debug
288
- rows = @sqlitedbh.execute("select query, state, level from queue where state = 'failed' or state = 'pending' order by level limit 1")
289
- if rows
290
- rows.each do |row|
291
- query,state,level = row
292
- puts " #{query},#{state},#{level}" if @debug
293
- if level < max_level
294
- update_query(query,'queried')
295
- yield query
296
- end
297
- end
298
- end
280
+ row = @sqlitedbh.get_first_row "select query, state, level from queue where state = 'failed' or state = 'pending' " \
281
+ 'order by level limit 1'
282
+ return unless row
283
+
284
+ query, state, level = row
285
+ puts " #{query},#{state},#{level}" if @debug
286
+ return unless level < max_level
287
+
288
+ update_query query, 'queried'
289
+ yield query
299
290
  end
300
- end # class PDNSToolStateDB
291
+ end
301
292
  end
@@ -1,7 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module PassiveDNS # :nodoc:
2
4
  # coodinates the lookups accross all configured PassiveDNS providers
3
5
  class Client
4
6
  # version of PassiveDNS::Client
5
- VERSION = "2.1.12"
7
+ VERSION = '3.0.0'
6
8
  end
7
9
  end
@@ -1,128 +1,130 @@
1
- require "passivedns/client/version"
2
- # DESCRIPTION: queries passive DNS databases
1
+ # frozen_string_literal: true
2
+
3
+ # DESCRIPTION: queries passive DNS databases
3
4
  # This code is released under the LGPL: http://www.gnu.org/licenses/lgpl-3.0.txt
4
- # Please note that use of any passive dns database is subject to the terms of use of that passive dns database. Use of this script in violation of their terms is not encouraged in any way. Also, please do not add any obfuscation to try to work around their terms of service. If you need special services, ask the providers for help/permission.
5
- # Remember, these passive DNS operators are my friends. I don't want to have a row with them because some asshat used this library to abuse them.
6
- require 'passivedns/client/state'
7
- require 'passivedns/client/passivedb'
5
+ # Please note that use of any passive dns database is subject to the terms of use of that passive dns database. Use of this script in
6
+ # violation of their terms is not encouraged in any way. Also, please do not add any obfuscation to try to work around their terms of
7
+ # service. If you need special services, ask the providers for help/permission.
8
+ # Remember, these passive DNS operators are my friends. I don't want to have a row with them because some asshat used this library to
9
+ # abuse them.
10
+ require 'iniparse'
11
+ require_relative 'client/version'
12
+ require_relative 'client/state'
13
+ require_relative 'client/passivedb'
8
14
 
9
15
  # load all the providers
10
- $passivedns_providers = Array.new
11
- provider_path = File.dirname(__FILE__)+"/client/provider/*.rb"
12
- Dir.glob(provider_path).each do |provider|
13
- name = File.basename(provider, '.rb')
14
- require "passivedns/client/provider/#{name}.rb"
15
- if name != 'bfk'
16
- $passivedns_providers << name
17
- end
18
- end
19
-
20
- require 'configparser'
16
+ Dir.glob(File.join(__dir__, 'client', 'provider', '*.rb')).each { |provider| require provider }
21
17
 
22
18
  module PassiveDNS # :nodoc:
23
-
24
19
  class SecurityControl
25
- def allow(user_level)
26
- raise "unimplemented"
20
+ def allow(_user_level)
21
+ raise 'unimplemented'
27
22
  end
28
23
  end
29
-
24
+
30
25
  class TLPSecurityControl < SecurityControl
31
- LEVELS = ['white','green','yellow','red']
32
-
26
+ LEVELS = %w[white green yellow red].freeze
27
+
33
28
  def initialize(tlp)
34
- if tlp =~ /(white|green|yellow|red)/i
35
- @tlp = tlp.downcase
36
- @tlp_level = LEVELS.index(@tlp)
37
- else
38
- raise "Unknown TLP setting, #{tlp}"
39
- end
29
+ super()
30
+ raise "Unknown TLP setting, #{tlp}" unless /(white|green|yellow|red)/i.match?(tlp)
31
+
32
+ @tlp = tlp.downcase
33
+ @tlp_level = LEVELS.index @tlp
40
34
  end
41
-
42
- def allow(user_level)
43
- user_level = LEVELS.index(user_level.downcase)
44
- if user_level == nil
45
- raise "Invalid user level, #{user_level}"
46
- end
47
- return(user_level >= @tlp_level)
35
+
36
+ def allow(user_level) # rubocop:disable Naming/PredicateMethod
37
+ level = LEVELS.index user_level.downcase
38
+ raise "Invalid user level, #{user_level}" if level.nil?
39
+
40
+ level >= @tlp_level
48
41
  end
49
-
50
- def to_s()
42
+
43
+ def to_s
51
44
  @tlp
52
45
  end
53
46
  end
54
-
47
+
55
48
  # struct to contain the results from a PassiveDNS lookup
56
- class PDNSResult < Struct.new(:source, :response_time, :query, :answer, :rrtype, :ttl, :firstseen, :lastseen, :count, :security); end
49
+ # (the :count member intentionally shadows Struct#count)
50
+ PDNSResult = Struct.new :source, :response_time, :query, :answer, :rrtype, :ttl, :firstseen, :lastseen, :count, :security # rubocop:disable Lint/StructNewOverride
57
51
 
58
52
  # coodinates the lookups accross all configured PassiveDNS providers
59
53
  class Client
60
-
54
+ # the record types that are reported
55
+ RRTYPES = %w[A AAAA NS CNAME PTR SOA].freeze
56
+
57
+ # default location of the configuration file
58
+ DEFAULT_CONFIG_FILE = File.join Dir.home, '.passivedns-client'
59
+
60
+ # the names of all available providers, e.g., ["circl", "dnsdb", ...]
61
+ def self.provider_names
62
+ names = PassiveDB.providers.map(&:config_section_name)
63
+ names.sort!
64
+ names
65
+ end
66
+
67
+ # reads an INI-style configuration file and returns a hash of section name to a hash of settings.
68
+ # A missing file yields an empty configuration and a warning.
69
+ def self.load_config(configfile = DEFAULT_CONFIG_FILE)
70
+ unless File.exist? configfile
71
+ warn "Could not find config file at #{configfile}. Using a blank configuration."
72
+ return {}
73
+ end
74
+
75
+ IniParse.parse(File.read(configfile)).to_hash
76
+ rescue IniParse::IniParseError => e
77
+ raise "Unable to parse the config file #{configfile}: #{e.message}"
78
+ end
79
+
61
80
  # instantiate and configure all specified PassiveDNS providers
62
81
  # pdns array of passivedns provider names, e.g., ["dnsdb","virustotal"]
63
- # configfile filename of the passivedns-client configuration (this should probably be abstracted)
64
- def initialize(pdns=$passivedns_providers, configfile="#{ENV['HOME']}/.passivedns-client")
65
- cp = {}
66
- if File.exist?(configfile)
67
- cp = ConfigParser.new(configfile)
68
- else
69
- $stderr.puts "Could not find config file at #{configfile}. Using a blank configuration."
70
- end
71
- # this creates a map of all the PassiveDNS provider names and their classes
72
- class_map = {}
73
- PassiveDNS::Provider.constants.each do |const|
74
- if PassiveDNS::Provider.const_get(const).is_a?(Class) and PassiveDNS::Provider.const_get(const).superclass == PassiveDNS::PassiveDB
75
- class_map[PassiveDNS::Provider.const_get(const).config_section_name] = PassiveDNS::Provider.const_get(const)
76
- end
77
- end
78
-
79
- @pdnsdbs = []
80
- pdns.uniq.each do |pd|
81
- if class_map[pd]
82
- @pdnsdbs << class_map[pd].new(cp[pd] || {})
83
- else
84
- raise "Unknown Passive DNS provider: #{pd}"
85
- end
82
+ # configfile filename of the passivedns-client configuration
83
+ def initialize(pdns = self.class.provider_names, configfile = DEFAULT_CONFIG_FILE)
84
+ config = self.class.load_config configfile
85
+ class_map = PassiveDB.providers.to_h { |provider| [provider.config_section_name, provider] }
86
+
87
+ @pdnsdbs = pdns.uniq.map do |name| # rubocop:disable Performance/ChainArrayAllocation
88
+ raise "Unknown Passive DNS provider: #{name}" unless class_map[name]
89
+
90
+ class_map[name].new(config[name] || {})
86
91
  end
92
+ end
87
93
 
88
- end #initialize
89
-
90
94
  # set the debug flag
91
- def debug=(d)
95
+ def debug=(value)
92
96
  @pdnsdbs.each do |pdnsdb|
93
- pdnsdb.debug = d
97
+ pdnsdb.debug = value
94
98
  end
95
99
  end
96
-
97
- def timeout=(t)
100
+
101
+ # set the timeout, in seconds, for each provider's lookups
102
+ def timeout=(value)
98
103
  @pdnsdbs.each do |pdnsdb|
99
- pdnsdb.timeout = t
104
+ pdnsdb.timeout = value
100
105
  end
101
106
  end
102
-
107
+
103
108
  # perform the query lookup accross all configured PassiveDNS providers
104
- def query(item, limit=nil)
105
- threads = []
106
- @pdnsdbs.each do |pdnsdb|
107
- threads << Thread.new(item) do |q|
108
- pdnsdb.lookup(q, limit)
109
+ def query(item, limit = nil)
110
+ threads = @pdnsdbs.map do |pdnsdb|
111
+ Thread.new item do |q|
112
+ Thread.current.report_on_exception = false
113
+ pdnsdb.lookup q, limit
109
114
  end
110
115
  end
111
-
116
+
112
117
  results = []
113
118
  threads.each do |thr|
114
- rv = thr.join.value
115
- if rv
116
- rv.each do |r|
117
- if ["A","AAAA","NS","CNAME","PTR"].index(r.rrtype)
118
- results << r
119
- end
120
- end
119
+ thr.value&.each do |r|
120
+ results << r if RRTYPES.include? r.rrtype
121
121
  end
122
+ rescue StandardError => e
123
+ # one failing provider should not lose the answers from the others
124
+ warn "ERROR: #{e.message}"
122
125
  end
123
-
124
- return results
125
- end #query
126
-
127
- end # Client
128
- end # PassiveDNS
126
+
127
+ results
128
+ end
129
+ end
130
+ end