rubydns 0.6.2 → 0.6.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NDQzNGMwMWNhMTU4MDViZWRjYjc0NzJiMDNlZmE5MmU3MmEwMTc0NQ==
4
+ MGQ5MTRkMjlkNDRiZGQ2OGIxYzc4NWZhMGY1YzQxYTdkNTIyMGE5Ng==
5
5
  data.tar.gz: !binary |-
6
- NmRiZTY5OTg3MzYwMWNiMWQxN2ZhMGI0ZTgzMTg2YmM3NWFjYTY1ZA==
6
+ YTAzYWRhYTQxZWY0MGI3NTAyNmE1OTIzZmYwNThjODY3NTYwODNhNA==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- MTQ0NjAzNzRlMTEyZTI5ZGM4YThhMGM1ZjhmOGI3Mzg5YjFhNThkYTEzOWRk
10
- NzM5MDQwNmJlMDA3ZjcyY2FjNWI2NTBkNGMyZGFlZGIxMzRlMzE1YjQyODE0
11
- Y2IyYTM2MDg5MmQ4Mjc2MDcyMDYzNWY3OWFkY2RjYzVhMmM5NGQ=
9
+ NzA1NTM4MmVkYTQ5YmQ0NjU3MmMyOWVlZGVjMWY0MmI5ZDg0OWYxZDMzYzQz
10
+ NWU4YTM5MTE5N2IwZGYxYTJhMDk0NGQyYjNjNGNlZDVkYWFmNGNkYjYyYWM4
11
+ ZTk4ZWI0NzI1MjNkYjVjNTI2MTc2MWJkMzNmOGU0OTJhM2EzZTA=
12
12
  data.tar.gz: !binary |-
13
- ODkzMGViNTZjZmNhZWQ3OGY4NTY4Mzc0MzZmN2I3NjNiOTBhMzNmODM3MjY5
14
- NTk2MmE4N2MzYWE1YzgzMTI4MjU2NTRlNjEwZDJmOGUyZTE3NzQwZmFmNzVi
15
- ZTVjYTBkYjA1ZDg4Mjc5ZDI3YjA3ZmNlOTc4YzJiOTcyODA2MTY=
13
+ NTlkYzgxNzFmY2IwM2I3NTdmMjExOTAzODIwZGY3YjMwZDQ0Y2E2ZTc2MDQ3
14
+ NDE0MWUzZTJmZDg1ZGIxYTRkMzU3MzhiMjBjZDM1ZTM4NTQzODcwOTRkODE1
15
+ ZWJmYzBhMzA1MWNlMzA5MjZiZjVhZGRiMmIzZDg0MTVmMmVhOWM=
@@ -115,12 +115,19 @@ module RubyDNS
115
115
  #
116
116
  # A second argument, options, provides some control over the passthrough process.
117
117
  # :force => true, ensures that the query will occur even if recursion is not requested.
118
+ # :name => resource_name, override the name for the request as it is passed to
119
+ # the resolver. Implies :force.
120
+ # :resource_class => class, overried the resource class for a request as it is passed
121
+ # to the resolver.
118
122
  def passthrough(resolver, options = {}, &block)
119
- if @query.rd || options[:force]
123
+ if @query.rd || options[:force] || options[:name]
120
124
  # Resolver is asynchronous, so we are now deferred:
121
125
  defer!
122
126
 
123
- resolver.query(name, resource_class) do |response|
127
+ query_name = options[:name] || name
128
+ query_resource_class = options[:resource_class] || resource_class
129
+
130
+ resolver.query(query_name, query_resource_class) do |response|
124
131
  case response
125
132
  when RubyDNS::Message
126
133
  yield response
@@ -19,5 +19,5 @@
19
19
  # THE SOFTWARE.
20
20
 
21
21
  module RubyDNS
22
- VERSION = "0.6.2"
22
+ VERSION = "0.6.3"
23
23
  end
