WGmeta::Wrapper::Config - Class for interfacing the wireguard configuration
use Wireguard::WGmeta::Wrapper::Config; my $wg_meta = Wireguard::WGmeta::Wrapper::Config->new('<path to wireguard configuration>');
This class provides wrapper-functions around a wireguard configuration parsed by Wireguard::WGmeta::Parser::Middleware which allow to edit, add and remove interfaces and peers.
Please refer to Wireguard::WGmeta::Wrapper::ConfigT
use Wireguard::WGmeta::Wrapper::Config; my $wg-meta = Wireguard::WGmeta::Wrapper::Config->new('<path to wireguard configuration>'); # set an attribute (non wg-meta attributes forwarded to the original `wg set` command) wg_meta->set('wg0', 'WG_0_PEER_A_PUBLIC_KEY', '<attribute_name>', '<attribute_value>'); # set an alias for a peer wg_meta->set('wg0', 'WG_0_PEER_A_PUBLIC_KEY', 'alias', 'some_fancy_alias'); # disable peer (this comments out the peer in the configuration file) wg_meta->disable('wg0', 'some_fancy_alias'); # write config (if parameter is set to True, the config is overwritten, if set to False the resulting file is suffixed with '.not_applied' wg_meta->commit(1);
Creates a new instance of this class.
Parameters
$wireguard_home Path to Wireguard configuration files. Make sure the path ends with a `/`.
$wireguard_home
[$wg_meta_prefix] A custom wg-meta comment prefix, has to begin with either `;` or `#`. It is recommended to not change this setting, especially in a already deployed installation.
[$wg_meta_prefix]
[$wg_meta_disabled_prefix] A custom prefix for the commented out (disabled) sections, has to begin with either `;` or `#` and must not be equal with $wg_meta_prefix! (This is enforced and an exception is thrown if violated) It is recommended to not change this setting, especially in an already deployed installation.
[$wg_meta_disabled_prefix]
$wg_meta_prefix
[$not_applied_suffix] Suffix to add if commit() is set to not override an existing config.
[$not_applied_suffix]
commit()
[$custom_attributes] A reference to a hash defining custom attributes. Expects the following structure:
[$custom_attributes]
{ 'attr_key' => { 'validator' => 'Ref to validation function' }, 'example' => { 'validator' => sub ($attr, $value) { return ($attr eq 'example') ? 1 : 0; } }, ... }
Returns
An instance of WGmeta::Wrapper::Config
Sets a value on a specific interface section. If attribute_value == $value this sub is essentially a No-Op.
attribute_value
$value
$interface Valid interface identifier (e.g 'wg0')
$interface
$identifier Either an interface name, an alias or public-key of a peer
$identifier
$attribute Attribute name (case does matter!)
$attribute
[$unknown_callback = undef] A reference to a callback function which is fired when a previously unknown attribute is set. Expected signature:
[$unknown_callback = undef]
sub my_unknown_callback($attribute, $value) { # Handling of this particular case return $attribute, $value; }
If not defined, a warning is emitted
Raises
Exception if:
Value is not defined
Interface is invalid
Identifier is invalid (also if alias translation fails)
Attribute is not valid for target section (Interface, Peer)
Validation for the attribute value fails
None
Simply calls the validate() function defined in Wireguard::WGmeta::Validator or $custom_attributs
validate()
$custom_attributs
$attribute Attribute name
$value Attribute value
True if validation was successful (or no validator function present), False if not.
Disables an interface/peer and setting the wg-meta attribute `Disabled` to 1.
1
$interface Valid interface name (e.g 'wg0').
$identifier A valid identifier (or alias): If the target section is a peer, this is usually the public key of this peer. If target is an interface, its again the interface name.
Inverse method if "disable($interface, $identifier)"
Checks if an interface name is valid (present in parsed config)
$interface An interface name
True if present, undef if not.
Simply checks if an alias is valid for a given interface
Checks if an identifier is valid for a given interface
$identifier An identifier (no alias!)
Tries to translate an identifier (which may be an alias). no exception is thrown on failure, instead the $may_alias is returned.
$may_alias
$interface A valid interface name
$may_alias An identifier which could be a valid alias for this interface
If the alias is valid for the specified interface, the corresponding identifier is returned, else $may_alias
Returns a list of all files in $wireguard_home matching /.*\.conf$/.
$wireguard_home Path to a folder where wireguard configuration files are located
A reference to a list with absolute paths to the config files (possibly empty)
Writes down the parsed config to the wireguard configuration folder
[$is_hot_config = FALSE]) If set to TRUE, the existing configuration is overwritten (and possibly existing, not applied configs are deleted). Otherwise, the suffix '.not_applied' is appended to the filename
[$is_hot_config = FALSE])
[$no_checksum = FALSE]) If set to TRUE, no checksum is written
[$no_checksum = FALSE])
Exception if: Folder or file is not writeable
Return a list of all interfaces.
A list of all valid interface names. If no interfaces are available, an empty list is returned
Returns a hash representing a section of a given interface
$interface Valid interface name
$identifier Valid section identifier
A hash containing the requested section. If the requested section/interface is not present, an empty hash is returned.
Returns a list of valid sections of an interface (ordered as in the original config file).
A list of all sections of an interface. If interface is not present, an empty list is returned.
Adds a (minimally configured) interface. If more attributes are needed, please set them using the set() method.
set()
Caveat: No validation is performed on the values!
$interface_name A new interface name, must be unique.
$interface_name
$ip_address A string describing the ip net(s) (e.g '10.0.0.0/24, fdc9:281f:04d7:9ee9::2/64')
$ip_address
$listen_port The listen port for this interface.
$listen_port
$private_key A private key for this interface
$private_key
An exception if the interface name already exists.
Adds a peer to an exiting interface.
$interface A valid interface.
$ip_address A string describing the ip-address(es) of this this peer.
$public_key Public-key for this interface. This becomes the identifier of this peer.
$public_key
[$preshared_key] Optional argument defining the psk.
[$preshared_key]
[$alias] Optional argument defining an alias for this peer (wg-meta)
[$alias]
An exception if either the interface is invalid, the alias is already assigned or the public-key is already present on an other peer.
A tuple consisting of the iface private-key and listen port
Removes a peer (identified by it's public key or alias) from an interface.
$identifier A valid identifier (or an alias)
Exception if interface or identifier is invalid
Removes an interface. This command deletes the config file immediately. I.e no rollback possible!
Returns the number of peers.
Caveat: Does return the count represented in the current (parsed) configuration state.
[$interface = undef] If defined and valid, only return counts for this specific interface
[$interface = undef]
Number of peers
Method to reload an interface configuration from disk. Also useful to add an new (externally) created interface on-the-fly. If a config file with a .not_applied suffix is present (and its mtime is newer than the original one), it is taken as source for reloading the configuration data.
[$new = FALSE] If set to True, the parser looks at $wireguard_home for this new interface config.
[$new = FALSE]
[$force = FALSE] When set to True, the configuration is reloaded regardless of its mtime.
[$force = FALSE]
Exception: If the interface is invalid (or the config file is not found)
None, or undef if $new == True and the interface is in fact not a wg config.
$new == True
Register your callback handlers for the reload_from_disk() event here. Your handler is called after the reload happened, is blocking and exceptions are caught in an eval{}; environment.
reload_from_disk()
eval{};
$ref_handler Reference to a handler function. The following signature is expected:
$ref_handler
sub my_handler_function($interface, $ref_list_args){ ... }
$handler_id An identifier for you handler function. Must be unique!
$handler_id
[$ref_listener_args = []] A reference to an argument list for your handler function
[$ref_listener_args = []]
None, exception if $handler_id is already present.
Removes a reload callback handler by it's $handler_id.
$handler_id A valid handler id
1 on success, undef on failure.
To install Wireguard::WGmeta, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Wireguard::WGmeta
CPAN shell
perl -MCPAN -e shell install Wireguard::WGmeta
For more information on module installation, please visit the detailed CPAN module installation guide.