mirror of
https://github.com/neocities/neocities.git
synced 2025-04-24 17:22: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
|
task :environment do
|
||||||
require './environment.rb'
|
require './environment.rb'
|
||||||
|
@ -53,3 +53,29 @@ task :update_screenshots => [:environment] do
|
||||||
ScreenshotWorker.perform_async s.username
|
ScreenshotWorker.perform_async s.username
|
||||||
}
|
}
|
||||||
end
|
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
|
||||||
|
|
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
|
end
|
||||||
|
|
||||||
def self.banned_ip?(ip)
|
def self.banned_ip?(ip)
|
||||||
!Site.where(is_banned: true).
|
return true if Site.where(is_banned: true).
|
||||||
where(ip: ip).
|
where(ip: ip).
|
||||||
where(['updated_at > ?', Time.now-BANNED_TIME]).
|
where(['updated_at > ?', Time.now-BANNED_TIME]).
|
||||||
first.nil?
|
first
|
||||||
|
|
||||||
|
return true if BlockedIp[ip]
|
||||||
|
|
||||||
|
false
|
||||||
end
|
end
|
||||||
|
|
||||||
def is_following?(site)
|
def is_following?(site)
|
||||||
|
|
Loading…
Add table
Reference in a new issue