hostman 0.1.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/LICENSE.txt +1 -1
- data/README.md +8 -14
- data/bin/hostman +0 -0
- data/lib/hostman.rb +18 -46
- data/lib/hostman/executor.rb +45 -0
- data/lib/hostman/version.rb +1 -1
- data/test/hostman_test.rb +6 -6
- metadata +3 -6
- data/test/fixtures/hosts1 +0 -1
- data/test/fixtures/hosts2 +0 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7cd4f304ea4abc9a4b1b7f4b8e9a4fe4c0a554c6
|
4
|
+
data.tar.gz: 23504ce17bd69897849e9c0009ed3aefdb93eacd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7bcd752925acea24ea8e0e3a9492bb67d28bad310e5320b31e58776c79a2efbfea2eae66b1ee3b52841f3205e7e5593fb1c271b77ceb029d2431bd040ba168dc
|
7
|
+
data.tar.gz: aab3ca0c1740abbb81ad86a5dbfee3fd978aaa7713304d32722d5152824f578078f7804a35e1f4e130265f0fa6743775ec9869c11f81eac9cdf7e0b2dbbd5c7e
|
data/.gitignore
CHANGED
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -1,28 +1,22 @@
|
|
1
1
|
# Hostman
|
2
2
|
|
3
|
-
|
3
|
+
Hostman (host manager) is a stand-alone ruby gem that makes blocking and unblocking domains a breeze.
|
4
4
|
|
5
|
-
|
5
|
+
![Demo gif](http://i.giphy.com/l0HlDHG0f1yBrfAsg.gif)
|
6
6
|
|
7
7
|
## Installation
|
8
8
|
|
9
|
-
|
9
|
+
Since this gem will edit the file `/etc/hosts`, in most day to day cases you'll need ti install it as root so it can be called using sudo.
|
10
10
|
|
11
|
-
|
12
|
-
gem 'hostman'
|
13
|
-
```
|
11
|
+
Install it using sudo:
|
14
12
|
|
15
|
-
|
13
|
+
$ sudo gem install hostman
|
16
14
|
|
17
|
-
|
18
|
-
|
19
|
-
Or install it yourself as:
|
20
|
-
|
21
|
-
$ gem install hostman
|
15
|
+
**NOTE:** You can use several workarounds to avoid having to sudo everytime you run this, if you do, it is under your own risk.
|
22
16
|
|
23
17
|
## Usage
|
24
18
|
|
25
|
-
|
19
|
+
|
26
20
|
|
27
21
|
## Development
|
28
22
|
|
@@ -32,7 +26,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
32
26
|
|
33
27
|
## Contributing
|
34
28
|
|
35
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
29
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/jeanlescure/hostman. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
36
30
|
|
37
31
|
|
38
32
|
## License
|
data/bin/hostman
CHANGED
File without changes
|
data/lib/hostman.rb
CHANGED
@@ -1,64 +1,36 @@
|
|
1
1
|
require "hostman/version"
|
2
|
+
require "hostman/executor"
|
2
3
|
require "slop"
|
3
4
|
|
4
5
|
module Hostman
|
5
6
|
BLOCK_UNBLOCK_ERROR = "Both --block and --unblock set, doing nothing..."
|
6
7
|
|
7
|
-
|
8
|
-
|
8
|
+
def self.block hosts_file, domain
|
9
|
+
not_blocked = true
|
9
10
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
o.string '-f', '--hosts-file', 'override hosts file path', default: '/etc/hosts'
|
14
|
-
o.bool '-o', '--output-hosts', 'output hosts file contents after execution', default: false
|
15
|
-
o.on '--version', 'print the version' do
|
16
|
-
puts Hostman::VERSION
|
17
|
-
exit
|
11
|
+
File.open(hosts_file, "r") do |f|
|
12
|
+
f.each_line do |line|
|
13
|
+
not_blocked = false if line == "127.0.0.1\t#{domain}\n"
|
18
14
|
end
|
19
15
|
end
|
20
16
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
rescue ArgumentError => err
|
25
|
-
puts err
|
26
|
-
exit
|
27
|
-
end
|
28
|
-
|
29
|
-
if !opts[:block].nil?
|
30
|
-
not_blocked = true
|
31
|
-
|
32
|
-
File.open(opts["hosts-file"], "r") do |f|
|
33
|
-
f.each_line do |line|
|
34
|
-
not_blocked = false if line == "127.0.0.1\t#{opts[:block]}\n"
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
if not_blocked
|
39
|
-
File.open(opts["hosts-file"], "a") do |f|
|
40
|
-
f.puts "127.0.0.1\t#{opts[:block]}"
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
if !opts[:unblock].nil?
|
46
|
-
contents = ""
|
47
|
-
File.open(opts["hosts-file"], "r") do |f|
|
48
|
-
f.each_line do |line|
|
49
|
-
contents += line unless line == "127.0.0.1\t#{opts[:unblock]}\n"
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
File.open(opts["hosts-file"], "w") do |f|
|
54
|
-
f.puts contents
|
17
|
+
if not_blocked
|
18
|
+
File.open(hosts_file, "a") do |f|
|
19
|
+
f.puts "127.0.0.1\t#{domain}"
|
55
20
|
end
|
56
21
|
end
|
22
|
+
end
|
57
23
|
|
58
|
-
|
59
|
-
|
24
|
+
def self.unblock hosts_file, domain
|
25
|
+
contents = ""
|
26
|
+
File.open(hosts_file, "r") do |f|
|
27
|
+
f.each_line do |line|
|
28
|
+
contents += line unless line == "127.0.0.1\t#{domain}\n"
|
60
29
|
end
|
30
|
+
end
|
61
31
|
|
32
|
+
File.open(hosts_file, "w") do |f|
|
33
|
+
f.puts contents
|
62
34
|
end
|
63
35
|
end
|
64
36
|
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module Hostman
|
2
|
+
class Executor
|
3
|
+
def exec
|
4
|
+
|
5
|
+
opts = Slop.parse do |o|
|
6
|
+
o.array '-b', '--block', 'a list of hostnames', delimiter: ','
|
7
|
+
o.array '-u', '--unblock', 'a list of hostname', delimiter: ','
|
8
|
+
o.string '-f', '--hosts-file', 'override hosts file path', default: '/etc/hosts'
|
9
|
+
o.bool '-o', '--output-hosts', 'output hosts file contents after execution', default: false
|
10
|
+
o.on '-h', '--help', 'print this help message' do
|
11
|
+
puts o
|
12
|
+
exit
|
13
|
+
end
|
14
|
+
o.on '--version', 'print the version' do
|
15
|
+
puts Hostman::VERSION
|
16
|
+
exit
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
begin
|
21
|
+
raise ArgumentError, ::Hostman::BLOCK_UNBLOCK_ERROR if !opts[:block].empty? and !opts[:unblock].empty?
|
22
|
+
rescue ArgumentError => err
|
23
|
+
puts err
|
24
|
+
exit
|
25
|
+
end
|
26
|
+
|
27
|
+
if !opts[:block].empty?
|
28
|
+
opts[:block].each do |domain|
|
29
|
+
::Hostman.block opts["hosts-file"], domain
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
if !opts[:unblock].empty?
|
34
|
+
opts[:unblock].each do |domain|
|
35
|
+
::Hostman.unblock opts["hosts-file"], domain
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
if opts.output_hosts?
|
40
|
+
puts File.read(opts["hosts-file"])
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
data/lib/hostman/version.rb
CHANGED
data/test/hostman_test.rb
CHANGED
@@ -24,14 +24,14 @@ describe Hostman do
|
|
24
24
|
result.must_equal ::Fixtures::HOSTS_CONTENT
|
25
25
|
end
|
26
26
|
|
27
|
-
it "can block and unblock
|
28
|
-
`hostman --block test.com --hosts-file ./test/fixtures/hosts2`
|
27
|
+
it "can block and unblock domains" do
|
28
|
+
`hostman --block test.com,test2.com --block test3.com --hosts-file ./test/fixtures/hosts2`
|
29
29
|
|
30
|
-
result = `hostman --block
|
31
|
-
result.must_equal "#{::Fixtures::HOSTS_CONTENT}\n127.0.0.1\ttest.com\n127.0.0.1\ttest2.com"
|
30
|
+
result = `hostman --block test4.com --hosts-file ./test/fixtures/hosts2 --output-hosts`.chop
|
31
|
+
result.must_equal "#{::Fixtures::HOSTS_CONTENT}\n127.0.0.1\ttest.com\n127.0.0.1\ttest2.com\n127.0.0.1\ttest3.com\n127.0.0.1\ttest4.com"
|
32
32
|
|
33
|
-
result = `hostman --unblock test.com --hosts-file ./test/fixtures/hosts2 --output-hosts`.chop
|
34
|
-
result.must_equal "#{::Fixtures::HOSTS_CONTENT}\n127.0.0.1\
|
33
|
+
result = `hostman --unblock test.com,test2.com --unblock test3.com --hosts-file ./test/fixtures/hosts2 --output-hosts`.chop
|
34
|
+
result.must_equal "#{::Fixtures::HOSTS_CONTENT}\n127.0.0.1\ttest4.com"
|
35
35
|
end
|
36
36
|
|
37
37
|
it "will not block more than once" do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hostman
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jean M. Lescure
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-08-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -85,9 +85,8 @@ files:
|
|
85
85
|
- bin/setup
|
86
86
|
- hostman.gemspec
|
87
87
|
- lib/hostman.rb
|
88
|
+
- lib/hostman/executor.rb
|
88
89
|
- lib/hostman/version.rb
|
89
|
-
- test/fixtures/hosts1
|
90
|
-
- test/fixtures/hosts2
|
91
90
|
- test/hostman_test.rb
|
92
91
|
- test/test_helper.rb
|
93
92
|
homepage: http://jeanlescure.io
|
@@ -114,7 +113,5 @@ signing_key:
|
|
114
113
|
specification_version: 4
|
115
114
|
summary: Manage the blocked hosts in /etc/hosts
|
116
115
|
test_files:
|
117
|
-
- test/fixtures/hosts1
|
118
|
-
- test/fixtures/hosts2
|
119
116
|
- test/hostman_test.rb
|
120
117
|
- test/test_helper.rb
|
data/test/fixtures/hosts1
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
127.0.0.1 localhost
|
data/test/fixtures/hosts2
DELETED