Sha256: 2bb73d3324905cadeed93048bac5000544673f4dad8b2b0152f954eb3c5385af

Contents?: true

Size: 1.26 KB

Versions: 16

Compression:

Stored size: 1.26 KB

Contents

require 'async/dns'
require 'async/dns/system'
require 'mkit/mkit_interface'
require 'ipaddr'

# INTERFACES = [
#   [:udp, "127.0.0.20", 53],
#   [:tcp, "127.0.0.20", 53],
# ]
# @resolver = RubyDNS::Resolver.new(
#   [:udp, "192.168.4.254", 53],
#   [:tcp, "192.168.4.254", 53]
# )
# 
# # Use upstream DNS for name resolution.
UPSTREAM = RubyDNS::Resolver.new([
   [:udp, "8.8.8.8", 53],
   [:tcp, "8.8.8.8", 53]
 ])

Name = Resolv::DNS::Name
IN = Resolv::DNS::Resource::IN

module MKIt
  class DNS < Async::DNS::Server
    def initialize
      addr = MKIt::Interface.ip
      listen_addr = [
        [:udp, addr, 53],
        [:tcp, addr, 53],
      ]
      super(listen_addr)
      @logger.info!
      @resolver = RubyDNS::Resolver.new(Async::DNS::System.nameservers)
    end
    def process(name, resource_class, transaction)
      host = DnsHost.find_by_name(name)
      if host.nil?
        transaction.passthrough!(@resolver)
      else
        ipaddr = IPAddr.new host.ip
        if resource_class == Resolv::DNS::Resource::IN::A
          transaction.respond!(ipaddr.to_s)
        elsif resource_class == Resolv::DNS::Resource::IN::AAAA
          transaction.respond!(ipaddr.ipv4_mapped.to_s)
        else
          transaction.fail!(:NXDomain)
        end
      end
    end
  end
end


Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
mkit-0.9.0 lib/mkit/mkit_dns.rb
mkit-0.8.0 lib/mkit/mkit_dns.rb
mkit-0.7.2 lib/mkit/mkit_dns.rb
mkit-0.7.1 lib/mkit/mkit_dns.rb
mkit-0.7.0 lib/mkit/mkit_dns.rb
mkit-0.6.3 lib/mkit/mkit_dns.rb
mkit-0.6.2 lib/mkit/mkit_dns.rb
mkit-0.6.1 lib/mkit/mkit_dns.rb
mkit-0.6.0 lib/mkit/mkit_dns.rb
mkit-0.5.0 lib/mkit/mkit_dns.rb
mkit-0.4.3 lib/mkit/mkit_dns.rb
mkit-0.4.2 lib/mkit/mkit_dns.rb
mkit-0.4.1 lib/mkit/mkit_dns.rb
mkit-0.4.0 lib/mkit/mkit_dns.rb
mkit-0.3.0 lib/mkit/mkit_dns.rb
mkit-0.2.0 lib/mkit/mkit_dns.rb