File Coverage

blib/lib/Sendmail/AbuseIPDB.pm
Criterion Covered Total %
statement 45 80 56.2
branch 9 28 32.1
condition n/a
subroutine 11 12 91.6
pod 4 4 100.0
total 69 124 55.6


line stmt bran cond sub pod time code
1             package Sendmail::AbuseIPDB;
2              
3 1     1   51273 use 5.010001;
  1         3  
4 1     1   4 use strict;
  1         2  
  1         17  
5 1     1   4 use warnings;
  1         1  
  1         20  
6 1     1   4 use Carp;
  1         1  
  1         53  
7              
8 1     1   501 use IO::Socket::SSL qw(debug4);
  1         72101  
  1         7  
9              
10 1     1   482 use URI;
  1         7906  
  1         42  
11 1     1   569 use JSON; # imports encode_json, decode_json, to_json and from_json.
  1         11442  
  1         10  
12              
13             require Exporter;
14              
15             our @ISA = qw(Exporter);
16              
17             our @EXPORT_OK = ();
18             our @EXPORT = ();
19             our $VERSION = '0.07';
20              
21             my @categories = (
22             '', '', '', 'Fraud Orders', 'DDoS Attack', # 0 ... 4
23             '', '', '', '', 'Open Proxy', # 5 ... 9
24             'Web Spam', 'Email Spam', '', '', 'Port Scan', # 10 ... 14
25             'Hacking', '', '', 'Brute-Force', 'Bad Web Bot', # 15 ... 19
26             'Exploited Host', 'Web App Attack', 'SSH', 'IoT Targeted', '', # 20 ... 25
27             );
28              
29             my %categories; # Reverse direction lookup
30             for( my $i = 0; $i < scalar( @categories ); ++$i ) { $categories{$categories[$i]} = $i; }
31             delete( $categories{''});
32              
33             my %defaults = (
34             'BaseURL' => 'https://www.abuseipdb.com/',
35             'Days' => 30,
36             'Debug' => 0,
37             'Key' => '',
38             );
39              
40              
41             sub new( $@ )
42             {
43 4     4 1 10983 my $this = bless { %defaults }, shift;
44 4         23 my %args = @_;
45 4 100       14 unless( defined( $args{Key})) { croak( 'Key argument is mandatory, get your API key by creating an account' ); }
  1         147  
46 3         12 foreach my $k ( keys( %$this ))
47             {
48 12 100       21 if( defined( $args{ $k } )) { $this->{ $k } = $args{ $k }; }
  4         7  
49 12         17 delete $args{ $k };
50             }
51 3         7 foreach my $k ( keys( %args ))
52             {
53 1         115 croak( "Unknown argument $k" );
54             }
55              
56 1 50   1   786 if( $this->{Debug} ) { use Data::Dumper; print STDERR Dumper( $this ); }
  1         4247  
  1         421  
  2         5  
  0         0  
57 2         13 return( $this );
58             }
59              
60              
61             sub get( $$ )
62             {
63 2     2 1 1114 my $this = shift;
64 2         5 my $ip = shift;
65              
66 2         18 my $url = URI->new( "$this->{BaseURL}check/$ip/json" );
67 2         5611 $url->query_form( key => $this->{Key}, days => $this->{Days});
68              
69 2 50       272 if( $this->{BaseURL} eq 'test://' )
70             {
71 2 100       12 if( $ip eq '192.168.0.1' ) { return( %categories ); }
  1 50       23  
72 1         12 elsif( $ip eq '192.168.0.3' ) { return( $url->as_string ); }
73             }
74              
75 0         0 my $fh;
76 0         0 open( $fh, '-|', "/usr/bin/curl -s '$url'" );
77 0 0       0 unless( $fh ) { croak( "Cannout pipe from curl" ); }
  0         0  
78 0         0 my $json = '';
79 0         0 while( <$fh> )
80             {
81 0         0 $json .= $_;
82             }
83 0 0       0 if ($this->{Debug})
84             {
85 0         0 print STDERR "JSON: $json\n";
86             }
87              
88 0         0 my $result = from_json( $json );
89 0 0       0 if( $this->{Debug})
90             {
91 0         0 require Data::Dumper;
92 0         0 print STDERR "RESULT:" . Dumper( $result );
93             }
94              
95 0 0       0 if( ref($result) eq 'HASH' )
96             {
97 0         0 return( $result );
98             }
99              
100 0 0       0 if( ref($result) eq 'ARRAY' )
101             {
102 0         0 return( @$result );
103             }
104              
105 0         0 return();
106             }
107              
108              
109             sub catg( $$ )
110             {
111 1     1 1 556 my $this = shift;
112 1         8 return( $categories[ shift ]);
113             }
114              
115              
116             sub filter( $$@ )
117             {
118 0     0 1   my $this = shift;
119 0           my @result;
120 0           my $category = shift;
121              
122 0 0         unless( $category =~ m{^[0-9]+$})
123             {
124 0           my $c = $categories{ $category };
125 0 0         unless( defined( $c ))
126             {
127 0           croak( "Unknown category $category" );
128             }
129 0           $category = $c;
130             }
131 0           while( @_ )
132             {
133 0           my $item = shift;
134 0           foreach my $c ( @{$item->{category}} )
  0            
135             {
136 0 0         if( $c == $category )
137             {
138 0           push( @result, $item );
139 0           last;
140             }
141             }
142             }
143 0           return( @result );
144             }
145              
146             1;
147             __END__