Browse Source

close #61 implement dnsbl

pull/1/head
Nicolae Claudius 11 years ago
parent
commit
a324c2f569
  1. 8
      app/models/blacklisted_domain.rb
  2. 1
      config/application.rb
  3. 15
      lib/dnsbl.rb
  4. 15
      spec/lib/dnsbl_spec.rb
  5. 1
      spec/models/blacklisted_domain_spec.rb

8
app/models/blacklisted_domain.rb

@ -1,8 +1,12 @@
class BlacklistedDomain < ActiveRecord::Base class BlacklistedDomain < ActiveRecord::Base
# attr_accessible :name # attr_accessible :name
def self.include?(name) scope :of, ->(domain_name) {
where("blacklisted_domains.name = ? OR ? LIKE CONCAT('%.', blacklisted_domains.name)", where("blacklisted_domains.name = ? OR ? LIKE CONCAT('%.', blacklisted_domains.name)",
name, name).exists? domain_name, domain_name)
}
def self.include?(domain_name)
of(domain_name).exists? || Dnsbl.include?(domain_name)
end end
end end

1
config/application.rb

@ -15,6 +15,7 @@ module Entrydns
config.autoload_paths += %W( config.autoload_paths += %W(
#{config.root}/app/controllers/concerns #{config.root}/app/controllers/concerns
#{config.root}/app/models/concerns #{config.root}/app/models/concerns
#{config.root}/lib
) )
# Only load the plugins named here, in the order given (default is alphabetical). # Only load the plugins named here, in the order given (default is alphabetical).

15
lib/dnsbl.rb

@ -0,0 +1,15 @@
class Dnsbl
PROVIDER = '.in.dnsbl.org'
@dns = Resolv::DNS.new
def self.include?(domain_name)
query(domain_name) != nil
end
def self.query(domain_name)
@dns.getresource(domain_name + PROVIDER, Resolv::DNS::Resource::IN::A)
rescue Resolv::ResolvError
nil
end
end

15
spec/lib/dnsbl_spec.rb

@ -0,0 +1,15 @@
require 'spec_helper'
describe Dnsbl do
include_context "data"
it "queries correctly" do
Dnsbl.query('does-not-exist-domain-1234567890.net') == nil
end
it "distinguishes bad domains from good ones" do
Dnsbl.include?('place4porn.net').should == true
Dnsbl.include?('entrydns.net').should == false
end
end

1
spec/models/blacklisted_domain_spec.rb

@ -7,6 +7,7 @@ describe BlacklistedDomain do
blacklisted_domain blacklisted_domain
BlacklistedDomain.should include(blacklisted_domain.name) BlacklistedDomain.should include(blacklisted_domain.name)
BlacklistedDomain.should include("sub.#{blacklisted_domain.name}") BlacklistedDomain.should include("sub.#{blacklisted_domain.name}")
BlacklistedDomain.should include("place4porn.net")
BlacklistedDomain.should_not include(FactoryGirl.generate(:domain_name)) BlacklistedDomain.should_not include(FactoryGirl.generate(:domain_name))
end end
end end

Loading…
Cancel
Save