Class: Matchd::Server

Inherits:
Async::DNS::Server
  • Object
show all
Defined in:
lib/matchd/server.rb

Overview

Specific implementation of the Async::DNS::Server using a list of rules to determine it’s response

Rules will get #call‘ed in order until the first one matches (returns truthy). If none matches, a default Rule::Passthrough will get executed forwarding the query to the resolver defined via the `:resolver` option.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rules, listen, options = {}) ⇒ Server

Returns a new instance of Server.

Parameters:

  • rules (Matchd::Registry|Array<Matchd.Rule>)

    A rules registry

  • listen (Array)

    On which interfaces to listen (‘[[<protocol>, <ip>, <port>], …]`). See `Matchd::Glue::AsyncEndpoint` for more formats.

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :resolver (Array|Async::DNS::Resolver)

    The upstream resolvers (‘[[<protocol>, <ip>, <port>], …]`). See `Matchd::Glue::AsyncEndpoint` for more formats. (default: `Async::DNS::System.nameservers`)

  • :forced_passthtough (TrueClass|FalseClass)

    The default Passthrough rule’s ‘force` option



15
16
17
18
19
20
21
22
23
# File 'lib/matchd/server.rb', line 15

def initialize(rules, listen, options = {})
  @rules = rules
  @resolver = options.delete(:resolver)
  @forced_passthtough = options.delete(:forced_passthtough) { true }

  listen = Matchd::Glue::AsyncEndpoint.parse(listen)

  super(listen, *options)
end

Instance Attribute Details

#resolverObject (readonly)

Returns the value of attribute resolver.



25
26
27
# File 'lib/matchd/server.rb', line 25

def resolver
  @resolver
end

#rulesObject (readonly)

Returns the value of attribute rules.



25
26
27
# File 'lib/matchd/server.rb', line 25

def rules
  @rules
end

Instance Method Details

#passthrough!(name, resource_class, transaction) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/matchd/server.rb', line 35

def passthrough!(name, resource_class, transaction)
  Matchd::Rule::Passthrough.new(
    "passthrough" => resolver,
    "match" => name,
    "resource_class" => resource_class,
    "force" => @forced_passthtough
  ).visit!(self, name, resource_class, transaction)
end

#process(name, resource_class, transaction) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/matchd/server.rb', line 27

def process(name, resource_class, transaction)
  found = rules.any? do |rule|
    rule.call(self, name, resource_class, transaction)
  end

  passthrough!(name, resource_class, transaction) unless found
end