2017-10-04 15:16:10 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: email_domain_blocks
|
|
|
|
#
|
2022-04-29 23:27:03 +02:00
|
|
|
# id :bigint(8) not null, primary key
|
|
|
|
# domain :string default(""), not null
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
# parent_id :bigint(8)
|
2017-10-04 15:16:10 +02:00
|
|
|
#
|
|
|
|
|
|
|
|
class EmailDomainBlock < ApplicationRecord
|
2022-04-29 23:27:03 +02:00
|
|
|
self.ignored_columns = %w(
|
|
|
|
ips
|
|
|
|
last_refresh_at
|
|
|
|
)
|
|
|
|
|
2018-12-26 06:38:42 +01:00
|
|
|
include DomainNormalizable
|
2017-11-14 20:37:17 +01:00
|
|
|
|
2020-03-12 22:35:20 +01:00
|
|
|
belongs_to :parent, class_name: 'EmailDomainBlock', optional: true
|
|
|
|
has_many :children, class_name: 'EmailDomainBlock', foreign_key: :parent_id, inverse_of: :parent, dependent: :destroy
|
|
|
|
|
2019-08-08 23:04:19 +02:00
|
|
|
validates :domain, presence: true, uniqueness: true, domain: true
|
2017-11-14 20:37:17 +01:00
|
|
|
|
2022-02-24 17:28:23 +01:00
|
|
|
# Used for adding multiple blocks at once
|
|
|
|
attr_accessor :other_domains
|
2020-03-12 22:35:20 +01:00
|
|
|
|
2022-02-24 17:28:23 +01:00
|
|
|
def history
|
|
|
|
@history ||= Trends::History.new('email_domain_blocks', id)
|
2020-03-12 22:35:20 +01:00
|
|
|
end
|
|
|
|
|
2022-08-24 19:00:55 +02:00
|
|
|
class Matcher
|
|
|
|
def initialize(domain_or_domains, attempt_ip: nil)
|
|
|
|
@uris = extract_uris(domain_or_domains)
|
|
|
|
@attempt_ip = attempt_ip
|
|
|
|
end
|
2022-02-24 17:28:23 +01:00
|
|
|
|
2022-08-24 19:00:55 +02:00
|
|
|
def match?
|
|
|
|
blocking? || invalid_uri?
|
2022-02-24 17:28:23 +01:00
|
|
|
end
|
2020-03-12 22:35:20 +01:00
|
|
|
|
2022-08-24 19:00:55 +02:00
|
|
|
private
|
2017-11-14 20:37:17 +01:00
|
|
|
|
2022-08-24 19:00:55 +02:00
|
|
|
def invalid_uri?
|
|
|
|
@uris.any?(&:nil?)
|
|
|
|
end
|
2017-11-14 20:37:17 +01:00
|
|
|
|
2022-08-24 19:00:55 +02:00
|
|
|
def blocking?
|
|
|
|
blocks = EmailDomainBlock.where(domain: domains_with_variants).order(Arel.sql('char_length(domain) desc'))
|
|
|
|
blocks.each { |block| block.history.add(@attempt_ip) } if @attempt_ip.present?
|
|
|
|
blocks.any?
|
2017-11-14 20:37:17 +01:00
|
|
|
end
|
|
|
|
|
2022-08-24 19:00:55 +02:00
|
|
|
def domains_with_variants
|
|
|
|
@uris.flat_map do |uri|
|
|
|
|
next if uri.nil?
|
|
|
|
|
|
|
|
segments = uri.normalized_host.split('.')
|
|
|
|
|
|
|
|
segments.map.with_index { |_, i| segments[i..-1].join('.') }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def extract_uris(domain_or_domains)
|
|
|
|
Array(domain_or_domains).map do |str|
|
|
|
|
domain = begin
|
|
|
|
if str.include?('@')
|
|
|
|
str.split('@', 2).last
|
|
|
|
else
|
|
|
|
str
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Addressable::URI.new.tap { |u| u.host = domain.strip } if domain.present?
|
|
|
|
rescue Addressable::URI::InvalidURIError, IDN::Idna::IdnaError
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.block?(domain_or_domains, attempt_ip: nil)
|
|
|
|
Matcher.new(domain_or_domains, attempt_ip: attempt_ip).match?
|
2017-10-04 15:16:10 +02:00
|
|
|
end
|
|
|
|
end
|