2013-10-22 02:49:51 +08:00
|
|
|
require_dependency 'screening_model'
|
|
|
|
|
|
|
|
# A ScreenedIpAddress record represents an IP address or subnet that is being watched,
|
|
|
|
# and possibly blocked from creating accounts.
|
|
|
|
class ScreenedIpAddress < ActiveRecord::Base
|
|
|
|
|
|
|
|
include ScreeningModel
|
|
|
|
|
|
|
|
default_action :block
|
|
|
|
|
2013-10-23 04:30:30 +08:00
|
|
|
validates :ip_address, ip_address_format: true, presence: true
|
2013-10-22 02:49:51 +08:00
|
|
|
|
|
|
|
def self.watch(ip_address, opts={})
|
|
|
|
match_for_ip_address(ip_address) || create(opts.slice(:action_type).merge(ip_address: ip_address))
|
|
|
|
end
|
|
|
|
|
2013-11-06 00:24:13 +08:00
|
|
|
# In Rails 4.0.0, validators are run to handle invalid assignments to inet columns (as they should).
|
|
|
|
# In Rails 4.0.1, an exception is raised before validation happens, so we need this hack for
|
|
|
|
# inet/cidr columns:
|
2013-11-05 07:32:35 +08:00
|
|
|
def ip_address=(val)
|
2014-02-19 02:00:46 +08:00
|
|
|
if val.nil?
|
|
|
|
self.errors.add(:ip_address, :invalid)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2014-02-22 03:14:30 +08:00
|
|
|
return write_attribute(:ip_address, val) if val.is_a?(IPAddr)
|
|
|
|
|
2014-02-19 02:00:46 +08:00
|
|
|
num_wildcards = val.count('*')
|
|
|
|
if num_wildcards == 0
|
|
|
|
write_attribute(:ip_address, val)
|
|
|
|
else
|
|
|
|
v = val.gsub(/\/.*/, '')
|
|
|
|
if v[v.index('*')..-1] =~ /[^\.\*]/
|
|
|
|
self.errors.add(:ip_address, :invalid)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
write_attribute(:ip_address, "#{v.gsub('*', '0')}/#{32 - (num_wildcards * 8)}")
|
|
|
|
end
|
2013-11-18 06:31:44 +08:00
|
|
|
|
|
|
|
# this gets even messier, Ruby 1.9.2 raised a different exception to Ruby 2.0.0
|
|
|
|
# handle both exceptions
|
|
|
|
rescue ArgumentError, IPAddr::InvalidAddressError
|
2013-11-05 07:32:35 +08:00
|
|
|
self.errors.add(:ip_address, :invalid)
|
|
|
|
end
|
|
|
|
|
2014-02-18 23:33:08 +08:00
|
|
|
# Return a string with the ip address and mask in standard format. e.g., "127.0.0.0/8".
|
|
|
|
# Ruby's IPAddr class has no method for getting this.
|
|
|
|
def ip_address_with_mask
|
|
|
|
if ip_address
|
|
|
|
mask = ip_address.instance_variable_get(:@mask_addr).to_s(2).count('1')
|
|
|
|
if mask == 32
|
|
|
|
ip_address.to_s
|
|
|
|
else
|
2014-08-15 02:20:52 +08:00
|
|
|
"#{ip_address}/#{ip_address.instance_variable_get(:@mask_addr).to_s(2).count('1')}"
|
2014-02-18 23:33:08 +08:00
|
|
|
end
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-10-22 02:49:51 +08:00
|
|
|
def self.match_for_ip_address(ip_address)
|
|
|
|
# The <<= operator on inet columns means "is contained within or equal to".
|
|
|
|
#
|
|
|
|
# Read more about PostgreSQL's inet data type here:
|
|
|
|
#
|
|
|
|
# http://www.postgresql.org/docs/9.1/static/datatype-net-types.html
|
|
|
|
# http://www.postgresql.org/docs/9.1/static/functions-net.html
|
2014-05-06 21:41:59 +08:00
|
|
|
find_by("'#{ip_address.to_s}' <<= ip_address")
|
2013-10-22 02:49:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.should_block?(ip_address)
|
2013-10-24 05:11:11 +08:00
|
|
|
exists_for_ip_address_and_action?(ip_address, actions[:block])
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.is_whitelisted?(ip_address)
|
|
|
|
exists_for_ip_address_and_action?(ip_address, actions[:do_nothing])
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.exists_for_ip_address_and_action?(ip_address, action_type)
|
2013-10-22 02:49:51 +08:00
|
|
|
b = match_for_ip_address(ip_address)
|
|
|
|
b.record_match! if b
|
2013-10-24 05:11:11 +08:00
|
|
|
!!b and b.action_type == action_type
|
2013-10-22 02:49:51 +08:00
|
|
|
end
|
|
|
|
end
|
2013-12-05 14:40:35 +08:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: screened_ip_addresses
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# ip_address :inet not null
|
|
|
|
# action_type :integer not null
|
|
|
|
# match_count :integer default(0), not null
|
|
|
|
# last_match_at :datetime
|
2014-05-28 09:49:50 +08:00
|
|
|
# created_at :datetime
|
|
|
|
# updated_at :datetime
|
2013-12-05 14:40:35 +08:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_screened_ip_addresses_on_ip_address (ip_address) UNIQUE
|
|
|
|
# index_screened_ip_addresses_on_last_match_at (last_match_at)
|
|
|
|
#
|