File Coverage

blib/lib/WG/Status.pm
Criterion Covered Total %
statement 8 69 11.5
branch 0 22 0.0
condition n/a
subroutine 3 7 42.8
pod 0 4 0.0
total 11 102 10.7


line stmt bran cond sub pod time code
1             package WG::Status;
2              
3 1     1   55674 use 5.022002;
  1         4  
4 1     1   4 use strict;
  1         2  
  1         17  
5 1     1   3 use warnings;
  1         2  
  1         722  
6              
7              
8             our $VERSION = '0.04';
9              
10             require Exporter;
11             our @ISA = qw(Exporter);
12             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             sub wg_status {
27 0     0 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 0         if ($peercount) {
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 0         push @peers, $peerdef if defined $peerdef;
56 0           $$ifdef{peers} = \@peers;
57             }
58 0           push @{$wg}, $ifdef;
  0            
59             }
60              
61 0           return $wg;
62             }
63              
64              
65              
66             sub kernel_module {
67             # probably need to look at this once we get to kernel version 5.6
68 0     0 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             sub parse_interface {
78 0     0 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 0         if ($line =~ /^wg\d+/) { $ifdef{interface} = $line }
  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             sub parse_peer {
96 0     0 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 0         if ($line =~ /^.{43,}$/) { $peerdef{'peer'} = $line }
  0 0          
    0          
    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;
133              
134             __END__