wafris 2.2.0 → 2.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/wafris/version.rb +1 -1
  3. data/lib/wafris.rb +44 -36
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 722b44b21ce69ec8d515c442b3ee5fd783298de0714105eb9cd9585f3f007af9
4
- data.tar.gz: d93da5905d328163e235e193998e63033a2be5b1e509a4b62ecbe804b0023289
3
+ metadata.gz: 0fad1d400c6106dc0f2f29b6b46a0ab919f4060ef45a4bde4dbb8607b95c1cae
4
+ data.tar.gz: b3b854a117e0a7021bf3194e339da5f0acdf320439f973226e850249bc445dcd
5
5
  SHA512:
6
- metadata.gz: 9945fef04660a4e6aeaf6aac33669bcfc210c1386ba684e7a147602f76542c0764c98dd9072fe35faace14bbaaf5d0be492e21c754da9b2607dae204ee350b1b
7
- data.tar.gz: d219c32ddb8f9375bb1b006d3b0b3d49c5fe47ca4632e5bb8b8bd93a398c05d2dd183d5ad5a79510d4de89bf6f677d0c84ed038ea40fd0a7b4319bc6aff28d03
6
+ metadata.gz: bd8e5e8109a7aa1b51c1361d7c288d11e2cca0ea4a5c0e66a198bbde30d9835b57ce2711f2869c575cc604577b4fe927836bfb0e47764f4e08a4c39c2a2e82db
7
+ data.tar.gz: fb171df540399f4a128e9bd28a1f80c3aa41e6a97e3cccc5eae0f2fa13b1de86be516d227bce3582fc9cefb35eb067e1b4ced665e79e5af89672187195eac109
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Wafris
4
- VERSION = "2.2.0"
4
+ VERSION = "2.3.0"
5
5
  end
data/lib/wafris.rb CHANGED
@@ -187,20 +187,25 @@ module Wafris
187
187
 
188
188
  url_and_api_key = @configuration.upsync_url + "/" + @configuration.api_key
189
189
 
190
+ LogSuppressor.puts_log("[Upsync] Beginning request thread...")
191
+ current_time = Time.now
190
192
  response = HTTParty.post(
191
193
  url_and_api_key,
192
194
  body: body,
193
195
  headers: headers,
194
- timeout: 10
196
+ timeout: 5
195
197
  )
196
198
 
197
199
  if response.code == 200
198
200
  @configuration.upsync_status = "Complete"
199
201
  else
200
- LogSuppressor.puts_log("Upsync Error. HTTP Response: #{response.code}")
202
+ LogSuppressor.puts_log("[Upsync] Error. HTTP Response: #{response.code}")
201
203
  end
202
204
  rescue HTTParty::Error => e
203
- LogSuppressor.puts_log("Upsync Error. Failed to send upsync requests: #{e.message}")
205
+ LogSuppressor.puts_log("[Upsync] Thread Error. Failed to send upsync requests: #{e.message}")
206
+ ensure
207
+ elapsed_time = Time.now - current_time
208
+ LogSuppressor.puts_log("[Upsync] request thread complete in #{elapsed_time.round(2)} seconds.")
204
209
  end
205
210
 
206
211
  # This method is used to queue upsync requests. It takes in several parameters including:
@@ -226,7 +231,7 @@ module Wafris
226
231
  @configuration.upsync_queue = []
227
232
  @configuration.last_upsync_timestamp = Time.now.to_i
228
233
 
229
- send_upsync_requests(requests_array)
234
+ Thread.new { send_upsync_requests(requests_array) }
230
235
  end
231
236
 
232
237
  @configuration.upsync_status = "Enabled"
@@ -255,10 +260,10 @@ module Wafris
255
260
  begin
256
261
  lockfile = File.open(lockfile_path, File::RDWR | File::CREAT | File::EXCL)
257
262
  rescue Errno::EEXIST
258
- LogSuppressor.puts_log("[Wafris][Downsync] Lockfile already exists, skipping downsync.")
263
+ LogSuppressor.puts_log("[Downsync][downsync_db] Lockfile already exists, skipping downsync.")
259
264
  return
260
265
  rescue Exception => e
261
- LogSuppressor.puts_log("[Wafris][Downsync] Error creating lockfile: #{e.message}")
266
+ LogSuppressor.puts_log("[Downsync] Error creating lockfile: #{e.message}")
262
267
  end
