blib/lib/WG/Status.pm

filter: Statementstmt 17.5% 13 / 74 Branchbran 0.0% 0 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 42.8% 3 / 7 Podpod 0.0% 0 / 4 totaltotal 14.9% 16 / 107 CC 16 SCAR 36.9
CC
16
Cov
14%
parse_peer
72.0
wg_status
30.0
parse_interface
30.0
CRAP 40.0
↑ Summary
1 package WG::Status;
2  
3 1 ▶use 5.022002;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7  
8 1 our $VERSION = '0.04';
9  
10 1 require Exporter;
11 1 our @ISA = qw(Exporter);
12 1 our @EXPORT_OK = qw(wg_status kernel_module);
13  
14  
15 # our possible peer attributes: not every peer will have all attributes
16 #my @attr = ('peer',
17 # 'endpoint',
18 # 'allowed ips',
19 # 'latest handshake',
20 # 'transfer',
21 # 'persistent keepalive'
22 # );
23  
24  
25  
26 0 ▶sub wg_status {
Subroutine wg_status: not called CC=5 SCAR=34.0
Pod undocumented
27 0 my $WG = shift;
28  
29 0 open (my $showall, "$WG show all |");
30 0 local $/ = undef;
31 0 my $show = <$showall>;
32 0 close $showall;
33  
34 0 my @interface = split /interface: /, $show;
35 0 shift @interface; # lose the first "entry", since it's bogus
36  
37 0 my $wg; # our data structure
38  
39 my $ifcount;
40 0 foreach my $if (@interface) {
41 0 $ifcount++;
42 0 my @stanza = split /peer: /, $if;
43 0 my $peercount = -1;
44 0 my $ifdef;
45 my @peers;
46 0 foreach my $peer (@stanza) {
47 0 $peercount++;
48 0 my $peerdef;
49 0 ▶ if ($peercount) {
Branch: 1 branch
BranchTrueFalseTotal
if ($peercount) { } 0 0 0
50 0 $peerdef = parse_peer($peer);
51 } else {
52 # special case (not a peer definition, but an interface def)
53 0 $ifdef = parse_interface($peer);
54 }
55 0 ▶ push @peers, $peerdef if defined $peerdef;
Branch: 1 branch
BranchTrueFalseTotal
if defined $peerdef 0 0 0
56 0 $$ifdef{peers} = \@peers;
57 }
58 0 push @{$wg}, $ifdef;
59 }
60  
61 0 return $wg;
62 }
63  
64  
65  
66 0 ▶sub kernel_module {
Subroutine kernel_module: not called CC=1 SCAR=6.9
Pod undocumented
67 # probably need to look at this once we get to kernel version 5.6
68 0 open (my $grepmod, "lsmod | grep -c wireguard |");
69 0 my $lsmod = <$grepmod>;
70 0 close $grepmod;
71 0 chomp $lsmod;
72  
73 0 return $lsmod;
74 }
75  
76  
77 0 ▶sub parse_interface {
Subroutine parse_interface: not called CC=5 SCAR=34.0
Pod undocumented
78 0 my $interface = shift;
79  
80 0 my @line = split /\n/, $interface;
81 0 my %ifdef;
82 0 foreach my $line (@line) {
83 0 $line =~ s/^\ +//g;
84 0 my @parts = split / /, $line;
85 0 ▶ if ($line =~ /^wg\d+/) { $ifdef{interface} = $line }
Branch: 3 branches
BranchTrueFalseTotal
if ($line =~ /^wg\d+/u) { } 0 0 0
elsif ($line =~ /^public\ key:/u) { } 0 0 0
elsif ($line =~ /^listening port:/u) { } 0 0 0
86 0 elsif ($line =~ /^public\ key:/) { $ifdef{'public key'} = $parts[2] }
87 0 elsif ($line =~ /^listening port:/) { $ifdef{'listening port'} = $parts[2] }
88 }
89  
90 0 return \%ifdef;
91 }
92  
93  
94  
95 0 ▶sub parse_peer {
Subroutine parse_peer: not called CC=8 SCAR=42.8
Pod undocumented
96 0 my $peer = shift;
97  
98 0 my @line = split /\n/, $peer;
99 0 my %peerdef;
100 0 foreach my $line (@line) {
101 0 $line =~ s/^\ +//g;
102 0 my @parts = split / /, $line;
103 0 ▶ if ($line =~ /^.{43,}$/) { $peerdef{'peer'} = $line }
Branch: 6 branches
BranchTrueFalseTotal
if ($line =~ /^.{43,}$/u) { } 0 0 0
elsif ($line =~ /^endpoint:/u) { } 0 0 0
elsif ($line =~ /^allowed ips:/u) { } 0 0 0
elsif ($line =~ /^latest\ handshake:/u) { } 0 0 0
elsif ($line =~ /^transfer:/u) { } 0 0 0
elsif ($line =~ /^persistent\ keepalive:/u) { } 0 0 0
104  
105 0 elsif ($line =~ /^endpoint:/) { $peerdef{'endpoint'} = $parts[1] }
106  
107 0 elsif ($line =~ /^allowed ips:/) { shift @parts;
108 0 shift @parts;
109 0 $peerdef{'allowed ips'} = join ' ', @parts;
110 }
111  
112 0 elsif ($line =~ /^latest\ handshake:/) { shift @parts;
113 0 shift @parts;
114 0 $peerdef{'latest handshake'} = join ' ', @parts;
115 }
116  
117 0 elsif ($line =~ /^transfer:/) { shift @parts;
118 0 $peerdef{'transfer'} = join ' ', @parts;
119 }
120  
121 0 elsif ($line =~ /^persistent\ keepalive:/) { shift @parts;
122 0 shift @parts;
123 0 $peerdef{'persistent keepalive'} = join ' ', @parts;
124 }
125 }
126  
127 0 return \%peerdef;
128 }
129  
130  
131  
132 1 1;
133  
134 __END__
↑ Summary