Permalink
Browse files

Cache whois lookups in a YAML::DBM database

  • Loading branch information...
1 parent 92e9864 commit 7d7305ee0586b9be9bed896c05e45571d27c7458 @purcell committed Nov 6, 2012
Showing with 24 additions and 1 deletion.
  1. +24 −1 bin/postfix-policy-whois
View
25 bin/postfix-policy-whois
@@ -19,6 +19,8 @@ DNS_TO_WHOIS_CHECKLIST = {
$0='postfix-policy-whois'
+DBFILE = "#{ENV['TMPDIR']}/#{$0}-cache"
+
require 'syslog'
$LOG = Syslog.open($0, Syslog::LOG_PID | Syslog::LOG_PERROR, Syslog::LOG_MAIL)
@@ -56,7 +58,7 @@ end
require 'socket'
-def whois_via(server, domain)
+def whois_via_raw(server, domain)
$LOG.info("Checking whois for #{domain} using #{server}")
Timeout::timeout(5) do
TCPSocket.open(server, 43) do |sock|
@@ -66,6 +68,27 @@ def whois_via(server, domain)
end
end
+require 'yaml/dbm'
+WHOIS_CACHE = YAML::DBM::open(DBFILE, 500, DBM::WRCREAT)
+at_exit { WHOIS_CACHE.close() }
+
+def whois_via(server, domain)
+ key = "#{domain}@#{server}"
+ val = WHOIS_CACHE[key]
+ if val
+ $LOG.debug("WHOIS cache hit for #{key.inspect}, created #{val[:created]}")
+ else
+ $LOG.debug("WHOIS cache miss for #{key.inspect}")
+ whois = whois_via_raw(server, domain)
+ WHOIS_CACHE[key] = val = {
+ :whois => whois,
+ :created => Time.now
+ }
+ end
+ val[:whois]
+end
+
+
def whois(domain)
top_level_whois_server = domain.downcase.split(".").last + ".whois-servers.net"
response = whois_via(top_level_whois_server, domain)

0 comments on commit 7d7305e

Please sign in to comment.