blib/lib/LWP/UserAgent/Tor.pm

filter: Statementstmt 40.6% 37 / 91 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 75.0% 12 / 16 Podpod 100% 2 / 2 totaltotal 31.8% 51 / 160 SCAR 51.4
CC
20
Cov
28%
_start_tor_proc
132.0
new
42.0
DESTROY
12.0
CRAP 170.4
↑ 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 File::MMagic;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use File::Which qw(which);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use LWP::Protocol::socks;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Net::EmptyPort qw(empty_port);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Proc::Background;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 ▶use base 'LWP::UserAgent';
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 our $VERSION = '0.05';
18  
19 sub new {
20 0 ▶ my ($class, %args) = @_;
Subroutine new: not called CC=6 SCAR=37.4
Pod documented
21  
22 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
23 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
24 0 my $port;
25 0 ▶ while (($port = empty_port()) == $tor_control_port){};
Branch: 1 branch
BranchTrueFalseTotal
if (($port = empty_port()) == $tor_control_port) 0 0 0
26 0 $port;
27 };
28 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
29 0 my $tor_cfg = delete( $args{tor_cfg} );
30  
31 0 my $self = $class->SUPER::new(%args);
32 0 $self->{_tor_proc} = _start_tor_proc($tor_ip, $tor_port, $tor_control_port, $tor_cfg);
33 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
34 PeerAddr => $tor_ip,
35 PeerPort => $tor_control_port,
36 ) // croak 'could not connect to tor';
37  
38 0 $self->proxy( [ 'http', 'https' ], "socks://$tor_ip:$tor_port" );
39  
40 0 return bless $self, $class;
41 }
42  
43 sub DESTROY {
44 0 ▶ my ($self) = @_;
Subroutine DESTROY: not called CC=3 SCAR=24.8
45  
46 0 my $tor_proc = $self->{_tor_proc};
47 0 ▶ $tor_proc->die if defined $tor_proc;
Branch: 1 branch
BranchTrueFalseTotal
if defined $tor_proc 0 0 0
48 0 ▶ $self->SUPER::DESTROY if $self->can('SUPER::DESTROY');
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("SUPER::DESTROY") 0 0 0
49  
50 0 return;
51 }
52  
53 sub _start_tor_proc {
54 0 ▶ my ($ip, $port, $control_port, $cfg) = @_;
Subroutine _start_tor_proc: not called CC=11 SCAR=48.8
55  
56 # There must be a Tor binary in $PATH; it might be named "tor.real".
57 0 my $tor = which 'tor';
58 0 ▶ defined $tor or croak 'could not find tor binary in $PATH';
Branch: 1 branch
BranchTrueFalseTotal
defined $tor or croak("could not find tor binary in \$PATH") 0 0 0
59 0 my $tor_real = which 'tor.real';
60  
61 0 my $mm = File::MMagic->new;
62 0 my $file_format = $mm->checktype_filename($tor);
63  
64 0 my $binary_format = 'application/octet-stream';
65  
66 0 ▶ if ($file_format eq $binary_format) {
Branch: 3 branches
BranchTrueFalseTotal
if ($file_format eq $binary_format) { } 0 0 0
elsif ($file_format =~ /sh script text$/ and defined $tor_real) { } 0 0 0
elsif ($file_format =~ m[^x-system/x-error; ]) { } 0 0 0
67 # tor is a binary; do nothing.
68 }
69 ▶ elsif ($file_format =~ m/sh script text$/ && defined $tor_real) {
Condition0%0/3
$file_format =~ /sh script text$/ and defined $tor_real
!ll&&!rl&&r
000
MC/DC0%0/2
$file_format =~ /sh script text$/ and defined $tor_real
$file_format =~ /sh script text$/ defined $tor_real
Truth table0%0/3
A and B
A= $file_format =~ /sh script text$/B= defined $tor_real
ABresult
0X0
100
111
70 # tor is a shell script; it could be from a Tor Browser distribution.
71 0 $file_format = $mm->checktype_filename($tor_real);
72 0 ▶ if ($file_format eq $binary_format) {
Branch: 2 branches
BranchTrueFalseTotal
if ($file_format eq $binary_format) { } 0 0 0
elsif ($file_format =~ m[^x-system/x-error; ]) { } 0 0 0
73 # tor.real is the corresponding Tor binary.
74 0 $tor = $tor_real;
75 }
76 elsif ($file_format =~ m|^x-system/x-error; |) {
77 0 $file_format =~ s|^x-system/x-error; ||;
78 0 croak 'tor.real file format error detected: "' . $file_format . '"';
79 }
80 else {
81 0 croak 'could not find matching tor binary for tor shell script';
82 }
83 }
84 elsif ($file_format =~ m|^x-system/x-error; |) {
85 0 $file_format =~ s|^x-system/x-error; ||;
86 0 croak 'tor file format error detected: "' . $file_format . '"';
87 }
88 else {
89 0 croak 'could not work with tor file format "' . $file_format . '"';
90 }
91  
92 0 my $tor_cmd = "$tor --ControlListenaddress $ip:$control_port --ControlPort auto --SocksPort $port --quiet";
93 0 ▶ if (defined $cfg){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cfg) 0 0 0
94 0 ▶ croak 'tor config file does not exist' unless -e $cfg;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $cfg 0 0 0
95 0 $tor_cmd .= " -f $cfg";
96 }
97  
98 0 my $tor_proc = Proc::Background->new($tor_cmd);
99  
100 # starting tor...
101 0 sleep 1;
102  
103 0 ▶ if (!$tor_proc->alive) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tor_proc->alive) 0 0 0
104 0 croak "error running tor. Run tor manually to get a hint.";
105 }
106  
107 0 return $tor_proc;
108 }
109  
110  
111 sub rotate_ip {
112 0 ▶ my ($self) = @_;
Subroutine rotate_ip: not called CC=3 SCAR=24.8
Pod documented
113  
114 0 my $socket = $self->{_tor_socket};
115 0 my $answer = q{};
116  
117 0 $socket->send("AUTHENTICATE\n");
118 0 $socket->recv($answer, 1024);
119 0 ▶ return 0 unless $answer eq "250 OK\r\n";
Branch: 1 branch
BranchTrueFalseTotal
unless $answer eq "250 OK\r\n" 0 0 0
120  
121 0 $socket->send("SIGNAL NEWNYM\n");
122 0 $socket->recv($answer, 1024);
123 0 ▶ return 0 unless $answer eq "250 OK\r\n";
Branch: 1 branch
BranchTrueFalseTotal
unless $answer eq "250 OK\r\n" 0 0 0
124  
125 0 return 1;
126 }
127  
128 2 1;
129  
130 __END__
↑ Summary