Class: Kontejner::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/kontejner/server.rb

Constant Summary collapse

GOOGLE_SERVERS =
[ [:udp, '8.8.8.8', 53], [:tcp, '8.8.8.8', 53], [:udp, '8.8.4.4', 53], [:tcp, '8.8.4.4', 53] ]
IN =
Resolv::DNS::Resource::IN

Instance Method Summary collapse

Constructor Details

#initialize(domain:, docker:, ttl:) ⇒ Server

Returns a new instance of Server.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/kontejner/server.rb', line 10

def initialize(domain:,docker:,ttl:)
  @dns = RubyDNS::RuleBasedServer.new

  @logger = @dns.logger
  @logger.level = Logger::DEBUG

  @matcher = /(?<subdomain>[^.]+)\.#{domain}$/
  @ttl = ttl

  @upstream = RubyDNS::Resolver.new(GOOGLE_SERVERS)
  @docker = Kontejner::Resolver.new(docker: docker)

  @logger.info("Docker Connection #{docker}")
end

Instance Method Details

#run(options) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/kontejner/server.rb', line 45

def run(options)
  EventMachine.run do
    trap("INT") do
      EventMachine.stop
    end

    @dns.run(options)
  end

  @dns.fire(:stop)
end

#start(**options) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/kontejner/server.rb', line 25

def start(**options)
  @logger.debug("Matching #{@matcher}")

  @dns.match(@matcher, IN::A) do |t, match|
    ip = @docker.resolve(match[:subdomain])

    if ip
      t.respond!(ip, ttl: @ttl)
    else
      t.fail! :NXDomain
    end
  end

  @dns.otherwise do |q|
    q.passthrough!(@upstream)
  end

  run(options)
end