The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

WGmeta::Wrapper::Show - Class for parsing the `wg show dump` output

SYNOPSIS

 use Wireguard::WGmeta::Wrapper::Show;
 my $wg_show = Wireguard::WGmeta::Wrapper::Show->new(<wg show dump output as string>);

 # if you need just the parser
 my $out = `wg show dump`;
 my $ref_hash_parsed_show = wg_show_dump_parser($out);

DESCRIPTION

This class contains a parser for the output of wg show dump together with an interface to retrieve the parsed data. An important note: This class does not perform the necessary I/O by itself and therefore the output of the command wg show dump has to be captured into a string externally (e.g using "get_wg_show()" in Wireguard::WGmeta::Wrapper::Bridge).

EXAMPLES

 use Wireguard::WGmeta::Wrapper::Show;
 use Wireguard::WGmeta::Wrapper::Bridge;

 my ($out, $err) = get_wg_show();
 my $wg_show = Wireguard::WGmeta::Wrapper::Show->new($out);

 # get a specfic interface section
 wg_show->get_interface_section('wg0', '<interface_public_key>');

METHODS

new($wg_show_dump)

Creates a new instance of the show parser

Parameters

  • $wg_show_dump Output of the (external) command wg show dump.

Returns

Instance

wg_show_dump_parser($input)

Parser for the output of wg show dump. Aims to create a compatible with: "read_wg_configs($wireguard_home, $wg_meta_prefix, $disabled_prefix)" in Wireguard::WGmeta::Wrapper::Config:

    {
        'interface_name' => {
            'a_peer_pub_key' => {
                'interface'     => <parent_interface>,
                'public-key'    => <interface_public_key>,
                'preshared-key' => <interface_preshared_key>,
                'and_so_on'     => <value_of_attr>
            },
            'an_interface_name => {
                'interface'     => <parent_interface>,
                'private-key'   => <interface_private_key>,
                'and_so_on'     => <value_of_attr>
            }
        },
        'an_other_interface' => {
            [...]
        }
    }

An important remark: This parser is relatively intolerant when it comes to formatting due to the input is already in a "machine readable" format. It expects one peer/interface per line, the values in the exact same order as defined in @keys_peer/@keys_interface, separated by a whitespace character. Usually, you don't have to worry about this - it is just meant as word of warning.

Parameters

  • $input Output of wg show dump

Returns

A reference to a hash with the structure described above.

get_interface_list()

Returns a list with all available interface names

Parameters

Returns

A list with valid interface names.

iface_exists($interface)

Simply checks if data is available for a specific interface. Useful to check if an interface is up.

Parameters

  • $interface An interface name

Returns

If yes, returns True else False

get_interface_section($interface, $identifier)

Returns a specific section of an interface

Parameters

  • $interface A valid interface name, ideally retrieved through "get_interface_list()".

  • $identifier A valid identifier, if the requested section is a peer this is its public-key, otherwise the interface name again.

Returns

A hash of the requested section. If non-existent, empty hash.

get_section_list($interface)

Returns a sorted list of all peers belonging to given interface

Parameters

Returns

A list of peer public-keys (identifiers), if the interface does not exist -> empty list.

dump()

Simple dumper method to print contents of $self->{parsed_show}.