blib/lib/Onyphe/Api.pm

filter: Statementstmt 7.1% 38 / 533 Branchbran 0.0% 0 / 264 Conditioncond 0.0% 0 / 176 MC/DCmcdc 0.0% 0 / 105 Subroutinesub 18.9% 11 / 58 Podpod 0.0% 0 / 33 totaltotal 4.1% 49 / 1169 CC 314 SCAR 65.6
CC
314
Cov
4%
request
2256.0
post_request
2256.0
stream
552.0
CRAP 707.1
↑ Summary blib/lib/OPP/Proc/Filter.pm »
1 #
2 # $Id: Api.pm,v d8b98095b893 2023/08/30 05:24:07 gomor $
3 #
4 package Onyphe::Api;
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 our $VERSION = '4.10';
9  
10 2 ▶use experimental qw(signatures);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶use base qw(Onyphe);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 our @AS = qw(endpoint apikey username password);
15 2 __PACKAGE__->cgBuildAccessorsScalar(\@AS);
16 2 __PACKAGE__->cgBuildIndices;
17  
18 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use File::Temp qw(tempfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use File::Slurp qw(read_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use Mojo::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use JSON::XS qw(encode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use Mojo::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use Mojo::Util qw(b64_encode url_escape);
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 #
27 # Common functions:
28 #
29 ▶sub _ua ($self) {
Subroutine _ua: not called CC=1 SCAR=6.9
30 0 my $ua = Mojo::UserAgent->new(
31 connect_timeout => 5,
32 max_response_size => 0,
33 inactivity_timeout => 0,
34 );
35  
36 0 $ua->transactor->name('Onyphe::Api-v'.$VERSION);
37 0 $ua->proxy->detect;
38  
39 0 return $ua;
40 }
41  
42 0 ▶sub _headers ($self, $apikey, $ct = undef) {
Subroutine _headers: not called CC=10 SCAR=47.0
43 0 my $headers = {
44 'Authorization' => 'Bearer '.$apikey,
45 'X-Api-Key' => $apikey, # Ready for APIv3
46 'Content-Type' => 'application/json',
47 'Accept' => '*/*',
48 };
49 0 ▶ if (defined($ct)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ct) 0 0 0
50 0 $headers->{'Content-Type'} = $ct;
51 }
52 0 my $global = $self->config->{''};
53 0 ▶ my $username = $global->{api_unrated_email} || $self->username;
Condition0%0/3
$global->{'api_unrated_email'} || $self->username
l!l&&r!l&&!r
000
MC/DC0%0/2
$global->{'api_unrated_email'} || $self->username
$global->{'api_unrated_email'} $self->username
Truth table0%0/3
A || B
A= $global->{'api_unrated_email'}B= $self->username
ABresult
1X1
011
000
54 0 ▶ if ($global->{api_unrated_endpoint} && $username) {
Branch: 1 branch
BranchTrueFalseTotal
if ($global->{"api_unrated_endpoint"} and $username) 0 0 0
Condition0%0/3
$global->{"api_unrated_endpoint"} and $username
!ll&&!rl&&r
000
MC/DC0%0/2
$global->{"api_unrated_endpoint"} and $username
$global->{"api_unrated_endpoint"} $username
Truth table0%0/3
A and B
A= $global->{"api_unrated_endpoint"}B= $username
ABresult
0X0
100
111
55 print STDERR "VERBOSE: Using Unrated API endpoint: ".$global->{api_unrated_endpoint}.
56 0 ▶ ", with username: $username\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 0 0
57 0 ▶ if (!defined($username) || !defined($apikey)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $username and defined $apikey) 0 0 0
Condition0%0/3
defined $username and defined $apikey
!ll&&!rl&&r
000
MC/DC0%0/2
defined $username and defined $apikey
defined $username defined $apikey
Truth table0%0/3
A and B
A= defined $usernameB= defined $apikey
ABresult
0X0
100
111
58 0 ▶ print STDERR "ERROR: need api_unrated_email & api_key settings\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->silent 0 0 0
59 unless $self->silent;
60 0 return;
61 }
62 0 $username =~ s{\@}{_};
63 0 my $auth = b64_encode($username.':'.$apikey, '');
64 0 $headers->{Authorization} = 'Basic '.$auth;
65 }
66 0 return $headers;
67 }
68  
69 ▶sub get_maxpage ($self, $json) {
Subroutine get_maxpage: not called CC=3 SCAR=24.8
Pod undocumented
70 0 my $maxpage = $json->{max_page};
71 0 ▶ return defined($maxpage) && $maxpage ? $maxpage : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $maxpage && $maxpage ? : 0 0 0
Condition0%0/3
defined $maxpage and $maxpage
!ll&&!rl&&r
000
MC/DC0%0/2
defined $maxpage and $maxpage
defined $maxpage $maxpage
Truth table0%0/3
A and B
A= defined $maxpageB= $maxpage
ABresult
0X0
100
111
72 }
73  
74 ▶sub get_results ($self, $json) {
Subroutine get_results: not called CC=3 SCAR=24.8
Pod undocumented
75 0 my $results = $json->{results};
76 0 ▶ return defined($results) && @$results ? $results : [];
Branch: 1 branch
BranchTrueFalseTotal
defined $results && @$results ? : 0 0 0
Condition0%0/3
defined $results and @$results
!ll&&!rl&&r
000
MC/DC0%0/2
defined $results and @$results
defined $results @$results
Truth table0%0/3
A and B
A= defined $resultsB= @$results
ABresult
0X0
100
111
77 }
78  
79 ▶sub encode ($self, $result) {
Subroutine encode: not called CC=4 SCAR=30.0
Pod undocumented
80 0 ▶ return '' unless $result;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 0 0 0
81 0 my $encode;
82 0 eval {
83 0 $encode = encode_json($result);
84 };
85 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
86 0 chomp($@);
87 0 ▶ print STDERR "ERROR: encode failed: $@\n" unless $self->silent;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->silent 0 0 0
88 0 return '';
89 }
90 0 return $encode;
91 }
92  
93 #
94 # Request-based APIs
95 #
96 0 ▶sub _cb_request ($self, $results = undef, $cb_args = undef) {
Subroutine _cb_request: not called CC=3 SCAR=24.8
97 ▶ return sub ($results, $cb_args) {
Subroutine __ANON__: not called CC=5 SCAR=34.0
98 0 ▶ $results = ref($results) eq 'ARRAY' ? $results : [ $results ];
Branch: 1 branch
BranchTrueFalseTotal
ref $results eq 'ARRAY' ? : 0 0 0
99 0 for (@$results) {
100 0 ▶ next if defined($_->{'@category'}) && $_->{'@category'} eq 'none';
Branch: 1 branch
BranchTrueFalseTotal
if defined $_->{"\@category"} and $_->{"\@category"} eq "none" 0 0 0
Condition0%0/3
defined $_->{"\@category"} and $_->{"\@category"} eq "none"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_->{"\@category"} and $_->{"\@category"} eq "none"
defined $_->{"\@category"} $_->{"\@category"} eq "none"
Truth table0%0/3
A and B
A= defined $_->{"\@category"}B= $_->{"\@category"} eq "none"
ABresult
0X0
100
111
101 0 print $self->encode($_)."\n";
102 }
103 0 };
104 }
105  
106 ▶sub _params ($self, $params) {
Subroutine _params: not called CC=3 SCAR=24.8
107 0 my $first = 1;
108 0 my $args = '';
109 0 for my $p (@$params) {
110 0 my $op = '?';
111 0 ▶ $op = '&' unless $first;
Branch: 1 branch
BranchTrueFalseTotal
unless $first 0 0 0
112 0 $args .= $op.$p->{k}.'='.$p->{v};
113 0 $first = 0;
114 }
115 0 return $args;
116 }
117  
118 # $self->request('/search', 'protocol:ssh', 1, 10);
119 # $self->request('/simple/datascan', '8.8.8.8');
120 0 ▶sub request ($self, $api, $input = undef, $page = undef, $maxpage = undef, $params = undef, $cb = undef, $cb_args = undef) {
Subroutine request: not called CC=47 SCAR=77.2
Pod undocumented
121 0 my $global = $self->config->{''};
122 my $endpoint = $global->{api_unrated_endpoint} || $global->{api_endpoint}
123 0 ▶ || $self->endpoint;
Condition0%0/6
$global->{'api_unrated_endpoint'} || $global->{'api_endpoint'} || $self->endpoint
l!l&&r!l&&!r
000
$global->{"api_unrated_endpoint"} or $global->{"api_endpoint"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$global->{'api_unrated_endpoint'} || $global->{'api_endpoint'} || $self->endpoint
$global->{"api_unrated_endpoint"} $global->{"api_endpoint"} $self->endpoint
Truth table0%0/4
A or B || C
A= $global->{"api_unrated_endpoint"}B= $global->{"api_endpoint"}C= $self->endpoint
ABCresult
1XX1
01X1
0011
0000
124 0 ▶ my $apikey = $global->{api_key} || $self->apikey;
Condition0%0/3
$global->{'api_key'} || $self->apikey
l!l&&r!l&&!r
000
MC/DC0%0/2
$global->{'api_key'} || $self->apikey
$global->{'api_key'} $self->apikey
Truth table0%0/3
A || B
A= $global->{'api_key'}B= $self->apikey
ABresult
1X1
011
000
125  
126 # Use default callback when none given:
127 0 ▶ $cb ||= $self->_cb_request;
Condition0%0/3
$cb ||= $self->_cb_request
l!l&&r!l&&!r
000
MC/DC0%0/2
$cb ||= $self->_cb_request
$cb $self->_cb_request
Truth table0%0/3
A ||= B
A= $cbB= $self->_cb_request
ABresult
1X1
011
000
128  
129 0 ▶ $page ||= 1;
Condition0%0/2
$page ||= 1
l!l
00
MC/DC0%0/1
$page ||= 1
$page
Truth table0%0/2
A
A= $page
Aresult
11
00
130 0 ▶ $maxpage ||= $global->{api_maxpage} || 1;
Condition0%0/5
$maxpage ||= $global->{"api_maxpage"} || 1
l!l&&r!l&&!r
000
$global->{'api_maxpage'} || 1
l!l
00
MC/DC0%0/2
$maxpage ||= $global->{"api_maxpage"} || 1
$maxpage $global->{'api_maxpage'}
Truth table0%0/3
A ||= B
A= $maxpageB= $global->{'api_maxpage'}
ABresult
1X1
011
000
131  
132 0 my $ua = $self->_ua();
133 0 my $headers = $self->_headers($apikey);
134  
135 0 $api =~ s{^/*}{/}g;
136 0 my $this_max_page;
137 0 while (1) {
138 0 my $path = $endpoint.$api;
139 0 ▶ $path .= '/'.url_escape($input) if defined $input;
Branch: 1 branch
BranchTrueFalseTotal
if defined $input 0 0 0
140 0 my $p = [];
141 0 push @$p, { k => 'k', v => $apikey };
142 0 ▶ push @$p, { k => 'page', v => $page } if defined $page;
Branch: 1 branch
BranchTrueFalseTotal
if defined $page 0 0 0
143 0 ▶ push @$p, { k => 'trackquery', v => 'true' } if $global->{api_trackquery};
Branch: 1 branch
BranchTrueFalseTotal
if $global->{"api_trackquery"} 0 0 0
144 0 ▶ push @$p, { k => 'calculated', v => 'true' } if $global->{api_calculated};
Branch: 1 branch
BranchTrueFalseTotal
if $global->{"api_calculated"} 0 0 0
145 0 ▶ push @$p, { k => 'keepalive', v => 'true' } if $global->{api_keepalive};
Branch: 1 branch
BranchTrueFalseTotal
if $global->{"api_keepalive"} 0 0 0
146 0 ▶ if (defined($params) && ref($params) eq 'HASH' && keys %$params) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params and ref $params eq "HASH" and keys %$params) 0 0 0
Condition0%0/6
defined $params and ref $params eq "HASH" and keys %$params
!ll&&!rl&&r
000
defined $params and ref $params eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/3
defined $params and ref $params eq "HASH" and keys %$params
defined $params ref $params eq "HASH" keys %$params
Truth table0%0/4
A and B and C
A= defined $paramsB= ref $params eq "HASH"C= keys %$params
ABCresult
0XX0
10X0
1100
1111
147 0 for my $this (qw(size trackquery calculated keepalive)) {
148 0 ▶ next unless defined($params->{$this}) || defined($params->{'api_'.$this});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params->{$this} or defined $$params{'api_' . $this} 0 0 0
Condition0%0/3
defined $params->{$this} or defined $$params{'api_' . $this}
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $params->{$this} or defined $$params{'api_' . $this}
defined $params->{$this} defined $$params{'api_' . $this}
Truth table0%0/3
A or B
A= defined $params->{$this}B= defined $$params{'api_' . $this}
ABresult
1X1
011
000
149 0 ▶ push @$p, { k => $this, v => $params->{$this} || $global->{'api_'.$this} };
Condition0%0/3
$params->{$this} || $$global{'api_' . $this}
l!l&&r!l&&!r
000
MC/DC0%0/2
$params->{$this} || $$global{'api_' . $this}
$params->{$this} $$global{'api_' . $this}
Truth table0%0/3
A || B
A= $params->{$this}B= $$global{'api_' . $this}
ABresult
1X1
011
000
150 }
151 }
152 0 my $args = $self->_params($p);
153 0 ▶ $path .= $args if $args;
Branch: 1 branch
BranchTrueFalseTotal
if $args 0 0 0
154 0 my $url = Mojo::URL->new($path);
155  
156 0 ▶ print STDERR "VERBOSE: Calling API: $path\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 0 0
157  
158 0 RETRY:
159 my $res;
160 0 eval {
161 0 $res = $ua->get($url => $headers)->result;
162 };
163 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
164 0 chomp($@);
165 0 ▶ print STDERR "WARNING: Request API call failed: [$@], retrying...\n" unless $self->silent;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->silent 0 0 0
166 0 goto RETRY;
167 }
168 0 ▶ unless ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->is_success) 0 0 0
169 0 my $code = $res->code;
170 0 my $text = $res->json->{text};
171 # If code 429, retry with some sleep:
172 0 ▶ if ($code == 429) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code == 429) 0 0 0
173 0 ▶ print STDERR "WARNING: Too fast, sleeping before retry...\n" unless $self->silent;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->silent 0 0 0
174 0 sleep 1;
175 0 goto RETRY;
176 }
177 # Otherwise, stops and display error:
178 0 ▶ print STDERR "ERROR: Request API call failed: $code, $text\n" unless $self->silent;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->silent 0 0 0
179 0 return;
180 }
181  
182 0 my $json = $res->json;
183 # Fetch max_page value so we can iterate:
184 0 ▶ $this_max_page = $self->get_maxpage($json) unless defined $this_max_page;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $this_max_page 0 0 0
185 0 ▶ if (defined($input) && !$this_max_page) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $input and not $this_max_page) 0 0 0
Condition0%0/3
defined $input and not $this_max_page
!ll&&!rl&&r
000
MC/DC0%0/2
defined $input and not $this_max_page
defined $input not $this_max_page
Truth table0%0/3
A and not(B)
A= defined $inputB= not $this_max_page
ABresult
0X0
100
111
186 0 ▶ print STDERR "ERROR: Request API call failed, no max_page found\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->silent 0 0 0
187 unless $self->silent;
188 0 ▶ print STDERR "VERBOSE: ".Data::Dumper::Dumper($json)."\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 0 0
189 0 return;
190 }
191  
192 0 my $results = $self->get_results($json);
193 0 ▶ if (defined($input) && !@$results) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $input and not @$results) 0 0 0
Condition0%0/3
defined $input and not @$results
!ll&&!rl&&r
000
MC/DC0%0/2
defined $input and not @$results
defined $input not @$results
Truth table0%0/3
A and not(B)
A= defined $inputB= not @$results
ABresult
0X0
100
111
194 0 ▶ print STDERR "ERROR: Request API call failed, no results found\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->silent 0 0 0
195 unless $self->silent;
196 0 ▶ print STDERR "VERBOSE: ".Data::Dumper::Dumper($json)."\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 0 0
197 0 return;
198 }
199 0 $cb->($results, $cb_args);
200  
201 0 ▶ last unless (defined($page) && defined($maxpage));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $page and defined $maxpage 0 0 0
Condition0%0/3
defined $page and defined $maxpage
!ll&&!rl&&r
000
MC/DC0%0/2
defined $page and defined $maxpage
defined $page defined $maxpage
Truth table0%0/3
A and B
A= defined $pageB= defined $maxpage
ABresult
0X0
100
111
202  
203 0 ▶ last if ($page == $maxpage || $page >= $this_max_page);
Branch: 1 branch
BranchTrueFalseTotal
if $page == $maxpage or $page >= $this_max_page 0 0 0
Condition0%0/3
$page == $maxpage or $page >= $this_max_page
l!l&&r!l&&!r
000
MC/DC0%0/2
$page == $maxpage or $page >= $this_max_page
$page == $maxpage $page >= $this_max_page
Truth table0%0/3
A or B
A= $page == $maxpageB= $page >= $this_max_page
ABresult
1X1
011
000
204 0 $page++;
205 }
206  
207 0 return 1;
208 }
209  
210 # $self->post_request('/search', 'protocol:ssh', 1, 10);
211 # $self->post_request('/simple/datascan', '8.8.8.8');
212 0 ▶sub post_request ($self, $api, $input = undef, $page = undef, $maxpage = undef, $params = undef, $cb = undef, $cb_args = undef) {
Subroutine post_request: not called CC=47 SCAR=77.2
Pod undocumented
213 0 my $global = $self->config->{''};
214 my $endpoint = $global->{api_unrated_endpoint} || $global->{api_endpoint}
215 0 ▶ || $self->endpoint;
Condition0%0/6
$global->{'api_unrated_endpoint'} || $global->{'api_endpoint'} || $self->endpoint
l!l&&r!l&&!r
000
$global->{"api_unrated_endpoint"} or $global->{"api_endpoint"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$global->{'api_unrated_endpoint'} || $global->{'api_endpoint'} || $self->endpoint
$global->{"api_unrated_endpoint"} $global->{"api_endpoint"} $self->endpoint
Truth table0%0/4
A or B || C
A= $global->{"api_unrated_endpoint"}B= $global->{"api_endpoint"}C= $self->endpoint
ABCresult
1XX1
01X1
0011
0000
216 0 ▶ my $apikey = $global->{api_key} || $self->apikey;
Condition0%0/3
$global->{'api_key'} || $self->apikey
l!l&&r!l&&!r
000
MC/DC0%0/2
$global->{'api_key'} || $self->apikey
$global->{'api_key'} $self->apikey
Truth table0%0/3
A || B
A= $global->{'api_key'}B= $self->apikey
ABresult
1X1
011
000
217  
218 # Use default callback when none given:
219 0 ▶ $cb ||= $self->_cb_request;
Condition0%0/3
$cb ||= $self->_cb_request
l!l&&r!l&&!r
000
MC/DC0%0/2
$cb ||= $self->_cb_request
$cb $self->_cb_request
Truth table0%0/3
A ||= B
A= $cbB= $self->_cb_request
ABresult
1X1
011
000
220  
221 0 ▶ $page ||= 1;
Condition0%0/2
$page ||= 1
l!l
00
MC/DC0%0/1
$page ||= 1
$page
Truth table0%0/2
A
A= $page
Aresult
11
00
222 0 ▶ $maxpage ||= $global->{api_maxpage} || 1;
Condition0%0/5
$maxpage ||= $global->{"api_maxpage"} || 1
l!l&&r!l&&!r
000
$global->{'api_maxpage'} || 1
l!l
00
MC/DC0%0/2
$maxpage ||= $global->{"api_maxpage"} || 1
$maxpage $global->{'api_maxpage'}
Truth table0%0/3
A ||= B
A= $maxpageB= $global->{'api_maxpage'}
ABresult
1X1
011
000
223  
224 0 my $ua = $self->_ua();
225 0 my $headers = $self->_headers($apikey, 'application/x-www-form-urlencoded');
226  
227 0 $api =~ s{^/*}{/}g;
228 0 my $this_max_page;
229 0 while (1) {
230 0 my $path = $endpoint.$api;
231 0 my $p = [];
232 0 push @$p, { k => 'k', v => $apikey };
233 0 ▶ push @$p, { k => 'page', v => $page } if defined $page;
Branch: 1 branch
BranchTrueFalseTotal
if defined $page 0 0 0
234 0 ▶ push @$p, { k => 'trackquery', v => 'true' } if $global->{api_trackquery};
Branch: 1 branch
BranchTrueFalseTotal
if $global->{"api_trackquery"} 0 0 0
235 0 ▶ push @$p, { k => 'calculated', v => 'true' } if $global->{api_calculated};
Branch: 1 branch
BranchTrueFalseTotal
if $global->{"api_calculated"} 0 0 0
236 0 ▶ push @$p, { k => 'keepalive', v => 'true' } if $global->{api_keepalive};
Branch: 1 branch
BranchTrueFalseTotal
if $global->{"api_keepalive"} 0 0 0
237 0 ▶ if (defined($params) && ref($params) eq 'HASH' && keys %$params) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params and ref $params eq "HASH" and keys %$params) 0 0 0
Condition0%0/6
defined $params and ref $params eq "HASH" and keys %$params
!ll&&!rl&&r
000
defined $params and ref $params eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/3
defined $params and ref $params eq "HASH" and keys %$params
defined $params ref $params eq "HASH" keys %$params
Truth table0%0/4
A and B and C
A= defined $paramsB= ref $params eq "HASH"C= keys %$params
ABCresult
0XX0
10X0
1100
1111
238 0 for my $this (qw(size trackquery calculated keepalive)) {
239 0 ▶ next unless defined($params->{$this}) || defined($params->{'api_'.$this});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params->{$this} or defined $$params{'api_' . $this} 0 0 0
Condition0%0/3
defined $params->{$this} or defined $$params{'api_' . $this}
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $params->{$this} or defined $$params{'api_' . $this}
defined $params->{$this} defined $$params{'api_' . $this}
Truth table0%0/3
A or B
A= defined $params->{$this}B= defined $$params{'api_' . $this}
ABresult
1X1
011
000
240 0 ▶ push @$p, { k => $this, v => $params->{$this} || $global->{'api_'.$this} };
Condition0%0/3
$params->{$this} || $$global{'api_' . $this}
l!l&&r!l&&!r
000
MC/DC0%0/2
$params->{$this} || $$global{'api_' . $this}
$params->{$this} $$global{'api_' . $this}
Truth table0%0/3
A || B
A= $params->{$this}B= $$global{'api_' . $this}
ABresult
1X1
011
000
241 }
242 }
243 0 my $args = $self->_params($p);
244 0 ▶ $path .= $args if $args;
Branch: 1 branch
BranchTrueFalseTotal
if $args 0 0 0
245 0 my $url = Mojo::URL->new($path);
246  
247 0 ▶ print STDERR "VERBOSE: Calling API: $path\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 0 0
248  
249 0 ▶ $input = url_escape($input) if defined $input;
Branch: 1 branch
BranchTrueFalseTotal
if defined $input 0 0 0
250  
251 0 RETRY:
252 my $res;
253 0 eval {
254 0 $res = $ua->post($url => $headers => form => { query => $input })->result;
255 };
256 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
257 0 chomp($@);
258 0 ▶ print STDERR "WARNING: Request API call failed: [$@], retrying...\n" unless $self->silent;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->silent 0 0 0
259 0 goto RETRY;
260 }
261 0 ▶ unless ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->is_success) 0 0 0
262 0 my $code = $res->code;
263 0 my $text = $res->json->{text};
264 # If code 429, retry with some sleep:
265 0 ▶ if ($code == 429) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code == 429) 0 0 0
266 0 ▶ print STDERR "WARNING: Too fast, sleeping before retry...\n" unless $self->silent;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->silent 0 0 0
267 0 sleep 1;
268 0 goto RETRY;
269 }
270 # Otherwise, stops and display error:
271 0 ▶ print STDERR "ERROR: Request API call failed: $code, $text\n" unless $self->silent;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->silent 0 0 0
272 0 return;
273 }
274  
275 0 my $json = $res->json;
276 # Fetch max_page value so we can iterate:
277 0 ▶ $this_max_page = $self->get_maxpage($json) unless defined $this_max_page;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $this_max_page 0 0 0
278 0 ▶ if (defined($input) && !$this_max_page) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $input and not $this_max_page) 0 0 0
Condition0%0/3
defined $input and not $this_max_page
!ll&&!rl&&r
000
MC/DC0%0/2
defined $input and not $this_max_page
defined $input not $this_max_page
Truth table0%0/3
A and not(B)
A= defined $inputB= not $this_max_page
ABresult
0X0
100
111
279 0 ▶ print STDERR "ERROR: Request API call failed, no max_page found\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->silent 0 0 0
280 unless $self->silent;
281 0 ▶ print STDERR "VERBOSE: ".Data::Dumper::Dumper($json)."\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 0 0
282 0 return;
283 }
284  
285 0 my $results = $self->get_results($json);
286 0 ▶ if (defined($input) && !@$results) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $input and not @$results) 0 0 0
Condition0%0/3
defined $input and not @$results
!ll&&!rl&&r
000
MC/DC0%0/2
defined $input and not @$results
defined $input not @$results
Truth table0%0/3
A and not(B)
A= defined $inputB= not @$results
ABresult
0X0
100
111
287 0 ▶ print STDERR "ERROR: Request API call failed, no results found\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->silent 0 0 0
288 unless $self->silent;
289 0 ▶ print STDERR "VERBOSE: ".Data::Dumper::Dumper($json)."\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 0 0
290 0 return;
291 }
292 0 $cb->($results, $cb_args);
293  
294 0 ▶ last unless (defined($page) && defined($maxpage));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $page and defined $maxpage 0 0 0
Condition0%0/3
defined $page and defined $maxpage
!ll&&!rl&&r
000
MC/DC0%0/2
defined $page and defined $maxpage
defined $page defined $maxpage
Truth table0%0/3
A and B
A= defined $pageB= defined $maxpage
ABresult
0X0
100
111
295  
296 0 ▶ last if ($page == $maxpage || $page >= $this_max_page);
Branch: 1 branch
BranchTrueFalseTotal
if $page == $maxpage or $page >= $this_max_page 0 0 0
Condition0%0/3
$page == $maxpage or $page >= $this_max_page
l!l&&r!l&&!r
000
MC/DC0%0/2
$page == $maxpage or $page >= $this_max_page
$page == $maxpage $page >= $this_max_page
Truth table0%0/3
A or B
A= $page == $maxpageB= $page >= $this_max_page
ABresult
1X1
011
000
297 0 $page++;
298 }
299  
300 0 return 1;
301 }
302  
303 # $self->user();
304 0 ▶sub user ($self, $cb = undef, $cb_args = undef) {
Subroutine user: not called CC=3 SCAR=24.8
Pod undocumented
305 0 return $self->request('/user', undef, undef, undef, undef, $cb, $cb_args);
306 }
307  
308 # $self->summary('ip', '8.8.8.8');
309 # $self->summary('domain', 'example.com');
310 # $self->summary('hostname', 'www.example.com');
311 0 ▶sub summary ($self, $api, $oql, $params = undef, $cb = undef, $cb_args = undef) {
Subroutine summary: not called CC=4 SCAR=30.0
Pod undocumented
312 0 return $self->request('/summary/'.$api, $oql, undef, undef, $params, $cb, $cb_args);
313 }
314  
315 # $self->simple('datascan', 'Server: Apache');
316 # $self->simple('synscan', '8.8.8.8');
317 0 ▶sub simple ($self, $api, $oql, $params = undef, $cb = undef, $cb_args = undef) {
Subroutine simple: not called CC=4 SCAR=30.0
Pod undocumented
318 0 return $self->request('/simple/'.$api, $oql, undef, undef, $params, $cb, $cb_args);
319 }
320  
321 # $self->simple_best('geoloc', '8.8.8.8');
322 # $self->simple_best('inetnum', '8.8.8.8');
323 # $self->simple_best('threatlist', '8.8.8.8');
324 # $self->simple_best('whois', '8.8.8.8');
325 0 ▶sub simple_best ($self, $api, $oql, $params = undef, $cb = undef, $cb_args = undef) {
Subroutine simple_best: not called CC=4 SCAR=30.0
Pod undocumented
326 0 return $self->request('/simple/'.$api.'/best', $oql, undef, undef, $params, $cb, $cb_args);
327 }
328  
329 # $self->search('protocol:ssh', 1, 1000);
330 0 ▶sub search ($self, $oql, $page = 1, $maxpage = 1, $params = undef, $cb = undef, $cb_args = undef) {
Subroutine search: not called CC=6 SCAR=37.4
Pod undocumented
331 0 return $self->request('/search', $oql, $page, $maxpage, $params, $cb, $cb_args);
332 }
333  
334 0 ▶sub post_search ($self, $oql, $page = 1, $maxpage = 1, $params = undef, $cb = undef, $cb_args = undef) {
Subroutine post_search: not called CC=6 SCAR=37.4
Pod undocumented
335 0 return $self->post_request('/search', $oql, $page, $maxpage, $params, $cb, $cb_args);
336 }
337  
338 #
339 # Stream-based APIs
340 #
341 0 ▶sub _cb_stream ($self, $results = undef, $cb_args = undef) {
Subroutine _cb_stream: not called CC=3 SCAR=24.8
342 ▶ return sub ($results, $cb_args) {
Subroutine __ANON__: not called CC=4 SCAR=30.0
343 0 ▶ $results = ref($results) eq 'ARRAY' ? $results : [ $results ];
Branch: 1 branch
BranchTrueFalseTotal
ref $results eq 'ARRAY' ? : 0 0 0
344 0 for (@$results) {
345 0 ▶ next if m{.\@category.\s*:\s*.none.};
Branch: 1 branch
BranchTrueFalseTotal
if /.\@category.\s*:\s*.none./ 0 0 0
346 0 print "$_\n";
347 }
348 0 };
349 }
350  
351 0 ▶sub _on_read ($self, $cb = undef, $cb_args = undef, $buf = \'') {
Subroutine _on_read: not called CC=4 SCAR=30.0
352 # XXX: handle is_success and render ERROR
353 return sub {
354 0 ▶ my ($content, $bytes) = @_;
Subroutine __ANON__: not called CC=3 SCAR=24.8
355 0 $bytes = $$buf.$bytes; # Complete from previously incomplete lines
356 0 my ($this, $tail) = $bytes =~ m/^(.*\n)(.*)$/s;
357 # One line is not complete, add to buf and go to next:
358 0 ▶ if (!defined($this)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $this) { } 0 0 0
359 0 $buf = \$bytes;
360 }
361 else { # Lines complete, process them
362 0 ▶ $buf = defined($tail) ? \$tail : \'';
Branch: 1 branch
BranchTrueFalseTotal
defined $tail ? : 0 0 0
363 0 my $results = [ split(/\n/, $this) ];
364 0 return $cb->($results, $cb_args);
365 }
366 0 };
367 }
368  
369 # $self->stream('GET', '/export', 'protocol:ssh');
370 # $self->stream('POST', '/bulk/whois/best/ip', '/tmp/ip.txt');
371 0 ▶sub stream ($self, $method, $api, $input, $params = undef, $cb = undef, $cb_args = undef) {
Subroutine stream: not called CC=23 SCAR=63.1
Pod undocumented
372 0 my $global = $self->config->{''};
373 my $endpoint = $global->{api_unrated_endpoint} || $global->{api_endpoint}
374 0 ▶ || $self->endpoint;
Condition0%0/6
$global->{'api_unrated_endpoint'} || $global->{'api_endpoint'} || $self->endpoint
l!l&&r!l&&!r
000
$global->{"api_unrated_endpoint"} or $global->{"api_endpoint"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$global->{'api_unrated_endpoint'} || $global->{'api_endpoint'} || $self->endpoint
$global->{"api_unrated_endpoint"} $global->{"api_endpoint"} $self->endpoint
Truth table0%0/4
A or B || C
A= $global->{"api_unrated_endpoint"}B= $global->{"api_endpoint"}C= $self->endpoint
ABCresult
1XX1
01X1
0011
0000
375 0 ▶ my $apikey = $global->{api_key} || $self->apikey;
Condition0%0/3
$global->{'api_key'} || $self->apikey
l!l&&r!l&&!r
000
MC/DC0%0/2
$global->{'api_key'} || $self->apikey
$global->{'api_key'} $self->apikey
Truth table0%0/3
A || B
A= $global->{'api_key'}B= $self->apikey
ABresult
1X1
011
000
376  
377 # Use default callback when none given:
378 0 ▶ $cb ||= $self->_cb_stream;
Condition0%0/3
$cb ||= $self->_cb_stream
l!l&&r!l&&!r
000
MC/DC0%0/2
$cb ||= $self->_cb_stream
$cb $self->_cb_stream
Truth table0%0/3
A ||= B
A= $cbB= $self->_cb_stream
ABresult
1X1
011
000
379  
380 0 my $ua = $self->_ua();
381 0 my $headers = $self->_headers($apikey);
382  
383 0 my $path = $endpoint.$api;
384 0 ▶ unless (-f $input) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $input) 0 0 0
385 0 $path .= '/'.url_escape($input); # Build with OQL string
386 }
387  
388 0 my $p= [];
389 0 push @$p, { k => 'k', v => $apikey };
390 0 ▶ push @$p, { k => 'trackquery', v => 'true' } if $global->{api_trackquery};
Branch: 1 branch
BranchTrueFalseTotal
if $global->{"api_trackquery"} 0 0 0
391 0 ▶ push @$p, { k => 'calculated', v => 'true' } if $global->{api_calculated};
Branch: 1 branch
BranchTrueFalseTotal
if $global->{"api_calculated"} 0 0 0
392 0 ▶ push @$p, { k => 'keepalive', v => 'true' } if $global->{api_keepalive};
Branch: 1 branch
BranchTrueFalseTotal
if $global->{"api_keepalive"} 0 0 0
393 0 ▶ if (defined($params) && ref($params) eq 'HASH' && keys %$params) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params and ref $params eq "HASH" and keys %$params) 0 0 0
Condition0%0/6
defined $params and ref $params eq "HASH" and keys %$params
!ll&&!rl&&r
000
defined $params and ref $params eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/3
defined $params and ref $params eq "HASH" and keys %$params
defined $params ref $params eq "HASH" keys %$params
Truth table0%0/4
A and B and C
A= defined $paramsB= ref $params eq "HASH"C= keys %$params
ABCresult
0XX0
10X0
1100
1111
394 0 for my $this (qw(size trackquery calculated keepalive)) {
395 0 ▶ next unless defined($params->{$this}) || defined($params->{'api_'.$this});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params->{$this} or defined $$params{'api_' . $this} 0 0 0
Condition0%0/3
defined $params->{$this} or defined $$params{'api_' . $this}
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $params->{$this} or defined $$params{'api_' . $this}
defined $params->{$this} defined $$params{'api_' . $this}
Truth table0%0/3
A or B
A= defined $params->{$this}B= defined $$params{'api_' . $this}
ABresult
1X1
011
000
396 0 ▶ push @$p, { k => $this, v => $params->{$this} || $global->{'api_'.$this} };
Condition0%0/3
$params->{$this} || $$global{'api_' . $this}
l!l&&r!l&&!r
000
MC/DC0%0/2
$params->{$this} || $$global{'api_' . $this}
$params->{$this} $$global{'api_' . $this}
Truth table0%0/3
A || B
A= $params->{$this}B= $$global{'api_' . $this}
ABresult
1X1
011
000
397 }
398 }
399 0 my $args = $self->_params($p);
400 0 ▶ $path .= $args if $args;
Branch: 1 branch
BranchTrueFalseTotal
if $args 0 0 0
401  
402 0 ▶ print STDERR "VERBOSE: Calling API: $path\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 0 0
403  
404 0 my $url = Mojo::URL->new($path);
405  
406 0 my $buf = ''; # Will store incomplete lines for later processing
407 0 my $tx = $ua->build_tx($method => $url => $headers);
408 # Replace "read" events to disable default content parser:
409 0 $tx->res->content->unsubscribe('read')->on(read => $self->_on_read($cb, $cb_args, \$buf));
410  
411 0 ▶ if (-f $input) { # POST file content
Branch: 1 branch
BranchTrueFalseTotal
if (-f $input) 0 0 0
412 0 ▶ print STDERR "VERBOSE: Reading file: $input\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 0 0
413 0 $tx->req->content->asset(Mojo::Asset::File->new(path => $input));
414 }
415  
416 # Process transaction:
417 0 return $ua->start($tx);
418 }
419  
420 0 ▶sub post_stream ($self, $method, $api, $input, $params = undef, $cb = undef, $cb_args = undef) {
Subroutine post_stream: not called CC=22 SCAR=62.3
Pod undocumented
421 0 my $global = $self->config->{''};
422 my $endpoint = $global->{api_unrated_endpoint} || $global->{api_endpoint}
423 0 ▶ || $self->endpoint;
Condition0%0/6
$global->{'api_unrated_endpoint'} || $global->{'api_endpoint'} || $self->endpoint
l!l&&r!l&&!r
000
$global->{"api_unrated_endpoint"} or $global->{"api_endpoint"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$global->{'api_unrated_endpoint'} || $global->{'api_endpoint'} || $self->endpoint
$global->{"api_unrated_endpoint"} $global->{"api_endpoint"} $self->endpoint
Truth table0%0/4
A or B || C
A= $global->{"api_unrated_endpoint"}B= $global->{"api_endpoint"}C= $self->endpoint
ABCresult
1XX1
01X1
0011
0000
424 0 ▶ my $apikey = $global->{api_key} || $self->apikey;
Condition0%0/3
$global->{'api_key'} || $self->apikey
l!l&&r!l&&!r
000
MC/DC0%0/2
$global->{'api_key'} || $self->apikey
$global->{'api_key'} $self->apikey
Truth table0%0/3
A || B
A= $global->{'api_key'}B= $self->apikey
ABresult
1X1
011
000
425  
426 # Use default callback when none given:
427 0 ▶ $cb ||= $self->_cb_stream;
Condition0%0/3
$cb ||= $self->_cb_stream
l!l&&r!l&&!r
000
MC/DC0%0/2
$cb ||= $self->_cb_stream
$cb $self->_cb_stream
Truth table0%0/3
A ||= B
A= $cbB= $self->_cb_stream
ABresult
1X1
011
000
428  
429 0 my $ua = $self->_ua();
430 0 my $headers = $self->_headers($apikey, 'application/x-www-form-urlencoded');
431  
432 0 my $path = $endpoint.$api;
433 0 $input = url_escape($input); # Build with OQL string
434  
435 0 my $p= [];
436 0 push @$p, { k => 'k', v => $apikey };
437 0 ▶ push @$p, { k => 'trackquery', v => 'true' } if $global->{api_trackquery};
Branch: 1 branch
BranchTrueFalseTotal
if $global->{"api_trackquery"} 0 0 0
438 0 ▶ push @$p, { k => 'calculated', v => 'true' } if $global->{api_calculated};
Branch: 1 branch
BranchTrueFalseTotal
if $global->{"api_calculated"} 0 0 0
439 0 ▶ push @$p, { k => 'keepalive', v => 'true' } if $global->{api_keepalive};
Branch: 1 branch
BranchTrueFalseTotal
if $global->{"api_keepalive"} 0 0 0
440 0 ▶ if (defined($params) && ref($params) eq 'HASH' && keys %$params) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params and ref $params eq "HASH" and keys %$params) 0 0 0
Condition0%0/6
defined $params and ref $params eq "HASH" and keys %$params
!ll&&!rl&&r
000
defined $params and ref $params eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/3
defined $params and ref $params eq "HASH" and keys %$params
defined $params ref $params eq "HASH" keys %$params
Truth table0%0/4
A and B and C
A= defined $paramsB= ref $params eq "HASH"C= keys %$params
ABCresult
0XX0
10X0
1100
1111
441 0 for my $this (qw(size trackquery calculated keepalive)) {
442 0 ▶ next unless defined($params->{$this}) || defined($params->{'api_'.$this});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params->{$this} or defined $$params{'api_' . $this} 0 0 0
Condition0%0/3
defined $params->{$this} or defined $$params{'api_' . $this}
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $params->{$this} or defined $$params{'api_' . $this}
defined $params->{$this} defined $$params{'api_' . $this}
Truth table0%0/3
A or B
A= defined $params->{$this}B= defined $$params{'api_' . $this}
ABresult
1X1
011
000
443 0 ▶ push @$p, { k => $this, v => $params->{$this} || $global->{'api_'.$this} };
Condition0%0/3
$params->{$this} || $$global{'api_' . $this}
l!l&&r!l&&!r
000
MC/DC0%0/2
$params->{$this} || $$global{'api_' . $this}
$params->{$this} $$global{'api_' . $this}
Truth table0%0/3
A || B
A= $params->{$this}B= $$global{'api_' . $this}
ABresult
1X1
011
000
444 }
445 }
446 0 my $args = $self->_params($p);
447 0 ▶ $path .= $args if $args;
Branch: 1 branch
BranchTrueFalseTotal
if $args 0 0 0
448  
449 0 ▶ print STDERR "VERBOSE: Calling API: $path\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 0 0
450  
451 0 my $url = Mojo::URL->new($path);
452  
453 0 my $buf = ''; # Will store incomplete lines for later processing
454 0 my $tx = $ua->build_tx($method => $url => $headers => form => { query => $input });
455 # Replace "read" events to disable default content parser:
456 0 $tx->res->content->unsubscribe('read')->on(read => $self->_on_read($cb, $cb_args, \$buf));
457  
458 0 ▶ if (-f $input) { # POST file content
Branch: 1 branch
BranchTrueFalseTotal
if (-f $input) 0 0 0
459 0 ▶ print STDERR "VERBOSE: Reading file: $input\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 0 0
460 0 $tx->req->content->asset(Mojo::Asset::File->new(path => $input));
461 }
462  
463 # Process transaction:
464 0 return $ua->start($tx);
465 }
466  
467 ▶sub _check_file ($self, $file) {
Subroutine _check_file: not called CC=3 SCAR=24.8
468 0 ▶ unless (-f $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $file) 0 0 0
469 0 ▶ print STDERR "ERROR: file not found: $file\n" unless $self->silent;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->silent 0 0 0
470 0 return;
471 }
472 0 return $file;
473 }
474  
475 # $self->bulk_summary('ip', '/tmp/ip.txt');
476 # $self->bulk_summary('domain', '/tmp/domain.txt');
477 # $self->bulk_summary('hostname', '/tmp/hostname.txt');
478 0 ▶sub bulk_summary ($self, $api, $file, $params = undef, $cb = undef, $cb_args = undef) {
Subroutine bulk_summary: not called CC=5 SCAR=34.0
Pod undocumented
479 0 ▶ return unless $self->_check_file($file);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_check_file($file) 0 0 0
480 0 $api =~ s{^/*}{/}g;
481 0 return $self->stream('POST', '/bulk/summary'.$api, $file, $params, $cb, $cb_args);
482 }
483  
484 # $self->bulk_simple('ctl', '/tmp/ip.txt');
485 # $self->bulk_simple('resolver', '/tmp/ip.txt');
486 0 ▶sub bulk_simple ($self, $api, $file, $params = undef, $cb = undef, $cb_args = undef) {
Subroutine bulk_simple: not called CC=5 SCAR=34.0
Pod undocumented
487 0 ▶ return unless $self->_check_file($file);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_check_file($file) 0 0 0
488 0 $api =~ s{^/*}{/}g;
489 0 return $self->stream('POST', '/bulk/simple'.$api.'/ip', $file, $params, $cb, $cb_args);
490 }
491  
492 # $self->bulk_simple_best('threatlist', '/tmp/ip.txt');
493 # $self->bulk_simple_best('whois', '/tmp/ip.txt');
494 0 ▶sub bulk_simple_best ($self, $api, $file, $params = undef, $cb = undef, $cb_args = undef) {
Subroutine bulk_simple_best: not called CC=5 SCAR=34.0
Pod undocumented
495 0 ▶ return unless $self->_check_file($file);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_check_file($file) 0 0 0
496 0 $api =~ s{^/*}{/}g;
497 0 return $self->stream('POST', '/bulk/simple'.$api.'/best/ip', $file, $params, $cb, $cb_args);
498 }
499  
500 # $self->bulk_discovery('datascan', '/tmp/oql.txt');
501 # $self->bulk_discovery('domain', '/tmp/oql.txt');
502 # $self->bulk_discovery('domain', '/tmp/oql.txt', $oql);
503 0 ▶sub bulk_discovery ($self, $api, $file, $oql = undef, $params = undef, $cb = undef, $cb_args = undef) {
Subroutine bulk_discovery: not called CC=10 SCAR=47.0
Pod undocumented
504 0 ▶ return unless $self->_check_file($file);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_check_file($file) 0 0 0
505 0 $api =~ s{^/*}{/}g;
506 0 my $input = $file;
507 # Rewrite to another file when oql is given:
508 0 my ($fh, $filename);
509 0 ▶ if (defined($oql)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $oql) 0 0 0
510 0 my @lines = read_file($file);
511 0 ($fh, $filename) = tempfile();
512 0 $input = $filename;
513 0 for (@lines) {
514 0 chomp;
515 0 my $line = "$_ $oql";
516 0 utf8::encode($line);
517 0 print $fh "$line\n";
518 }
519 0 close($fh);
520 }
521 0 $self->stream('POST', '/bulk/discovery'.$api.'/asset', $input, $params, $cb, $cb_args);
522 0 ▶ unlink($filename) if defined($filename) && -f $filename;
Branch: 1 branch
BranchTrueFalseTotal
if defined $filename and -f $filename 0 0 0
Condition0%0/3
defined $filename and -f $filename
!ll&&!rl&&r
000
MC/DC0%0/2
defined $filename and -f $filename
defined $filename -f $filename
Truth table0%0/3
A and B
A= defined $filenameB= -f $filename
ABresult
0X0
100
111
523 0 return 1;
524 }
525  
526 # $self->export('protocol:ssh');
527 0 ▶sub export ($self, $oql, $params = undef, $cb = undef, $cb_args = undef) {
Subroutine export: not called CC=4 SCAR=30.0
Pod undocumented
528 0 return $self->stream('GET', '/export', $oql, $params, $cb, $cb_args);
529 }
530  
531 0 ▶sub post_export ($self, $oql, $params = undef, $cb = undef, $cb_args = undef) {
Subroutine post_export: not called CC=4 SCAR=30.0
Pod undocumented
532 0 return $self->stream('POST', '/export', $oql, $params, $cb, $cb_args);
533 }
534  
535 #
536 # Alert API
537 #
538 0 ▶sub _cb_alert ($self, $results = undef, $cb_args = undef) {
Subroutine _cb_alert: not called CC=3 SCAR=24.8
539 ▶ return sub ($results, $cb_args) {
Subroutine __ANON__: not called CC=3 SCAR=24.8
540 0 ▶ $results = ref($results) eq 'ARRAY' ? $results : [ $results ];
Branch: 1 branch
BranchTrueFalseTotal
ref $results eq 'ARRAY' ? : 0 0 0
541 0 for (@$results) {
542 0 print $self->encode($_)."\n";
543 }
544 0 };
545 }
546  
547 0 ▶sub alert ($self, $method, $api, $name, $oql, $email, $threshold = 0, $cb = undef, $cb_args = undef) {
Subroutine alert: not called CC=20 SCAR=60.4
Pod undocumented
548 0 my $global = $self->config->{''};
549 my $endpoint = $global->{api_unrated_endpoint} || $global->{api_endpoint}
550 0 ▶ || $self->endpoint;
Condition0%0/6
$global->{'api_unrated_endpoint'} || $global->{'api_endpoint'} || $self->endpoint
l!l&&r!l&&!r
000
$global->{"api_unrated_endpoint"} or $global->{"api_endpoint"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$global->{'api_unrated_endpoint'} || $global->{'api_endpoint'} || $self->endpoint
$global->{"api_unrated_endpoint"} $global->{"api_endpoint"} $self->endpoint
Truth table0%0/4
A or B || C
A= $global->{"api_unrated_endpoint"}B= $global->{"api_endpoint"}C= $self->endpoint
ABCresult
1XX1
01X1
0011
0000
551 0 ▶ my $apikey = $global->{api_key} || $self->apikey;
Condition0%0/3
$global->{'api_key'} || $self->apikey
l!l&&r!l&&!r
000
MC/DC0%0/2
$global->{'api_key'} || $self->apikey
$global->{'api_key'} $self->apikey
Truth table0%0/3
A || B
A= $global->{'api_key'}B= $self->apikey
ABresult
1X1
011
000
552  
553 # Use default callback when none given:
554 0 ▶ $cb ||= $self->_cb_alert;
Condition0%0/3
$cb ||= $self->_cb_alert
l!l&&r!l&&!r
000
MC/DC0%0/2
$cb ||= $self->_cb_alert
$cb $self->_cb_alert
Truth table0%0/3
A ||= B
A= $cbB= $self->_cb_alert
ABresult
1X1
011
000
555  
556 0 my $ua = $self->_ua();
557 0 my $headers = $self->_headers($apikey);
558  
559 0 $api =~ s{^/*}{/}g;
560  
561 0 my $path = $endpoint.$api;
562 0 $path .= '?k='.$apikey;
563  
564 0 ▶ print STDERR "VERBOSE: Calling API: $path\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 0 0
565  
566 0 my $url = Mojo::URL->new($path);
567  
568 0 my $post;
569 0 ▶ $post->{name} = $name if defined $name;
Branch: 1 branch
BranchTrueFalseTotal
if defined $name 0 0 0
570 0 ▶ $post->{query} = $oql if defined $oql;
Branch: 1 branch
BranchTrueFalseTotal
if defined $oql 0 0 0
571 0 ▶ $post->{email} = $email if defined $email;
Branch: 1 branch
BranchTrueFalseTotal
if defined $email 0 0 0
572 0 ▶ $post->{threshold} = $threshold if defined $threshold;
Branch: 1 branch
BranchTrueFalseTotal
if defined $threshold 0 0 0
573  
574 0 my @args = ( $url => $headers );
575 0 ▶ @args = ( $url => $headers => json => $post ) if defined $post;
Branch: 1 branch
BranchTrueFalseTotal
if defined $post 0 0 0
576  
577 #print STDERR "DEBUG: args: ".Data::Dumper::Dumper(\@args)."\n";
578  
579 0 RETRY:
580 my $res;
581 0 eval {
582 0 $res = $ua->$method(@args)->result;
583 };
584 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
585 0 chomp($@);
586 0 ▶ print STDERR "WARNING: Alert API call failed: [$@], retrying...\n" unless $self->silent;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->silent 0 0 0
587 0 goto RETRY;
588 }
589 0 ▶ unless ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->is_success) 0 0 0
590 0 my $code = $res->code;
591 0 my $text = $res->json->{text};
592 # If code 429, retry with some sleep:
593 0 ▶ if ($code == 429) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code == 429) 0 0 0
594 0 ▶ print STDERR "WARNING: Too fast, sleeping before retry...\n" unless $self->silent;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->silent 0 0 0
595 0 sleep 1;
596 0 goto RETRY;
597 }
598 # Otherwise, stops and display error:
599 0 ▶ print STDERR "ERROR: Alert API call failed: $code, $text\n" unless $self->silent;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->silent 0 0 0
600 0 return;
601 }
602  
603 0 my $json = $res->json;
604 0 $cb->($json, $cb_args);
605  
606 0 return 1;
607 }
608  
609 # $self->alert_list();
610 0 ▶sub alert_list ($self, $cb = undef, $cb_args = undef) {
Subroutine alert_list: not called CC=3 SCAR=24.8
Pod undocumented
611 0 return $self->alert('get', '/alert/list', undef, undef, undef, undef, $cb, $cb_args);
612 }
613  
614 # $self->alert_add('test', 'category:datascan protocol:ssh', 'email@example.com', '>1000');
615 0 ▶sub alert_add ($self, $name, $oql, $email, $threshold = undef, $cb = undef, $cb_args = undef) {
Subroutine alert_add: not called CC=6 SCAR=37.4
Pod undocumented
616 0 ▶ if ($self->verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->verbose) 0 0 0
617 0 print STDERR "VERBOSE: name: [$name]\n";
618 0 print STDERR "VERBOSE: oql: [$oql]\n";
619 0 print STDERR "VERBOSE: email: [$email]\n";
620 0 ▶ print STDERR "VERBOSE: threshold: [$threshold]\n" if defined $threshold;
Branch: 1 branch
BranchTrueFalseTotal
if defined $threshold 0 0 0
621 }
622 0 return $self->alert('post', '/alert/add', $name, $oql, $email, $threshold, $cb, $cb_args);
623 }
624  
625 # $self->alert_del(0);
626 0 ▶sub alert_del ($self, $id, $cb = undef, $cb_args = undef) {
Subroutine alert_del: not called CC=3 SCAR=24.8
Pod undocumented
627 0 return $self->alert('post', '/alert/del/'.$id, undef, undef, undef, undef, $cb, $cb_args);
628 }
629  
630 #
631 # On-demand APIs:
632 #
633 0 ▶sub _cb_ondemand ($self, $results = undef, $cb_args = undef) {
Subroutine _cb_ondemand: not called CC=3 SCAR=24.8
634 ▶ return sub ($results, $cb_args) {
Subroutine __ANON__: not called CC=3 SCAR=24.8
635 0 ▶ $results = ref($results) eq 'ARRAY' ? $results : [ $results ];
Branch: 1 branch
BranchTrueFalseTotal
ref $results eq 'ARRAY' ? : 0 0 0
636 0 for (@$results) {
637 0 print $self->encode($_)."\n";
638 }
639 0 };
640 }
641  
642 0 ▶sub ondemand ($self, $method, $api, $param, $post, $cb = undef, $cb_args = undef) {
Subroutine ondemand: not called CC=22 SCAR=62.3
Pod undocumented
643 0 my $global = $self->config->{''};
644 0 ▶ my $endpoint = $global->{api_ondemand_endpoint} || $self->endpoint;
Condition0%0/3
$global->{'api_ondemand_endpoint'} || $self->endpoint
l!l&&r!l&&!r
000
MC/DC0%0/2
$global->{'api_ondemand_endpoint'} || $self->endpoint
$global->{'api_ondemand_endpoint'} $self->endpoint
Truth table0%0/3
A || B
A= $global->{'api_ondemand_endpoint'}B= $self->endpoint
ABresult
1X1
011
000
645 0 ▶ my $apikey = $global->{api_ondemand_key} || $self->apikey;
Condition0%0/3
$global->{'api_ondemand_key'} || $self->apikey
l!l&&r!l&&!r
000
MC/DC0%0/2
$global->{'api_ondemand_key'} || $self->apikey
$global->{'api_ondemand_key'} $self->apikey
Truth table0%0/3
A || B
A= $global->{'api_ondemand_key'}B= $self->apikey
ABresult
1X1
011
000
646  
647 # Use default callback when none given:
648 0 ▶ $cb ||= $self->_cb_ondemand;
Condition0%0/3
$cb ||= $self->_cb_ondemand
l!l&&r!l&&!r
000
MC/DC0%0/2
$cb ||= $self->_cb_ondemand
$cb $self->_cb_ondemand
Truth table0%0/3
A ||= B
A= $cbB= $self->_cb_ondemand
ABresult
1X1
011
000
649  
650 0 my $ua = $self->_ua();
651 0 my $headers = $self->_headers($apikey);
652  
653 0 $api =~ s{^/*}{/}g;
654  
655 0 my $path = $endpoint.$api;
656 0 $path .= '?k='.$apikey;
657  
658 0 ▶ if (defined($param)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $param) 0 0 0
659 0 ▶ $post->{maxscantime} = $param->{maxscantime} if defined $param->{maxscantime};
Branch: 1 branch
BranchTrueFalseTotal
if defined $param->{"maxscantime"} 0 0 0
660 0 ▶ $post->{urlscan} = $param->{urlscan} ? 'true' : 'false' if defined $param->{urlscan};
Branch: 2 branches
BranchTrueFalseTotal
if defined $param->{"urlscan"} 0 0 0
$param->{'urlscan'} ? : 0 0 0
661 0 ▶ $post->{vulnscan} = $param->{vulnscan} ? 'true' : 'false' if defined $param->{vulnscan};
Branch: 2 branches
BranchTrueFalseTotal
if defined $param->{"vulnscan"} 0 0 0
$param->{'vulnscan'} ? : 0 0 0
662 0 ▶ $post->{import} = $param->{import} ? 'true' : 'false' if defined $param->{import};
Branch: 2 branches
BranchTrueFalseTotal
if defined $param->{"import"} 0 0 0
$param->{'import'} ? : 0 0 0
663 }
664  
665 0 ▶ print STDERR "VERBOSE: Calling API: $path\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 0 0
666  
667 0 my $url = Mojo::URL->new($path);
668  
669 0 my @args = ( $url => $headers );
670 0 ▶ @args = ( $url => $headers => json => $post ) if defined $post;
Branch: 1 branch
BranchTrueFalseTotal
if defined $post 0 0 0
671  
672 #print STDERR "DEBUG: args: ".Data::Dumper::Dumper(\@args)."\n";
673  
674 0 RETRY:
675 my $res;
676 0 eval {
677 0 $res = $ua->$method(@args)->result;
678 };
679 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
680 0 chomp($@);
681 0 ▶ print STDERR "WARNING: Ondemand API call failed: [$@], retrying...\n" unless $self->silent;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->silent 0 0 0
682 0 goto RETRY;
683 }
684 0 ▶ unless ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->is_success) 0 0 0
685 0 my $code = $res->code;
686 #print Data::Dumper::Dumper($res->body)."\n";
687 0 my $text = $res->json->{text};
688 # If code 429, retry with some sleep:
689 0 ▶ if ($code == 429) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code == 429) 0 0 0
690 0 ▶ print STDERR "WARNING: Too fast, sleeping before retry...\n" unless $self->silent;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->silent 0 0 0
691 0 sleep 1;
692 0 goto RETRY;
693 }
694 # Otherwise, stops and display error:
695 0 ▶ print STDERR "ERROR: Ondemand API call failed: $code, $text\n" unless $self->silent;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->silent 0 0 0
696 0 return;
697 }
698  
699 0 my $json = $res->json;
700 0 $cb->($json, $cb_args);
701  
702 0 return 1;
703 }
704  
705 0 ▶sub ondemand_scope_ip ($self, $target, $param = undef, $cb = undef, $cb_args = undef) {
Subroutine ondemand_scope_ip: not called CC=4 SCAR=30.0
Pod undocumented
706 0 return $self->ondemand('post', '/ondemand/scope/ip/single', $param, { ip => $target }, $cb, $cb_args);
707 }
708  
709 0 ▶sub ondemand_scope_domain ($self, $target, $param = undef, $cb = undef, $cb_args = undef) {
Subroutine ondemand_scope_domain: not called CC=4 SCAR=30.0
Pod undocumented
710 0 return $self->ondemand('post', '/ondemand/scope/domain/single', $param, { domain => $target }, $cb, $cb_args);
711 }
712  
713 0 ▶sub ondemand_scope_ip_bulk ($self, $file, $param = undef, $cb = undef, $cb_args = undef) {
Subroutine ondemand_scope_ip_bulk: not called CC=9 SCAR=45.0
Pod undocumented
714 0 ▶ if (! -f $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $file) 0 0 0
715 0 ▶ print STDERR "ERROR: Ondemand Scope Ip Bulk needs a file as input\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->silent 0 0 0
716 unless $self->silent;
717 }
718  
719 0 my @lines = read_file($file);
720 0 for (@lines) { chomp };
721 0 ▶ unless (@lines) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@lines) 0 0 0
722 0 ▶ print STDERR "ERROR: Ondemand Scope Ip Bulk needs a file with content\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->silent 0 0 0
723 unless $self->silent;
724 }
725  
726 0 my $target = join(',', @lines);
727  
728 0 return $self->ondemand('post', '/ondemand/scope/ip/bulk', $param, { ip => $target }, $cb, $cb_args);
729 }
730  
731 0 ▶sub ondemand_scope_domain_bulk ($self, $file, $param = undef, $cb = undef, $cb_args = undef) {
Subroutine ondemand_scope_domain_bulk: not called CC=9 SCAR=45.0
Pod undocumented
732 0 ▶ if (! -f $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $file) 0 0 0
733 0 ▶ print STDERR "ERROR: Ondemand Scope Domain Bulk needs a file as input\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->silent 0 0 0
734 unless $self->silent;
735 }
736  
737 0 my @lines = read_file($file);
738 0 for (@lines) { chomp };
739 0 ▶ unless (@lines) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@lines) 0 0 0
740 0 ▶ print STDERR "ERROR: Ondemand Scope Domain Bulk needs a file with content\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->silent 0 0 0
741 unless $self->silent;
742 }
743  
744 0 my $target = join(',', @lines);
745  
746 0 return $self->ondemand('post', '/ondemand/scope/domain/bulk', $param, { domain => $target }, $cb, $cb_args);
747 }
748  
749 0 ▶sub ondemand_scope_result ($self, $scan_id, $param = undef, $cb = undef, $cb_args = undef) {
Subroutine ondemand_scope_result: not called CC=4 SCAR=30.0
Pod undocumented
750 0 return $self->ondemand('get', '/ondemand/scope/result/'.$scan_id, $param, undef, $cb, $cb_args);
751 }
752  
753 0 ▶sub ondemand_resolver_ip ($self, $target, $param = undef, $cb = undef, $cb_args = undef) {
Subroutine ondemand_resolver_ip: not called CC=4 SCAR=30.0
Pod undocumented
754 0 return $self->ondemand('post', '/ondemand/resolver/ip/single', $param, { ip => $target }, $cb, $cb_args);
755 }
756  
757 0 ▶sub ondemand_resolver_domain ($self, $target, $param = undef, $cb = undef, $cb_args = undef) {
Subroutine ondemand_resolver_domain: not called CC=4 SCAR=30.0
Pod undocumented
758 0 return $self->ondemand('post', '/ondemand/resolver/domain/single', $param, { domain => $target }, $cb, $cb_args);
759 }
760  
761 0 ▶sub ondemand_resolver_hostname ($self, $target, $param = undef, $cb = undef, $cb_args = undef) {
Subroutine ondemand_resolver_hostname: not called CC=4 SCAR=30.0
Pod undocumented
762 0 return $self->ondemand('post', '/ondemand/resolver/hostname/single', $param, { hostname => $target }, $cb, $cb_args);
763 }
764  
765 0 ▶sub ondemand_resolver_result ($self, $scan_id, $param = undef, $cb = undef, $cb_args = undef) {
Subroutine ondemand_resolver_result: not called CC=4 SCAR=30.0
Pod undocumented
766 0 return $self->ondemand('get', '/ondemand/resolver/result/'.$scan_id, $param, undef, $cb, $cb_args);
767 }
768  
769 2 1;
770  
771 __END__
↑ Summary blib/lib/OPP/Proc/Filter.pm »