| 1 | # | ||
| 2 | # $Id$ | ||
| 3 | # | ||
| 4 | # network::whois Brik | ||
| 5 | # | ||
| 6 | package Metabrik::Network::Whois; | ||
| 7 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 1 | ▶ | use base qw(Metabrik); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | sub brik_properties { | ||
| 13 | return { | ||
| 14 | revision => '$Revision$', | ||
| 15 | tags => [ qw(unstable) ], | ||
| 16 | author => 'GomoR <GomoR[at]metabrik.org>', | ||
| 17 | license => 'http://opensource.org/licenses/BSD-3-Clause', | ||
| 18 | attributes => { | ||
| 19 | datadir => [ qw(datadir) ], | ||
| 20 | rtimeout => [ qw(timeout) ], | ||
| 21 | last_server => [ qw(server) ], | ||
| 22 | src_ip => [ qw(ip_list) ], | ||
| 23 | }, | ||
| 24 | attributes_default => { | ||
| 25 | rtimeout => 2, | ||
| 26 | }, | ||
| 27 | commands => { | ||
| 28 | target => [ qw(domain|ip_address) ], | ||
| 29 | queried_server => [ ], | ||
| 30 | }, | ||
| 31 | require_modules => { | ||
| 32 | 'Net::Whois::Raw' => [ ], | ||
| 33 | 'Metabrik::String::Parse' => [ ], | ||
| 34 | }, | ||
| 35 | }; | ||
| 36 | } | ||
| 37 | |||
| 38 | sub target { | ||
| 39 | my $self = shift; | ||
| 40 | my ($target) = @_; | ||
| 41 | |||
| 42 | $self->brik_help_run_undef_arg('target', $target) or return; | ||
| 43 | |||
| 44 | $Net::Whois::Raw::TIMEOUT = $self->rtimeout; | ||
| 45 | $Net::Whois::Raw::CACHE_DIR = $self->datadir.'/cache'; | ||
| 46 | |||
| 47 | # Whois server custo | ||
| 48 | #$Net::Whois::Raw::Data::servers{TLD} = SRV; | ||
| 49 | |||
| 50 | my $info; | ||
| 51 | my $server; | ||
| 52 | eval { | ||
| 53 | ($info, $server) = Net::Whois::Raw::whois($target) | ||
| 54 | or return $self->log->error("target: whois for target [$target] failed"); | ||
| 55 | }; | ||
| 56 | if ($@) { | ||
| 57 | chomp($@); | ||
| 58 | if ($@ =~ /(Connection timeout to \S+)/) { | ||
| 59 | $@ = $1; | ||
| 60 | } | ||
| 61 | elsif ($@ =~ /(\S+): Invalid argument: /) { | ||
| 62 | $@ = "Invalid server $1"; | ||
| 63 | } | ||
| 64 | elsif ($@ =~ /(\S+): Connection refused: /) { | ||
| 65 | $@ = "Connection refused to $1"; | ||
| 66 | } | ||
| 67 | |||
| 68 | return $self->log->error("target: failed target [$target]: [$@]"); | ||
| 69 | } | ||
| 70 | |||
| 71 | if (! defined($info)) { | ||
| 72 | return $self->log->error("target: whois returned nothing"); | ||
| 73 | } | ||
| 74 | |||
| 75 | my $sp = Metabrik::String::Parse->new_from_brik_init($self) or return; | ||
| 76 | my $lines = $sp->to_array($info) or return; | ||
| 77 | |||
| 78 | for (@$lines) { | ||
| 79 | if (/Whois Requests exceeded the allowed limit/i | ||
| 80 | || /Your request cannot be completed at this time due to query limit controls/i | ||
| 81 | || /Maximum Daily connection limit reached. Lookup refused/i | ||
| 82 | || /database is contained within a list of IP addresses that may have failed/i | ||
| 83 | || /Connection refused: exceeded maximum connection limit from /i | ||
| 84 | ) { | ||
| 85 | return $self->log->error("target: failed target [limit exceeded]"); | ||
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 89 | $self->last_server($server); | ||
| 90 | |||
| 91 | return $lines; | ||
| 92 | } | ||
| 93 | |||
| 94 | sub queried_server { | ||
| 95 | my $self = shift; | ||
| 96 | |||
| 97 | return $self->last_server || 'undef'; | ||
| 98 | } | ||
| 99 | |||
| 100 | 1; | ||
| 101 | |||
| 102 | __END__ | ||