mirror of
https://github.com/neocities/neocities.git
synced 2025-04-24 09:12:35 +02:00
block ips rake task
This commit is contained in:
parent
3095185121
commit
7445f8006b
6 changed files with 60 additions and 6 deletions
28
Rakefile
28
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
|
||||
|
|
|
@ -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
|
||||
Sinatra::Application.set :protection, except: :session_hijacking
|
||||
|
|
12
migrations/038_blocked_ips.rb
Normal file
12
migrations/038_blocked_ips.rb
Normal file
|
@ -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
|
9
migrations/039_add_banned_at.rb
Normal file
9
migrations/039_add_banned_at.rb
Normal file
|
@ -0,0 +1,9 @@
|
|||
Sequel.migration do
|
||||
up {
|
||||
DB.add_column :sites, :banned_at, :timestamp
|
||||
}
|
||||
|
||||
down {
|
||||
DB.drop_column :sites, :banned_at
|
||||
}
|
||||
end
|
3
models/blocked_ip.rb
Normal file
3
models/blocked_ip.rb
Normal file
|
@ -0,0 +1,3 @@
|
|||
class BlockedIp < Sequel::Model
|
||||
|
||||
end
|
|
@ -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)
|
||||
|
|
Loading…
Add table
Reference in a new issue