|
| 1 | +# Getting Started |
| 2 | + |
| 3 | +This guide explains how to get started running your own DNS server with RubyDNS. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +Create a new directory for your project, with a gemfile, and then add the gem to your project: |
| 8 | + |
| 9 | +~~~ bash |
| 10 | +$ bundle add rubydns |
| 11 | +~~~ |
| 12 | + |
| 13 | +## Usage |
| 14 | + |
| 15 | +### Simple DNS Server |
| 16 | + |
| 17 | +This example demonstrates how to create a simple DNS server that responds to `test.local A` and forwards all other requests to the system default resolver. |
| 18 | + |
| 19 | +``` ruby |
| 20 | +#!/usr/bin/env ruby |
| 21 | +require 'rubydns' |
| 22 | + |
| 23 | +# Use the system default resolver for upstream queries: |
| 24 | +upstream = Async::DNS::Resolver.default |
| 25 | + |
| 26 | +# We will use port 5300 so we don't need to run the server as root: |
| 27 | +endpoint = Async::DNS::Endpoint.for("localhost", port: 5300) |
| 28 | + |
| 29 | +# Start the RubyDNS server: |
| 30 | +RubyDNS.run(endpoint) do |
| 31 | + match(%r{test.local}, Resolv::DNS::Resource::IN::A) do |transaction| |
| 32 | + transaction.respond!("10.0.0.80") |
| 33 | + end |
| 34 | + |
| 35 | + # Default DNS handler |
| 36 | + otherwise do |transaction| |
| 37 | + transaction.passthrough!(upstream) |
| 38 | + end |
| 39 | +end |
| 40 | +``` |
| 41 | + |
| 42 | +### Custom Servers |
| 43 | + |
| 44 | +It is possible to create and integrate your own custom servers, however this functionality has now moved to [`Async::DNS::Server`](https://github.com/socketry/async-dns). |
| 45 | + |
| 46 | +``` ruby |
| 47 | +class MyServer < Async::DNS::Server |
| 48 | + def process(name, resource_class, transaction) |
| 49 | + transaction.fail!(:NXDomain) |
| 50 | + end |
| 51 | +end |
| 52 | + |
| 53 | +Async do |
| 54 | + task = MyServer.new.run |
| 55 | + |
| 56 | + # ... do other things, e.g. run specs/tests |
| 57 | + |
| 58 | + # Shut down the server manually if required, otherwise it will run indefinitely. |
| 59 | + # task.stop |
| 60 | +end |
| 61 | +``` |
| 62 | + |
| 63 | +This is the best way to integrate with other projects. |
0 commit comments