From afa1c150dae327d1f93957d044b14f208482668e Mon Sep 17 00:00:00 2001 From: Shlee Date: Sat, 17 Feb 2024 01:13:50 +1030 Subject: [PATCH] Create ip_spamlist_urls_scheduler.rb --- .../scheduler/ip_spamlist_urls_scheduler.rb | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 app/workers/scheduler/ip_spamlist_urls_scheduler.rb diff --git a/app/workers/scheduler/ip_spamlist_urls_scheduler.rb b/app/workers/scheduler/ip_spamlist_urls_scheduler.rb new file mode 100644 index 00000000000..9fedeb467a3 --- /dev/null +++ b/app/workers/scheduler/ip_spamlist_urls_scheduler.rb @@ -0,0 +1,76 @@ +# frozen_string_literal: true + +class Scheduler::IPBlocklistURLScheduler + include Sidekiq::Worker + + sidekiq_options retry: 0 + + def perform + if ENV['SCHEDULED_IPBLOCK_URLS'].present? + @block_url = ENV['SCHEDULED_IPBLOCK_URLS'] + @blockips = [] + grab_ip_blocks_addresses! + add_ip_blocks_addresses! + end + + if ENV['SCHEDULED_IPLIMIT_URLS'].present? + @limit_url = ENV['SCHEDULED_IPBLIMIT_URLS'] + @limitips = [] + grab_ip_limit_addresses! + add_ip_limit_addresses! + end + end + + def grab_ip_blocks_addresses! + @block_url.split(',').each do |url| + Request.new(:get, url).perform do |res| + @blockips.insert = res.body + end + end + end + + def grab_ip_limit_addresses! + @limit_url.split(',').each do |url| + Request.new(:get, url).perform do |res| + @limitips.insert = res.body + end + end + end + + def add_ip_blocks_addresses! + @blockips.each do |ip| + ip_block = IpBlock.find_by(ip: ip) + + if ip_block.present? + ip_block.update(expires_in: 24.hours.to_i) + next + end + + IpBlock.create( + ip: ip, + severity: :sign_up_block, + comment: 'Scheduled IPBlock', + expires_in: 24.hours.to_i + ) + end + end + + def add_ip_limit_addresses! + @limitips.each do |ip| + ip_limit = Iplimit.find_by(ip: ip) + + if ip_limit.present? + ip_limit.update(expires_in: 24.hours.to_i) + next + end + + Iplimit.create( + ip: ip, + severity: :sign_up_requires_approval, + comment: 'Scheduled IPLimit', + expires_in: 24.hours.to_i + ) + end + end + +end