blib/lib/LWP/UserAgent/Tor.pm

filter: Statementstmt 44.9% 31 / 69 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 71.4% 10 / 14 Podpod 100% 2 / 2 totaltotal 36.1% 43 / 119 CC 13 SCAR 27.7
CC
13
Cov
32%
new
42.0
_start_tor_proc
20.0
DESTROY
12.0
CRAP 15.9
↑ Summary
1 package LWP::UserAgent::Tor;
2  
3 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use LWP::Protocol::socks;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Net::EmptyPort qw(empty_port);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Proc::Background;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶use base 'LWP::UserAgent';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 our $VERSION = '0.04';
16  
17 sub new {
18 0 ▶ my ($class, %args) = @_;
Subroutine new: not called CC=6 SCAR=37.4
Pod documented
19  
20 0 ▶ my $tor_control_port = delete( $args{tor_control_port} ) // empty_port();
Condition0%0/3
delete $args{"tor_control_port"} // empty_port()
l!l&&r!l&&!r
000
MC/DC0%0/2
delete $args{"tor_control_port"} // empty_port()
delete $args{"tor_control_port"} empty_port()
Truth table0%0/3
A // B
A= delete $args{"tor_control_port"}B= empty_port()
ABresult
1X1
011
000
21 0 ▶ my $tor_port = delete( $args{tor_port} ) // do {
Condition0%0/3
delete $args{"tor_port"} // do { my $port; while (($port = empty_port()) == $tor_control_port) { (); } ; $port }
l!l&&r!l&&!r
000
MC/DC0%0/2
delete $args{"tor_port"} // do { my $port; while (($port = empty_port()) == $tor_control_port) { (); } ; $port }
delete $args{"tor_port"} do { my $port; while (($port = empty_port()) == $tor_control_port) { (); } ; $port }
Truth table0%0/3
A // B
A= delete $args{"tor_port"}B= do { my $port; while (($port = empty_port()) == $tor_control_port) { (); } ; $port }
ABresult
1X1
011
000
22 0 my $port;
23 0 ▶ while (($port = empty_port()) == $tor_control_port){};
Branch: 1 branch
BranchTrueFalseTotal
if (($port = empty_port()) == $tor_control_port) 0 0 0
24 0 $port;
25 };
26 0 ▶ my $tor_ip = delete( $args{tor_ip} ) // 'localhost';
Condition0%0/2
delete $args{"tor_ip"} // "localhost"
l!l
00
MC/DC0%0/1
delete $args{"tor_ip"} // "localhost"
delete $args{"tor_ip"}
Truth table0%0/2
A
A= delete $args{"tor_ip"}
Aresult
11
00
27 0 my $tor_cfg = delete( $args{tor_cfg} );
28  
29 0 my $self = $class->SUPER::new(%args);
30 0 $self->{_tor_proc} = _start_tor_proc($tor_ip, $tor_port, $tor_control_port, $tor_cfg);
31 0 ▶ $self->{_tor_socket} = IO::Socket::INET->new(
Condition0%0/3
"IO::Socket::INET"->new("PeerAddr", $tor_ip, "PeerPort", $tor_control_port) // croak("could not connect to tor")
l!l&&r!l&&!r
000
MC/DC0%0/2
"IO::Socket::INET"->new("PeerAddr", $tor_ip, "PeerPort", $tor_control_port) // croak("could not connect to tor")
"IO::Socket::INET"->new("PeerAddr", $tor_ip, "PeerPort", $tor_control_port) croak("could not connect to tor")
Truth table0%0/3
A // B
A= "IO::Socket::INET"->new("PeerAddr", $tor_ip, "PeerPort", $tor_control_port)B= croak("could not connect to tor")
ABresult
1X1
011
000
32 PeerAddr => $tor_ip,
33 PeerPort => $tor_control_port,
34 ) // croak 'could not connect to tor';
35  
36 0 $self->proxy( [ 'http', 'https' ], "socks://$tor_ip:$tor_port" );
37  
38 0 return bless $self, $class;
39 }
40  
41 sub DESTROY {
42 0 ▶ my ($self) = @_;
Subroutine DESTROY: not called CC=3 SCAR=24.8
43  
44 0 my $tor_proc = $self->{_tor_proc};
45 0 ▶ $tor_proc->die if defined $tor_proc;
Branch: 1 branch
BranchTrueFalseTotal
if defined $tor_proc 0 0 0
46 0 ▶ $self->SUPER::DESTROY if $self->can('SUPER::DESTROY');
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("SUPER::DESTROY") 0 0 0
47  
48 0 return;
49 }
50  
51 sub _start_tor_proc {
52 0 ▶ my ($ip, $port, $control_port, $cfg) = @_;
Subroutine _start_tor_proc: not called CC=4 SCAR=30.0
53  
54 0 my $tor_cmd = "tor --ControlListenaddress $ip:$control_port --ControlPort auto --SocksPort $port --quiet";
55 0 ▶ if (defined $cfg){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cfg) 0 0 0
56 0 ▶ croak 'tor config file does not exist' unless -e $cfg;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $cfg 0 0 0
57 0 $tor_cmd .= " -f $cfg";
58 }
59  
60 0 my $tor_proc = Proc::Background->new($tor_cmd);
61  
62 # starting tor...
63 0 sleep 1;
64  
65 0 ▶ if (!$tor_proc->alive) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tor_proc->alive) 0 0 0
66 0 croak "error running tor (probably not installed?). Run tor manually to get a hint.";
67 }
68  
69 0 return $tor_proc;
70 }
71  
72  
73 sub rotate_ip {
74 0 ▶ my ($self) = @_;
Subroutine rotate_ip: not called CC=3 SCAR=24.8
Pod documented
75  
76 0 my $socket = $self->{_tor_socket};
77 0 my $answer = q{};
78  
79 0 $socket->send("AUTHENTICATE\n");
80 0 $socket->recv($answer, 1024);
81 0 ▶ return 0 unless $answer eq "250 OK\r\n";
Branch: 1 branch
BranchTrueFalseTotal
unless $answer eq "250 OK\r\n" 0 0 0
82  
83 0 $socket->send("SIGNAL NEWNYM\n");
84 0 $socket->recv($answer, 1024);
85 0 ▶ return 0 unless $answer eq "250 OK\r\n";
Branch: 1 branch
BranchTrueFalseTotal
unless $answer eq "250 OK\r\n" 0 0 0
86  
87 0 return 1;
88 }
89  
90 2 1;
91  
92 __END__
↑ Summary