Class: VagrantDNS::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-dns/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(registry, listen:, ttl:, resolver:, passthrough:) ⇒ Server

Returns a new instance of Server.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/vagrant-dns/server.rb', line 9

def initialize(registry, listen:, ttl:, resolver:, passthrough:)
  @registry = registry.to_hash
  @listen = listen
  @ttl = ttl

  @resolver = if resolver.nil? || resolver == :system
    RubyDNS::Resolver.new(Async::DNS::System.nameservers)
  elsif !resolver || resolver.empty?
    nil
  else
    RubyDNS::Resolver.new(resolver)
  end

  if passthrough && !resolver
    puts "[Warning] 'passthrough' config has no effect, sice no passthrough resolver is set."
  end

  @passthrough = !!@resolver && passthrough
end

Instance Attribute Details

#listenObject (readonly)

Returns the value of attribute listen.



7
8
9
# File 'lib/vagrant-dns/server.rb', line 7

def listen
  @listen
end

#passthroughObject (readonly)

Returns the value of attribute passthrough.



7
8
9
# File 'lib/vagrant-dns/server.rb', line 7

def passthrough
  @passthrough
end

#registryObject (readonly)

Returns the value of attribute registry.



7
8
9
# File 'lib/vagrant-dns/server.rb', line 7

def registry
  @registry
end

#resolverObject (readonly)

Returns the value of attribute resolver.



7
8
9
# File 'lib/vagrant-dns/server.rb', line 7

def resolver
  @resolver
end

#ttlObject (readonly)

Returns the value of attribute ttl.



7
8
9
# File 'lib/vagrant-dns/server.rb', line 7

def ttl
  @ttl
end

Instance Method Details

#runObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/vagrant-dns/server.rb', line 29

def run
  # need those clusures for the +RubyDNS::run_server+ block
  passthrough = self.passthrough
  registry = self.registry
  resolver = self.resolver
  ttl = self.ttl

  _passthrough = if passthrough
    proc do |transaction|
      transaction.passthrough!(resolver) do |response|
        puts "Passthrough response: #{response.inspect}"
      end
    end
  end

  RubyDNS::run_server(listen) do
    # match all known patterns first
    registry.each do |pattern, ip|
      match(pattern, Resolv::DNS::Resource::IN::A) do |transaction, match_data|
        transaction.respond!(ip, ttl: ttl)
      end
    end

    case passthrough
    when true
      # forward everything
      otherwise(&_passthrough)
    when false
      # fail known patterns for non-A queries as NotImp
      registry.each do |pattern, ip|
        match(pattern) do |transaction, match_data|
          transaction.fail!(:NotImp)
        end
      end

      # unknown pattern end up as NXDomain
      otherwise do |transaction|
        transaction.fail!(:NXDomain)
      end
    when :unknown
      # fail known patterns for non-A queries as NotImp
      registry.each do |pattern, ip|
        match(pattern) do |transaction, match_data|
          transaction.fail!(:NotImp)
        end
      end

      # forward only unknown patterns
      otherwise(&_passthrough)
    end
  end
end