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::Config - Parser for wireguard configuration files.

SYNOPSIS

 use Wireguard::WGmeta::Parser::Config;

 my $content = read_file('<path_to_wireguard_conf_file');
 my $hash_parsed_configs = parse_wg_config($content, '<interface_name>', '#+', '#-');

 # and similarly to transform the parsed config into a wireguard compatible format again
 my $interface_config = create_wg_config($hash_parsed_configs->{<interface_name>}, '#+', '#-')

DESCRIPTION

Parser for wireguard configuration files supporting optional wg-meta attributes.

METHODS

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

Parses the contents of $config_file_content and returns a hash with the following structure:

    {
        'interface_name' => {
            'section_order' => <list_of_available_section_identifiers>,
            'alias_map'     => <mapping_alias_to_identifier>,
            'checksum'      => <calculated_checksum_of_this_interface_config>,
            'n_peers'       => <number_of_peers_for_this_interface>,
            'interface_name' => <interface_name>,
            'a_identifier'    => {
                'type'  => <'Interface' or 'Peer'>,
                'order' => <list_of_attributes_in_their_original_order>,
                'attr0' => <value_of_attr0>,
                'attrN' => <value_of_attrN>
            },
            'an_other_identifier => {
                [...]
            }
        }
    }

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 together with the corresponding "create_wg_config($ref_interface_config, $wg_meta_prefix, $disabled_prefix [, $plain = FALSE])".

  • 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.

  • If a section is marked as "disabled", this is represented in the attribute $wg_meta_prefix. 'Disabled' . However, does only exist if this section has been enabled/disabled once.

  • To check whether a file is actually a Wireguard interface config, the parser first checks the presence of the string [Interface]. If not present, the file is skipped (without warning!). And in this case the parser returns undefined!

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).

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_config($ref_interface_config, $wg_meta_prefix, $disabled_prefix [, $plain = FALSE])

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.