Footprintless/Tunnel.pm

filter: Statementstmt 32.1% 37 / 115 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 48 MC/DCmcdc 0.0% 0 / 27 Subroutinesub 52.3% 11 / 21 Podpod 100% 5 / 5 totaltotal 21.9% 53 / 242 CC 31 SCAR 46.5
CC
31
Cov
20%
_init
272.0
open
90.0
_build_command
20.0
CRAP 104.6
↑ Summary Footprintless/App/Command/config.pm »
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Footprintless::Tunnel;
5 1 $Footprintless::Tunnel::VERSION = '1.29';
6 # ABSTRACT: Provides tunneling over ssh
7 # PODNAME: Footprintless::Tunnel
8  
9 1 ▶use parent qw(Footprintless::MixableBase);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use File::Path qw(make_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Footprintless::Mixins qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
16 _entity
17 1 );
18 1 ▶use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Log::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use POSIX ":sys_wait_h";
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 1 my $logger = Log::Any->get_logger();
23  
24 1 my $number = 0;
25  
26 0 ▶sub _build_command {
Subroutine _build_command: not called CC=4 SCAR=30.0
27 0 my ( $self, $command ) = @_;
28  
29 0 my @command = ( $self->{ssh}, ' -S ', $self->{control_socket} );
30  
31 0 ▶ if ( $command eq 'open' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($command eq 'open') { } 0 0 0
32 0 push( @command, ' -nfN -oControlMaster=yes -L ' );
33 0 ▶ if ( $self->{local_hostname} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"local_hostname"}) 0 0 0
34 0 push( @command, $self->{local_hostname}, ':' );
35 }
36 push( @command,
37 $self->{local_port}, ':', $self->{destination_hostname},
38 0 ':', $self->{destination_port} );
39 }
40 else {
41 0 push( @command, ' -O ', $command );
42 }
43  
44 0 push( @command, ' ' );
45 0 ▶ if ( $self->{tunnel_username} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"tunnel_username"}) 0 0 0
46 0 push( @command, $self->{tunnel_username}, '@' );
47 }
48 0 push( @command, $self->{tunnel_hostname}, ' 2> /dev/null' );
49  
50 0 return join( '', @command );
51 }
52  
53 0 ▶sub close {
Subroutine close: not called CC=3 SCAR=24.8
Pod documented
54 0 my ($self) = @_;
55  
56 0 ▶ if ( $self->{pid} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"pid"}) 0 0 0
57 0 my $command = $self->_build_command('exit');
58 0 $logger->tracef( 'closing tunnel with: `%s`', $command );
59 0 `$command`;
60 0 my $child = waitpid( $self->{pid}, WNOHANG );
61 0 $logger->debugf( 'forked child closed: %s', $child );
62 0 delete( $self->{control_socket} );
63 0 delete( $self->{pid} );
64 0 ▶ if ( $self->{dynamic_local_port} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"dynamic_local_port"}) 0 0 0
65 0 delete( $self->{local_port} );
66 0 delete( $self->{dynamic_local_port} );
67 }
68 }
69 }
70  
71 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=1 SCAR=6.9
72 0 $_[0]->close();
73 }
74  
75 0 ▶sub _find_port {
Subroutine _find_port: not called CC=1 SCAR=6.9
76  
77 # results in slight race condition, but for now, its ok.
78 0 my $sock = IO::Socket::INET->new(
79 Proto => 'tcp',
80 LocalPort => 0,
81 LocalAddr => 'localhost'
82 );
83 0 my $port = $sock->sockport();
84 0 $sock->close();
85 0 return $port;
86 }
87  
88 0 ▶sub get_local_hostname {
Subroutine get_local_hostname: not called CC=1 SCAR=6.9
Pod documented
89 0 return $_[0]->{local_hostname};
90 }
91  
92 0 ▶sub get_local_port {
Subroutine get_local_port: not called CC=1 SCAR=6.9
Pod documented
93 0 return $_[0]->{local_port};
94 }
95  
96 0 ▶sub _init {
Subroutine _init: not called CC=16 SCAR=56.1
97 0 my ( $self, %options ) = @_;
98  
99 0 my $entity = $self->_entity( $self->{coordinate} );
100 0 ▶ $self->{ssh} = $options{ssh} || $entity->{ssh} || 'ssh -q';
Condition0%0/5
$options{'ssh'} || $entity->{'ssh'} || 'ssh -q'
l!l
00
$options{"ssh"} or $entity->{"ssh"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$options{'ssh'} || $entity->{'ssh'} || 'ssh -q'
$options{"ssh"} $entity->{"ssh"}
Truth table0%0/3
A or B
A= $options{"ssh"}B= $entity->{"ssh"}
ABresult
1X1
011
000
101 $self->{local_hostname} = $options{local_hostname}
102 0 ▶ || $entity->{local_hostname};
Condition0%0/3
$options{'local_hostname'} || $entity->{'local_hostname'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$options{'local_hostname'} || $entity->{'local_hostname'}
$options{'local_hostname'} $entity->{'local_hostname'}
Truth table0%0/3
A || B
A= $options{'local_hostname'}B= $entity->{'local_hostname'}
ABresult
1X1
011
000
103 0 ▶ $self->{local_port} = $options{local_port} || $entity->{local_port};
Condition0%0/3
$options{'local_port'} || $entity->{'local_port'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$options{'local_port'} || $entity->{'local_port'}
$options{'local_port'} $entity->{'local_port'}
Truth table0%0/3
A || B
A= $options{'local_port'}B= $entity->{'local_port'}
ABresult
1X1
011
000
104 $self->{tunnel_hostname} = $options{tunnel_hostname}
105 0 ▶ || $entity->{tunnel_hostname};
Condition0%0/3
$options{'tunnel_hostname'} || $entity->{'tunnel_hostname'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$options{'tunnel_hostname'} || $entity->{'tunnel_hostname'}
$options{'tunnel_hostname'} $entity->{'tunnel_hostname'}
Truth table0%0/3
A || B
A= $options{'tunnel_hostname'}B= $entity->{'tunnel_hostname'}
ABresult
1X1
011
000
106 $self->{tunnel_username} = $options{tunnel_username}
107 0 ▶ || $entity->{tunnel_username};
Condition0%0/3
$options{'tunnel_username'} || $entity->{'tunnel_username'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$options{'tunnel_username'} || $entity->{'tunnel_username'}
$options{'tunnel_username'} $entity->{'tunnel_username'}
Truth table0%0/3
A || B
A= $options{'tunnel_username'}B= $entity->{'tunnel_username'}
ABresult
1X1
011
000
108 $self->{destination_hostname} = $options{destination_hostname}
109 0 ▶ || $entity->{destination_hostname};
Condition0%0/3
$options{'destination_hostname'} || $entity->{'destination_hostname'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$options{'destination_hostname'} || $entity->{'destination_hostname'}
$options{'destination_hostname'} $entity->{'destination_hostname'}
Truth table0%0/3
A || B
A= $options{'destination_hostname'}B= $entity->{'destination_hostname'}
ABresult
1X1
011
000
110 $self->{destination_port} = $options{destination_port}
111 0 ▶ || $entity->{destination_port};
Condition0%0/3
$options{'destination_port'} || $entity->{'destination_port'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$options{'destination_port'} || $entity->{'destination_port'}
$options{'destination_port'} $entity->{'destination_port'}
Truth table0%0/3
A || B
A= $options{'destination_port'}B= $entity->{'destination_port'}
ABresult
1X1
011
000
112 $self->{control_socket_dir} =
113 $options{control_socket_dir}
114 || $entity->{control_socket_dir}
115 0 ▶ || File::Spec->catdir( ( $ENV{HOME} ? $ENV{HOME} : $ENV{USERPROFILE} ),
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'HOME'} ? : 0 0 0
Condition0%0/6
$options{'control_socket_dir'} || $entity->{'control_socket_dir'} || 'File::Spec'->catdir($ENV{'HOME'} ? $ENV{'HOME'} : $ENV{'USERPROFILE'}, '.ssh', 'control_socket')
l!l&&r!l&&!r
000
$options{"control_socket_dir"} or $entity->{"control_socket_dir"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$options{'control_socket_dir'} || $entity->{'control_socket_dir'} || 'File::Spec'->catdir($ENV{'HOME'} ? $ENV{'HOME'} : $ENV{'USERPROFILE'}, '.ssh', 'control_socket')
$options{"control_socket_dir"} $entity->{"control_socket_dir"} 'File::Spec'->catdir($ENV{'HOME'} ? $ENV{'HOME'} : $ENV{'USERPROFILE'}, '.ssh', 'control_socket')
Truth table0%0/4
A or B || C
A= $options{"control_socket_dir"}B= $entity->{"control_socket_dir"}C= 'File::Spec'->catdir($ENV{'HOME'} ? $ENV{'HOME'} : $ENV{'USERPROFILE'}, '.ssh', 'control_socket')
ABCresult
1XX1
01X1
0011
0000
116 '.ssh', 'control_socket' );
117 0 ▶ $self->{tries} = $options{tries} || $entity->{tries} || 10;
Condition0%0/5
$options{'tries'} || $entity->{'tries'} || 10
l!l
00
$options{"tries"} or $entity->{"tries"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$options{'tries'} || $entity->{'tries'} || 10
$options{"tries"} $entity->{"tries"}
Truth table0%0/3
A or B
A= $options{"tries"}B= $entity->{"tries"}
ABresult
1X1
011
000
118 $self->{wait_seconds} =
119 $options{wait_seconds}
120 || $entity->{wait_seconds}
121 0 ▶ || 1;
Condition0%0/5
$options{'wait_seconds'} || $entity->{'wait_seconds'} || 1
l!l
00
$options{"wait_seconds"} or $entity->{"wait_seconds"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$options{'wait_seconds'} || $entity->{'wait_seconds'} || 1
$options{"wait_seconds"} $entity->{"wait_seconds"}
Truth table0%0/3
A or B
A= $options{"wait_seconds"}B= $entity->{"wait_seconds"}
ABresult
1X1
011
000
122  
123 0 return $self;
124 }
125  
126 0 ▶sub is_open {
Subroutine is_open: not called CC=3 SCAR=24.8
Pod documented
127 0 my ($self) = @_;
128  
129 0 ▶ if ( !$self->{control_socket} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"control_socket"}) 0 0 0
130 0 return 0;
131 }
132  
133 0 my $command = $self->_build_command('check');
134 0 $logger->tracef( 'checking tunnel with: `%s`', $command );
135 0 `$command`;
136 0 ▶ return ( WIFEXITED( ${^CHILD_ERROR_NATIVE} ) && WEXITSTATUS( ${^CHILD_ERROR_NATIVE} ) == 0 );
Condition0%0/3
WIFEXITED(${^CHILD_ERROR_NATIVE}) && WEXITSTATUS(${^CHILD_ERROR_NATIVE}) == 0
!ll&&!rl&&r
000
MC/DC0%0/2
WIFEXITED(${^CHILD_ERROR_NATIVE}) && WEXITSTATUS(${^CHILD_ERROR_NATIVE}) == 0
WIFEXITED(${^CHILD_ERROR_NATIVE}) WEXITSTATUS(${^CHILD_ERROR_NATIVE}) == 0
Truth table0%0/3
A && B
A= WIFEXITED(${^CHILD_ERROR_NATIVE})B= WEXITSTATUS(${^CHILD_ERROR_NATIVE}) == 0
ABresult
0X0
100
111
137 }
138  
139 0 ▶sub open {
Subroutine open: not called CC=9 SCAR=45.0
Pod documented
140 0 my ( $self, %options ) = @_;
141  
142 0 ▶ if ( !$self->{local_port} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"local_port"}) 0 0 0
143 0 $self->{local_port} = $self->_find_port();
144 0 $self->{dynamic_local_port} = 1;
145 }
146 0 $self->{control_socket} = $self->_temp_control_socket();
147 0 $self->{pid} = fork();
148 0 ▶ croak("too few resources to open tunnel") if ( !defined( $self->{pid} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"pid"} 0 0 0
149  
150 0 ▶ if ( $self->{pid} == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"pid"} == 0) 0 0 0
151 0 my $command = $self->_build_command('open');
152 0 $logger->debugf( 'opening tunnel with: `%s`', $command );
153 0 exec($command);
154 0 exit(0);
155 }
156  
157 0 my $open = 0;
158 0 ▶ my $remaining_tries = $options{tries} || $self->{tries};
Condition0%0/3
$options{'tries'} || $self->{'tries'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$options{'tries'} || $self->{'tries'}
$options{'tries'} $self->{'tries'}
Truth table0%0/3
A || B
A= $options{'tries'}B= $self->{'tries'}
ABresult
1X1
011
000
159 0 ▶ my $wait_seconds = $options{wait_seconds} || $self->{wait_seconds};
Condition0%0/3
$options{'wait_seconds'} || $self->{'wait_seconds'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$options{'wait_seconds'} || $self->{'wait_seconds'}
$options{'wait_seconds'} $self->{'wait_seconds'}
Truth table0%0/3
A || B
A= $options{'wait_seconds'}B= $self->{'wait_seconds'}
ABresult
1X1
011
000
160 0 ▶ while ( $remaining_tries-- > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remaining_tries-- > 0) 0 0 0
161 0 ▶ if ( $self->is_open() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_open) 0 0 0
162 0 $open = 1;
163 0 last;
164 }
165 0 $logger->tracef( 'not yet open, %s tries remaining. sleeping...', $remaining_tries );
166 0 sleep($wait_seconds);
167 }
168  
169 0 ▶ croak('failed to open tunnel') if ( !$open );
Branch: 1 branch
BranchTrueFalseTotal
unless $open 0 0 0
170  
171 0 $logger->debug('tunnel open');
172 }
173  
174 0 ▶sub _temp_control_socket {
Subroutine _temp_control_socket: not called CC=1 SCAR=6.9
175 0 my ($self) = shift;
176  
177 0 make_path( $self->{control_socket_dir} );
178 0 return File::Spec->catfile( $self->{control_socket_dir}, $$ . '_' . $number++ );
179 }
180  
181 1 1;
182  
183 __END__
↑ Summary Footprintless/App/Command/config.pm »