resolve-hostname 0.0.1

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: db097d50197dcc1acea87f5791721ca570040527
4
+ data.tar.gz: 59fb02477c21f54bdabb4580e6d3c78ec93b9930
5
+ SHA512:
6
+ metadata.gz: 0ded77d0a22711f83efbd46bf97bf9a6c45ea1424b8f969986a7ba061f5979acbe1b7b85de188d6d59ccf3612953df2b48dae075eeb98926920d522e9b4178f7
7
+ data.tar.gz: 9776797a3bd95078ecbd8194483b740f4e21d98c883cb37d06e1dfafd695ebb2c4423a2e1988e61d5d155a934ba9e5ae085692495a035e34671ff5409f0a3656
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in resolve-dns-cached.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 TAGOMORI Satoshi
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,94 @@
1
+ # Resolve::Hostname
2
+
3
+ `Resolve::Hostname` is hostname resolver with:
4
+
5
+ * caching with specified TTL
6
+ * reloading of name server configurations (ex: `/etc/resolv.conf`)
7
+ * skipping system resolver (default) or not when specified
8
+ * primary IP address version specification (default: IPv4)
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ gem 'resolve-hostname'
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install resolve-hostname
23
+
24
+ ## Usage
25
+
26
+ require 'resolve/hostname'
27
+
28
+ resolver = Resolve::Hostname.new
29
+ resolver.getaddress('www.google.com') #=> "173.194.72.103"
30
+ resolver.getaddress('www.google.com') #=> "173.194.72.103" # from cache
31
+
32
+ Cache TTL(seconds) setting available for each resolver instances like this:
33
+
34
+ r = Resolve::Hostname.new(:ttl => 10) # default 60 seconds
35
+
36
+ r.getaddress('www.example.com')
37
+ sleep 11
38
+ r.getaddress('www.example.com') # not from cache, but from actual dns record (and cached)
39
+
40
+ You can specify `resolver_ttl` with expectation for re-reading of `/etc/resolv.conf` in long life daemons.
41
+
42
+ r = Resolve::Hostname.new(:ttl => 10, :resolver_ttl => 20)
43
+ r.getaddress('www.example.com')
44
+
45
+ Resolver raises `Resolve::Hostname::NotFoundError` when any records be found, and you can stop it (nil returned):
46
+
47
+ r1 = Resolve::Hostname.new
48
+ r1.getaddress('does-not-exists.example.com') # Resolve::Hostname::NotFoundError raised
49
+
50
+ r2 = Resolve::Hostname.new(:raise_notfound => false)
51
+ r2.getaddress('does-not-exists.example.com') #=> nil
52
+
53
+ ### System Resolver
54
+
55
+ for `/etc/hosts`:
56
+
57
+ r = Resolve::Hostname.new(:system_resolver => true)
58
+ r.getaddress('my-db-server.local')
59
+
60
+ ### IPv6 address query
61
+
62
+ For queries about IPv6 addresses:
63
+
64
+ r = Resolve::Hostname.new(:version => :ipv6)
65
+ r.getaddress('example.com') #=> '2001:500:88:200::10'
66
+ r.getaddress('ipv4only.example.com') #=> '192.0.43.10'
67
+
68
+ ### To deny other version of IP address
69
+
70
+ Specify `:permit_other_version => false` if you are in IPv4 network and want not to get IPv6 address:
71
+
72
+ r1 = Resolve::Hostname.new
73
+ r1.getaddress('ipv6only.example.com') #=> '2001:500:88:200::10'
74
+
75
+ r2 = Resolve::Hostname.new(:version => :ipv4, :permit_other_version => false)
76
+ r2.getaddress('ipv6only.example.com') # Resolve::Hostname::NotFoundError raised
77
+
78
+ ## Contributing
79
+
80
+ 1. Fork it
81
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
82
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
83
+ 4. Push to the branch (`git push origin my-new-feature`)
84
+ 5. Create new Pull Request
85
+
86
+ ## TODO
87
+
88
+ * negative cache support
89
+ * DNS round robin support
90
+
91
+ ## Copyright
92
+
93
+ * [MIT License](http://www.opensource.org/licenses/MIT).
94
+ * Copyright (c) 2013- TAGOMORI Satoshi (tagomoris)
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rspec/core/rake_task'
4
+ RSpec::Core::RakeTask.new(:test) do |t|
5
+ t.rspec_opts = ["-c", "-f progress"] # '--format specdoc'
6
+ t.pattern = 'spec/**/*_spec.rb'
7
+ end
8
+
9
+ task :test => :spec
10
+ task :default => :spec
@@ -0,0 +1,182 @@
1
+ require "resolve/hostname/version"
2
+
3
+ require 'ipaddr'
4
+ require 'socket'
5
+ require 'resolv'
6
+
7
+ module Resolve
8
+ class Hostname
9
+ class NotFoundError < StandardError; end
10
+ end
11
+ end
12
+
13
+ module Resolve
14
+ class Hostname
15
+ attr_accessor :ttl, :resolver_ttl, :resolver_expires
16
+ attr_reader :cache # for testing
17
+
18
+ DEFAULT_EXPIRATION_SECONDS = 60
19
+ DEFAULT_RESOLVER_TTL = 1800 # for rereading of /etc/resolve.conf
20
+
21
+ DEFAULT_ENABLE_SYSTEM_RESOLVER = false
22
+
23
+ DEFAULT_PRIMARY_ADDRESS_VERSION = :ipv4
24
+ DEFAULT_PERMIT_SECONDARY_ADDRESS_VERSION = true
25
+ ADDRESS_VERSIONS = [:ipv4, :ipv6]
26
+
27
+ DEFAULT_RAISE_NOTFOUND = true
28
+
29
+ #TODO: negative caching not implemented
30
+ # DEFAULT_NEGATIVE_CACHE = false # disabled
31
+
32
+ #TODO: DNS RoundRobin with resolv
33
+ # DEFAULT_SUPPORTS_DNS_RR = false
34
+
35
+ def initialize(opts={})
36
+ @primary_ip_version = opts[:version] || DEFAULT_PRIMARY_ADDRESS_VERSION
37
+ unless ADDRESS_VERSIONS.include? @primary_ip_version
38
+ raise ArgumentError, "unknown version of ip address: #{opts[:version]}"
39
+ end
40
+
41
+ @ttl = opts[:ttl] || DEFAULT_EXPIRATION_SECONDS
42
+ @resolver_ttl = opts[:resolver_ttl] || DEFAULT_RESOLVER_TTL
43
+
44
+ @system_resolver_enabled = opts.fetch(:system_resolver, DEFAULT_ENABLE_SYSTEM_RESOLVER)
45
+ @permit_secondary_address_version = opts.fetch(:permit_other_version, DEFAULT_PERMIT_SECONDARY_ADDRESS_VERSION)
46
+ @raise_notfound = opts.fetch(:raise_notfound, DEFAULT_RAISE_NOTFOUND)
47
+
48
+ @cache = {}
49
+ @mutex = Mutex.new
50
+
51
+ @resolver = nil
52
+ @resolver_expires = nil
53
+ end
54
+
55
+ def getaddress(name)
56
+ unless @cache[name]
57
+ @mutex.synchronize do
58
+ @cache[name] ||= CachedValue.new(@ttl)
59
+ end
60
+ end
61
+ @cache[name].get_or_refresh{ resolve(name) }
62
+ end
63
+
64
+ def primary_ip_version
65
+ @primary_ip_version
66
+ end
67
+
68
+ def secondary_ip_version
69
+ @primary_ip_version == :ipv4 ? :ipv6 : :ipv4
70
+ end
71
+
72
+ def primary_version_address?(str)
73
+ if @primary_ip_version == :ipv4
74
+ IPAddr.new(str).ipv4?
75
+ else
76
+ IPAddr.new(str).ipv6?
77
+ end
78
+ end
79
+
80
+ def resolve(name)
81
+ secondary = nil
82
+
83
+ if @system_resolver_enabled
84
+ addr = resolve_builtin(name)
85
+ if addr
86
+ return addr if primary_version_address?(addr)
87
+ secondary = addr
88
+ end
89
+ end
90
+
91
+ addr = resolve_resolv(name, primary_ip_version)
92
+ if addr
93
+ return addr if primary_version_address?(addr)
94
+ secondary ||= addr
95
+ end
96
+
97
+ if secondary.nil? && @permit_secondary_address_version
98
+ secondary = resolve_resolv(name, secondary_ip_version)
99
+ end
100
+
101
+ addr = resolve_magic(name)
102
+ if addr
103
+ return addr if primary_version_address?(addr)
104
+ secondary ||= addr
105
+ end
106
+
107
+ return secondary if secondary && @permit_secondary_address_version
108
+
109
+ raise NotFoundError, "cannot resolve hostname #{name}" if @raise_notfound
110
+
111
+ nil
112
+ end
113
+
114
+ def resolv_instance
115
+ return @resolver if @resolver && @resolver_expires >= Time.now
116
+
117
+ @resolver_expires = Time.now + @resolver_ttl
118
+ @resolver = Resolv::DNS.new
119
+
120
+ @resolver
121
+ end
122
+
123
+ def resolve_resolv(name, version)
124
+ t = case version
125
+ when :ipv4
126
+ Resolv::DNS::Resource::IN::A
127
+ when :ipv6
128
+ Resolv::DNS::Resource::IN::AAAA
129
+ else
130
+ raise ArgumentError, "invalid ip address version:#{version}"
131
+ end
132
+ begin
133
+ resolv_instance.getresource(name, t).address.to_s
134
+ rescue Resolv::ResolvError => e
135
+ raise unless e.message.start_with?('DNS result has no information for')
136
+ nil
137
+ end
138
+ end
139
+
140
+ def resolve_builtin(name)
141
+ begin
142
+ IPSocket.getaddress(name)
143
+ rescue SocketError => e
144
+ raise unless e.message.start_with?('getaddrinfo: nodename nor servname provided, or not known')
145
+ nil
146
+ end
147
+ end
148
+
149
+ def resolve_magic(name)
150
+ if name =~ /^localhost$/i
151
+ return @primary_ip_version == :ipv4 ? '127.0.0.1' : '::1'
152
+ end
153
+ nil
154
+ end
155
+
156
+ class CachedValue
157
+ attr_accessor :value, :expires, :mutex
158
+
159
+ #TODO: negative cache
160
+ def initialize(ttl)
161
+ @value = nil
162
+ @ttl = ttl
163
+ @expires = Time.now + ttl
164
+ @mutex = Mutex.new
165
+ end
166
+
167
+ def get_or_refresh
168
+ return @value if @value && @expires >= Time.now
169
+
170
+ @mutex.synchronize do
171
+ return @value if @value && @expires >= Time.now
172
+
173
+ @value = yield
174
+ # doesn't do negative cache (updating of @expires is passed when something raised above)
175
+ @expires = Time.now + @ttl
176
+ end
177
+
178
+ @value
179
+ end
180
+ end
181
+ end
182
+ end
@@ -0,0 +1,5 @@
1
+ module Resolve
2
+ class Hostname
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'resolve/hostname/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "resolve-hostname"
8
+ spec.version = Resolve::Hostname::VERSION
9
+ spec.authors = ["TAGOMORI Satoshi"]
10
+ spec.email = ["tagomoris@gmail.com"]
11
+ spec.description = %q{With caching, selector for IPv4/IPv6, and many other features}
12
+ spec.summary = %q{Hostname resolver with caching}
13
+ spec.homepage = "https://github.com/tagomoris/resolve-dns-cached"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ end
@@ -0,0 +1,92 @@
1
+ require_relative './spec_helper'
2
+
3
+ require 'resolve/hostname'
4
+
5
+ describe Resolve::Hostname do
6
+ context 'initialized as near default' do
7
+ r = Resolve::Hostname.new(:ttl => 2, :resolver_ttl => 2)
8
+
9
+ describe '#primary_ip_version' do
10
+ it 'returns :ipv4' do
11
+ expect(r.primary_ip_version).to be(:ipv4)
12
+ end
13
+ end
14
+
15
+ describe '#secondary_ip_version' do
16
+ it 'returns :ipv6' do
17
+ expect(r.secondary_ip_version).to be(:ipv6)
18
+ end
19
+ end
20
+
21
+ describe '#primary_version_address?' do
22
+ context 'with valid IPv4 address' do
23
+ it 'returns true' do
24
+ expect(r.primary_version_address?('127.0.0.1')).to be_true
25
+ end
26
+ end
27
+
28
+ context 'with invalid IPv4 address' do
29
+ it 'returns false' do
30
+ expect{ r.primary_version_address?('256.256.256.256') }.to raise_error(IPAddr::InvalidAddressError)
31
+ expect{ r.primary_version_address?('256.256.256.0') }.to raise_error(IPAddr::InvalidAddressError)
32
+ expect{ r.primary_version_address?('256.256.0.0') }.to raise_error(IPAddr::InvalidAddressError)
33
+ expect{ r.primary_version_address?('256.0.0.0') }.to raise_error(IPAddr::InvalidAddressError)
34
+ expect{ r.primary_version_address?('0.0.0.0') }.not_to raise_error()
35
+ end
36
+ end
37
+
38
+ context 'with IPv6 address' do
39
+ it 'returns false' do
40
+ expect(r.primary_version_address?('::1')).to be_false
41
+ end
42
+ end
43
+ end
44
+
45
+ describe '#resolv_instance' do
46
+ it 'returns instance newly instanciated instead of already expired' do
47
+ first = r.resolv_instance
48
+
49
+ r.resolver_expires = Time.now - 1
50
+
51
+ expect(r.resolv_instance).not_to be(first)
52
+ end
53
+ end
54
+
55
+ describe '#getaddress' do
56
+ it 'returns ipv4 address string for www.google.com' do
57
+ expect(r.getaddress('www.google.com')).to match(/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/)
58
+ end
59
+
60
+ it 'returns 192.0.43.10 for example.com' do
61
+ expect(r.getaddress('example.com')).to eq('192.0.43.10')
62
+ end
63
+
64
+ it 'returns same ruby object for second query' do
65
+ first = r.getaddress('google.com')
66
+ expect(r.getaddress('google.com')).to be(first)
67
+ end
68
+
69
+ it 'raise error for records non-existing' do
70
+ expect { r.getaddress('not-existing.example.com') }.to raise_error(Resolve::Hostname::NotFoundError)
71
+ end
72
+
73
+ it 'returns newly fetched object instead of expired cache' do
74
+ first = r.getaddress('www.example.com')
75
+
76
+ r.cache['www.example.com'].expires = Time.now - 1
77
+
78
+ expect(r.getaddress('www.example.com')).not_to be(first)
79
+ end
80
+
81
+ it 'returns 127.0.0.1 for localhost' do
82
+ expect(r.getaddress('localhost')).to eq('127.0.0.1')
83
+ end
84
+ end
85
+ end
86
+
87
+ context 'initialized as system resolver enabled'
88
+ context 'initialized with ipv6 primary'
89
+ context 'initialized as not permitted for secondary address version'
90
+
91
+ context 'initialized as not to raise NotFoundError'
92
+ end
@@ -0,0 +1,17 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+
12
+ # Run specs in random order to surface order dependencies. If you find an
13
+ # order dependency and want to debug it, you can fix the order by providing
14
+ # the seed, which is printed after each run.
15
+ # --seed 1234
16
+ config.order = 'random'
17
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: resolve-hostname
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - TAGOMORI Satoshi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-07-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: With caching, selector for IPv4/IPv6, and many other features
56
+ email:
57
+ - tagomoris@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - .rspec
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - lib/resolve/hostname.rb
69
+ - lib/resolve/hostname/version.rb
70
+ - resolve-hostname.gemspec
71
+ - spec/resolve_hostname_spec.rb
72
+ - spec/spec_helper.rb
73
+ homepage: https://github.com/tagomoris/resolve-dns-cached
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.0.2
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Hostname resolver with caching
97
+ test_files:
98
+ - spec/resolve_hostname_spec.rb
99
+ - spec/spec_helper.rb
100
+ has_rdoc: