Class: Proxy::Dns::Dnsmasq::Default

Inherits:
Record
  • Object
show all
Defined in:
lib/smart_proxy_dns_dnsmasq/backend/default.rb

Defined Under Namespace

Classes: AddressEntry, CNAMEEntry, HostEntry, PTREntry

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Record

#do_create, #do_remove

Constructor Details

#initialize(config, reload_cmd, dns_ttl) ⇒ Default

Returns a new instance of Default.



5
6
7
8
9
10
11
# File 'lib/smart_proxy_dns_dnsmasq/backend/default.rb', line 5

def initialize(config, reload_cmd, dns_ttl)
  @config_file = config
  @reload_cmd = reload_cmd
  @dirty = false

  super(dns_ttl)
end

Instance Attribute Details

#config_fileObject (readonly)

Returns the value of attribute config_file.



3
4
5
# File 'lib/smart_proxy_dns_dnsmasq/backend/default.rb', line 3

def config_file
  @config_file
end

#dirtyObject (readonly)

Returns the value of attribute dirty.



3
4
5
# File 'lib/smart_proxy_dns_dnsmasq/backend/default.rb', line 3

def dirty
  @dirty
end

#reload_cmdObject (readonly)

Returns the value of attribute reload_cmd.



3
4
5
# File 'lib/smart_proxy_dns_dnsmasq/backend/default.rb', line 3

def reload_cmd
  @reload_cmd
end

Instance Method Details

#add_cname(name, canonical) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/smart_proxy_dns_dnsmasq/backend/default.rb', line 50

def add_cname(name, canonical)
  # dnsmasq will silently ignore broken CNAME records, even though they stay in config
  # So avoid flooding the configuration if broken CNAME entries are added
  return true if configuration.find { |entry| entry.is_a?(CNAMEEntry) && entry.name == name }

  c = CNAMEEntry.new
  c.name = name
  c.target = canonical
  configuration << c
  @dirty = true
end

#add_entry(type, fqdn, ip) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/smart_proxy_dns_dnsmasq/backend/default.rb', line 21

def add_entry(type, fqdn, ip)
  case type
  when 'A', 'AAAA'
    e = AddressEntry.new
    e.ip = ip
    e.fqdn = [fqdn]
  when 'PTR'
    e = PTREntry.new
    e.ip = IPAddr.new(ip).reverse
    e.fqdn = fqdn
  end

  configuration << e
  @dirty = true
end

#remove_cname(name) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/smart_proxy_dns_dnsmasq/backend/default.rb', line 62

def remove_cname(name)
  c = configuration.find { |entry| entry.is_a?(CNAMEEntry) && entry.name == name }
  return true unless c

  configuration.delete c
  @dirty = true
end

#remove_entry(type, fqdn = nil, ip = nil) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/smart_proxy_dns_dnsmasq/backend/default.rb', line 37

def remove_entry(type, fqdn = nil, ip = nil)
  e = case type
      when 'A', 'AAAA'
        configuration.find { |entry| entry.is_a?(AddressEntry) && entry.fqdn.include?(fqdn) }
      when 'PTR'
        configuration.find { |entry| entry.is_a?(PTREntry) && entry.ip.include?(ip.split('.').reverse.join('.')) }
      end
  return true unless e

  configuration.delete e
  @dirty = true
end

#update!Object



13
14
15
16
17
18
19
# File 'lib/smart_proxy_dns_dnsmasq/backend/default.rb', line 13

def update!
  return unless @dirty
  @dirty = false

  File.write(@config_file, configuration.join("\n") + "\n")
  system(@reload_cmd)
end