resolver_replace 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: 13a898dc3155fb550affbfe858cb070d033e2d74
4
+ data.tar.gz: dcfc046f9e40bade813df3f46715667965e2bf46
5
+ SHA512:
6
+ metadata.gz: e498affc9788ec2e3bac1dd092d9bac53eceba8a83e925b567537b8fbc7025366777bf55324e186685824f55ae22675328824f9d6530255f1ea5caa0d8be57ab
7
+ data.tar.gz: f5c747339d275a779a66defce9c1575eef9845afd71353ac2fa64372296237c2bd67d074fab1879ee42fb5c0fc764d2d4704600c21fb9fd3c14a6a43da14df85
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ## 0.0.1
2
+
3
+ First version
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+ gem 'mysql2' # for sample plugin test
data/Gemfile.lock ADDED
@@ -0,0 +1,44 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ resolver_replace (0.0.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ coderay (1.1.0)
10
+ diff-lcs (1.2.5)
11
+ method_source (0.8.2)
12
+ mysql2 (0.3.16)
13
+ pry (0.9.12.6)
14
+ coderay (~> 1.0)
15
+ method_source (~> 0.8)
16
+ slop (~> 3.4)
17
+ pry-nav (0.2.3)
18
+ pry (~> 0.9.10)
19
+ rake (10.3.2)
20
+ rspec (3.0.0)
21
+ rspec-core (~> 3.0.0)
22
+ rspec-expectations (~> 3.0.0)
23
+ rspec-mocks (~> 3.0.0)
24
+ rspec-core (3.0.2)
25
+ rspec-support (~> 3.0.0)
26
+ rspec-expectations (3.0.2)
27
+ diff-lcs (>= 1.2.0, < 2.0)
28
+ rspec-support (~> 3.0.0)
29
+ rspec-mocks (3.0.2)
30
+ rspec-support (~> 3.0.0)
31
+ rspec-support (3.0.2)
32
+ slop (3.5.0)
33
+
34
+ PLATFORMS
35
+ ruby
36
+
37
+ DEPENDENCIES
38
+ bundler (~> 1.6)
39
+ mysql2
40
+ pry
41
+ pry-nav
42
+ rake
43
+ resolver_replace!
44
+ rspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Naotoshi Seo
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,72 @@
1
+ # resolver-replace
2
+
3
+ Replace the DNS resolver.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ gem install resolver_replace
9
+ ```
10
+
11
+ ## How it works
12
+
13
+ Ruby comes with a pure Ruby replacement, [resolv-replace](https://github.com/ruby/ruby/blob/b1f2effda85efd03bd4ad5c06e0aae5e14f3f864/lib/resolv-replace.rb).
14
+ It replaces the libc resolver with the built-in [Resolv](http://apidock.com/ruby/Resolv) class which is a thread-aware DNS resolver library.
15
+
16
+ This gem works similarly, but allows to replace the resolver with your favorite resolver.
17
+
18
+ ## How to use
19
+
20
+ For example, if you want to replace the resolver with `Resolv::DNS`, write as followings:
21
+
22
+ ```ruby
23
+ require 'resolve' # Resolve::DNS
24
+ require 'resolver-replace' # ResolverReplace
25
+
26
+ resolver = Resolv::DNS.new(:nameserver => ['210.251.121.21'],
27
+ :search => ['ruby-lang.org'],
28
+ :ndots => 1)
29
+
30
+ ResolverReplace.configure(
31
+ getaddress: Proc.new {|host| resolver.getaddress(host) },
32
+ getaddresses: Proc.new {|host| resolver.getaddresses(host) },
33
+ error_class: Resolv::ResolvError,
34
+ )
35
+ ```
36
+
37
+ ## Plugin
38
+
39
+ Some gems like `mysql2` implement its connection in its C extension without using ruby library.
40
+ In such case, another monkeypatch must be applied. A plugin scheme of ResolveReplace is ready for such case.
41
+
42
+ The `mysql2` plugin is available for example. Use as followings:
43
+
44
+ ```ruby
45
+ ResolverReplace.load_plugin('mysql2')
46
+ ResolverReplace.configure(
47
+ getaddress: Proc.new {|host| resolver.getaddress(host) },
48
+ getaddresses: Proc.new {|host| resolver.getaddresses(host) },
49
+ error_class: Resolv::ResolvError,
50
+ )
51
+ ```
52
+
53
+ ## ChangeLog
54
+
55
+ See [CHANGELOG.md](CHANGELOG.md) for details.
56
+
57
+ ## See Also
58
+
59
+ * [例えば Resolver をすげかえる - sonots:blog](http://blog.livedoor.jp/sonots/archives/38822072.html)
60
+
61
+ ## Contributing
62
+
63
+ 1. Fork it
64
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
65
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
66
+ 4. Push to the branch (`git push origin my-new-feature`)
67
+ 5. Create new [Pull Request](../../pull/new/master)
68
+
69
+ ## Copyright
70
+
71
+ Copyright (c) 2014 Naotoshi Seo. See [LICENSE.txt](LICENSE.txt) for details.
72
+
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rspec/core'
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec) do |spec|
6
+ spec.pattern = FileList['spec/**/*_spec.rb']
7
+ end
8
+ task :default => :spec
9
+
10
+ desc 'Open an irb session preloaded with the gem library'
11
+ task :console do
12
+ sh 'irb -rubygems -I lib -r resolver_replace'
13
+ end
14
+ task :c => :console
@@ -0,0 +1,11 @@
1
+ require 'resolve'
2
+
3
+ resolver = Resolv::DNS.new(:nameserver => ['210.251.121.21'],
4
+ :search => ['ruby-lang.org'],
5
+ :ndots => 1)
6
+
7
+ ResolverReplace.configure(
8
+ getaddress_proc: Proc.new {|host| resolver.getaddress(host) },
9
+ getaddresses_proc: Proc.new {|host| resolver.getaddresses(host) },
10
+ error_class: Resolv::ResolvError,
11
+ )
@@ -0,0 +1,15 @@
1
+ if defined?(Mysql2)
2
+ module Mysql2
3
+ class Client
4
+ alias original_resolver_connect connect
5
+ def connect user, pass, host, port, database, socket, flags
6
+ begin
7
+ ip = ResolverReplace.getaddress(host).to_s
8
+ original_resolver_connect user, pass, ip, port, database, socket, flags
9
+ rescue ResolverReplace.error_class => e
10
+ raise Mysql2::Error, "#{e.class}: #{e.message}"
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,102 @@
1
+ class ResolverReplace
2
+ def self.register!(params = {})
3
+ unless @getaddress = params[:getaddress]
4
+ raise ArgumentError, 'getaddress is required'
5
+ end
6
+ unless @getaddresses = params[:getaddresses]
7
+ raise ArgumentError, 'getaddresses is required'
8
+ end
9
+ @error_class = params[:error_class] || StandardError
10
+ end
11
+
12
+ def self.getaddress(host)
13
+ @getaddress.call(host)
14
+ end
15
+
16
+ def self.getaddresses(host)
17
+ @getaddresses.call(host)
18
+ end
19
+
20
+ def self.error_class
21
+ @error_class
22
+ end
23
+
24
+ def self.load_plugin(name)
25
+ load "resolver_replace/plugins/#{name}.rb"
26
+ end
27
+ end
28
+
29
+ # reference: ruby/lib/resolv-replace.rb
30
+ require 'socket'
31
+
32
+ class << IPSocket
33
+ # :stopdoc:
34
+ alias original_resolver_getaddress getaddress
35
+ # :startdoc:
36
+ def getaddress(host)
37
+ begin
38
+ return ResolverReplace.getaddress(host).to_s
39
+ rescue ResolverReplace.error_class => e
40
+ raise SocketError, "#{e.class} #{e.message}"
41
+ end
42
+ end
43
+ end
44
+
45
+ class TCPSocket < IPSocket
46
+ # :stopdoc:
47
+ alias original_resolver_initialize initialize
48
+ # :startdoc:
49
+ def initialize(host, serv, *rest)
50
+ rest[0] = IPSocket.getaddress(rest[0]) unless rest.empty?
51
+ original_resolver_initialize(IPSocket.getaddress(host), serv, *rest)
52
+ end
53
+ end
54
+
55
+ class UDPSocket < IPSocket
56
+ # :stopdoc:
57
+ alias original_resolver_bind bind
58
+ # :startdoc:
59
+ def bind(host, port)
60
+ host = IPSocket.getaddress(host) if host != ""
61
+ original_resolver_bind(host, port)
62
+ end
63
+
64
+ # :stopdoc:
65
+ alias original_resolver_connect connect
66
+ # :startdoc:
67
+ def connect(host, port)
68
+ original_resolver_connect(IPSocket.getaddress(host), port)
69
+ end
70
+
71
+ # :stopdoc:
72
+ alias original_resolver_send send
73
+ # :startdoc:
74
+ def send(mesg, flags, *rest)
75
+ if rest.length == 2
76
+ host, port = rest
77
+ begin
78
+ addrs = ResolverReplace.getaddresses(host)
79
+ rescue ResolverReplace.error_class => e
80
+ raise SocketError, "#{e.class} #{e.message}"
81
+ end
82
+ addrs[0...-1].each {|addr|
83
+ begin
84
+ return original_resolver_send(mesg, flags, addr, port)
85
+ rescue SystemCallError
86
+ end
87
+ }
88
+ original_resolver_send(mesg, flags, addrs[-1], port)
89
+ else
90
+ original_resolver_send(mesg, flags, *rest)
91
+ end
92
+ end
93
+ end
94
+
95
+ class SOCKSSocket < TCPSocket
96
+ # :stopdoc:
97
+ alias original_resolver_initialize initialize
98
+ # :startdoc:
99
+ def initialize(host, serv)
100
+ original_resolver_initialize(IPSocket.getaddress(host), port)
101
+ end
102
+ end if defined? SOCKSSocket
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "resolver_replace"
7
+ spec.version = "0.0.1"
8
+ spec.authors = ["Naotoshi Seo"]
9
+ spec.email = ["sonots@gmail.com"]
10
+ spec.summary = %q{Replace the DNS resolver}
11
+ spec.description = %q{Replace the DNS resolver.}
12
+ spec.homepage = ""
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.6"
21
+ spec.add_development_dependency "rake"
22
+ spec.add_development_dependency "rspec"
23
+ spec.add_development_dependency "pry"
24
+ spec.add_development_dependency "pry-nav"
25
+ end
@@ -0,0 +1,42 @@
1
+ require_relative 'spec_helper'
2
+ require 'resolver_replace'
3
+
4
+ describe ResolverReplace do
5
+ let(:host) { 'ruby-lang.org' }
6
+ let(:ip) { '127.0.0.1' }
7
+
8
+ before do
9
+ ResolverReplace.register!(
10
+ getaddress: Proc.new {|host| ip },
11
+ getaddresses: Proc.new {|host| ip },
12
+ error_class: StandardError,
13
+ )
14
+ end
15
+
16
+ it 'IPSocket' do
17
+ expect(IPSocket.getaddress(host)).to be == ip
18
+ end
19
+
20
+ it 'net/http' do
21
+ require 'net/http'
22
+ begin
23
+ Net::HTTP.get_print(host, '/index.html')
24
+ rescue => e
25
+ expect(e.message).to match(/Connection refused.*127.0.0.1/)
26
+ end
27
+ end
28
+
29
+ it 'mysql2' do
30
+ require 'mysql2'
31
+ ResolverReplace.load_plugin("mysql2")
32
+ begin
33
+ Mysql2::Client.new(
34
+ host: 'foo',
35
+ username: 'root',
36
+ database: 'foo',
37
+ )
38
+ rescue => e
39
+ expect(e.message).not_to match(/Unknown MySQL server/)
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,15 @@
1
+ # encoding: UTF-8
2
+ require 'rubygems'
3
+ require 'bundler'
4
+ Bundler.setup(:default, :test)
5
+ Bundler.require(:default, :test)
6
+ require 'pry'
7
+ ROOT = File.dirname(File.dirname(__FILE__))
8
+
9
+ if ENV['TRAVIS']
10
+ require 'coveralls'
11
+ Coveralls.wear!
12
+ end
13
+
14
+ $TESTING=true
15
+ $:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: resolver_replace
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Naotoshi Seo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-25 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.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
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
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry-nav
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Replace the DNS resolver.
84
+ email:
85
+ - sonots@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - CHANGELOG.md
91
+ - Gemfile
92
+ - Gemfile.lock
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - example/example.rb
97
+ - lib/resolver_replace.rb
98
+ - lib/resolver_replace/plugins/mysql2.rb
99
+ - resolver_replace.gemspec
100
+ - spec/resolver_replace_spec.rb
101
+ - spec/spec_helper.rb
102
+ homepage: ''
103
+ licenses:
104
+ - MIT
105
+ metadata: {}
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 2.2.2
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: Replace the DNS resolver
126
+ test_files:
127
+ - spec/resolver_replace_spec.rb
128
+ - spec/spec_helper.rb