Virani/Client.pm

filter: Statementstmt 25.6% 19 / 74 Branchbran 0.0% 0 / 40 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 75.0% 6 / 8 Podpod 100% 2 / 2 totaltotal 21.7% 27 / 124 CC 21 SCAR 51.0
CC
21
Cov
17%
fetch
272.0
new
42.0
BEGIN
1.0
CRAP 164.6
« Virani.pm ↑ Summary
1 package Virani::Client;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use File::Slurp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Virani::Client - Client for remotely accessing Virani vis HTTP or HTTPS.
13  
14 =head1 VERSION
15  
16 Version 0.0.2
17  
18 =cut
19  
20 1 our $VERSION = '0.0.2';
21  
22 =head1 SYNOPSIS
23  
24 Quick summary of what the module does.
25  
26 Perhaps a little code snippet.
27  
28 use Virani::Client;
29  
30 my $virani_client = Virani::Client->new(url=>$url);
31  
32  
33 =head1 METHODS
34  
35  
36 =head2 new
37  
38 Initiates the object.
39  
40 - url :: The URL to use to contact mojo-virani by.
41 Default :: undef
42  
43 - apikey :: The API key if needed.
44 Default :: undef
45  
46 - timeout :: Timeout for fetching it in seconds.
47 Default :: 60
48  
49 - verify_hostname :: Check the cert if using HTTPS.
50 Default :: 1
51  
52 Of the above keys, only 'url' is a requirement.
53  
54 If verify_hostname is undef, the following enviromental variables are
55 checked in the following order.
56  
57 VIRANI_VERIFY_HOSTNAME
58 HTTPS_VERIFY_HOSTNAME
59 PERL_LWP_VERIFY_HOSTNAME
60  
61 =cut
62  
63 0 ▶sub new {
Subroutine new: not called CC=6 SCAR=37.4
Pod documented
64 0 my ( $blank, %opts ) = @_;
65  
66 0 ▶ if ( !defined( $opts{url} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"url"}) 0 0 0
67 0 die('No url defined');
68 }
69  
70 0 ▶ if ( !defined( $opts{verify_hostname} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"verify_hostname"}) 0 0 0
71 0 ▶ if ( defined( $ENV{VIRANI_VERIFY_HOSTNAME} ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $ENV{'VIRANI_VERIFY_HOSTNAME'}) { } 0 0 0
elsif (defined $ENV{'HTTPS_VERIFY_HOSTNAME'}) { } 0 0 0
elsif (defined $ENV{'PERL_LWP_VERIFY_HOSTNAME'}) { } 0 0 0
72 0 $opts{verify_hostname} = $ENV{VIRANI_VERIFY_HOSTNAME};
73 }
74 elsif ( defined( $ENV{HTTPS_VERIFY_HOSTNAME} ) ) {
75 0 $opts{verify_hostname} = $ENV{HTTPS_VERIFY_HOSTNAME};
76  
77 }
78 elsif ( defined( $ENV{PERL_LWP_VERIFY_HOSTNAME} ) ) {
79 0 $opts{verify_hostname} = $ENV{PERL_LWP_VERIFY_HOSTNAME};
80 }
81 else {
82 0 $opts{verify_hostname} = 1;
83 }
84 }
85  
86 my $self = {
87 apikey => $opts{apikey},
88 url => $opts{url},
89 timeout => 60,
90 verify_hostname => $opts{verify_hostname},
91 0 };
92 0 bless $self;
93  
94 0 return $self;
95 }
96  
97 =head2 fetch
98  
99 Reaches out via HTTP or HTTPS and fetches the PCAP and JSON metadata.
100  
101 - start :: A L<Time::Piece> object of when to start looking.
102 - Default :: undef
103  
104 - end :: A L<Time::Piece> object of when to stop looking.
105 - Default :: undef
106  
107 - filter :: The BPF or tshark filter to use.
108 - Default :: undef
109  
110 - set :: The PCAP set to use. Will use what ever the default is set to if undef or blank.
111 - Default :: undef
112  
113 - file :: The file to output to. The metadata writen to a file of the same name
114 with '.json' appended.
115 - Default :: out.pcap
116  
117 - type :: 'tcpdump', 'bpf2tshark', or 'tshark', depending on what one wants the filter todo.
118 If not set, the remote system uses what ever is defined as the default for that set.
119 - Default :: undef
120  
121 The following are required
122  
123 start
124 end
125 filter
126  
127 IF the command success the raw unparsed JSON of the metadata is returned.
128  
129 my $raw_metadata_json=$virani_client->(start=>$start, end=>$end, filter=>$filter);
130  
131 =cut
132  
133 0 ▶sub fetch {
Subroutine fetch: not called CC=16 SCAR=56.1
Pod documented
134 0 my ( $self, %opts ) = @_;
135  
136 0 ▶ if ( !defined( $opts{filter} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"filter"}) 0 0 0
137 0 die('Nothing specified for $opts{filter}');
138 }
139  
140 0 ▶ if ( !defined( $opts{file} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"file"}) 0 0 0
141 0 $opts{file} = 'out.pcap';
142 }
143  
144 # basic sanity checking
145 0 ▶ if ( !defined( $opts{start} ) ) {
Branch: 4 branches
BranchTrueFalseTotal
if (not defined $opts{'start'}) { } 0 0 0
elsif (not defined $opts{'end'}) { } 0 0 0
elsif (ref $opts{'start'} ne 'Time::Piece') { } 0 0 0
elsif (ref $opts{'end'} ne 'Time::Piece') { } 0 0 0
146 0 die('$opts{start} not defined');
147 }
148 elsif ( !defined( $opts{end} ) ) {
149 0 die('$opts{start} not defined');
150 }
151 elsif ( ref( $opts{start} ) ne 'Time::Piece' ) {
152 0 die('$opts{start} is not a Time::Piece object');
153 }
154 elsif ( ref( $opts{end} ) ne 'Time::Piece' ) {
155 0 die('$opts{end} is not a Time::Piece object');
156 }
157  
158 my $ua = LWP::UserAgent->new(
159 protocols_allowed => [ 'http', 'https' ],
160 timeout => $self->{timeout},
161 0 );
162 0 ▶ if ( $self->{verify_hostname} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'verify_hostname'}) { } 0 0 0
163 0 $ua->ssl_opts( verify_hostname => 1 );
164 }
165 else {
166 0 $ua->ssl_opts( verify_hostname => 0, SSL_verify_mode => 0 );
167 }
168  
169 0 $opts{filter} =~ s/\ /\%20/g;
170  
171 # put the url together
172 0 my $url = $self->{url} . '?start=' . $opts{start}->epoch . '&end=' . $opts{end}->epoch;
173 0 ▶ if (defined($opts{type})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"type"}) 0 0 0
174 0 $url = $url . '&type=' . $opts{type};
175 }
176 0 ▶ if ( defined( $self->{apikey} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"apikey"}) 0 0 0
177 0 $url = $url . '&apikey=' . $self->{apikey};
178 }
179 0 ▶ if ( defined( $opts{set} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"set"}) 0 0 0
180 0 $url = $url . '&set=' . $opts{set};
181 }
182 0 $url = $url . '&bpf=' . $opts{filter};
183  
184 # get the PCAP
185 0 my $res;
186 0 eval { $res = $ua->request( GET $url); };
187 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
188 0 die( 'Fetch failed... ' . $@ );
189 }
190 0 ▶ if ( $res->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
191 0 my $pcap = $res->decoded_content;
192 0 ▶ write_file( $opts{file}, $pcap ) || die( 'PCAP write to "' . $opts{file} . '" failed... ' . $@ );
Branch: 1 branch
BranchTrueFalseTotal
write_file($opts{"file"}, $pcap) or die 'PCAP write to "' . $opts{"file"} . '" failed... ' . $@ 0 0 0
193 }
194 else {
195 0 die( 'Fetch failed... ' . $url . ' ... ' . $res->status_line . ' ... ' . $res->decoded_content );
196 }
197  
198 # get the meta
199 0 $url = $url . '&get_meta=1';
200 0 my $raw_json;
201 0 eval { $res = $ua->request( GET $url); };
202 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
203 0 die( 'Fetch failed... ' . $@ );
204 }
205 0 ▶ if ( $res->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
206 0 $raw_json = $res->decoded_content;
207 0 print "Metadata...\n" . $raw_json;
208 }
209 else {
210 0 die( 'Fetch failed... ' . $url . ' ... ' . $res->status_line . ' ... ' . $res->decoded_content );
211 }
212  
213 0 return $raw_json;
214 }
215  
216 =head1 AUTHOR
217  
218 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
219  
220 =head1 BUGS
221  
222 Please report any bugs or feature requests to C<bug-virani at rt.cpan.org>, or through
223 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Virani>. I will be notified, and then you'll
224 automatically be notified of progress on your bug as I make changes.
225  
226  
227  
228  
229 =head1 SUPPORT
230  
231 You can find documentation for this module with the perldoc command.
232  
233 perldoc Virani::Client
234  
235  
236 You can also look for information at:
237  
238 =over 4
239  
240 =item * RT: CPAN's request tracker (report bugs here)
241  
242 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Virani>
243  
244 =item * Search CPAN
245  
246 L<https://metacpan.org/release/Virani>
247  
248 =back
249  
250  
251 =head1 ACKNOWLEDGEMENTS
252  
253  
254 =head1 LICENSE AND COPYRIGHT
255  
256 This software is Copyright (c) 2024 by Zane C. Bowers-Hadley.
257  
258 This is free software, licensed under:
259  
260 The GNU Lesser General Public License, Version 2.1, February 1999
261  
262  
263 =cut
264  
265 1 1; # End of Virani::Client
« Virani.pm ↑ Summary