diff options
author | Patrick J Cherry <patrick@bytemark.co.uk> | 2012-11-21 11:57:17 +0000 |
---|---|---|
committer | Patrick J Cherry <patrick@bytemark.co.uk> | 2012-11-21 11:57:17 +0000 |
commit | 877afea59e9d85cecbefa2fe01df5adc6fa252b1 (patch) | |
tree | edd06426ddde9a076e9644d04442b661d4d4e849 | |
parent | 9f4910bef307cb9c0c78c4e45710956f3521774c (diff) |
Removed duplicate code from Alert#in_source_list, and
SourceList#include. Also cut down on DNS queries, and IPAddr
conversions.
-rw-r--r-- | lib/mauve/alert.rb | 45 | ||||
-rw-r--r-- | lib/mauve/source_list.rb | 19 |
2 files changed, 14 insertions, 50 deletions
diff --git a/lib/mauve/alert.rb b/lib/mauve/alert.rb index 52c098c..4629191 100644 --- a/lib/mauve/alert.rb +++ b/lib/mauve/alert.rb @@ -190,50 +190,7 @@ module Mauve source_list = Mauve::Configuration.current.source_lists[listname] return false unless source_list.is_a?(SourceList) - host = self.subject - - # - # Pick out hostnames from URIs. - # - if host =~ /^[a-z][a-z0-9+-]+:\/\// - begin - uri = URI.parse(host) - host = uri.host unless uri.host.nil? - rescue URI::InvalidURIError => ex - # ugh - logger.warn "Did not recognise URI #{host}" - end - end - - return true if source_list.list.any? do |l| - case l - when String - host == l - when Regexp - host =~ l - when IPAddr - begin - l.include?(IPAddr.new(host)) - rescue ArgumentError => err - # rescue random IPAddr argument errors - false - end - else - false - end - end - - return false unless source_list.list.any?{|l| l.is_a?(IPAddr)} - - @subject_ips ||= MauveResolv.get_ips_for(host).collect{|i| IPAddr.new(i)} - - return false if @subject_ips.nil? or @subject_ips.empty? - - return source_list.list.select{|i| i.is_a?(IPAddr)}.any? do |list_ip| - @subject_ips.any?{|ip| list_ip.include?(ip)} - end - - return false + source_list.include?(self.subject) end # Returns the alert level diff --git a/lib/mauve/source_list.rb b/lib/mauve/source_list.rb index 0596177..ad84670 100644 --- a/lib/mauve/source_list.rb +++ b/lib/mauve/source_list.rb @@ -123,6 +123,13 @@ module Mauve end end + host_as_ip = nil + begin + host_as_ip = IPAddr.new(host) + rescue ArgumentError + # Rescue IPAddr argument errors, i.e. host is not an IP address. + end + return true if self.list.any? do |l| case l when String @@ -130,17 +137,17 @@ module Mauve when Regexp host =~ l when IPAddr - begin - l.include?(IPAddr.new(host)) - rescue ArgumentError - # rescue random IPAddr argument errors - false - end + host_as_ip.is_a?(IPAddr) and l.include?(host_as_ip) else false end end + # + # To cut down the amount of DNS queries, we'll bail out at this point. + # + return false + return false unless self.list.any?{|l| l.is_a?(IPAddr)} ips = MauveResolv.get_ips_for(host).collect{|i| IPAddr.new(i)} |