rubydns 1.0.3 → 2.0.0.pre.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. checksums.yaml +4 -4
  2. data/.rspec +4 -0
  3. data/.travis.yml +9 -12
  4. data/Gemfile +4 -1
  5. data/README.md +49 -151
  6. data/Rakefile +2 -7
  7. data/examples/basic-dns.rb +24 -0
  8. data/examples/cname.rb +25 -0
  9. data/examples/flakey-dns.rb +2 -2
  10. data/examples/simple.rb +25 -0
  11. data/examples/soa-dns.rb +82 -0
  12. data/examples/test-dns-1.rb +83 -0
  13. data/examples/test-dns-2.rb +83 -0
  14. data/examples/wikipedia-dns.rb +4 -18
  15. data/lib/rubydns.rb +9 -23
  16. data/lib/rubydns/{server.rb → rule_based_server.rb} +2 -160
  17. data/lib/rubydns/version.rb +1 -1
  18. data/rubydns.gemspec +3 -6
  19. data/spec/rubydns/daemon_spec.rb +26 -22
  20. data/spec/rubydns/injected_supervisor_spec.rb +10 -7
  21. data/spec/rubydns/passthrough_spec.rb +31 -24
  22. data/spec/spec_helper.rb +43 -0
  23. metadata +21 -100
  24. data/lib/rubydns/chunked.rb +0 -34
  25. data/lib/rubydns/extensions/resolv.rb +0 -85
  26. data/lib/rubydns/extensions/string.rb +0 -28
  27. data/lib/rubydns/handler.rb +0 -188
  28. data/lib/rubydns/logger.rb +0 -31
  29. data/lib/rubydns/message.rb +0 -76
  30. data/lib/rubydns/resolver.rb +0 -294
  31. data/lib/rubydns/system.rb +0 -146
  32. data/lib/rubydns/transaction.rb +0 -204
  33. data/lib/rubydns/transport.rb +0 -75
  34. data/spec/rubydns/celluloid_bug_spec.rb +0 -92
  35. data/spec/rubydns/ipv6_spec.rb +0 -70
  36. data/spec/rubydns/message_spec.rb +0 -56
  37. data/spec/rubydns/origin_spec.rb +0 -106
  38. data/spec/rubydns/resolver_performance_spec.rb +0 -110
  39. data/spec/rubydns/resolver_spec.rb +0 -152
  40. data/spec/rubydns/server/bind9/generate-local.rb +0 -25
  41. data/spec/rubydns/server/bind9/local.zone +0 -5014
  42. data/spec/rubydns/server/bind9/named.conf +0 -14
  43. data/spec/rubydns/server/bind9/named.run +0 -0
  44. data/spec/rubydns/server/million.rb +0 -85
  45. data/spec/rubydns/server/rubydns.stackprof +0 -0
  46. data/spec/rubydns/server_performance_spec.rb +0 -136
  47. data/spec/rubydns/slow_server_spec.rb +0 -89
  48. data/spec/rubydns/socket_spec.rb +0 -77
  49. data/spec/rubydns/system_spec.rb +0 -60
  50. data/spec/rubydns/transaction_spec.rb +0 -138
  51. data/spec/rubydns/truncation_spec.rb +0 -59
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0bbfcdd537ed47ea32a9fd572e8f53994535c1d4
4
- data.tar.gz: 656870a2f618afdf7c855ea92c90233fdcf8d55a
3
+ metadata.gz: 81194c2e8cae24a9908e8b4a89004555dc239dc2
4
+ data.tar.gz: df55b4550b2380779083664b4d4031fd1bf420da
5
5
  SHA512:
6
- metadata.gz: 7c16e4fa1973b74001b5312e20dec2679462ad7277adf4b27711a70c0824e15dcbc92246753937304121a9cabf2c440d2e2453ed63ad7f1afe0fbc28a56fb690
7
- data.tar.gz: 064149625cf5011ccb709164be9458690ed313a70b6b708da757cf956431f3bf87b62e8fe72278dca49472e4babef29d1be38ba6a67721ecdee61e37e4dd2e30
6
+ metadata.gz: 053b7994f85527e132900059f185ac3ff7d9a94b7cd75d4c1d6266a42100bbf93e340ecbdda74333106759ebc3429027f095b8185d9bd627b3182a87d385e475
7
+ data.tar.gz: 3964d053132451e904cd10e5630303af8686ba36721ef01b2aa337211b069e1c5d45978f4109cc5f71c4c52aaef5973c25879b8d10a34c3d449b32b0f0135c92
data/.rspec ADDED
@@ -0,0 +1,4 @@
1
+ --color
2
+ --format documentation
3
+ --warnings
4
+ --require spec_helper
@@ -1,14 +1,11 @@
1
1
  language: ruby
