Last active
February 5, 2017 15:56
-
-
Save peterkeen/f87ae8db4e7cf05d8e0fc69aa2f86748 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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