| 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()
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()
| |||||||||||||||||||||
| 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
}
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
}
| |||||||||||||||||||||
| 27 | 0 | my $port; | |||||||||||||||||||
| 28 | 0 | ▶ | while (($port = empty_port()) == $tor_control_port){}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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")
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)
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 50 | 0 | $_tor_data_dir->remove_tree; | |||||||||||||||||||
| 51 | 0 | ▶ | $self->SUPER::DESTROY if $self->can('SUPER::DESTROY'); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 102 | 0 | ▶ | croak 'tor config file does not exist' unless -e $cfg; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | 0 | return 1; | |||||||||||||||||||
| 134 | } | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | 2 | 1; | |||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | __END__ | ||||||||||||||||||||