blib/lib/Vayne/SSH/Tunnel.pm

filter: Statementstmt 37.8% 28 / 74 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 22 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 69.2% 9 / 13 Podpod 0.0% 0 / 1 totaltotal 25.6% 37 / 144 CC 20 SCAR 52.7
CC
20
Cov
23%
Run
342.0
_fmt_ssh
12.0
__ANON__
2.0
CRAP 193.9
↑ Summary
1 package Vayne::SSH::Tunnel;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use 5.008_005;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = '0.01';
7  
8 1 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use YAML::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Sys::Hostname;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use FindBin qw($Bin);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Fcntl qw( :flock );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Net::EmptyPort qw(empty_port);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 sub Run
17 0 ▶{
Subroutine Run: not called CC=18 SCAR=58.3
Pod undocumented
18 0 my(%option, $cmd, $lport) = @_;
19  
20 0 $lport = empty_port();
21 0 $cmd = _fmt_ssh(%option, lport => $lport );
22  
23 0 ▶ die "can not find user $option{user}" unless my(undef, undef, $uid, $gid) = getpwnam $option{user};
Branch: 1 branch
BranchTrueFalseTotal
unless my(undef, undef, $uid, $gid) = getpwnam $option{"user"} 0 0 0
24  
25 0 POSIX::setgid($gid);
26 0 POSIX::setuid($uid);
27 0 print $cmd, "\n";
28  
29 #fork
30 0 setpgrp(0,0);
31 0 ▶ $SIG{TERM} = $SIG{HUP} = sub{ print "Sig recv, killed!\n"; kill 'KILL', -$$};
Subroutine __ANON__: not called CC=1 SCAR=6.9
32 0 ▶ $SIG{ALRM} = sub{ print "Alarm timeout, killed!\n"; kill 'KILL', -$$};
Subroutine __ANON__: not called CC=1 SCAR=6.9
33  
34 0 ▶ my($pid, %grep, $lock_fh) = open my $fh, "$cmd|" or die "can't fork cmd $cmd";
Branch: 1 branch
BranchTrueFalseTotal
() = open(my $fh, "$cmd|") or die "can't fork cmd $cmd" 0 0 0
35  
36 0 ▶ alarm $option{timeout} if $option{timeout};
Branch: 1 branch
BranchTrueFalseTotal
if $option{"timeout"} 0 0 0
37  
38 #read
39 0 $|++;
40
41 0 ▶ READ: while(my $line = <$fh>)
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 0 0 0
42 {
43 0 alarm 0;
44 0 print $line;
45  
46 #write conf
47 0 ▶ if( $line =~ /Entering interactive session/ and $option{confdir} )
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /Entering interactive session/ and $option{"confdir"}) 0 0 0
Condition0%0/3
$line =~ /Entering interactive session/ and $option{"confdir"}
!ll&&!rl&&r
000
MC/DC0%0/2
$line =~ /Entering interactive session/ and $option{"confdir"}
$line =~ /Entering interactive session/ $option{"confdir"}
Truth table0%0/3
A and B
A= $line =~ /Entering interactive session/B= $option{"confdir"}
ABresult
0X0
100
111
48 {
49 0 my $path = File::Spec->join($option{confdir}, $option{name}. ".". $$);
50  
51 0 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless (not defined $lock_fh and open $lock_fh, ">", $path and flock $lock_fh, 6) 0 0 0
Condition0%0/6
not defined $lock_fh and open $lock_fh, ">", $path and flock $lock_fh, 6
!ll&&!rl&&r
000
not defined $lock_fh and open $lock_fh, ">", $path
!ll&&!rl&&r
000
MC/DC0%0/3
not defined $lock_fh and open $lock_fh, ">", $path and flock $lock_fh, 6
not defined $lock_fh open $lock_fh, ">", $path flock $lock_fh, 6
Truth table0%0/4
not(A) and B and C
A= not defined $lock_fhB= open $lock_fh, ">", $pathC= flock $lock_fh, 6
ABCresult
0XX0
10X0
1100
1111
52 not defined $lock_fh
53 and open $lock_fh, '>', $path
54 and flock $lock_fh, LOCK_EX | LOCK_NB
55 )
56 {
57 0 warn "can not open $path";
58 0 last READ;
59 }
60  
61 0 print $lock_fh YAML::XS::Dump { $option{title} => "127.0.0.1:$lport" };
62 0 $lock_fh->flush;
63
64
65 }
66  
67 0 ▶ print "channel open reach $option{max_channel}!\n" and last if $line =~ /channel (\d+):/ && $1 >= $option{max_channel};
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /channel (\d+):/ and $1 >= $option{"max_channel"} 0 0 0
Condition0%0/8
$line =~ /channel (\d+):/ && $1 >= $option{'max_channel'} && (print("channel open reach $option{'max_channel'}!\n") && (last))
!ll&&!rl&&r
000
$line =~ /channel (\d+):/ and $1 >= $option{"max_channel"}
!ll&&!rl&&r
000
print qq[channel open reach $option{"max_channel"}!\n] and last
!ll
00
MC/DC0%0/3
$line =~ /channel (\d+):/ && $1 >= $option{'max_channel'} && (print("channel open reach $option{'max_channel'}!\n") && (last))
$line =~ /channel (\d+):/ $1 >= $option{"max_channel"} print qq[channel open reach $option{"max_channel"}!\n]
Truth table0%0/4
A and B && C
A= $line =~ /channel (\d+):/B= $1 >= $option{"max_channel"}C= print qq[channel open reach $option{"max_channel"}!\n]
ABCresult
0XX0
10X0
1100
1111
68  
69 0 ▶ while(my($word, $times) = each %{ $option{grep_word} })
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$option{"grep_word"};}) 0 0 0
70 {
71 0 ▶ next unless $line =~ /$word/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /$word/ 0 0 0
72 0 $grep{$word} += 1;
73 0 ▶ print "match '$word' reach $times!\n" and last READ if $grep{$word} >= $times;
Branch: 1 branch
BranchTrueFalseTotal
if $grep{$word} >= $times 0 0 0
Condition0%0/5
$grep{$word} >= $times && (print("match '${word}' reach $times!\n") && (last READ))
!ll&&!rl&&r
000
print "match '${word}' reach $times!\n" and last READ
!ll
00
MC/DC0%0/2
$grep{$word} >= $times && (print("match '${word}' reach $times!\n") && (last READ))
$grep{$word} >= $times print "match '${word}' reach $times!\n"
Truth table0%0/3
A && B
A= $grep{$word} >= $timesB= print "match '${word}' reach $times!\n"
ABresult
0X0
100
111
74 }
75  
76 0 ▶ alarm $option{timeout} if $option{timeout};
Branch: 1 branch
BranchTrueFalseTotal
if $option{"timeout"} 0 0 0
77 }
78  
79 0 kill 'KILL', -$$;
80 }
81  
82  
83 #ssh -tt -L 127.0.0.1:7920:127.0.0.1:7920 foo@ser1.net ssh -v -o 'ServerAliveInterval=10' -o 'ServerAliveCountMax=3' -tt -L localhost:7920:127.0.0.1:7920 ser2.net ssh -v -o 'ServerAliveInterval=10' -o 'ServerAliveCountMax=1' -N -L localhost:7920:127.0.0.1:1080 foo@ser3.net 2>&1
84 sub _fmt_ssh
85 0 ▶{
Subroutine _fmt_ssh: not called CC=3 SCAR=24.8
86 0 my(%opt, @way, $option, $cmd, $hostname) = @_;
87 0 @way = @{ $opt{way} };
88 0 $option = join ' ', map{ "-o '$_'" }@{$opt{ option }};
89  
90 0 $hostname = hostname;
91 0 ▶ shift @way if $way[0] =~ /\@$hostname$/;
Branch: 1 branch
BranchTrueFalseTotal
if $way[0] =~ /\@$hostname$/ 0 0 0
92  
93 0 for(splice @way, 0, -1)
94 {
95 0 $cmd .= sprintf "ssh -tt %s -L 127.0.0.1:%s:127.0.0.1:%s %s ", $option, $opt{lport}, $opt{lport}, $_;
96 }
97  
98 0 $cmd .= sprintf "ssh -v %s -N -L 127.0.0.1:%s:127.0.0.1:%s %s 2>&1", $option, $opt{lport}, $opt{dport}, $way[-1];
99 }
100  
101 1 1;
102 __END__
↑ Summary