hosts_updater 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f035702771324651cd8c2fa2f1e7248237e96b8b
4
+ data.tar.gz: afb2b6556b7133fc4261a5cd02006f33e42fda02
5
+ SHA512:
6
+ metadata.gz: aa6f8c165bdfbec09b539ec9ed4602307fe09e9f3a7df91a65bc16eed63ec5a7a5178fb941b191f232640d41936a7a64fdff64a3f316fbb4a5151cada952f52b
7
+ data.tar.gz: cd03ec664d6908db1a0bc0c706441a43093ff999625ca8a745e28df3b669a0fa0db47ef94c12f57860d3dd604a565413ad10f21d4458da41dac3c29d0edbf08c
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ Gemfile.lock
2
+ pkg/*.gem
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2014 Bernard Potocki
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,51 @@
1
+ # Hosts Updater
2
+
3
+ This script indends to help manage large collection of blacklisted domains. It's best to describe it as replacement for adblock, but instead of slowing down your browser it blocks domains of ad and scam websites at DNS level.
4
+
5
+ ## Quick start guide
6
+
7
+ ```sh
8
+ $ gem install hosts_updater
9
+ $ sudo hosts-updater --help
10
+ ```
11
+
12
+ It required sudo in order to write to `/etc/hosts` file.
13
+
14
+ ## What it does?
15
+
16
+ This script download lists of malicious domains from following websites:
17
+
18
+ - http://www.malwaredomainlist.com/hostslist/hosts.txt
19
+ - http://winhelp2002.mvps.org/hosts.txt
20
+ - http://someonewhocares.org/hosts/hosts
21
+ - http://pgl.yoyo.org/adservers/serverlist.php?hostformat=hosts&mimetype=plaintext
22
+
23
+ All of those lists are combined, duplicates removed and combined with your current `hosts` file (no data will should lost as original `hosts` file is copied and reused in future).
24
+
25
+ ## How is it better than AdBlock/Ghostery etc?
26
+
27
+ Each browser plugin that blocks ads is using [large amount of resources, hogs browser, and often causes strange errors](http://www.reddit.com/r/programming/comments/25j41u/adblock_pluss_effect_on_firefoxs_memory_usage/chhpomw). In constrast to this blacklisting domains in `/etc/hosts` takes couple seconds after file change, but after that has nearly zero impact on performance. Additionally AdBlock is removing parts of websited very aggresively, often leaving indended layout broken - it should be less common problem with domain blacklisting.
28
+
29
+ On the other hand there are small amount of ads that are unblockable by domain blacklisting - that are visible from time to time, but probably not often enough to complain ;)
30
+
31
+ ## How to configure what domains I want whitelisted/blacklisted?
32
+
33
+ During first run `hosts-updater` will create folder in `/etc/hosts.d`, in which it will store all configuration files:
34
+
35
+ - `hosts.custom` - this will be copy of old `/etc/hosts` and it will be added to `/etc/hosts` at top upon each use of `hosts-updater`. You should store you developer domains and all similar stuff inside of this file, as it will never be modified by this script. If you need to blacklist additional domains you should put them here too.
36
+ - `hosts.auto` - this will be regenerated upon calling `hosts-updater` with `--update` flag. It stores downloaded blacklists and is used to regenerate `/etc/hosts`. It's good idea to refresh it from time to time.
37
+ - `hosts.whitelist` - in this file you can place domain that you want to have access to despite figuring in one of downloaded lists. In order to do so simply paste full domain name inside there (one domain per line) and it will be picked up during next `hosts-updater` run.
38
+
39
+ ## Any other configuration options?
40
+
41
+ See `hosts-updater --help` for more configuration options.
42
+
43
+ ## Are you owner of those blacklists?
44
+
45
+ No - I'm just using them with proper attribution. All kudos and suggestions should be sent to maintainers of appropriate lists.
46
+
47
+ ## License
48
+
49
+ MIT License
50
+
51
+ Copyright © 2014 Bernard Potocki
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
data/bin/hosts-updater ADDED
@@ -0,0 +1,60 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+ require 'hosts_updater'
5
+
6
+ options = { :sources => {} }
7
+
8
+ OptionParser.new do |opts|
9
+ opts.banner = "Usage: hosts-updater [options]"
10
+
11
+ opts.on("-h", "--help", "Show this message") do
12
+ puts opts
13
+ exit
14
+ end
15
+
16
+ opts.on("--hosts-file", "Path to hosts file to write (default: /etc/hosts)") do |file|
17
+ opts[:hosts_file] = file
18
+ end
19
+
20
+ opts.on("--hosts-directory", "Path to hosts.d config directory (default: /etc/hosts.d/)") do |file|
21
+ opts[:hosts_dir] = file
22
+ end
23
+
24
+ opts.on("--hosts-auto-name", "Name for auto-generated file in hosts.d dir (default: hosts.auto)") do |file|
25
+ opts[:hosts_auto_name] = file
26
+ end
27
+
28
+ opts.on("--hosts-custom-name", "Name for custom hosts file in hosts.d dir (default: hosts.custom)") do |file|
29
+ opts[:hosts_custom_name] = file
30
+ end
31
+
32
+ opts.on("--hosts-whitelist-name", "Name for file with whitelisted domains in hosts.d dir (defaule: hosts.whitelist)") do |file|
33
+ opts[:hosts_whitelist_name] = file
34
+ end
35
+
36
+
37
+ opts.on("-i", "--ip", "Redirect all blacklisted domains to following IP (default: 0.0.0.0)") do |ip|
38
+ options[:id] = ip
39
+ end
40
+
41
+ opts.on("-u", "--[no-]update", "Update sources") do |u|
42
+ options[:update] = u
43
+ end
44
+
45
+ HostsUpdater::SOURCES.each do |source_key, source_data|
46
+ opts.on("--skip-#{source_key}", "Skip downloading #{source_data[1]} (#{source_data[0]})") do |s|
47
+ options[:sources][source_key] = false
48
+ end
49
+ end
50
+
51
+ opts.on("-q", "--quiet", "Run completely silently") do |q|
52
+ options[:quiet] = q
53
+ end
54
+
55
+ opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
56
+ options[:verbose] = v
57
+ end
58
+ end.parse!
59
+
60
+ HostsUpdater.new(options).run
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "hosts_updater/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "hosts_updater"
7
+ s.version = HostsUpdater::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Bernard Potocki"]
10
+ s.email = ["bernard.potocki@imanel.org"]
11
+ s.homepage = "http://github.com/imanel/hosts_updater"
12
+ s.license = 'MIT'
13
+ s.summary = %q{Update your /etc/hosts with list of unwanted domains}
14
+ s.description = %q{Update your /etc/hosts with list of unwanted domains}
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.lines.map { |f| File.basename(f.chomp) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_dependency('hosts', '~> 0.1.1')
22
+ end
@@ -0,0 +1,146 @@
1
+ require 'hosts'
2
+ require 'logger'
3
+ require 'open-uri'
4
+
5
+ class HostsUpdater
6
+
7
+ SOURCES = {
8
+ :mdl => ['http://www.malwaredomainlist.com/hostslist/hosts.txt', 'Malware Domain List'],
9
+ :mvps => ['http://winhelp2002.mvps.org/hosts.txt', 'MVPS Hosts'],
10
+ :swc => ['http://someonewhocares.org/hosts/hosts', "Dan Pollock's List"],
11
+ :yoyo => ['http://pgl.yoyo.org/adservers/serverlist.php?hostformat=hosts&mimetype=plaintext', "Peter Lowe's Ad Server List"]
12
+ }
13
+
14
+ DEFAULT_OPTIONS = {
15
+ :hosts_file => '/etc/hosts',
16
+ :hosts_directory => '/etc/hosts.d/',
17
+ :hosts_auto_name => 'hosts.auto',
18
+ :hosts_custom_name => 'hosts.custom',
19
+ :hosts_whitelist_name => 'hosts.whitelist',
20
+ :ip => '0.0.0.0',
21
+ :sources => SOURCES,
22
+ :update => false,
23
+ :quiet => false,
24
+ :verbose => false
25
+ }
26
+
27
+ def initialize(options = {})
28
+ sources = options.delete(:sources) || {}
29
+ @options = DEFAULT_OPTIONS.merge(options)
30
+ @options[:sources].merge!(sources)
31
+ end
32
+
33
+ # Create required files, update auto file if necessary and generate hosts file.
34
+ # Need to be ran as root in order to access /etc/hosts
35
+ def run
36
+ unless ENV['USER'] == 'root'
37
+ logger.error 'Error: Must run as root'
38
+ exit
39
+ end
40
+
41
+ bootstrap
42
+ update_auto_file if @options[:update]
43
+ update_hosts_file
44
+
45
+ logger.info 'Done.'
46
+ end
47
+
48
+ private
49
+
50
+ def logger
51
+ return @logger if defined?(@logger)
52
+ @logger = Logger.new(STDOUT)
53
+ @logger.level = if @options[:quiet]
54
+ Logger::ERROR
55
+ elsif @options[:verbose]
56
+ Logger::DEBUG
57
+ else
58
+ Logger::INFO
59
+ end
60
+ @logger.formatter = proc { |severity, datetime, progname, msg| "#{msg}\n" }
61
+ @logger
62
+ end
63
+
64
+ # Create /etc/hosts.d and files inside.
65
+ # If /etc/hosts.d/hosts.custom does not exists then it will copy content of /etc/hosts there.
66
+ def bootstrap
67
+ unless Dir.exists? @options[:hosts_directory]
68
+ logger.debug "Creating configuration directory at #{@options[:hosts_directory]}"
69
+ FileUtils.mkdir_p @options[:hosts_directory]
70
+ end
71
+
72
+ unless File.exists? hosts_custom_location
73
+ logger.debug "Copying #{@options[:hosts_file]} to #{hosts_custom_location}"
74
+ File.write(hosts_custom_location, File.read(@options[:hosts_file]))
75
+ end
76
+
77
+ unless File.exists? hosts_auto_location
78
+ logger.debug "Writing default #{hosts_auto_location}"
79
+ File.touch(hosts_auto_location)
80
+ end
81
+
82
+ unless File.exists? hosts_whitelist_location
83
+ logger.debug "Writing default #{hosts_whitelist_location}"
84
+ File.write(hosts_whitelist_location, "# List domains that should be ignored here.\n# One domain per line, lines starting with # will be ignored.\n")
85
+ end
86
+ end
87
+
88
+ # Download files mentioned in SOURCES and combine them into one saved as hosts_auto_location.
89
+ # Any comments and duplicates are ommited from original files.
90
+ def update_auto_file
91
+ elements = []
92
+ @options[:sources].each do |source_key, source_data|
93
+ next unless source_data
94
+ elements += download_source(source_data)
95
+ end
96
+ hosts = Hosts::File.new
97
+ hosts.elements = elements.uniq(&:name).sort { |e1,e2| e1.name <=> e2.name }
98
+ hosts.elements.insert 0, Hosts::Comment.new(' This file was auto-generated by hosts-updater.')
99
+ hosts.elements.insert 1, Hosts::Comment.new(' Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.')
100
+ hosts.elements.insert 2, Hosts::EmptyElement.new
101
+
102
+ File.write(hosts_auto_location, hosts.to_s(:force_generation => true))
103
+ end
104
+
105
+ # Combine hosts.custom and hosts.auto files into one and write to hosts_location.
106
+ # Domains listed in hosts.whitelist are ommited from hosts.auto list, but not from hosts.custom
107
+ def update_hosts_file
108
+ auto = Hosts::File.read(hosts_auto_location).elements
109
+ auto.reject! { |el| ! el.is_a? Aef::Hosts::Entry }
110
+ auto.reject! { |el| whitelist.include? el.name }
111
+ auto.each do |el|
112
+ el.address = @options[:ip]
113
+ end
114
+
115
+ hosts = Hosts::File.new(@options[:hosts_file])
116
+ hosts.elements = Hosts::File.read(hosts_custom_location).elements
117
+ hosts.elements << Hosts::EmptyElement.new
118
+ hosts.elements << Hosts::Section.new('HOSTS-UPDATER', :elements => auto)
119
+ logger.debug "Writing to #{@options[:hosts_file]}"
120
+ hosts.write
121
+ end
122
+
123
+ def download_source(source)
124
+ logger.debug "Downloading source #{source[0]}" + (source[1] ? " (#{source[1]})" : "")
125
+ data = open(source[0])
126
+ Hosts::File.parse(data.read).elements.select { |el| el.is_a? Aef::Hosts::Entry }
127
+ end
128
+
129
+ # Read hosts.whitelist file and ignore all lines starting with #
130
+ def whitelist
131
+ @whitelist ||= File.read(hosts_whitelist_location).lines.collect(&:strip).reject { |el| /^#/ =~ el }
132
+ end
133
+
134
+ def hosts_auto_location
135
+ @options[:hosts_directory] + @options[:hosts_auto_name]
136
+ end
137
+
138
+ def hosts_custom_location
139
+ @options[:hosts_directory] + @options[:hosts_custom_name]
140
+ end
141
+
142
+ def hosts_whitelist_location
143
+ @options[:hosts_directory] + @options[:hosts_whitelist_name]
144
+ end
145
+
146
+ end
@@ -0,0 +1,3 @@
1
+ class HostsUpdater
2
+ VERSION = "1.0.0"
3
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hosts_updater
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Bernard Potocki
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: hosts
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.1.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.1.1
27
+ description: Update your /etc/hosts with list of unwanted domains
28
+ email:
29
+ - bernard.potocki@imanel.org
30
+ executables:
31
+ - hosts-updater
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - ".gitignore"
36
+ - Gemfile
37
+ - LICENSE
38
+ - README.md
39
+ - Rakefile
40
+ - bin/hosts-updater
41
+ - hosts_updater.gemspec
42
+ - lib/hosts_updater.rb
43
+ - lib/hosts_updater/version.rb
44
+ homepage: http://github.com/imanel/hosts_updater
45
+ licenses:
46
+ - MIT
47
+ metadata: {}
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubyforge_project:
64
+ rubygems_version: 2.2.2
65
+ signing_key:
66
+ specification_version: 4
67
+ summary: Update your /etc/hosts with list of unwanted domains
68
+ test_files: []