Class: DnsOne::ZoneSearch

Inherits:
Object show all
Includes:
Singleton
Defined in:
lib/dns_one/zone_search.rb

Constant Summary collapse

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

Instance Method Summary collapse

Instance Method Details

#query(dom_name, res_class, ip_address) ⇒ Object



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
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/dns_one/zone_search.rb', line 28

def query dom_name, res_class, ip_address
    return unless dom_name =~ Util::DOM_REGEX

    dom_name = dom_name.dup
    res_class_short = Util.last_mod res_class # :A, :NS, found in conf.yml:record_sets items
    Global.logger.debug "request #{ dom_name }/#{res_class_short} from #{ip_address}..."

    records = []

    rec_set_name, from_cache = find_record_set dom_name
    Global.logger.debug "domain #{ rec_set_name ? "found, rec_set_name = '#{rec_set_name}'" : 'not found' }"
    return unless rec_set_name

    # use first record set if rec_set_name == ''
    rec_set_name = @conf.record_sets.to_h.keys.first if rec_set_name == ''
 
    rec_set = @conf.record_sets[rec_set_name]
    Global.logger.debug "record set #{ rec_set ? 'found' : 'not found' }"
    return records unless rec_set

    # TODO: move parsing logic to own class

    recs = rec_set[res_class_short.to_sym]
    Global.logger.debug "record(s) #{ recs ? 'found' : 'not found' }"

    # Loop over 1 or more
    recs = [recs]
    recs.flatten! unless res_class == IN::SOA

    recs.compact.each do |val_raw|
        val = if res_class == IN::NS
            Name.create val_raw
        elsif res_class == IN::SOA
            [0, 1].each{|i| val_raw[i] = Name.create val_raw[i] }
            val_raw
        else
            val_raw
        end
        records << OpenStruct.new(val: val, res_class: res_class, section: 'answer')
    end

    [records, from_cache]
end

#setupObject



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/dns_one/zone_search.rb', line 14

def setup
    @conf = Global.conf
    check_record_sets
    @backend = set_backend
    @cache = Cache.new Global.conf.cache_max
    @ignore_subdomains_re = build_ignore_subdomains_re

    if @backend.preload_dummy?
        query 'dummy.com.br', Resolv::DNS::Resource::IN, '1.2.3.4'
    end

    self
end