263
268
 
264
269
  begin
@@ -280,10 +285,14 @@ module Wafris
280
285
  # puts "Downloading from #{@configuration.downsync_url}/#{db_rule_category}/#{@configuration.api_key}?current_version=#{current_filename}&process_id=#{Process.pid}"
281
286
  uri = "#{@configuration.downsync_url}/#{db_rule_category}/#{@configuration.api_key}?#{data.to_query}"
282
287
 
288
+ LogSuppressor.puts_log("[Downsync] Beginning request thread for #{db_rule_category}...")
289
+ current_time = Time.now
290
+
283
291
  response = HTTParty.get(
284
292
  uri,
285
293
  follow_redirects: true, # Enable following redirects
286
- max_redirects: 2 # Maximum number of redirects to follow
294
+ max_redirects: 2, # Maximum number of redirects to follow
295
+ timeout: 30
287
296
  )
288
297
 
289
298
  # TODO: What to do if timeout
@@ -291,13 +300,12 @@ module Wafris
291
300
 
292
301
  if response.code == 401
293
302
  @configuration.upsync_status = "Disabled"
294
- LogSuppressor.puts_log("[Wafris][Downsync] Unauthorized: Bad or missing API key")
295
- LogSuppressor.puts_log("[Wafris][Downsync] API Key: #{@configuration.api_key}")
303
+ LogSuppressor.puts_log("[Downsync] Unauthorized: Bad or missing API key")
304
+ LogSuppressor.puts_log("[Downsync] API Key: #{@configuration.api_key}")
296
305
  filename = current_filename
297
306
 
298
307
  elsif response.code == 304
299
308
  @configuration.upsync_status = "Enabled"
300
- LogSuppressor.puts_log("[Wafris][Downsync] No new rules to download")
301
309
 
302
310
  filename = current_filename
303
311
 
@@ -332,12 +340,12 @@ module Wafris
332
340
  # DB file is bad or empty so keep using whatever we have now
333
341
  else
334
342
  filename = old_file_name
335
- LogSuppressor.puts_log("[Wafris][Downsync] DB Error - No tables exist in the db file #{@configuration.db_file_path}/#{filename}")
343
+ LogSuppressor.puts_log("[Downsync] DB Error - No tables exist in the db file #{@configuration.db_file_path}/#{filename}")
336
344
  end
337
345
 
338
346
  end
339
347
  rescue => e
340
- LogSuppressor.puts_log("[Wafris][Downsync] Error downloading rules: #{e.message}")
348
+ LogSuppressor.puts_log("[Downsync] Error downloading rules: #{e.message}")
341
349
 
342
350
  # This gets set even if the API key is bad or other issues
343
351
  # to prevent hammering the distribution server on every request
@@ -353,42 +361,47 @@ module Wafris
353
361
  # Ensure the lockfile is removed after operations
354
362
  lockfile.close
355
363
  File.delete(lockfile_path)
364
+
365
+ elapsed_time = Time.now - current_time
366
+ LogSuppressor.puts_log("[Downsync] request thread complete in #{elapsed_time.round(2)} seconds for #{db_rule_category}.")
356
367
  end
357
368
 
358
369
  filename
359
370
  end
360
371
 
372
+ def sync_interval(db_rule_category)
373
+ if db_rule_category == "custom_rules"
374
+ @configuration.downsync_custom_rules_interval
375
+ else
376
+ @configuration.downsync_data_subscriptions_interval
377
+ end
378
+ end
379
+
361
380
  # Returns the current database file,
362
381
  # if the file is older than the interval, it will download the latest db
363
382
  # if the file doesn't exist, it will download the latest db
364
383
  # if the lockfile exists, it will return the current db
365
384
  def current_db(db_rule_category)
366
- interval = if db_rule_category == "custom_rules"
367
- @configuration.downsync_custom_rules_interval
368
- else
369
- @configuration.downsync_data_subscriptions_interval
370
- end
371
-
372
385
  # Checks for existing current modfile, which contains the current db filename
373
386
  if File.exist?("#{@configuration.db_file_path}/#{db_rule_category}.modfile")
374
387
 
375
- LogSuppressor.puts_log("[Wafris][Downsync] Modfile exists, skipping downsync")
388
+ LogSuppressor.puts_log("[Downsync] Modfile exists, skipping downsync")
376
389
 
