class Proxy::Dns::Dnsmasq::Openwrt

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/openwrt.rb, line 7
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/openwrt.rb, line 45
def add_cname(name, canonical)
  found = find_type(:cname, :name, name)
  return true if found && found.options[:target] == canonical

  c = found
  c = DSL::Config.new :cname unless c
  c.options[:cname] = name
  c.options[:target] = canonical

  configuration << c unless found
  @dirty = true
end
add_entry(type, fqdn, ip) click to toggle source
# File lib/smart_proxy_dns_dnsmasq/backend/openwrt.rb, line 23
def add_entry(type, fqdn, ip)
  raise Proxy::Dns::Error, "OpenWRT UCI can't manage IPv6 entries" if type == 'AAAA' || type == 'PTR' && IPAddr.new(ip).ipv6?
  found = find_type(:domain, :name, fqdn)
  return true if found && found.options[:ip] == ip

  h = found
  h = DSL::Config.new :domain unless h
  h.options[:name] = fqdn
  h.options[:ip] = ip

  configuration << h unless found
  @dirty = true
end
remove_cname(name) click to toggle source
# File lib/smart_proxy_dns_dnsmasq/backend/openwrt.rb, line 58
def remove_cname(name)
  return true unless c = find_type(:cname, :name, name)

  configuration.delete c
  @dirty = true
end
remove_entry(type, fqdn = nil, ip = nil) click to toggle source
# File lib/smart_proxy_dns_dnsmasq/backend/openwrt.rb, line 37
def remove_entry(type, fqdn = nil, ip = nil)
  raise Proxy::Dns::Error, "OpenWRT UCI can't manage IPv6 entries" if type == 'AAAA' || type == 'PTR' && IPAddr.new(ip).ipv6?
  return true unless h = find_type(:domain, fqdn && :name || :ip, fqdn || ip)

  configuration.delete h
  @dirty = true
end
update!() click to toggle source
# File lib/smart_proxy_dns_dnsmasq/backend/openwrt.rb, line 15
def update!
  return unless @dirty
  @dirty = false

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

Private Instance Methods

configuration() click to toggle source
# File lib/smart_proxy_dns_dnsmasq/backend/openwrt.rb, line 85
def configuration
  load! unless @configuration
  @configuration
end
find_type(filter_type, search_type, value) click to toggle source
# File lib/smart_proxy_dns_dnsmasq/backend/openwrt.rb, line 67
def find_type(filter_type, search_type, value)
  configuration.find do |config|
    next unless config.type == filter_type

    config.options.find do |name, opt|
      next unless name == search_type

      opt == value
    end
  end
end
load!() click to toggle source
# File lib/smart_proxy_dns_dnsmasq/backend/openwrt.rb, line 79
def load!
  @configuration = []
  dsl = DSL.new(@configuration)
  dsl.instance_eval open(@config_file).read, @config_file
end