The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

WGmeta::Parser::Middleware - Middleware between the parser and wrapper class(es)

SYNOPSIS

 use Wireguard::WGmeta::Parser::Middleware;
 use Wireguard::WGmeta::Util;

 # Parse a wireguard configuration file
 my $config_contents = read_file('/path/to/config.conf', 'interface_name');
 my $parsed_config = parse_wg_config2($config_contents);

 # And convert it to string representation again
 my $new_config_content = create_wg_config2($parsed_config);

DESCRIPTION

Acts as a middleware between Wireguard::WGmeta::Wrapper::Config and Wireguard::WGmeta::Parser::Conf. Most importantly it implements the entry_handler() and section_handler() callbacks of Wireguard::WGmeta::Parser::Conf.

METHODS

parse_wg_config2($config_file_content, $interface_name [, $wg_meta_prefix, $disabled_prefix, $use_checksum])

Using the entry_handler() and section_handler(), this method enriches the parsed config by several artefacts: peer_count, alias_maps, and checksum. Considering this minimal config example:

    #+root_attr1 = value1

    [Interface]
    ListenPort = 12345

    [Peer]
    #+Alias = some_alias
    PublicKey = peer_1

We end up with the following structure:

    {
        'root_attr1' => value1,
        INT_PREFIX.'root_order' => [
            'root_attr1'
        ],
        INT_PREFIX.'section_order' => [
            'interface_1',
            'peer_1'
        ],
        INT_PREFIX.'n_peers' => 1,
        INT_PREFIX.'observer_wg_meta_attrs => {
            'alias' => 1
        },
        INT_PREFIX.'alias_map => {
            'some_alias' => 'peer_1'
        },
        'interface_name => 'interface_name',
        'interface_1 => {
            'listen-port' => 12345,
            INT_PREFIX.'type' => 'Interface',
            INT_PREFIX.'order' => [
                'listen-port',
            ]
        },
        'peer_1' => {
            'alias' => 'some_alias',
            INT_PREFIX.'type => 'Peer',
            INT_PREFIX.'order => [
                'alias',
            ]
        }
    }

Remarks

  • All attributes listed in Wireguard::WGmeta::ValidAttributes are referenced by their key. This means, if you want for example access PublicKey the key would be public-key. Any attribute not present in Wireguard::WGmeta::ValidAttributes is stored (and written back) as they appear in Config.

  • This method can be used as stand-alone in conjunction with the L<>

  • If the section is of type 'Peer' the identifier equals to its public-key, otherwise its the interface name again.

  • wg-meta attributes are always prefixed with $wg_meta_prefix.

Parameters

  • $config_file_content String containing the contents of a Wireguard configuration file.

  • $interface_name Interface name

  • [$wg_meta_prefix = '#+'] wg-meta prefix. Must start with '#' or ';'

  • [$disabled_prefix = '#-'] disabled prefix. Must start with '#' or ';'

  • [$use_checksum = TRUE] If set to False, checksum is not checked

Raises

An exceptions if:

  • If the parser ends up in an invalid state (e.g a section without information). Or An alias is defined twice.

A warning:

  • On a checksum mismatch

Returns

A reference to a hash with the structure described above. Or if the configuration file is not a Wireguard configuration: undef.

create_wg_config2($ref_interface_config [, $wg_meta_prefix, $disabled_prefix, $no_checksum])

Turns a reference of interface-config hash (just a single interface!) back into a wireguard config.

Parameters

Returns

A string, ready to be written down as a config file.

1 POD Error

The following errors were encountered while parsing the POD:

Around line 100:

An empty L<>