Skip to content

Instantly share code, notes, and snippets.

@peterkeen
Last active Feb 5, 2017
Embed
What would you like to do?
require 'rubydns'
require 'httparty'
require 'lru_redux'
class ZeroTierAPI
include HTTParty
base_uri "https://my.zerotier.com/api"
headers 'Authorization' => "Bearer #{ENV['ZT_API_TOKEN']}"
def initialize
@cache = LruRedux::TTL::ThreadSafeCache.new(100, 300)
end
def members
@cache.getset('members') do
network_members = self.class.get("/network/#{ENV['ZT_NETWORK_ADDRESS']}/member").keys
network_members.map do |member|
self.class.get("/network/#{ENV['ZT_NETWORK_ADDRESS']}/member/#{member}")
end
end
end
def member(name)
members.detect { |m| m['nodeId'] == host || m['name'] == host }
end
end
INTERFACES = [
[:udp, "0.0.0.0", 5300],
[:tcp, "0.0.0.0", 5300]
]
ZT = ZeroTierAPI.new
RubyDNS::run_server(:listen => INTERFACES) do
match(/\w+\.#{ENV['ZT_NETWORK_NAME']/) do |transaction|
host, _ = transaction.name.split('.', 2)
member = ZT.member(host)
# inactive members sometimes have subnets as their ipAssignment
if member && member['config']['ipAssignments'].first !~ /\//
transaction.respond!(member['config']['ipAssignments'].first, ttl: 300)
else
transaction.fail!(:NXDomain)
end
end
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