lib/Simple/IPInfo.pm untested

filter: Statementstmt 0.0% 0 / 163 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 41 MC/DCmcdc 0.0% 0 / 24 Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 288 CC 15 SCAR 27.4
CC
15
Cov
0%
iterate_ipinfo
56.0
ip_to_inet
12.0
is_ip
12.0
CRAP 15.4
1 # ABSTRACT: Get IP/IPList Info (location, as number, etc)
2 package Simple::IPInfo;
3 require Exporter;
4 @ISA = qw(Exporter);
5 @EXPORT = qw(
6 read_ipinfo
7 iterate_ipinfo
8 get_ipinfo
9
10 get_ip_loc
11 get_ip_as
12
13 ip_to_inet
14 inet_to_ip
15 cidr_to_range
16 );
17 use utf8;
18 use Data::Validate::IP qw/is_ipv4 is_ipv6 is_public_ipv4/;
19 use SimpleR::Reshape;
20 use Data::Dumper;
21 use JSON;
22 use File::Spec;
23 use Net::CIDR qw/cidr2range/;
24 use Socket qw/inet_aton inet_ntoa/;
25 use Memoize;
26 memoize( 'read_ipinfo' );
27
28 our $DEBUG = 0;
29
30 our $VERSION = 0.12;
31
32 my ( $vol, $dir, $file ) = File::Spec->splitpath( __FILE__ );
33 our $IPINFO_LOC_F = File::Spec->catpath( $vol, $dir, "inet_loc.csv" );
34 our $IPINFO_AS_F = File::Spec->catpath( $vol, $dir, "inet_as.csv" );
35
36 my @key = qw/country area isp country_code area_code isp_code as/;
37 our %UNKNOWN = map { $_ => '' } @key;
38 our %ERROR = map { $_ => 'error' } @key;
39 our %LOCAL = map { $_ => 'local' } @key;
40
41 sub cidr_to_range {
42 my ( $cidr, %opt ) = @_;
43 $opt{inet} //= 1;
44
45 my ( $addr_range ) = cidr2range( $cidr );
46 my @addr = split /-/, $addr_range;
47 return @addr unless ( $opt{inet} );
48
49 my @inet = map { unpack( 'N', inet_aton( $_ ) ) } @addr;
50 return @inet;
51 }
52
53 sub get_ipinfo {
54 my ( $ip_list, %opt ) = @_;
55 $opt{i} ||= 0;
56 $opt{return_arrayref} //= 1;
57
58 my $i = 0;
59 my $ip_inet = read_table(
60 $ip_list,
61 conv_sub => sub {
62 my ( $r ) = @_;
63 my ( $ip, $inet, $rr ) = calc_ip_inet( $r->[ $opt{i} ], \%opt );
64 return [ $i++, $inet ];
65 },
66 return_arrayref => 1,
67 );
68 my @sorted_ip_inet = sort { $a->[1] <=> $b->[1] } @$ip_inet;
69
70 my $get_ip_info = iterate_ipinfo(
71 \@sorted_ip_inet,
72 i => 1,
73 ipinfo_file => $opt{ipinfo_file},
74 ipinfo_names => $opt{ipinfo_names},
75 return_arrayref => 1,
76 );
77
78 my @sorted_ip_info = sort { $a->[0] <=> $b->[0] } @$get_ip_info;
79
80 $i = 0;
81 my $start_col = $opt{reserve_inet} ? 1 : 2;
82 my $end_col = $#{ $sorted_ip_info[0] };
83 my $res = read_table(
84 $ip_list,
85 %opt,
86 conv_sub => sub {
87 my ( $r ) = @_;
88 return [ @$r, @{ $sorted_ip_info[ $i++ ] }[ $start_col .. $end_col ] ];
89 },
90 );
91
92 return $res || $opt{write_file};
93 } ## end sub get_ipinfo
94
95 sub iterate_ipinfo {
96
97 #deal with large ip_list file, ip_list is inet-sorted
98 my ( $ip_list, %opt ) = @_;
99 $opt{i} ||= 0;
100 $opt{return_arrayref} ||= 0;
101 $opt{ipinfo_file} ||= $IPINFO_LOC_F;
102 $opt{ipinfo_names} ||= [qw/country area isp country_code area_code isp_code/];
103
104 my $ip_info = read_ipinfo( $opt{ipinfo_file} );
105 my $n = $#$ip_info;
106
107 my ( $i, $ir ) = ( 0, $ip_info->[0] );
108 my ( $s, $e ) = @{$ir}{qw/s e/};
109
110 my $res = read_table(
111 $ip_list,
112 conv_sub => sub {
113 my ( $r ) = @_;
114 my ( $ip, $inet, $rr ) = calc_ip_inet( $r->[ $opt{i} ], \%opt );
115
116 return [ @$r, @{$rr}{ @{ $opt{ipinfo_names} } } ] if($rr);
117
118 #start from i=0
119 if($i>$n){
120 $i = 0;
121 ( $s, $e ) = @{$ip_info->[$i]}{qw/s e/};
122 }
123
124 #<- to nearby $i
125 while($inet<$s and $i>0){
126 $i = int($i/2);
127 ( $s, $e ) = @{$ip_info->[$i]}{qw/s e/};
128 }
129
130 #-> to nearby $i
131 while ( $inet > $e and $i < $n ) {
132 $i++;
133 $ir = $ip_info->[$i];
134 ( $s, $e ) = @{$ir}{qw/s e/};
135 }
136
137 my $res_r;
138 if ( $inet >= $s and $inet <= $e and $i <= $n ) {
139 $res_r = $ir;
140 print "$i : $ip, start $res_r->{s}, end $res_r->{e}, inet $inet\n" if ( $DEBUG );
141 }elsif ( $inet < $s or $i > $n ) {
142 $res_r = \%UNKNOWN;
143 }
144
145 return [ @$r, @{$res_r}{ @{ $opt{ipinfo_names} } } ];
146 },
147 %opt,
148 );
149
150 print "\n" if ( $DEBUG );
151 return $res || $opt{write_file};
152 } ## end sub iterate_ipinfo
153
154 sub inet_to_ip {
155 my ( $inet ) = @_;
156 return inet_ntoa( pack( 'N', $inet ) );
157 }
158
159 sub ip_to_inet {
160 my ( $ip ) = @_;
161 return ( -1, \%ERROR ) unless ( is_ip( $ip ) );
162 my $inet = unpack( "N", inet_aton( $ip ) );
163 return ( $inet, \%LOCAL ) unless ( is_public_ip( $ip ) );
164 return ( $inet, undef );
165 }
166
167 sub calc_ip_inet {
168 my ( $c, $opt ) = @_;
169
170 my $ip = $c =~ /^\d+$/ ? inet_to_ip( $c ) : $c;
171
172 my @res = ( $ip, ip_to_inet( $ip ) );
173
174 #$res[0]=~s/\.\d+$/.0/ if($opt->{use_ip_c});
175
176 return @res;
177 }
178
179 sub get_ip_as {
180 my ( $ip_list, %opt ) = @_;
181 $opt{ipinfo_file} = $IPINFO_AS_F;
182 $opt{ipinfo_names} = [qw/as/];
183 return get_ipinfo( $ip_list, %opt );
184 }
185
186 sub get_ip_loc {
187 my ( $ip_list, %opt ) = @_;
188 $opt{ipinfo_file} = $IPINFO_LOC_F;
189 return get_ipinfo( $ip_list, %opt );
190 }
191
192 sub read_ipinfo {
193 my ( $f, $charset ) = @_;
194 $f //= $IPINFO_LOC_F;
195 $charset //= 'utf8';
196
197 my @d;
198 open my $fh, "<:$charset", $f;
199 chomp( my $h = <$fh> );
200 my @head = split /,/, $h;
201 while ( my $c = <$fh> ) {
202 chomp( $c );
203 my @line = split /,/, $c;
204 my %k = map { $head[$_] => $line[$_] } ( 0 .. $#head );
205 push @d, \%k;
206 }
207 close $fh;
208 return \@d;
209 } ## end sub read_ipinfo
210
211 sub is_ip {
212 my ( $ip ) = @_;
213 return 1 if ( is_ipv4( $ip ) );
214 return 1 if ( is_ipv6( $ip ) );
215 return;
216 }
217
218 sub is_public_ip {
219 my ( $ip ) = @_;
220 return 1 if ( is_public_ipv4( $ip ) );
221 return;
222 }
223
224 1;