Sha256: 78dd055ed1774473d803a910234c1ca8e0ab59deb956388c6b3664290809d19e

Contents?: true

Size: 1.49 KB

Versions: 7

Compression:

Stored size: 1.49 KB

Contents

require 'rubydns'

module Landrush
  class Server < RExec::Daemon::Base
    Name = Resolv::DNS::Name
    IN   = Resolv::DNS::Resource::IN

    def self.port
      @port ||= 10053
    end

    def self.port=(port)
      @port = port
    end

    def self.upstream_servers
      # Doing collect to cast protocol to symbol because JSON store doesn't know about symbols
      @upstream_servers ||= Store.config.get('upstream').collect {|i| [i[0].to_sym, i[1], i[2]]}
    end

    def self.interfaces
      [
        [:udp, "0.0.0.0", port],
        [:tcp, "0.0.0.0", port]
      ]
    end

    def self.upstream
      @upstream ||= RubyDNS::Resolver.new(upstream_servers)
    end

    def self.pid
      RExec::Daemon::ProcessFile.recall(self)
    end

    # For RExec
    def self.working_directory
      Landrush.working_dir
    end

    def self.running?
      RExec::Daemon::ProcessFile.status(self) == :running
    end

    def self.prefork
      super
    end

    def self.run
      server = self
      RubyDNS::run_server(:listen => interfaces) do
        self.logger.level = Logger::INFO

        match(/.*/, IN::A) do |transaction|
          host = Store.hosts.find(transaction.name)
          if host
            transaction.respond!(Store.hosts.get(host), {:ttl => 0})
          else
            transaction.passthrough!(server.upstream)
          end
        end

        # Default DNS handler
        otherwise do |transaction|
          transaction.passthrough!(server.upstream)
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
landrush-0.15.3 lib/landrush/server.rb
landrush-0.15.2 lib/landrush/server.rb
landrush-0.15.1 lib/landrush/server.rb
landrush-0.15.0 lib/landrush/server.rb
landrush-0.14.1 lib/landrush/server.rb
landrush-0.14.0 lib/landrush/server.rb
landrush-0.13.1 lib/landrush/server.rb