From ae20b5abfabe99fc90d7380472b2d1d45633a6c1 Mon Sep 17 00:00:00 2001 From: seanheaton Date: Mon, 14 Oct 2013 12:03:14 -0400 Subject: [PATCH] A Goodie to extract IPv4 information encoded in Teredo tunnel IPv6 addresses --- dist.ini | 2 ++ lib/DDG/Goodie/Teredo.pm | 71 ++++++++++++++++++++++++++++++++++++++++ t/Teredo.t | 29 ++++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 lib/DDG/Goodie/Teredo.pm create mode 100644 t/Teredo.t diff --git a/dist.ini b/dist.ini index eebc89658..068811290 100644 --- a/dist.ini +++ b/dist.ini @@ -48,6 +48,8 @@ Games::Sudoku::Component = 0.02 Data::RandomPerson = 0.4 URI::Escape = 3.31 Lingua::EN::Words2Nums = 0 +Net::IP = 0 +Math::BaseConvert = 0 [Prereqs / TestRequires] Test::More = 0.98 diff --git a/lib/DDG/Goodie/Teredo.pm b/lib/DDG/Goodie/Teredo.pm new file mode 100644 index 000000000..5dd3cddcf --- /dev/null +++ b/lib/DDG/Goodie/Teredo.pm @@ -0,0 +1,71 @@ +package DDG::Goodie::Teredo; +# ABSTRACT: Provides the Teredo server IPv4 address, +# NAT IPv4 address, and port number encoded in a given +# Teredo tunnel IPv6 address. + +use DDG::Goodie; +use Net::IP; +use Math::BaseConvert; + +=keyword primary_example_queries + +"teredo 2001:0000:4136:e378:8000:63bf:3fff:fdd2"; +"teredo 2001::CE49:7601:E866:EFFF:62C3:FFFE"; + +=cut + +=keyword description + +"Teredo address analyzer"; + +=cut + +=keyword name + +"Teredo extractor"; + +=cut + +=keyword code_url + +"https://github.com/duckduckgo/lib/DDG/Goodie/Teredo.pm"; + +=cut + +=keyword topics + +"sysadmin"; + +=cut + +=keyword category + +"transformations"; + +=cut + +=keyword attribution + +github => ['https://github.com/seanheaton','seanheaton'], +twitter => ['http://twitter.com/seanograph','@seanograph'], +email => ['mailto:seanoftime@gmail.com','seanoftime@gmail.com']; + +=cut + +triggers start => 'teredo'; + +handle remainder => sub { + + my $ip = new Net::IP ($_,6) if $_; + + if ((defined $ip) && ($ip->version() == 6) && (substr($ip->ip(),0,9) eq "2001:0000")) { + my $binip = $ip->binip(); + my $server = new Net::IP (Net::IP::ip_bintoip((substr $binip, 32, 32),4)); + my $port = 65535 - cnv((substr $binip, 80, 16),2,10); + my $client = new Net::IP (Net::IP::ip_bintoip(~(substr $binip, 96, 32),4)); + return "Teredo Server IPv4: " . $server->ip() . "\nNAT Public IPv4: " . $client->ip() . "\nClient Port: " . $port; + } + return; +}; +zci is_cached => 1; +1; diff --git a/t/Teredo.t b/t/Teredo.t new file mode 100644 index 000000000..46885cf76 --- /dev/null +++ b/t/Teredo.t @@ -0,0 +1,29 @@ +#!/usr/bin/env perl + +use strict; +use warnings; + +use Test::More; +use DDG::Test::Goodie; + +zci answer_type => 'teredo'; +zci is_cached => 1; + +ddg_goodie_test( + [ + 'DDG::Goodie::Teredo' + ], + + # Sample queries + 'teredo 2001:0000:4136:e378:8000:63bf:3fff:fdd2' => + test_zci("Teredo Server IPv4: 65.54.227.120\nNAT Public IPv4: 192.0.2.45\nClient Port: 40000"), + + 'teredo 2001:0000:4136:E378:8000:EC77:7C94:FFFE' => + test_zci("Teredo Server IPv4: 65.54.227.120\nNAT Public IPv4: 131.107.0.1\nClient Port: 5000"), + + 'teredo 2001::CE49:7601:E866:EFFF:62C3:FFFE' => + test_zci("Teredo Server IPv4: 206.73.118.1\nNAT Public IPv4: 157.60.0.1\nClient Port: 4096"), +); + + +done_testing;