hostscare 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/bin/hostscare +19 -0
  3. data/lib/hostscare.rb +60 -0
  4. metadata +46 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 2712bc088d6b1a66fb7ada3bf66bb6bf06f1aff374021b98192f0d5ea635f4fd
4
+ data.tar.gz: d7591c0586617c7ace35e96f8ed2eb57f77c3769b3e94770de94ce0597422e3b
5
+ SHA512:
6
+ metadata.gz: 433a3a0fddf167ebe29c252b8a3fb3fbc82b53e1cfd8942b863856a5c6b5ff174c2f724fdb692cbf3d4e32f8a96c417bfa6d0c36a7538394b3f4b8c76c821b7e
7
+ data.tar.gz: 1656e01c6a27239c5a49e101725371f409b6b35b4fc6af6b5bb2a23f31c0714f59cd1e3e546e3016ebc66e88e310e00c01f3c782c7a6517fa10b93ae7812fab7
@@ -0,0 +1,19 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'hostscare'
5
+
6
+ command = ARGV.count.positive? ? ARGV[0] : nil
7
+
8
+ case command
9
+ when 'cleanup'
10
+ Hostscare.cleanup
11
+ when 'update'
12
+ Hostscare.update
13
+ when 'version'
14
+ puts Gem.loaded_specs['hostscare'].version
15
+ when nil
16
+ puts 'Command required'
17
+ else
18
+ puts "Command #{command} not found"
19
+ end
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'open-uri'
4
+
5
+ module Hostscare
6
+ PLACEHOLDER_START = '## START-someonewhocares'
7
+ PLACEHOLDER_END = '## END-someonewhocares'
8
+ LOCAL_HOSTS_FILE = '/etc/hosts'
9
+ PUBLIC_HOSTS_FILE = 'https://someonewhocares.org/hosts/hosts'
10
+
11
+ def self.cleanup
12
+ hosts = File.read(LOCAL_HOSTS_FILE)
13
+ write(raw_hosts(hosts), LOCAL_HOSTS_FILE)
14
+ end
15
+
16
+ def self.update
17
+ hosts = File.read(LOCAL_HOSTS_FILE)
18
+ someonewhocares_hosts = URI.open(PUBLIC_HOSTS_FILE).read
19
+
20
+ new_hosts = build_hosts(raw_hosts(hosts),
21
+ PLACEHOLDER_START,
22
+ PLACEHOLDER_END,
23
+ someonewhocares_hosts)
24
+
25
+ write(new_hosts, LOCAL_HOSTS_FILE)
26
+ end
27
+
28
+ # Compose the new hosts file
29
+ def self.build_hosts(hosts, ph_start, ph_end, someonewhocares_hosts)
30
+ final_hosts = [hosts,
31
+ ph_start,
32
+ "# -- Updated on #{Time.now}",
33
+ someonewhocares_hosts,
34
+ ph_end]
35
+
36
+ final_hosts.map(&:strip)
37
+ .join("\n\n")
38
+ end
39
+
40
+ def self.write(txt, path)
41
+ File.open(path, 'w') do |f|
42
+ f.write(txt)
43
+ end
44
+ puts "🍺 #{path} updated!"
45
+ rescue Errno::EACCES
46
+ puts "❌ Requires sudo to update the file #{path}"
47
+ end
48
+
49
+ # Extract hosts without those coming from someonewhocares
50
+ def self.raw_hosts(txt)
51
+ hosts = []
52
+ header_txt = txt.scan(/^((.|\n)*)#{PLACEHOLDER_START}/)
53
+ hosts << header_txt[0][0] if header_txt.count.positive?
54
+
55
+ footer_txt = txt.scan(/#{PLACEHOLDER_END}((.|\n)*)/)
56
+ hosts << footer_txt[0][0] if footer_txt.count.positive?
57
+
58
+ hosts.count.positive? ? hosts.map(&:strip).join("\n\n") : txt
59
+ end
60
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hostscare
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - ghn
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-09-08 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Avoid the worst part of the internet by routing all those website to
14
+ localhost
15
+ email:
16
+ executables:
17
+ - hostscare
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - bin/hostscare
22
+ - lib/hostscare.rb
23
+ homepage: https://gitlab.com/ghn/hostscare
24
+ licenses:
25
+ - MIT
26
+ metadata: {}
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubygems_version: 3.0.1
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: Update your hosts file with the list from someonewhocares
46
+ test_files: []