test/fortune-dns.rb in rubydns-0.3.4 vs test/fortune-dns.rb in rubydns-0.4.0

- old
+ new

@@ -1,6 +1,7 @@ #!/usr/bin/env ruby +# encoding: utf-8 # Copyright (c) 2009, 2011 Samuel G. D. Williams. <http://www.oriontransfer.co.nz> # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal @@ -36,14 +37,22 @@ if RExec.current_user != "root" $stderr.puts "Sorry, this command needs to be run as root!" exit 1 end +# dig @localhost fortune CNAME +# + +# HELLO = "こんいちは".force_encoding('ASCII-8BIT') + # The Daemon itself class FortuneDNS < RExec::Daemon::Base @@var_directory = File.dirname(__FILE__) + Name = Resolv::DNS::Name + IN = Resolv::DNS::Resource::IN + def self.run # Don't buffer output (for debug purposes) $stderr.sync = true cache = {} @@ -51,34 +60,45 @@ # Start the RubyDNS server RubyDNS::run_server do on(:start) do RExec.change_user(RUN_AS) + if ARGV.include?("--debug") + @logger.level = Logger::DEBUG + else + @logger.level = Logger::WARN + end end - - match(/(.*)\.fortune/, :TXT) do |match, transaction| + + match(/stats\.fortune/, IN::TXT) do |match, transaction| + $stderr.puts "Sending stats: #{stats.inspect}" + transaction.respond!(stats.inspect) + end + + match(/(.+)\.fortune/, IN::TXT) do |match, transaction| fortune = cache[match[1]] stats[:requested] += 1 if fortune - transaction.respond!(fortune) + transaction.respond!(*fortune.chunked) else - transaction.failure(:NXDomain) + transaction.failure!(:NXDomain) end end - match(/stats.fortune/, :TXT) do |match, transaction| - transaction.respond!(stats.inspect) + match(/fortune/, [IN::CNAME, IN::TXT]) do |match, transaction| + fortune = `fortune`.gsub(/\s+/, " ").strip + checksum = Digest::MD5.hexdigest(fortune) + cache[checksum] = fortune + + transaction.respond!("Text Size: #{fortune.size} Byte Size: #{fortune.bytesize}", :resource_class => IN::TXT, :ttl => 0) + transaction.respond!(Name.create(checksum + ".fortune"), :resource_class => IN::CNAME, :ttl => 0) end - match(/fortune/, [:CNAME]) do |match, transaction| + match(/short.fortune/, IN::TXT) do |match, transation| fortune = `fortune -s`.gsub(/\s+/, " ").strip - checksum = Digest::MD5.hexdigest(fortune) - cache[checksum] = fortune - name = Resolv::DNS::Name.create(checksum + ".fortune") - - transaction.respond!(name, :resource_class => :CNAME, :ttl => 0) + transaction.respond!(*fortune.chunked, :ttl => 0) end # Default DNS handler otherwise do |transaction| transaction.failure!(:NXDomain)