GetLessMail gets more info
Sterling Camden
In one of my curious moods, I began to wonder how difficult it would be to figure out the location of an email sender based on the IP address shown in the “Received” header fields. It turns out to be more difficult than you may have thought, because:
- An email often contains multiple “Received” headers, one for each relay point. The innermost (last) is the original sender.
- However, the original transmission is often within a local network, so the first one or few IPs may be in the reserved local range.
- No free, global, authoritative database exists that contains the location of all IPs. At least, not that I’ve found. However, there are some free databases you can download that are updated from time to time.
- The owner of the IP address may not be located at the same place as the connection. In fact, it usually isn’t, but it may be close.
Despite these impediments, I have implemented IP Geolocation for Ruby, and created a method specialized for GetLessMail that uses it.
The two scripts IPGeo.rb and IPGeoMail.rb should be placed somewhere in your Ruby require path. The example database, which I downloaded from http://linuxbox.co.uk/ip-address-whois-database.php, should be placed in /usr/local/share/IPGeo (or you can modify the script to access it wherever you choose). The included dot.getlessmail shows how you could use it to add an “X-IP-Location” header that provides the IP Location data, if found.
As I intimated, you could also use IPGeo.rb outside of the context of email. It would be trivial to write a script that accepts an IP Address and prints out the information. Like so:
require 'IPGeo'
$<.each do |line|
puts IPGeo.locate IPGeo.get_ip(line)
end
Of course, this information is only as good as your database. The one I've included hasn't been updated since August 2009. You can probably find better databases out there, if you're willing to spend some money on them. I'm not.
You can get the updated tarball using the button below, or scrape it out of the BitBucket.
Posted in Ruby, Unix |
4 Comments » RSS 2.0 | Sphere it!




