blib/lib/LWP/UserAgent/Tor.pm

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