| 1 | package App::BCSSH::Proxy; | ||||||||||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use Moo; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use File::Temp (); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use IO::Select; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use IO::Socket::UNIX; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use App::BCSSH::Message; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||
| 9 | 1 | has agent_path => (is => 'ro'); | |||||||||||||||||||||||||||||||||||
| 10 | 0 | ▶ | has handlers => (is => 'ro', default => sub { { } } ); | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||
| 11 | 0 | ▶ | has umask => (is => 'ro', default => sub { 0077 } ); | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||
| 12 | 1 | has _temp_dir => (is => 'lazy', init_arg => undef); | |||||||||||||||||||||||||||||||||||
| 13 | 0 | ▶ | sub _build__temp_dir { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_build__temp_dir: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||
| 14 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||
| 15 | 0 | my $old_mask = umask($self->umask); | |||||||||||||||||||||||||||||||||||
| 16 | 0 | my $dir = File::Temp->newdir(TMPDIR => 1); | |||||||||||||||||||||||||||||||||||
| 17 | 0 | umask($old_mask); | |||||||||||||||||||||||||||||||||||
| 18 | 0 | return $dir; | |||||||||||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||||||||||
| 20 | 0 | ▶ | has socket_path => (is => 'ro', lazy => 1, default => sub { $_[0]->_temp_dir . '/agent-proxy' } ); | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||
| 21 | 0 | ▶ | has _select => (is => 'ro', default => sub { IO::Select->new }); | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||
| 22 | 1 | has server_socket => (is => 'lazy'); | |||||||||||||||||||||||||||||||||||
| 23 | 0 | ▶ | sub _build_server_socket { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_build_server_socket: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||
| 24 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||
| 25 | 0 | unlink $self->socket_path; | |||||||||||||||||||||||||||||||||||
| 26 | 0 | my $old_mask = umask($self->umask); | |||||||||||||||||||||||||||||||||||
| 27 | 0 | ▶ | my $server = IO::Socket::UNIX->new( | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 28 | Local => $self->socket_path, | ||||||||||||||||||||||||||||||||||||
| 29 | Listen => 10, | ||||||||||||||||||||||||||||||||||||
| 30 | ) or die "$!"; | ||||||||||||||||||||||||||||||||||||
| 31 | 0 | umask($old_mask); | |||||||||||||||||||||||||||||||||||
| 32 | 0 | $self->_select->add($server); | |||||||||||||||||||||||||||||||||||
| 33 | 0 | return $server; | |||||||||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||||||||||
| 35 | 0 | ▶ | has _clients => (is => 'ro', default => sub { { } }); | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||
| 37 | 0 | ▶ | sub proxy { | ||||||||||||||||||||||||||||||||||
|
Subroutine
proxy: not called CC=11 SCAR=48.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 38 | 0 | my ($self, $parent_fh) = @_; | |||||||||||||||||||||||||||||||||||
| 39 | 0 | my $done; | |||||||||||||||||||||||||||||||||||
| 40 | 0 | ▶ | local $SIG{$_} = sub { $done = 1 } for qw(HUP INT TERM QUIT); | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||
| 41 | 0 | my $select = $self->_select; | |||||||||||||||||||||||||||||||||||
| 42 | 0 | ▶ | $select->add($parent_fh) | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 43 | if $parent_fh; | ||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||
| 45 | 0 | my $server = $self->server_socket; | |||||||||||||||||||||||||||||||||||
| 46 | 0 | my $clients = $self->_clients; | |||||||||||||||||||||||||||||||||||
| 47 | 0 | ▶ | until ($done) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 48 | 0 | for my $socket ($select->can_read) { | |||||||||||||||||||||||||||||||||||
| 49 | 0 | ▶ | if ($parent_fh && $socket == $parent_fh) { | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition0%0/3
$parent_fh and $socket == $parent_fh
MC/DC0%0/2
$parent_fh and $socket == $parent_fh
$parent_fh
$socket == $parent_fh
Truth table0%0/3
A and B
A= $parent_fhB= $socket == $parent_fh
| |||||||||||||||||||||||||||||||||||||
| 50 | 0 | $done = 1; | |||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||
| 52 | elsif ($socket == $server) { | ||||||||||||||||||||||||||||||||||||
| 53 | 0 | $self->new_client($socket); | |||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||
| 55 | elsif ($socket->sysread(my $buf, 4096)) { | ||||||||||||||||||||||||||||||||||||
| 56 | 0 | $self->read_message($clients->{$socket}, $buf); | |||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||
| 58 | else { | ||||||||||||||||||||||||||||||||||||
| 59 | 0 | $self->close_client($clients->{$socket}{client}); | |||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||||
| 63 | 0 | for my $client (values %$clients) { | |||||||||||||||||||||||||||||||||||
| 64 | 0 | $self->close_client($client->{client}); | |||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||
| 66 | 0 | ▶ | $select->remove($parent_fh) | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 67 | if $parent_fh; | ||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||
| 70 | 0 | ▶ | sub read_message { | ||||||||||||||||||||||||||||||||||
|
Subroutine
read_message: not called CC=8 SCAR=42.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 71 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||
| 72 | 0 | my $client = shift; | |||||||||||||||||||||||||||||||||||
| 73 | 0 | my $buffer = shift; | |||||||||||||||||||||||||||||||||||
| 74 | 0 | ▶ | if (! $client->{filtered}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 75 | 0 | $client->{remote}->syswrite($buffer); | |||||||||||||||||||||||||||||||||||
| 76 | 0 | return; | |||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||
| 78 | 0 | $client->{buffer} .= $buffer; | |||||||||||||||||||||||||||||||||||
| 79 | 0 | my $len = $client->{message_length}; | |||||||||||||||||||||||||||||||||||
| 80 | 0 | ▶ | if (!$len && length $client->{buffer} >= 4) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $len and length $client->{"buffer"} >= 4
MC/DC0%0/2
not $len and length $client->{"buffer"} >= 4
not $len
length $client->{"buffer"} >= 4
Truth table0%0/3
not(A) and B
A= not $lenB= length $client->{"buffer"} >= 4
| |||||||||||||||||||||||||||||||||||||
| 81 | 0 | $len = $client->{message_length} = unpack 'N', substr($client->{buffer}, 0, 4, ''); | |||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | if ( $len && length $client->{buffer} >= $len ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$len and length $client->{"buffer"} >= $len
MC/DC0%0/2
$len and length $client->{"buffer"} >= $len
$len
length $client->{"buffer"} >= $len
Truth table0%0/3
A and B
A= $lenB= length $client->{"buffer"} >= $len
| |||||||||||||||||||||||||||||||||||||
| 84 | 0 | my $message = substr($client->{buffer}, 0, $len, ''); | |||||||||||||||||||||||||||||||||||
| 85 | 0 | my $type = unpack 'c', substr($message, 0, 1, ''); | |||||||||||||||||||||||||||||||||||
| 86 | 0 | delete $client->{message_length}; | |||||||||||||||||||||||||||||||||||
| 87 | 0 | ▶ | if (my $handler = $self->handlers->{$type}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||
| 88 | 0 | my $socket = $client->{client}; | |||||||||||||||||||||||||||||||||||
| 89 | 0 | ▶ | my $send = sub { | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||
| 90 | 0 | my ($type, $out_message) = @_; | |||||||||||||||||||||||||||||||||||
| 91 | 0 | my $response = make_response($type, $out_message); | |||||||||||||||||||||||||||||||||||
| 92 | 0 | $socket->syswrite($response); | |||||||||||||||||||||||||||||||||||
| 93 | 0 | }; | |||||||||||||||||||||||||||||||||||
| 94 | 0 | ▶ | try { | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||
| 95 | 0 | $handler->($message, $send, $socket); | |||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||
| 97 | 0 | ▶ | catch { | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||
| 98 | 0 | $socket->syswrite(make_response(SSH_AGENT_FAILURE)); | |||||||||||||||||||||||||||||||||||
| 99 | 0 | }; | |||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||
| 101 | elsif (my $remote = $client->{remote}) { | ||||||||||||||||||||||||||||||||||||
| 102 | 0 | $remote->syswrite(make_response($type, $message)); | |||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||
| 104 | else { | ||||||||||||||||||||||||||||||||||||
| 105 | 0 | $remote->syswrite(make_response(SSH_AGENT_FAILURE)); | |||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||
| 110 | 0 | ▶ | sub new_client { | ||||||||||||||||||||||||||||||||||
|
Subroutine
new_client: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 111 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||
| 112 | 0 | my $server = shift; | |||||||||||||||||||||||||||||||||||
| 113 | 0 | my $client = $server->accept; | |||||||||||||||||||||||||||||||||||
| 114 | 0 | my $agent_path = $self->agent_path; | |||||||||||||||||||||||||||||||||||
| 115 | 0 | ▶ | my $agent = $agent_path && IO::Socket::UNIX->new(Peer => $agent_path); | ||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$agent_path && 'IO::Socket::UNIX'->new('Peer', $agent_path)
MC/DC0%0/2
$agent_path && 'IO::Socket::UNIX'->new('Peer', $agent_path)
$agent_path
'IO::Socket::UNIX'->new('Peer', $agent_path)
Truth table0%0/3
A && B
A= $agent_pathB= 'IO::Socket::UNIX'->new('Peer', $agent_path)
| |||||||||||||||||||||||||||||||||||||
| 116 | 0 | $self->_select->add($client); | |||||||||||||||||||||||||||||||||||
| 117 | 0 | $self->_clients->{$client} = { | |||||||||||||||||||||||||||||||||||
| 118 | filtered => 1, | ||||||||||||||||||||||||||||||||||||
| 119 | buffer => '', | ||||||||||||||||||||||||||||||||||||
| 120 | client => $client, | ||||||||||||||||||||||||||||||||||||
| 121 | remote => $agent, | ||||||||||||||||||||||||||||||||||||
| 122 | }; | ||||||||||||||||||||||||||||||||||||
| 123 | 0 | ▶ | if ($agent) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 124 | 0 | $self->_select->add($agent); | |||||||||||||||||||||||||||||||||||
| 125 | 0 | $self->_clients->{$agent} = { | |||||||||||||||||||||||||||||||||||
| 126 | client => $agent, | ||||||||||||||||||||||||||||||||||||
| 127 | remote => $client, | ||||||||||||||||||||||||||||||||||||
| 128 | }; | ||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||
| 130 | 0 | return 1; | |||||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||
| 133 | 0 | ▶ | sub close_client { | ||||||||||||||||||||||||||||||||||
|
Subroutine
close_client: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 134 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||
| 135 | 0 | my $socket = shift; | |||||||||||||||||||||||||||||||||||
| 136 | 0 | $self->_select->remove($socket); | |||||||||||||||||||||||||||||||||||
| 137 | 0 | $socket->close; | |||||||||||||||||||||||||||||||||||
| 138 | 0 | my $client = delete $self->_clients->{$socket}; | |||||||||||||||||||||||||||||||||||
| 139 | 0 | ▶ | if ($client && $client->{remote}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$client and $client->{"remote"}
MC/DC0%0/2
$client and $client->{"remote"}
$client
$client->{"remote"}
Truth table0%0/3
A and B
A= $clientB= $client->{"remote"}
| |||||||||||||||||||||||||||||||||||||
| 140 | 0 | $self->close_client($client->{remote}); | |||||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||
| 144 | 1 | 1; | |||||||||||||||||||||||||||||||||||
| 145 | __END__ | ||||||||||||||||||||||||||||||||||||