Skip to content

Instantly share code, notes, and snippets.

@peterkeen
Created February 4, 2017 01:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterkeen/26a4565a29efc4d17168ff7de8f6f80a to your computer and use it in GitHub Desktop.
Save peterkeen/26a4565a29efc4d17168ff7de8f6f80a to your computer and use it in GitHub Desktop.
require 'rubydns'
INTERFACES = [
[:udp, "0.0.0.0", 5300],
[:tcp, "0.0.0.0", 5300]
]
IN = Resolv::DNS::Resource::IN
UPSTREAM = RubyDNS::Resolver.new([[:udp, "8.8.8.8", 53], [:tcp, "8.8.8.8", 53]])
# name => [
# [ "label", TYPE, [args to create type]... ]
# ]
# args can include options which are stripped.
# the only supported option is alias, which acts like an AWS ALIAS type
CONFIG = {
:subspace => [
["@", IN::SOA,
["subspace.bugsplat.info", "domains.bugsplat.info", Time.now.to_i, 0, 0, 0, 30]
],
["@", IN::NS,
["dns1.bugsplat.info"],
["dns2.bugsplat.info"]
],
["@", IN::A,
['subspace.bugsplat.info', alias: true],
],
["www", IN::A,
['subspace.bugsplat.info', alias: true],
],
],
"whatever.com" => :subspace,
"whatever2.net" => :subspace,
}
def alias_for(name, resource_class)
begin
UPSTREAM.addresses_for(name, resource_class)
rescue RubyDNS::ResolutionFailure
nil
end
end
RubyDNS::run_server(:listen => INTERFACES) do
CONFIG.each do |domain, config|
next if domain.is_a? Symbol
config = CONFIG[config] if config.is_a? Symbol
config.each do |nameconfig|
nameconfig = nameconfig.dup
name = nameconfig.shift
type = nameconfig.shift
values = nameconfig
fqdn = name == '@' ? domain : "#{name}.#{domain}"
match(fqdn, type) do |transaction|
rrs = []
if values.length == 1 && values.last.last.kind_of?(Hash) && values.last.last[:alias]
rrs = alias_for(values.last.first, type).map { |v| type.new(v) }
else
values.each do |value|
value = value.dup
options = value.pop if value.last.kind_of?(Hash)
value = value.map { |v| v.kind_of?(String) && v =~ /[a-zA-Z]/ ? Resolv::DNS::Name.create(v) : v }
rrs << type.new(*Array(value))
end
end
transaction.send(:append_question!)
transaction.add(rrs, ttl: 30)
end
end
end
# Default DNS handler
otherwise do |transaction|
transaction.fail!(:NXDomain)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment