diff --git a/Rakefile b/Rakefile index d79daa6e..aab1deb1 100644 --- a/Rakefile +++ b/Rakefile @@ -1,4 +1,4 @@ -require "rake/testtask" +require 'rake/testtask' task :environment do require './environment.rb' @@ -53,3 +53,29 @@ task :update_screenshots => [:environment] do ScreenshotWorker.perform_async s.username } end + +desc 'Update banned IPs list' +task :update_blocked_ips => [:environment] do + + uri = URI.parse('http://www.stopforumspam.com/downloads/listed_ip_90.zip') + blocked_ips_zip = Tempfile.new('blockedipszip', Dir.tmpdir, 'wb') + blocked_ips_zip.binmode + + Net::HTTP.start(uri.host, uri.port) do |http| + resp = http.get(uri.path) + blocked_ips_zip.write(resp.body) + blocked_ips_zip.flush + end + + Zip::File.open(blocked_ips_zip.path) do |zip_file| + ips = zip_file.glob('listed_ip_90.txt').first.get_input_stream.read + insert_hashes = [] + ips.each_line {|ip| insert_hashes << {ip: ip.strip, created_at: Time.now}} + ips = nil + + DB.transaction do + DB[:blocked_ips].delete + DB[:blocked_ips].multi_insert insert_hashes + end + end +end diff --git a/environment.rb b/environment.rb index 5c55fd02..a5da18a3 100644 --- a/environment.rb +++ b/environment.rb @@ -100,4 +100,4 @@ end Sinatra::Application.set :erb, escape_html: true # Session fix for Internet Fucking Explorer https://github.com/rkh/rack-protection/issues/11 -Sinatra::Application.set :protection, except: :session_hijacking \ No newline at end of file +Sinatra::Application.set :protection, except: :session_hijacking diff --git a/migrations/038_blocked_ips.rb b/migrations/038_blocked_ips.rb new file mode 100644 index 00000000..4cfeb521 --- /dev/null +++ b/migrations/038_blocked_ips.rb @@ -0,0 +1,12 @@ +Sequel.migration do + up { + DB.create_table! :blocked_ips do + String :ip, primary_key: true + DateTime :created_at + end + } + + down { + DB.drop_table :blocked_ips + } +end \ No newline at end of file diff --git a/migrations/039_add_banned_at.rb b/migrations/039_add_banned_at.rb new file mode 100644 index 00000000..2dfe5fac --- /dev/null +++ b/migrations/039_add_banned_at.rb @@ -0,0 +1,9 @@ +Sequel.migration do + up { + DB.add_column :sites, :banned_at, :timestamp + } + + down { + DB.drop_column :sites, :banned_at + } +end \ No newline at end of file diff --git a/models/blocked_ip.rb b/models/blocked_ip.rb new file mode 100644 index 00000000..e4388638 --- /dev/null +++ b/models/blocked_ip.rb @@ -0,0 +1,3 @@ +class BlockedIp < Sequel::Model + +end \ No newline at end of file diff --git a/models/site.rb b/models/site.rb index 69fd01a7..0a30a3c3 100644 --- a/models/site.rb +++ b/models/site.rb @@ -117,10 +117,14 @@ class Site < Sequel::Model end def self.banned_ip?(ip) - !Site.where(is_banned: true). - where(ip: ip). - where(['updated_at > ?', Time.now-BANNED_TIME]). - first.nil? + return true if Site.where(is_banned: true). + where(ip: ip). + where(['updated_at > ?', Time.now-BANNED_TIME]). + first + + return true if BlockedIp[ip] + + false end def is_following?(site)