| 1 | #=============================================================================== | ||
| 2 | # PODNAME: Net::IP::Identifier::WhoisParser | ||
| 3 | # ABSTRACT: parse WHOIS result, extracting particular information | ||
| 4 | # | ||
| 5 | # AUTHOR: Reid Augustin (REID) | ||
| 6 | # EMAIL: reid@hellosix.com | ||
| 7 | # CREATED: Sat May 2 15:51:45 PDT 2015 | ||
| 8 | #=============================================================================== | ||
| 9 | |||
| 10 | use 5.002; | ||
| 11 | use strict; | ||
| 12 | use warnings; | ||
| 13 | |||
| 14 | package Net::IP::Identifier::WhoisParser; | ||
| 15 | use Math::BigInt; | ||
| 16 | use Net::IP; # for IP_IDENTICAL, OVERLAP, etc | ||
| 17 | use Net::IP::Identifier::Net; | ||
| 18 | use Net::IP::Identifier::Regex; | ||
| 19 | use Moo; | ||
| 20 | use namespace::clean; | ||
| 21 | |||
| 22 | our $VERSION = '0.111'; # VERSION | ||
| 23 | |||
| 24 | has verbose => ( # verbose mode | ||
| 25 | is => 'rw', | ||
| 26 | ); | ||
| 27 | has text => ( | ||
| 28 | is => 'rw', | ||
| 29 | ); | ||
| 30 | has relevant_lines => ( | ||
| 31 | is => 'lazy', | ||
| 32 | default => \&get_relevant_lines, | ||
| 33 | ); | ||
| 34 | has entity => ( # if entity is found in the WHOIS result from entity strings | ||
| 35 | is => 'lazy', | ||
| 36 | default => \&get_entity, | ||
| 37 | ); | ||
| 38 | has range => ( # a Net::IP::Identifier::Net object | ||
| 39 | is => 'lazy', | ||
| 40 | default => \&get_range, | ||
| 41 | ); | ||
| 42 | has _entity => ( # if entity is found before ->entity call, stash it here | ||
| 43 | is => 'rw', | ||
| 44 | ); | ||
| 45 | has _range => ( # if range is found before ->range call, stash it here | ||
| 46 | is => 'rw', | ||
| 47 | ); | ||
| 48 | |||
| 49 | # some class variables | ||
| 50 | |||
| 51 | #my $Re = Net::IP::Identifier::Regex->new; | ||
| 52 | #my $re_any = $Re->IP_any; | ||
| 53 | #my $re_netblock = $Re->netblock; | ||
| 54 | |||
| 55 | my @whois_stop_regexs = ( # lines beyond which we should not go | ||
| 56 | qr[^parent:]i, | ||
| 57 | qr[^route([s6])?:]i, | ||
| 58 | qr[^mnt-routes:]i, | ||
| 59 | ); | ||
| 60 | |||
| 61 | my @whois_range_regexs = ( # lines which might contain the range | ||
| 62 | qr[^inet6?num:\s*(.+)]i, | ||
| 63 | qr[^NetRange:\s*(.+)]i, | ||
| 64 | qr[^CIDR:\s*(\S+)]i, | ||
| 65 | qr[^Network:IP-Network(?:-Block)?:\s*(.+)]i, | ||
| 66 | qr/^a\.\s*\[Network Number\]\s*(.*)/i, | ||
| 67 | ); | ||
| 68 | |||
| 69 | my @whois_entity_regexs = ( # lines that might contain the entity | ||
| 70 | qr[^Organization:\s*(.*)]i, | ||
| 71 | qr[^org-name:\s*(.*)]i, | ||
| 72 | qr[^descr:\s*(.*)]i, | ||
| 73 | qr[^owner:\s*(.*)]i, | ||
| 74 | qr[^Network:Org-Name:(.*)]i, | ||
| 75 | qr/^g\.\s*\[Organization\]\s*(.*)/i, | ||
| 76 | ); | ||
| 77 | |||
| 78 | sub get_relevant_lines { | ||
| 79 | my ($self) = @_; | ||
| 80 | |||
| 81 | my @lines; | ||
| 82 | my $non_server_lines = 0; | ||
| 83 | |||
| 84 | for my $line (split "\n", $self->text) { | ||
| 85 | $line =~ s/\s*$//; | ||
| 86 | next if (not $line =~ m/\S/); # skip blank lines | ||
| 87 | if ($line =~ m/^[#%]/) { # comments | ||
| 88 | if ($line =~ m/ Information related to / and | ||
| 89 | $non_server_lines > 0) { # if it's not the first thing | ||
| 90 | last; # skip everything else | ||
| 91 | } | ||
| 92 | next; # skip comments | ||
| 93 | } | ||
| 94 | if ($line =~ m/^No match for "/ or | ||
| 95 | $line =~ m/^descr:.* address block not managed by/) { | ||
| 96 | return []; # no relevant lines | ||
| 97 | } | ||
| 98 | for my $re (@whois_stop_regexs) { | ||
| 99 | last if ($line =~ m/$re/); | ||
| 100 | } | ||
| 101 | push @lines, $line; | ||
| 102 | $non_server_lines++ if (not $line =~ m/^\[/); | ||
| 103 | # check for ARIN style listing: | ||
| 104 | if (my ($entity, $range) = $line =~ m/(.*?) \S+ \(NET6?[\da-fA-F-]+\) (.*)/) { | ||
| 105 | $range = Net::IP::Identifier::Net->new($range); | ||
| 106 | if (defined $self->_range and | ||
| 107 | $self->_range->overlaps($range) == $IP_A_IN_B_OVERLAP) { | ||
| 108 | # previous _range is inside new range. we want | ||
| 109 | # the smallest, so swap | ||
| 110 | $range = $self->_range; | ||
| 111 | $entity = $self->_entity; | ||
| 112 | } | ||
| 113 | else { | ||
| 114 | } | ||
| 115 | $self->_range($range); | ||
| 116 | $self->_entity($entity); | ||
| 117 | } | ||
| 118 | } | ||
| 119 | return \@lines; | ||
| 120 | } | ||
| 121 | |||
| 122 | sub get_range { | ||
| 123 | my ($self) = @_; | ||
| 124 | |||
| 125 | my @ranges; | ||
| 126 | for my $line (@{$self->relevant_lines}) { | ||
| 127 | return $self->_range if ($self->_range); | ||
| 128 | for my $re (@whois_range_regexs) { | ||
| 129 | if (my ($match) = $line =~ m/$re/) { | ||
| 130 | push @ranges, Net::IP::Identifier::Net->new($match); | ||
| 131 | } | ||
| 132 | } | ||
| 133 | } | ||
| 134 | return $self->smallest_net(\@ranges); | ||
| 135 | } | ||
| 136 | |||
| 137 | # find and return smallest net in a group of nets | ||
| 138 | sub smallest_net { | ||
| 139 | my ($self, $nets) = @_; | ||
| 140 | |||
| 141 | for my $aa (0 .. $#{$nets}) { | ||
| 142 | for my $bb ($aa+1 .. $#{$nets}) { | ||
| 143 | next if (not defined $nets->[$aa] or not defined $nets->[$bb]); | ||
| 144 | my $overlap = $nets->[$aa]->overlaps($nets->[$bb]); | ||
| 145 | if ($overlap eq $IP_IDENTICAL) { # duplicate netblocks, choose on notation | ||
| 146 | if ($nets->[$aa]->src_str =~ m/\+/) { # avoid '+' notation | ||
| 147 | $nets->[$aa] = undef; | ||
| 148 | } | ||
| 149 | elsif ($nets->[$aa]->src_str =~ m/-/) { # prefer '-' notation | ||
| 150 | $nets->[$bb] = undef; | ||
| 151 | } | ||
| 152 | else { | ||
| 153 | $nets->[$aa] = undef; | ||
| 154 | } | ||
| 155 | } | ||
| 156 | elsif ($overlap eq $IP_A_IN_B_OVERLAP) { # aa is inside bb | ||
| 157 | $nets->[$bb] = undef | ||
| 158 | } | ||
| 159 | elsif ($overlap eq $IP_B_IN_A_OVERLAP) { # bb is inside aa | ||
| 160 | $nets->[$aa] = undef | ||
| 161 | } | ||
| 162 | } | ||
| 163 | } | ||
| 164 | for my $net (@{$nets}) { | ||
| 165 | return $net if (defined $net); # should only be one, if not - shrug... | ||
| 166 | } | ||
| 167 | return; # no nets to start with? | ||
| 168 | } | ||
| 169 | |||
| 170 | sub get_entity { | ||
| 171 | my ($self) = @_; | ||
| 172 | |||
| 173 | for my $line (@{$self->relevant_lines}) { | ||
| 174 | return $self->_entity if ($self->_entity); | ||
| 175 | for my $re (@whois_entity_regexs) { | ||
| 176 | if (my ($match) = $line =~ m/$re/) { | ||
| 177 | return $match; | ||
| 178 | } | ||
| 179 | } | ||
| 180 | } | ||
| 181 | return; # undef | ||
| 182 | } | ||
| 183 | |||
| 184 | 1; | ||
| 185 | |||
| 186 | __END__ |