2
- before_install:
3
- - sudo apt-get update -qq
4
- - sudo apt-get install -y bind9
2
+ sudo: false
5
3
  rvm:
6
- - 2.2
7
- - 2.1
8
- - 2.0.0
9
- - 1.9.3
10
- - rbx-2
11
- matrix:
12
- allow_failures:
13
- - rvm: rbx-2
14
- env: COVERAGE=true
4
+ - 2.2.6
5
+ - 2.3.0
6
+ - 2.4.0
7
+ - ruby-head
8
+ addons:
9
+ apt:
10
+ packages:
11
+ - bind9
data/Gemfile CHANGED
@@ -1,8 +1,11 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- # Specify your gem's dependencies in trenni.gemspec
4
3
  gemspec
5
4
 
5
+ group :development do
6
+ gem "process-daemon"
7
+ end
8
+
6
9
  group :test do
7
10
  gem 'simplecov'
8
11
  gem 'coveralls', require: false
data/README.md CHANGED
@@ -1,18 +1,14 @@
1
1
  # RubyDNS
2
- [![Gitter](https://badges.gitter.im/Join Chat.svg)](https://gitter.im/ioquatix/rubydns?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
3
2
 
4
- RubyDNS is a high-performance DNS server which can be easily integrated into other projects or used as a stand-alone daemon. By default it uses rule-based pattern matching. Results can be hard-coded, computed, fetched from a remote DNS server or fetched from a local cache, depending on requirements.
5
-
6
- In addition, RubyDNS includes a high-performance asynchronous DNS resolver built on top of [Celluloid][1]. This module can be used by itself in client applications without using the full RubyDNS server stack.
3
+ [![Gitter](https://badges.gitter.im/Join+Chat.svg)](https://gitter.im/ioquatix/rubydns?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
7
4
 
8
- For examples and documentation please see the main [project page][2].
5
+ RubyDNS is a high-performance DNS server which can be easily integrated into other projects or used as a stand-alone daemon. By default it uses rule-based pattern matching. Results can be hard-coded, computed, fetched from a remote DNS server or fetched from a local cache, depending on requirements.
9
6
 
10
- [1]: https://celluloid.io
11
- [2]: http://www.codeotaku.com/projects/rubydns/
7
+ [![Build Status](https://travis-ci.org/ioquatix/rubydns.svg)](https://travis-ci.org/ioquatix/rubydns)
8
+ [![Code Climate](https://codeclimate.com/github/ioquatix/rubydns.svg)](https://codeclimate.com/github/ioquatix/rubydns)
9
+ [![Coverage Status](https://coveralls.io/repos/ioquatix/rubydns/badge.svg)](https://coveralls.io/r/ioquatix/rubydns)
12
10
 
13
- [![Build Status](https://travis-ci.org/ioquatix/rubydns.svg?branch=master)](https://travis-ci.org/ioquatix/rubydns)
14
- [![Code Climate](https://codeclimate.com/github/ioquatix/rubydns.png)](https://codeclimate.com/github/ioquatix/rubydns)
15
- [![Coverage Status](https://coveralls.io/repos/ioquatix/rubydns/badge.svg?branch=master)](https://coveralls.io/r/ioquatix/rubydns?branch=master)
11
+ [![RubyDNS Introduction](http://img.youtube.com/vi/B9ygq0xh3HQ/maxresdefault.jpg)](https://www.youtube.com/watch?v=B9ygq0xh3HQ&feature=youtu.be&hd=1 "RubyDNS Introduction")
16
12
 
17
13
  ## Installation
18
14
 
@@ -30,36 +26,42 @@ Or install it yourself as:
30
26
 
31
27
  ## Usage
32
28
 
33
- This is copied from `test/examples/test-dns-2.rb`. It has been simplified slightly.
29
+ There are [lots of examples available](examples/README.md) in the `examples/` directory.
30
+
31
+ ### Basic DNS Server
34
32
 
35
- #!/usr/bin/env ruby
36
- require 'rubydns'
33
+ Here is the code from `examples/basic-dns.rb`:
37
34
 
38
- INTERFACES = [
39
- [:udp, "0.0.0.0", 5300],
40
- [:tcp, "0.0.0.0", 5300]
41
- ]
42
- Name = Resolv::DNS::Name
43
- IN = Resolv::DNS::Resource::IN
35
+ ```ruby
36
+ #!/usr/bin/env ruby
37
+ require 'rubydns'
44
38
 
45
- # Use upstream DNS for name resolution.
46
- UPSTREAM = RubyDNS::Resolver.new([[:udp, "8.8.8.8", 53], [:tcp, "8.8.8.8", 53]])
39
+ INTERFACES = [
40
+ [:udp, "0.0.0.0", 5300],
41
+ [:tcp, "0.0.0.0", 5300],
42
+ ]
47
43
 
48
- # Start the RubyDNS server
49
- RubyDNS::run_server(:listen => INTERFACES) do
50
- match(/test\.mydomain\.org/, IN::A) do |transaction|
51
- transaction.respond!("10.0.0.80")
52
- end
44
+ IN = Resolv::DNS::Resource::IN
45
+
46
+ # Use upstream DNS for name resolution.
47
+ UPSTREAM = RubyDNS::Resolver.new([[:udp, "8.8.8.8", 53], [:tcp, "8.8.8.8", 53]])
48
+
49
+ # Start the RubyDNS server
50
+ RubyDNS::run_server(:listen => INTERFACES) do
51
+ match(%r{test.local}, IN::A) do |transaction|
52
+ transaction.respond!("10.0.0.80")
53
+ end
53
54
 
54
- # Default DNS handler
55
- otherwise do |transaction|
56
- transaction.passthrough!(UPSTREAM)
57
- end
55
+ # Default DNS handler
56
+ otherwise do |transaction|
57
+ transaction.passthrough!(UPSTREAM)
58
58
  end
59
+ end
60
+ ```
59
61
 
60
- Start the server using `./test.rb`. You can then test it using dig:
62
+ Start the server using `RUBYOPT=-w ./examples/basic-dns.rb`. You can then test it using dig:
61
63
 
62
- $ dig @localhost -p 5300 test.mydomain.org
64
+ $ dig @localhost -p 5300 test.local
63
65
  $ dig @localhost -p 5300 google.com
64
66
 
65
67
  ### File Handle Limitations
@@ -70,24 +72,29 @@ On some platforms (e.g. Mac OS X) the number of file descriptors is relatively l
70
72
 
71
73
  It is possible to create and integrate your own custom servers.
72
74
 
73
- class MyServer < RubyDNS::Server
74
- def process(name, resource_class, transaction)
75
- transaction.fail!(:NXDomain)
76
- end
75
+ ```ruby
76
+ class MyServer < RubyDNS::Server
77
+ def process(name, resource_class, transaction)
78
+ transaction.fail!(:NXDomain)
77
79
  end
80
+ end
81
+
82
+ Async::Reactor.run do
83
+ task = MyServer.run
78
84
 
79
- # Use the RubyDNS infrastructure for running the daemon:
80
- # If asynchronous is true, it will return immediately, otherwise, it will block the current thread until Ctrl-C is pressed (SIGINT).
81
- RubyDNS::run_server(asynchronous: false, server_class: MyServer)
85
+ # ... do other things
82
86
 
83
- # Directly instantiate the celluloid supervisor:
84
- supervisor = MyServer.supervise
85
- supervisor.actors.first.run
87
+ # Shut down the server:
88
+ task.stop
89
+ end
90
+ ```
86
91
 
87
92
  This is the best way to integrate with other projects.
88
93
 
89
94
  ## Performance
90
95
 
96
+ **Due to changes in the underlying code, there have been some very minor performance regressions. The numbers below will be updated in due course.**
97
+
91
98
  We welcome additional benchmarks and feedback regarding RubyDNS performance. To check the current performance results, consult the [travis build job output](https://travis-ci.org/ioquatix/rubydns).
92
99
 
93
100
  ### Server
@@ -114,115 +121,6 @@ These benchmarks are included in the unit tests.
114
121
 
115
122
  DNSSEC is currently not supported and is [unlikely to be supported in the future](http://sockpuppet.org/blog/2015/01/15/against-dnssec/).
116
123
 
117
- ## Compatibility
118
-
119
- ### Migrating from RubyDNS 0.8.x to 0.9.x
120
-
121
- RubyDNS 0.9.0 is based on a branch which replaced EventMachine with Celluloid. This reduces the complexity in writing concurrent systems hugely, but it is also a largely untested code path. RubyDNS 0.8.x using EventMachine has been tested over 4 years now by many projects.
122
-
123
- The reason for the change is simply that EventMachine is now a dead project and no longer being maintained/supported. The ramifications of this are: no IPv6 support, crashes/segfaults in certain workloads with no workable solution going forward, and lack of integration with external libraries.
124
-
125
- The difference for authors integrating RubyDNS in a daemon should be minimal. For users integrating RubyDNS into an existing system, you need to be aware of the contracts imposed by Celluloid, namely, whether it affects other parts of your system. Some areas of Celluloid are well developed, others are still needing attention (e.g. how it handles forking child processes). We expect the 0.8 branch should remain stable for a long time, but 0.9 branch will eventually become the 1.0 release.
126
-
127
- The benefits of using Celluloid include: fault tolerance, high performance, scalability across multiple hardware threads (when using Rubinius or JRuby), simpler integration with 3rd party data (e.g. `defer` has now been removed since it isn't necessary with celluloid).
128
-
129
- ### Migrating from RubyDNS 0.7.x to 0.8.x
130
-
131
- The primary change is the removal of the dependency on `RExec` which was used for daemons and the addition of the testing dependency `process-daemon`. In order to create and run your own daemon, you may use `process-daemon` or another tool of your choice.
132
-
133
- The transaction options are now conveniently available:
134
-
135
- transaction.options[key] == transaction[key]
136
-
137
- The remote peer address used to be available directly via `transaction[:peer]` but profiling revealed that the `EventMachine::Connection#get_peername` was moderately expensive. Therefore, the incoming connection is now available in `transaction[:connection]` and more specifically `transaction[:peer]` is no longer available and replaced by `transaction[:connection].peername` which gives `[ip_address, port]`.
138
-
139
- ### Migrating from RubyDNS 0.6.x to 0.7.x
140
-
141
- The asynchronous deferred processing became the default and only method for processing requests in `0.7.0`. This simplifies the API but there were a few changes, notably the removal of `defer!` and the addition of `defer`. The reason for this was due to issues relating to deferred processing and the flow of control, which were confusing and introduced bugs in specific situations. Now, you can assume flow control through the entire block even with non-blocking functions.
142
-
143
- RubyDNS::run_server(:listen => SERVER_PORTS) do
144
- match(/\.*.com/, IN::A) do |transaction|
145
- # Won't block and won't continue until fiber.resume is called.
146
- defer do |fiber|
147
- # No domain exists, after 5 seconds:
148
- EventMachine::Timer.new(5) do
149
- transaction.fail!(:NXDomain)
150
-
151
- fiber.resume
152
- end
153
- end
154
- end
155
-
156
- otherwise do
157
- transaction.fail!(:NXDomain)
158
- end
159
- end
160
-
161
- You can see a complete example in `test/test_slow_server.rb`.
162
-
163
- #### Server structure changes
164
-
165
- When integrating RubyDNS into another project, the rule based DSL is often a hurdle rather than a feature. Thus, the rule-based DSL component of `RubyDNS::Server` class has been separated into a derived `RubyDNS::RuleBasedServer` class. `RubyDNS::Server` can be derived and the `RubyDNS::Server#process` method can be overridden to provide a single entry point for DNS processing.
166
-
167
- In addition, `RubyDNS::Server#run` can now start the server, provided you are within an `EventMachine#run` context. The existing entry point, `RubyDNS::run_server` provides the same rule-based DSL as previous versions.
168
-
169
- #### Method name changes
170
-
171
- Some method names have changed to improve consistency.
172
-
173
- - `failure!` became `fail!`
174
- - `append` became `add`
175
- - `append_query!` became `append!`
176
-
177
- ### Migrating from RubyDNS 0.5.x to 0.6.x
178
-
179
- The order of arguments to pattern based rules has changed. For regular expression based rules, the arguments are now ordered `|transaction, match_data|`. The main reason for this change was that in many cases match_data is not important and can thus be ignored, e.g. `|transaction|`.
180
-
181
- Going forward, Ruby 1.8.x is no longer supported.
182
-
183
- ### Migrating from RubyDNS 0.4.x to 0.5.x
184
-
185
- The system standard resolver was synchronous, and this could stall the server when making upstream requests to other DNS servers. A new resolver `RubyDNS::Resolver` now provides an asynchronous interface and the `Transaction::passthrough` makes exclusive use of this to provide high performance asynchonous resolution.
186
-
187
- Here is a basic example of how to use the new resolver in full. It is important to provide both `:udp` and `:tcp` connection specifications, so that large requests will be handled correctly:
188
-
189
- resolver = RubyDNS::Resolver.new([[:udp, "8.8.8.8", 53], [:tcp, "8.8.8.8", 53]])
190
-
191
- EventMachine::run do
192
- resolver.query('google.com', IN::A) do |response|
193
- case response
194
- when RubyDNS::Message
195
- puts "Got response: #{response.answers.first}"
196
- else
197
- # Response is of class RubyDNS::ResolutionFailure
198
- puts "Failed: #{response.message}"
199
- end
200
-
201
- EventMachine::stop
202
- end
203
- end
204
-
205
- Existing code that uses `Resolv::DNS` as a resolver will need to be updated:
206
-
207
- # 1/ Add this at the top of your file; Host specific system information:
208
- require 'rubydns/system'
209
-
210
- # 2/ Change from R = Resolv::DNS.new to:
211
- R = RubyDNS::Resolver.new(RubyDNS::System::nameservers)
212
-
213
- Everything else in the server can remain the same. You can see a complete example in `test/test_resolver.rb`.
214
-
215
- ### Migrating from RubyDNS 0.3.x to 0.4.x
216
-
217
- Due to changes in `resolv.rb`, superficial parts of RubyDNS have changed. Rather than using `:A` to specify A-records, one must now use the class name.
218
-
219
- match(..., :A)
220
-
221
- becomes
222
-
223
- IN = Resolv::DNS::Resource::IN
224
- match(..., IN::A)
225
-
226
124
  ## Contributing
227
125
 
228
126
  1. Fork it
data/Rakefile CHANGED
@@ -1,11 +1,6 @@
1
1
  require "bundler/gem_tasks"
2
2
  require "rspec/core/rake_task"
3
3
 
4
- RSpec::Core::RakeTask.new(:spec) do |task|
5
- task.rspec_opts = ["--require", "simplecov"] if ENV['COVERAGE']
6
- end
4
+ RSpec::Core::RakeTask.new(:test)
7
5
 
8
- task :default => :spec
9
-
10
- require 'celluloid'
11
- Celluloid.logger.level = Logger::ERROR
6
+ task :default => :test
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubydns'
3
+
4
+ INTERFACES = [
5
+ [:udp, "0.0.0.0", 5300],
6
+ [:tcp, "0.0.0.0", 5300],
7
+ ]
8
+
9
+ IN = Resolv::DNS::Resource::IN
10
+
11
+ # Use upstream DNS for name resolution.
12
+ UPSTREAM = RubyDNS::Resolver.new([[:udp, "8.8.8.8", 53], [:tcp, "8.8.8.8", 53]])
13
+
14
+ # Start the RubyDNS server
15
+ RubyDNS::run_server(:listen => INTERFACES) do
16
+ match(%r{test.local}, IN::A) do |transaction|
17
+ transaction.respond!("10.0.0.80")
18
+ end
19
+
20
+ # Default DNS handler
21
+ otherwise do |transaction|
22
+ transaction.passthrough!(UPSTREAM)
23
+ end
24
+ end
@@ -0,0 +1,25 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubydns'
4
+ require 'rubydns/system'
5
+
6
+ INTERFACES = [
7
+ [:udp, "0.0.0.0", 5300],
8
+ [:tcp, "0.0.0.0", 5300]
9
+ ]
10
+
11
+ Name = Resolv::DNS::Name
12
+ IN = Resolv::DNS::Resource::IN
13
+
14
+ UPSTREAM = RubyDNS::Resolver.new([[:udp, "8.8.8.8", 53], [:tcp, "8.8.8.8", 53]])
15
+
16
+ RubyDNS::run_server(:listen => INTERFACES) do
17
+ # How to respond to something other than what was requested.
18
+ match(//, IN::A) do |transaction|
19
+ transaction.respond!(Name.create('foo.bar'), resource_class: IN::CNAME)
20
+ end
21
+
22
+ otherwise do |transaction|
23
+ transaction.passthrough!(UPSTREAM)
24
+ end
25
+ end
@@ -49,8 +49,8 @@ class FlakeyDNS < Process::Daemon
49
49
  logger.info 'Dropping domain MICROSOFT...'
50
50
  transaction.fail!(:NXDomain)
51
51
  else
52
- # Pass the request to the otherwise handler
53
- false
52
+ logger.info 'Passing DNS request upstream...'
53
+ transaction.passthrough!(fallback_resolver_supervisor.actors.first)
54
54
  end
55
55
  end
56
56
 
@@ -0,0 +1,25 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubydns'
3
+
4
+ INTERFACES = [
5
+ [:udp, '0.0.0.0', 5300],
6
+ [:tcp, '0.0.0.0', 5300]
7
+ ]
8
+
9
+ Name = Resolv::DNS::Name
10
+ IN = Resolv::DNS::Resource::IN
11
+
12
+ # Use upstream DNS for name resolution.
13
+ UPSTREAM = RubyDNS::Resolver.new([[:udp, '8.8.8.8', 53], [:tcp, '8.8.8.8', 53]])
14
+
15
+ # Start the RubyDNS server
16
+ RubyDNS.run_server(listen: INTERFACES) do
17
+ match(/test.mydomain.org/, IN::A) do |transaction|
18
+ transaction.respond!('10.0.0.80')
19
+ end
20
+
21
+ # Default DNS handler
22
+ otherwise do |transaction|
23
+ transaction.passthrough!(UPSTREAM)
24
+ end
25
+ end
@@ -0,0 +1,82 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # Copyright, 2009, 2012, by Samuel G. D. Williams. <http://www.codeotaku.com>
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in
13
+ # all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ # THE SOFTWARE.
22
+
23
+ require 'rubygems'
24
+ require 'rubydns'
25
+
26
+ $R = Resolv::DNS.new
27
+ Name = Resolv::DNS::Name
28
+ IN = Resolv::DNS::Resource::IN
29
+
30
+ RubyDNS.run_server(listen: [[:udp, '0.0.0.0', 5400]]) do
31
+ # SOA Record
32
+ # dig @localhost -p 5400 SOA mydomain.org
33
+ match('mydomain.org', IN::SOA) do |transaction|
34
+ #
35
+ # For more details about these headers please see:
36
+ # http://www.ripe.net/ripe/docs/ripe-203.html
37
+ #
38
+
39
+ transaction.respond!(
40
+ Name.create('ns.mydomain.org.'), # Master Name
41
+ Name.create('admin.mydomain.org.'), # Responsible Name
42
+ File.mtime(__FILE__).to_i, # Serial Number
43
+ 1200, # Refresh Time
44
+ 900, # Retry Time
45
+ 3_600_000, # Maximum TTL / Expiry Time
46
+ 172_800 # Minimum TTL
47
+ )
48
+
49
+ transaction.append!(transaction.question, IN::NS, section: :authority)
50
+ end
51
+
52
+ # Default NS record
53
+ # dig @localhost -p 5400 mydomain.org NS
54
+ match('mydomain.org', IN::NS) do |transaction|
55
+ transaction.respond!(Name.create('ns.mydomain.org.'))
56
+ end
57
+
58
+ # For this exact address record, return an IP address
59
+ # dig @localhost -p 5400 CNAME bob.mydomain.org
60
+ match(/([^.]+).mydomain.org/, IN::CNAME) do |transaction|
61
+ transaction.respond!(Name.create('www.mydomain.org'))
62
+ transaction.append!('www.mydomain.org', IN::A)
63
+ end
64
+
65
+ match('80.0.0.10.in-addr.arpa', IN::PTR) do |transaction|
66
+ transaction.respond!(Name.create('www.mydomain.org.'))
67
+ end
68
+
69
+ match('www.mydomain.org', IN::A) do |transaction|
70
+ transaction.respond!('10.0.0.80')
71
+ end
72
+
73
+ match('ns.mydomain.org', IN::A) do |transaction|
74
+ transaction.respond!('10.0.0.10')
75
+ end
76
+
77
+ # Default DNS handler
78
+ otherwise do |transaction|
79
+ # Non-Existant Domain
80
+ transaction.fail!(:NXDomain)
81
+ end
82
+ end