377
390
  # Get last Modified Time and current database file name
378
391
  last_db_synctime = File.mtime("#{@configuration.db_file_path}/#{db_rule_category}.modfile").to_i
379
392
  returned_db = File.read("#{@configuration.db_file_path}/#{db_rule_category}.modfile").strip
380
393
 
381
- LogSuppressor.puts_log("[Wafris][Downsync] Modfile Last Modified Time: #{last_db_synctime}")
382
- LogSuppressor.puts_log("[Wafris][Downsync] DB in Modfile: #{returned_db}")
394
+ LogSuppressor.puts_log("[Downsync] Modfile Last Modified Time: #{last_db_synctime}")
395
+ LogSuppressor.puts_log("[Downsync] DB in Modfile: #{returned_db}")
383
396
 
384
397
  # Check if the db file is older than the interval
385
- if (Time.now.to_i - last_db_synctime) > interval
398
+ if (Time.now.to_i - last_db_synctime) > sync_interval(db_rule_category)
386
399
 
387
- LogSuppressor.puts_log("[Wafris][Downsync] DB is older than the interval")
400
+ LogSuppressor.puts_log("[Downsync] DB is older than the interval")
388
401
 
389
402
  # Make sure that another process isn't already downloading the rules
390
403
  if !File.exist?("#{@configuration.db_file_path}/#{db_rule_category}.lockfile")
391
- returned_db = downsync_db(db_rule_category, returned_db)
404
+ Thread.new { downsync_db(db_rule_category, returned_db) }
392
405
  end
393
406
 
394
407
  returned_db
@@ -396,7 +409,7 @@ module Wafris
396
409
  # Current db is up to date
397
410
  else
398
411
 
399
- LogSuppressor.puts_log("[Wafris][Downsync] DB is up to date")
412
+ LogSuppressor.puts_log("[Downsync] DB is up to date")
400
413
 
401
414
  returned_db = File.read("#{@configuration.db_file_path}/#{db_rule_category}.modfile").strip
402
415
 
@@ -413,24 +426,18 @@ module Wafris
413
426
  # No modfile exists, so download the latest db
414
427
  else
415
428
 
416
- LogSuppressor.puts_log("[Wafris][Downsync] No modfile exists, downloading latest db")
429
+ LogSuppressor.puts_log("[Downsync] No modfile exists, downloading latest #{db_rule_category} db")
417
430
 
418
431
  # Make sure that another process isn't already downloading the rules
419
432
  if File.exist?("#{@configuration.db_file_path}/#{db_rule_category}.lockfile")
420
- LogSuppressor.puts_log("[Wafris][Downsync] Lockfile exists, skipping downsync")
433
+ LogSuppressor.puts_log("[Downsync][current_db] Lockfile exists, skipping downsync")
421
434
  # Lockfile exists, but no modfile with a db filename
422
435
  nil
423
436
  else
424
-
425
- LogSuppressor.puts_log("[Wafris][Downsync] No modfile exists, downloading latest db")
426
437
  # No modfile exists, so download the latest db
427
- returned_db = downsync_db(db_rule_category, nil)
438
+ Thread.new { downsync_db(db_rule_category, nil) }
428
439
 
429
- if returned_db.nil?
430
- nil
431
- else
432
- returned_db
433
- end
440
+ nil
434
441
  end
435
442
  end
436
443
  end
@@ -444,11 +451,12 @@ module Wafris
444
451
 
445
452
  return "Passed" if @configuration.api_key.nil?
446
453
 
454
+ # Now current_db can return the actual db, nil, or a future object
447
455
  rules_db_filename = current_db("custom_rules")
448
456
  data_subscriptions_db_filename = current_db("data_subscriptions")
449
457
 
450
458
  # Checks to see if the filenames are present before loading the db
451
- if rules_db_filename.to_s.strip != "" && data_subscriptions_db_filename.strip.to_s.strip != ""
459
+ if rules_db_filename.to_s.strip != "" && data_subscriptions_db_filename.to_s.strip != ""
452
460
 
453
461
  rules_db = SQLite3::Database.new "#{@configuration.db_file_path}/#{rules_db_filename}"
454
462
  data_subscriptions_db =
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wafris
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.0
4
+ version: 2.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Buckbee
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2024-11-26 00:00:00.000000000 Z
12
+ date: 2024-11-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rack