dark_domains 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,83 @@
1
+ require "rubygems"
2
+ require "ruby-debug"
3
+ require "uri"
4
+
5
+ class Domain
6
+
7
+ DOMAIN_URI = /^\w[\w\.\-\_]+\.[\w\.]{2,7}$/i.freeze
8
+
9
+ # Keep it simple
10
+ attr_accessor :domain
11
+
12
+ # Class methods
13
+ #===========================================================================
14
+
15
+ # Return all of the banned domains
16
+ def self.banned_domains
17
+ @@banned_domains ||= {}
18
+ end
19
+
20
+ # Return true if this is a known spam domain, false otherwise
21
+ def self.banned?(suspect_domain)
22
+ banned_domains[normalized_domain(suspect_domain)].eql?(true)
23
+ end
24
+
25
+ # Ban a domain
26
+ def self.ban!(domain)
27
+ banned_domains[normalized_domain(domain)] = true
28
+ end
29
+
30
+ # Return a normalized URL
31
+ def self.normalized_domain(domain)
32
+ # Strip www subdomains, but leave others
33
+ domain = domain.gsub(/www\./, '')
34
+ # Support protocols/ports/etc
35
+ domain =~ DOMAIN_URI ? domain : URI.parse(domain).host
36
+ end
37
+
38
+ # Instance
39
+ #===========================================================================
40
+
41
+ # Create a domain instance
42
+ def initialize(domain)
43
+ self.domain = domain
44
+ end
45
+
46
+ # Return true if we are a banned domain
47
+ def banned?
48
+ Domain.banned?(domain)
49
+ end
50
+
51
+ # Ban this specific domain instance
52
+ def ban!
53
+ Domain.ban!(domain)
54
+ end
55
+
56
+ # Blacklists
57
+ #===========================================================================
58
+
59
+ # Load existing known bad spammer domains, returns number of domains loaded
60
+ # @todo make this pull from a dynamic source on the net
61
+ def self.load_blacklist(absolute_path = nil)
62
+ absolute_path = default_blacklist_path if !absolute_path
63
+
64
+ raise "unable to find blacklist data file: #{ absolute_path }" \
65
+ unless File.exists?(absolute_path)
66
+
67
+ # keep for the total we'll return
68
+ existing_size = banned_domains.size
69
+
70
+ # Grab the domains, squash the newlines, ignore comments and blank lines
71
+ File.readlines(absolute_path).each{ |d| Domain.ban!(d.chomp) unless d =~ /(^\#|^\s+$)/ }
72
+
73
+ puts "loaded #{ banned_domains.size } banned domains." if $DEBUG
74
+
75
+ return banned_domains.size - existing_size
76
+ end
77
+
78
+ # Path to the default blacklist
79
+ def self.default_blacklist_path
80
+ File.expand_path(File.join(__FILE__, "..", "..", "data", "blacklist.txt"))
81
+ end
82
+
83
+ end
data/script/console ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # File: script/console
3
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
4
+
5
+ libs = " -r irb/completion"
6
+ # Perhaps use a console_lib to store any extra methods I may want available in the cosole
7
+ # libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
8
+ libs << " -r #{File.dirname(__FILE__) + '/../lib/dark_domains.rb'}"
9
+ puts "Loading dark_domains gem"
10
+ exec "#{irb} #{libs} --simple-prompt"
data/script/destroy ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/destroy'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Destroy.new.run(ARGV)
data/script/generate ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/generate'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Generate.new.run(ARGV)
@@ -0,0 +1,79 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe Domain do
4
+
5
+ context "(class)" do
6
+
7
+ before do
8
+ @valid_domain = "socialface.com"
9
+ @banned_domain = "myspace.com"
10
+
11
+ Domain.ban!(@banned_domain)
12
+ end
13
+
14
+ it "should have a #banned_domains method" do
15
+ Domain.should respond_to(:banned_domains)
16
+ end
17
+
18
+ it "should have a #ban! method" do
19
+ Domain.should respond_to(:ban!)
20
+ end
21
+
22
+ it "should have a #banned? method" do
23
+ Domain.should respond_to(:banned?)
24
+ end
25
+
26
+ it "should return false for valid or unknown domains" do
27
+ Domain.banned?(@valid_domain).should be_false
28
+ end
29
+
30
+ it "should return true for known spam domains" do
31
+ Domain.banned?(@banned_domain).should be_true
32
+ end
33
+
34
+ it "should ignore protocol information from domains" do
35
+ Domain.banned?("http://#{ @banned_domain }").should be_true
36
+ end
37
+
38
+ it "should ignore www subdomain prefixes" do
39
+ Domain.banned?("www.#{ @banned_domain }").should be_true
40
+ end
41
+
42
+ end
43
+
44
+ context "(blacklists)" do
45
+
46
+ it "should load an existing default blacklist" do
47
+ lambda do
48
+ begin
49
+ Domain.load_blacklist
50
+ end
51
+ end.should change(Domain.banned_domains, :size)
52
+ end
53
+
54
+ it "should have the path to a default blacklist" do
55
+ File.exists?(Domain::default_blacklist_path).should be_true
56
+ end
57
+
58
+ end
59
+
60
+ context "(instance)" do
61
+
62
+ before do
63
+ @valid_domain = Domain.new("socialface.com")
64
+ @banned_domain = Domain.new("myspace.com")
65
+
66
+ @banned_domain.ban!
67
+ end
68
+
69
+ it "should return false if not banned" do
70
+ @valid_domain.banned?.should be_false
71
+ end
72
+
73
+ it "should return false if not banned" do
74
+ @banned_domain.banned?.should be_true
75
+ end
76
+
77
+ end
78
+
79
+ end
@@ -0,0 +1,5 @@
1
+ $TESTING=true
2
+ $:.push File.join(File.dirname(__FILE__), '..', 'lib')
3
+
4
+ require "lib/dark_domains/models/domain.rb"
5
+
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dark_domains
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Often Void, Inc.
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-12-29 00:00:00 -06:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hoe
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 2.4.0
24
+ version:
25
+ description: A Ruby plugin to load and query banned domains.
26
+ email:
27
+ - admin@oftenvoid.com
28
+ executables: []
29
+
30
+ extensions: []
31
+
32
+ extra_rdoc_files:
33
+ - History.txt
34
+ - Manifest.txt
35
+ - PostInstall.txt
36
+ - lib/dark_domains/data/blacklist.txt
37
+ files:
38
+ - History.txt
39
+ - Manifest.txt
40
+ - PostInstall.txt
41
+ - README.rdoc
42
+ - Rakefile
43
+ - lib/dark_domains.rb
44
+ - lib/dark_domains/models/domain.rb
45
+ - script/console
46
+ - script/destroy
47
+ - script/generate
48
+ - spec/domain_spec.rb
49
+ - spec/spec_helper.rb
50
+ - lib/dark_domains/data/blacklist.txt
51
+ has_rdoc: true
52
+ homepage: http://github.com/oftenvoid/dark_domains
53
+ licenses: []
54
+
55
+ post_install_message: PostInstall.txt
56
+ rdoc_options:
57
+ - --main
58
+ - README.rdoc
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: "0"
66
+ version:
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: "0"
72
+ version:
73
+ requirements: []
74
+
75
+ rubyforge_project: dark_domains
76
+ rubygems_version: 1.3.5
77
+ signing_key:
78
+ specification_version: 3
79
+ summary: A Ruby plugin to load and query banned domains.
80
+ test_files: []
81
+