@@ -0,0 +1,119 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+
4
+ # Copyright, 2009, 2012, by Samuel G. D. Williams. <http://www.codeotaku.com>
5
+ #
6
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ # of this software and associated documentation files (the "Software"), to deal
8
+ # in the Software without restriction, including without limitation the rights
9
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ # copies of the Software, and to permit persons to whom the Software is
11
+ # furnished to do so, subject to the following conditions:
12
+ #
13
+ # The above copyright notice and this permission notice shall be included in
14
+ # all copies or substantial portions of the Software.
15
+ #
16
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ # THE SOFTWARE.
23
+
24
+ require 'rubygems'
25
+
26
+ require 'rexec'
27
+ require 'rexec/daemon'
28
+
29
+ require 'rubygems'
30
+
31
+ require 'rubydns'
32
+ require 'rubydns/extensions/string'
33
+
34
+ require 'em-http'
35
+ require 'cgi'
36
+ require 'nokogiri'
37
+ require 'json'
38
+
39
+ require 'digest/md5'
40
+
41
+ # You might need to change the user name "daemon". This can be a user name or a user id.
42
+ RUN_AS = "daemon"
43
+
44
+ if RExec.current_user != "root"
45
+ $stderr.puts "Sorry, this command needs to be run as root!"
46
+ exit 1
47
+ end
48
+
49
+ module Wikipedia
50
+ def self.summary_url(title)
51
+ "http://en.wikipedia.org/w/api.php?action=parse&page=#{CGI.escape title}&prop=text&section=0&format=json"
52
+ end
53
+
54
+ def self.extract_summary(json_text)
55
+ document = JSON::parse(json_text)
56
+ return Nokogiri::HTML(document["parse"]["text"]["*"]).css('p')[0].text
57
+ rescue
58
+ return "Invalid Article."
59
+ end
60
+ end
61
+
62
+ # To use, start the daemon and try:
63
+ # dig @localhost fortune CNAME
64
+ class WikipediaDNS < RExec::Daemon::Base
65
+ @@base_directory = File.dirname(__FILE__)
66
+
67
+ Name = Resolv::DNS::Name
68
+ IN = Resolv::DNS::Resource::IN
69
+
70
+ def self.run
71
+ # Don't buffer output (for debug purposes)
72
+ $stderr.sync = true
73
+
74
+ cache = {}
75
+ stats = {:requested => 0}
76
+
77
+ # Start the RubyDNS server
78
+ RubyDNS::run_server do
79
+ on(:start) do
80
+ RExec.change_user(RUN_AS)
81
+ if ARGV.include?("--debug")
82
+ @logger.level = Logger::DEBUG
83
+ else
84
+ @logger.level = Logger::WARN
85
+ end
86
+ end
87
+
88
+ match(/stats\.wikipedia/, IN::TXT) do |transaction|
89
+ transaction.respond!(*stats.inspect.chunked)
90
+ end
91
+
92
+ match(/(.+)\.wikipedia/, IN::TXT) do |transaction, match_data|
93
+ title = match_data[1]
94
+ stats[:requested] += 1
95
+
96
+ transaction.defer!
97
+
98
+ http = EventMachine::HttpRequest.new(Wikipedia.summary_url(title)).get
99
+
100
+ http.callback do
101
+ summary = Wikipedia.extract_summary(http.response)
102
+ transaction.respond!(*summary.chunked)
103
+ end
104
+
105
+ http.errback do
106
+ transaction.failure!(:ServFail)
107
+ end
108
+ end
109
+
110
+ # Default DNS handler
111
+ otherwise do |transaction|
112
+ transaction.failure!(:NXDomain)
113
+ end
114
+ end
115
+ end
116
+ end
117
+
118
+ # RExec daemon runner
119
+ WikipediaDNS.daemonize
@@ -43,6 +43,10 @@ class TestPassthroughServer < RExec::Daemon::Base
43
43
  transaction.passthrough!(resolver)
44
44
  end
45
45
 
46
+ match(/a-(.*\.org)/) do |transaction, match_data|
47
+ transaction.passthrough!(resolver, :name => match_data[1])
48
+ end
49
+
46
50
  # Default DNS handler
47
51
  otherwise do |transaction|
48
52
  transaction.failure!(:NXDomain)
@@ -75,6 +79,26 @@ class PassthroughTest < Test::Unit::TestCase
75
79
  end
76
80
  end
77
81
 
82
+ assert answer != nil
83
+ assert answer.count > 0
84
+ end
85
+
86
+ def test_basic_dns_prefix
87
+ answer = nil
88
+
89
+ assert_equal :running, RExec::Daemon::ProcessFile.status(TestPassthroughServer)
90
+
91
+ EventMachine.run do
92
+ resolver = RubyDNS::Resolver.new(TestPassthroughServer::SERVER_PORTS)
93
+
94
+ resolver.query("a-slashdot.org") do |response|
95
+ answer = response.answer.first
96
+
97
+ EventMachine.stop
98
+ end
99
+ end
100
+
101
+ assert answer != nil
78
102
  assert answer.count > 0
79
103
  end
80
104
  end
@@ -35,7 +35,7 @@ class ResolverTest < Test::Unit::TestCase
35
35
  end
36
36
 
37
37
  EventMachine::run do
38
- resolver.query('nonexistant.private') do |response|
38
+ resolver.query('foobar.oriontransfer.org') do |response|
39
39
  assert_equal response.rcode, Resolv::DNS::RCode::NXDomain
40
40
  EventMachine::stop
41
41
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubydns
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.2
4
+ version: 0.6.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-05-14 00:00:00.000000000 Z
11
+ date: 2013-06-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rexec
@@ -83,6 +83,7 @@ files:
83
83
  - test/examples/soa-dns.rb
84
84
  - test/examples/test-dns-1.rb
85
85
  - test/examples/test-dns-2.rb
86
+ - test/examples/wikipedia-dns.rb
86
87
  - test/helper.rb
87
88
  - test/hosts.txt
88
89
  - test/test_daemon.rb
@@ -123,6 +124,7 @@ test_files:
123
124
  - test/examples/soa-dns.rb
124
125
  - test/examples/test-dns-1.rb
125
126
  - test/examples/test-dns-2.rb
127
+ - test/examples/wikipedia-dns.rb
126
128
  - test/helper.rb
127
129
  - test/hosts.txt
128
130
  - test/test_daemon.rb