File Coverage

lib/Wireguard/WGmeta/Parser/Show.pm
Criterion Covered Total %
statement 12 34 35.2
branch 0 2 0.0
condition n/a
subroutine 4 5 80.0
pod 1 1 100.0
total 17 42 40.4


line stmt bran cond sub pod time code
1             =pod
2              
3             =head1 NAME
4              
5             WGmeta::Parser::Show - Parser for `wg show dump`
6              
7             =head1 SYNOPSIS
8              
9             use Wireguard::WGmeta::Parser::Show;
10              
11             my $out = `wg show dump`;
12             my $ref_hash_parsed_show = wg_show_dump_parser($out);
13              
14             =head1 DESCRIPTION
15              
16             This class contains a parser for the output of C.
17              
18             =head1 METHODS
19              
20             =cut
21              
22             package Wireguard::WGmeta::Parser::Show;
23 1     1   7 use strict;
  1         2  
  1         30  
24 1     1   15 use warnings FATAL => 'all';
  1         2  
  1         57  
25              
26 1     1   6 use experimental 'signatures';
  1         2  
  1         5  
27              
28 1     1   103 use base 'Exporter';
  1         2  
  1         404  
29             our @EXPORT = qw(wg_show_dump_parser);
30              
31             our $VERSION = "0.3.3"; # do not change manually, this variable is updated when calling make
32              
33              
34             =head3 wg_show_dump_parser($input)
35              
36             Parser for the output of C:
37              
38             {
39             'interface_name' => {
40             'a_peer_pub_key' => {
41             'interface' => ,
42             'public-key' => ,
43             'preshared-key' => ,
44             'and_so_on' =>
45             },
46             'an_interface_name => {
47             'interface' => ,
48             'private-key' => ,
49             'and_so_on' =>
50             }
51             },
52             'an_other_interface' => {
53             [...]
54             }
55             }
56              
57             An important remark: This parser is relatively intolerant when it comes to formatting due to the input is already in a "machine readable" format.
58             It expects one peer/interface per line, the values in the exact same order as defined in @keys_peer/@keys_interface,
59             separated by a whitespace character. Usually, you don't need to worry about this - it is just meant as word of warning.
60              
61             B
62              
63             =over 1
64              
65             =item
66              
67             C<$input> Output of C
68              
69             =back
70              
71             B
72              
73             A reference to a hash with the structure described above.
74              
75             =cut
76 0     0 1   sub wg_show_dump_parser($input) {
  0            
  0            
77 0           my $interface = '';
78 0           my $parsed_show = {};
79              
80 0           my @keys_interface = qw(interface private-key public-key listen-port fwmark);
81 0           my @keys_peer = qw(interface public-key preshared-key endpoint allowed-ips latest-handshake transfer-rx transfer-tx persistent-keepalive);
82 0           for my $line (split /\n/, $input) {
83 0           my @split_line = split /\s/, $line;
84 0 0         unless ($split_line[0] eq $interface) {
85 0           $interface = $split_line[0];
86             # handle interface
87 0           my $idx = 0;
88 0           map {$parsed_show->{$interface}{$interface}{$_} = $split_line[$idx];
  0            
89 0           $idx++} @keys_interface;
90             }
91             else {
92 0           my %peer;
93 0           my $idx = 0;
94 0           map {$peer{$_} = $split_line[$idx];
  0            
95 0           $idx++;} @keys_peer;
96 0           $parsed_show->{$interface}{$peer{'public-key'}} = \%peer;
97             }
98             }
99 0           return $parsed_show;
100             }
101              
102             1;