Class: DnsOne::Server

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

Instance Method Summary collapse

Constructor Details

#initializeServer

Returns a new instance of Server.



8
9
10
11
# File 'lib/dns_one/server.rb', line 8

def initialize 
    @zone_search = ZoneSearch.instance.setup
    @req_log = nil
end

Instance Method Details

#dns_daemon_interfacesObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/dns_one/server.rb', line 60

def dns_daemon_interfaces
    if RExec.current_user == 'root'
        Global.conf.interfaces
    else
        ports = Global.conf.interfaces.map do |port|
            if port[2] <= 1024
                Global.logger.warn "Changing listening port #{port.join ':'} to #{port[2] + 10000} for non-root process."
                port[2] += 10000 
            end
            port
        end
        ports
    end
end

#resolve(transaction) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/dns_one/server.rb', line 21

def resolve transaction
    rcode = :NoError
    resp_log = []

    begin
        domain_name = transaction.question.to_s
        ip_address = transaction.options[:peer] rescue nil

        records, from_cache = @zone_search.query domain_name, transaction.resource_class, ip_address

        if records
            if records.empty?
                transaction.fail! :NoError
            else
                records.each do |rec|
                    resp_log << rec
                    transaction.respond! *[rec.val].flatten, {resource_class: rec.res_class, section: rec.section}
                end
            end
        else
            rcode = :NXDomain
            transaction.fail! :NXDomain
        end
    rescue => e
        rcode = :ServFail
    end

    @req_log.on_response *[
        ip_address, 
        domain_name, 
        transaction.resource_class, 
        rcode, 
        resp_log, 
        from_cache
    ]

    raise e if e
end

#runObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/dns_one/server.rb', line 75

def run
    srv = self

    RubyDNS::run_server(dns_daemon_interfaces) do
        on(:start) do
            srv.start
        end

        match(/(.+)/) do |transaction|
            srv.resolve transaction
        end

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

#startObject



13
14
15
16
17
18
19
# File 'lib/dns_one/server.rb', line 13

def start
    if RExec.current_user == 'root'
        RExec.change_user Global.conf.run_as
    end
    @req_log ||= ReqLog::ReqLog.new
    Global.logger.info "Running as #{RExec.current_user}"
end