Sha256: 48b1a8ba96ef995535d1947fd532376da3cb7be66f13895e262bbfaab294bbf4

Contents?: true

Size: 2 KB

Versions: 5

Compression:

Stored size: 2 KB

Contents

require "dns_one/zone_search"

module DnsOne; class Server

    DEFAULT_RUN_AS = "dnsone"

    DNS_DAEMON_INTERFACES = [
        [:udp, "0.0.0.0", 53],
        [:tcp, "0.0.0.0", 53],
        [:udp, "::", 5300],
        [:tcp, "::", 5300]
    ]

    def initialize conf, conf_zone_search
        @conf = conf
        @zone_search = ZoneSearch.instance.setup conf_zone_search
    end

    def run
        zone_search = @zone_search
        conf = @conf

        RubyDNS::run_server(listen: dns_daemon_interfaces, logger: Log.logger) do
            on(:start) do
                if RExec.current_user == 'root'
                    run_as = conf[:run_as] || DEFAULT_RUN_AS
                    RExec.change_user run_as
                end
                Log.i "Running as #{RExec.current_user}"
            end

            match(/(.+)/) do |t| # transaction
                domain_name = t.question.to_s
                ip_address = t.options[:peer] rescue nil

                records = zone_search.query domain_name, t.resource_class, ip_address

                if records
                    if records.empty?
                        t.fail! :NoError
                    else
                        records.each do |rec|
                            t.respond! *[rec.val].flatten, {resource_class: rec.res_class, section: rec.section}
                        end
                    end
                else
                    t.fail! :NXDomain
                end
            end

            otherwise do |t|
                t.fail! :NXDomain
            end
        end
    end

    def dns_daemon_interfaces
        if RExec.current_user == 'root'
            DNS_DAEMON_INTERFACES
        else
            ports = DNS_DAEMON_INTERFACES.map do |port|
                if port[2] <= 1024
                    Log.w "Changing listening port #{port[2]} to #{port[2] + 10000} for non-root process."
                    port[2] += 10000 
                end
                port
            end
            ports
        end
    end

end; end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
dns_one-0.4.28 lib/dns_one/server.rb
dns_one-0.4.27 lib/dns_one/server.rb
dns_one-0.4.26 lib/dns_one/server.rb
dns_one-0.4.25 lib/dns_one/server.rb
dns_one-0.4.24 lib/dns_one/server.rb