em-resolv-replace 1.1.1 → 1.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/History.rdoc +15 -0
- data/LICENSE +1 -1
- data/Rakefile +1 -20
- data/VERSION +1 -1
- data/lib/em-dns-resolver.rb +30 -14
- data/lib/em-resolv-replace.rb +24 -4
- data/test/test_em-resolv-replace.rb +16 -0
- metadata +45 -62
data/History.rdoc
ADDED
data/LICENSE
CHANGED
data/Rakefile
CHANGED
@@ -1,23 +1,6 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'rake'
|
3
3
|
|
4
|
-
begin
|
5
|
-
require 'jeweler'
|
6
|
-
Jeweler::Tasks.new do |gem|
|
7
|
-
gem.name = "em-resolv-replace"
|
8
|
-
gem.summary = %Q{EventMachine-aware DNS lookup for Ruby}
|
9
|
-
gem.email = "mperham@gmail.com"
|
10
|
-
gem.homepage = "http://github.com/mperham/em-resolv-replace"
|
11
|
-
gem.authors = ["Mike Perham"]
|
12
|
-
gem.add_development_dependency "shoulda", ">= 2.10.3"
|
13
|
-
gem.add_development_dependency "mocha", ">= 0.9.8"
|
14
|
-
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
|
-
end
|
16
|
-
Jeweler::GemcutterTasks.new
|
17
|
-
rescue LoadError
|
18
|
-
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
19
|
-
end
|
20
|
-
|
21
4
|
require 'rake/testtask'
|
22
5
|
Rake::TestTask.new(:test) do |test|
|
23
6
|
test.libs << 'lib' << 'test'
|
@@ -38,11 +21,9 @@ rescue LoadError
|
|
38
21
|
end
|
39
22
|
end
|
40
23
|
|
41
|
-
task :test => :check_dependencies
|
42
|
-
|
43
24
|
task :default => :test
|
44
25
|
|
45
|
-
require '
|
26
|
+
require 'rdoc/task'
|
46
27
|
Rake::RDocTask.new do |rdoc|
|
47
28
|
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
48
29
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.1.
|
1
|
+
1.1.2
|
data/lib/em-dns-resolver.rb
CHANGED
@@ -8,14 +8,27 @@ module EventMachine
|
|
8
8
|
##
|
9
9
|
|
10
10
|
def self.resolve(hostname)
|
11
|
+
type = Resolv::DNS::Resource::IN::A
|
11
12
|
if ::Resolv::AddressRegex =~ hostname
|
12
13
|
# hostname contains an IP address, nothing to resolve
|
13
|
-
Request.new(nil, hostname)
|
14
|
+
Request.new(nil, hostname, type)
|
14
15
|
else
|
15
|
-
Request.new(socket, hostname)
|
16
|
+
Request.new(socket, hostname, type)
|
16
17
|
end
|
17
18
|
end
|
18
19
|
|
20
|
+
def self.reverse(address)
|
21
|
+
case address
|
22
|
+
when Resolv::IPv4::Regex
|
23
|
+
ptr = Resolv::IPv4.create(address).to_name
|
24
|
+
when Resolv::IPv6::Regex
|
25
|
+
ptr = Resolv::IPv6.create(address).to_name
|
26
|
+
else
|
27
|
+
raise ArgumentError, "invalid address: #{address}"
|
28
|
+
end
|
29
|
+
Request.new(socket, ptr, Resolv::DNS::Resource::IN::PTR)
|
30
|
+
end
|
31
|
+
|
19
32
|
def self.socket
|
20
33
|
if defined?(@socket) && @socket.connected?
|
21
34
|
@socket
|
@@ -30,7 +43,7 @@ module EventMachine
|
|
30
43
|
def self.nameserver
|
31
44
|
unless defined?(@nameserver)
|
32
45
|
IO::readlines('/etc/resolv.conf').each do |line|
|
33
|
-
if line =~ /^nameserver
|
46
|
+
if line =~ /^nameserver\s+(.+)$/
|
34
47
|
@nameserver = $1.split(/\s+/).first
|
35
48
|
end
|
36
49
|
end
|
@@ -105,9 +118,10 @@ module EventMachine
|
|
105
118
|
include Deferrable
|
106
119
|
attr_accessor :retry_interval
|
107
120
|
attr_accessor :max_tries
|
108
|
-
def initialize(socket,
|
121
|
+
def initialize(socket, value, type)
|
109
122
|
@socket = socket
|
110
|
-
@
|
123
|
+
@value = value
|
124
|
+
@type = type
|
111
125
|
@tries = 0
|
112
126
|
@last_send = Time.at(0)
|
113
127
|
@retry_interval = 3
|
@@ -115,9 +129,9 @@ module EventMachine
|
|
115
129
|
EM.next_tick { tick }
|
116
130
|
end
|
117
131
|
def tick
|
118
|
-
# @
|
132
|
+
# @value already contains the response
|
119
133
|
if @socket.nil?
|
120
|
-
succeed [ @
|
134
|
+
succeed [ @value ]
|
121
135
|
return
|
122
136
|
end
|
123
137
|
|
@@ -132,17 +146,19 @@ module EventMachine
|
|
132
146
|
end
|
133
147
|
# Called by DnsSocket#receive_data
|
134
148
|
def receive_answer(msg)
|
135
|
-
|
149
|
+
result = []
|
136
150
|
msg.each_answer do |name,ttl,data|
|
137
|
-
|
138
|
-
|
139
|
-
|
151
|
+
case data
|
152
|
+
when Resolv::DNS::Resource::IN::A, Resolv::DNS::Resource::IN::AAAA
|
153
|
+
result << data.address.to_s
|
154
|
+
when Resolv::DNS::Resource::IN::PTR
|
155
|
+
result << data.name.to_s
|
140
156
|
end
|
141
157
|
end
|
142
|
-
if
|
158
|
+
if result.empty?
|
143
159
|
fail "rcode=#{msg.rcode}"
|
144
160
|
else
|
145
|
-
succeed
|
161
|
+
succeed result
|
146
162
|
end
|
147
163
|
end
|
148
164
|
private
|
@@ -165,7 +181,7 @@ module EventMachine
|
|
165
181
|
msg = Resolv::DNS::Message.new
|
166
182
|
msg.id = id
|
167
183
|
msg.rd = 1
|
168
|
-
msg.add_question @
|
184
|
+
msg.add_question @value, @type
|
169
185
|
msg
|
170
186
|
end
|
171
187
|
end
|
data/lib/em-resolv-replace.rb
CHANGED
@@ -20,18 +20,38 @@ class Resolv
|
|
20
20
|
EM.reactor_running? ? em_getaddresses(host) : orig_getaddresses(host)
|
21
21
|
end
|
22
22
|
|
23
|
+
alias :orig_getname :getname
|
24
|
+
|
25
|
+
def getname(address)
|
26
|
+
EM.reactor_running? ? em_getnames(address)[0] : orig_getname(address)
|
27
|
+
end
|
28
|
+
|
29
|
+
alias :orig_getnames :getnames
|
30
|
+
|
31
|
+
def getnames(address)
|
32
|
+
EM.reactor_running? ? em_getnames(address) : orig_getnames(address)
|
33
|
+
end
|
34
|
+
|
23
35
|
private
|
24
36
|
|
25
37
|
def em_getaddresses(host)
|
38
|
+
em_request(host, :each_address, :resolve)
|
39
|
+
end
|
40
|
+
|
41
|
+
def em_getnames(address)
|
42
|
+
em_request(address, :each_name, :reverse)
|
43
|
+
end
|
44
|
+
|
45
|
+
def em_request(value, hosts_method, resolv_method)
|
26
46
|
# Lookup in /etc/hosts
|
27
|
-
|
47
|
+
result = []
|
28
48
|
@hosts ||= Resolv::Hosts.new
|
29
|
-
@hosts.
|
30
|
-
return
|
49
|
+
@hosts.send(hosts_method, value) { |x| result << x.to_s }
|
50
|
+
return result unless result.empty?
|
31
51
|
|
32
52
|
# Nothing, hit DNS
|
33
53
|
fiber = Fiber.current
|
34
|
-
df = EM::DnsResolver.
|
54
|
+
df = EM::DnsResolver.send(resolv_method, value)
|
35
55
|
df.callback do |a|
|
36
56
|
fiber.resume(a)
|
37
57
|
end
|
@@ -64,4 +64,20 @@ class TestEmResolvReplace < Test::Unit::TestCase
|
|
64
64
|
end
|
65
65
|
end
|
66
66
|
|
67
|
+
should "reverse hostname without eventmachine" do
|
68
|
+
Resolv.any_instance.expects(:em_getname).times(0)
|
69
|
+
|
70
|
+
assert_equal 'localhost', Resolv.getname('127.0.0.1')
|
71
|
+
end
|
72
|
+
|
73
|
+
should "reverse hostname with eventmachine" do
|
74
|
+
Resolv.any_instance.expects(:orig_getname).times(0)
|
75
|
+
|
76
|
+
EM.run do
|
77
|
+
Fiber.new do
|
78
|
+
assert_equal 'localhost', Resolv.getname('127.0.0.1')
|
79
|
+
EM.stop
|
80
|
+
end.resume
|
81
|
+
end
|
82
|
+
end
|
67
83
|
end
|
metadata
CHANGED
@@ -1,62 +1,50 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: em-resolv-replace
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
|
6
|
-
- 1
|
7
|
-
- 1
|
8
|
-
- 1
|
9
|
-
version: 1.1.1
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.1.2
|
5
|
+
prerelease:
|
10
6
|
platform: ruby
|
11
|
-
authors:
|
7
|
+
authors:
|
12
8
|
- Mike Perham
|
13
9
|
autorequire:
|
14
10
|
bindir: bin
|
15
11
|
cert_chain: []
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
dependencies:
|
20
|
-
- !ruby/object:Gem::Dependency
|
12
|
+
date: 2011-12-11 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
21
15
|
name: shoulda
|
22
|
-
|
23
|
-
|
24
|
-
requirements:
|
25
|
-
- -
|
26
|
-
- !ruby/object:Gem::Version
|
27
|
-
|
28
|
-
- 2
|
29
|
-
- 10
|
30
|
-
- 3
|
31
|
-
version: 2.10.3
|
16
|
+
requirement: &70315159083700 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
32
22
|
type: :development
|
33
|
-
version_requirements: *id001
|
34
|
-
- !ruby/object:Gem::Dependency
|
35
|
-
name: mocha
|
36
23
|
prerelease: false
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
version: 0
|
24
|
+
version_requirements: *70315159083700
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: mocha
|
27
|
+
requirement: &70315159083140 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
46
33
|
type: :development
|
47
|
-
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70315159083140
|
48
36
|
description:
|
49
37
|
email: mperham@gmail.com
|
50
38
|
executables: []
|
51
|
-
|
52
39
|
extensions: []
|
53
|
-
|
54
|
-
extra_rdoc_files:
|
40
|
+
extra_rdoc_files:
|
55
41
|
- LICENSE
|
42
|
+
- History.rdoc
|
56
43
|
- README.rdoc
|
57
|
-
files:
|
44
|
+
files:
|
58
45
|
- .document
|
59
46
|
- .gitignore
|
47
|
+
- History.rdoc
|
60
48
|
- LICENSE
|
61
49
|
- README.rdoc
|
62
50
|
- Rakefile
|
@@ -65,36 +53,31 @@ files:
|
|
65
53
|
- lib/em-resolv-replace.rb
|
66
54
|
- test/helper.rb
|
67
55
|
- test/test_em-resolv-replace.rb
|
68
|
-
has_rdoc: true
|
69
56
|
homepage: http://github.com/mperham/em-resolv-replace
|
70
57
|
licenses: []
|
71
|
-
|
72
58
|
post_install_message:
|
73
|
-
rdoc_options:
|
59
|
+
rdoc_options:
|
74
60
|
- --charset=UTF-8
|
75
|
-
require_paths:
|
61
|
+
require_paths:
|
76
62
|
- lib
|
77
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
requirements:
|
86
|
-
- -
|
87
|
-
- !ruby/object:Gem::Version
|
88
|
-
|
89
|
-
- 0
|
90
|
-
version: "0"
|
63
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
64
|
+
none: false
|
65
|
+
requirements:
|
66
|
+
- - ! '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
none: false
|
71
|
+
requirements:
|
72
|
+
- - ! '>='
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
91
75
|
requirements: []
|
92
|
-
|
93
76
|
rubyforge_project:
|
94
|
-
rubygems_version: 1.
|
77
|
+
rubygems_version: 1.8.10
|
95
78
|
signing_key:
|
96
79
|
specification_version: 3
|
97
80
|
summary: EventMachine-aware DNS lookup for Ruby
|
98
|
-
test_files:
|
81
|
+
test_files:
|
99
82
|
- test/helper.rb
|
100
83
|
- test/test_em-resolv-replace.rb
|