From 877afea59e9d85cecbefa2fe01df5adc6fa252b1 Mon Sep 17 00:00:00 2001 From: Patrick J Cherry Date: Wed, 21 Nov 2012 11:57:17 +0000 Subject: Removed duplicate code from Alert#in_source_list, and SourceList#include. Also cut down on DNS queries, and IPAddr conversions. --- lib/mauve/alert.rb | 45 +-------------------------------------------- lib/mauve/source_list.rb | 19 +++++++++++++------ 2 files changed, 14 insertions(+), 50 deletions(-) (limited to 'lib/mauve') 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)} -- cgit v1.2.1