class Proxy::Dns::Dnsmasq::Default

Attributes

config_file[R]
dirty[R]
reload_cmd[R]

Public Class Methods

new(config, reload_cmd, dns_ttl) click to toggle source
Calls superclass method Proxy::Dns::Dnsmasq::Record::new
# 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

Public Instance Methods

add_cname(name, canonical) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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!() click to toggle source
# 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

Private Instance Methods

configuration() click to toggle source
# File lib/smart_proxy_dns_dnsmasq/backend/default.rb, line 125
def configuration
  load! unless @configuration
  @configuration
end
load!() click to toggle source
# File lib/smart_proxy_dns_dnsmasq/backend/default.rb, line 72
    def load!
      @configuration = []
      File.open(@config_file).each_line do |line|
        line = line.strip
        next if line.empty? || line.start_with?('#') || !line.include?('=')

        option, value = line.split('=')

        case option
        when 'address'
          data = value.split('/')
          data.shift

          entry = AddressEntry.new
          entry.ip = data.pop
          entry.fqdn = data
        when 'cname'
          data = value.split(',')

          entry = CNAMEEntry.new
          entry.name = data.shift
          entry.target = data.shift
          entry.ttl = data.shift
        when 'ptr-record'
          data = value.split(',')

          entry = PTREntry.new
          entry.ip = data[0]
          entry.fqdn = data[1]
#       TODO: Handle these properly
#       when 'host-record'
#         data = value.split(',')

#         entry = HostEntry.new
#         until data.empty?
#           v = data.pop
#           if !entry.ttl && /\A\d+\z/ === v
#             entry.ttl = v
#           end

#           begin
#             ip = IPAddr.new(v)
#             entry.ip << v
#           rescue IPAddr::InvalidAddressError
#             entry.fqdn << v
#           end
#         end
        end

        @configuration << entry if entry
      end
    end