Wireguard-WGmeta
view release on metacpan or search on metacpan
lib/Wireguard/WGmeta/Wrapper/Config.pm view on Meta::CPAN
C<$interface> Valid interface name
=item *
C<$identifier> Valid section identifier
=back
B<Returns>
A hash containing the requested section. If the requested section/interface is not present, an empty hash is returned.
=cut
sub get_interface_section($self, $interface, $identifier) {
$identifier = $self->try_translate_alias($interface, $identifier);
if (exists $self->{parsed_config}{$interface}{$identifier}) {
my %r = %{$self->{parsed_config}{$interface}{$identifier}};
return %r;
}
else {
return ();
}
}
=head3 get_section_list($interface)
Returns a list of valid sections of an interface (ordered as in the original config file).
B<Parameters>
=over 1
=item *
C<$interface> A valid interface name
=back
B<Returns>
A list of all sections of an interface. If interface is not present, an empty list is returned.
=cut
sub get_section_list($self, $interface) {
if ($self->is_valid_interface($interface)) {
return @{$self->{parsed_config}{$interface}{INTERNAL_KEY_PREFIX . 'section_order'}};
}
else {
return ();
}
}
sub get_wg_meta_prefix($self) {
return $self->{wg_meta_prefix};
}
sub get_disabled_prefix($self) {
return $self->{wg_meta_disabled_prefix};
}
=head3 add_interface($interface_name, $ip_address, $listen_port, $private_key)
Adds a (minimally configured) interface. If more attributes are needed, please set them using the C<set()> method.
B<Caveat:> No validation is performed on the values!
B<Parameters>
=over 1
=item *
C<$interface_name> A new interface name, must be unique.
=item *
C<$ip_address> A string describing the ip net(s) (e.g '10.0.0.0/24, fdc9:281f:04d7:9ee9::2/64')
=item *
C<$listen_port> The listen port for this interface.
=item *
C<$private_key> A private key for this interface
=back
B<Raises>
An exception if the interface name already exists.
B<Returns>
None
=cut
sub add_interface($self, $interface_name, $ip_address, $listen_port, $private_key) {
if ($self->is_valid_interface($interface_name)) {
die "Interface `$interface_name` already exists";
}
my %interface = (
'address' => $ip_address,
'listen-port' => $listen_port,
'private-key' => $private_key,
INTERNAL_KEY_PREFIX . 'type' => 'Interface',
INTERNAL_KEY_PREFIX . 'order' => [ 'address', 'listen-port', 'private-key' ]
);
$self->{parsed_config}{$interface_name}{$interface_name} = \%interface;
$self->{parsed_config}{$interface_name}{INTERNAL_KEY_PREFIX . 'alias_map'} = {};
$self->{parsed_config}{$interface_name}{INTERNAL_KEY_PREFIX . 'section_order'} = [ $interface_name ];
$self->{parsed_config}{$interface_name}{checksum} = 'none';
$self->{parsed_config}{$interface_name}{INTERNAL_KEY_PREFIX . 'mtime'} = 0.0;
$self->{parsed_config}{$interface_name}{INTERNAL_KEY_PREFIX . 'config_path'} = $self->{wireguard_home} . $interface_name . '.conf';
$self->{parsed_config}{$interface_name}{has_changed} = 1;
}
=head3 add_peer($interface, $ip_address, $public_key [, $alias, $preshared_key])
Adds a peer to an exiting interface.
B<Parameters>
=over 1
=item *
C<$interface> A valid interface.
=item *
C<$ip_address> A string describing the ip-address(es) of this this peer.
=item *
C<$public_key> Public-key for this interface. This becomes the identifier of this peer.
=item *
C<[$preshared_key]> Optional argument defining the psk.
=item *
C<[$alias]> Optional argument defining an alias for this peer (wg-meta)
=back
B<Raises>
An exception if either the interface is invalid, the alias is already assigned or the public-key is
already present on an other peer.
B<Returns>
A tuple consisting of the iface private-key and listen port
=cut
sub add_peer($self, $interface, $ip_address, $public_key, $alias = undef, $preshared_key = undef) {
# generate new key pair if not defined
if ($self->is_valid_interface($interface)) {
if ($self->is_valid_identifier($interface, $public_key)) {
die "An interface with this public-key already exists on `$interface`";
}
# generate peer config
( run in 0.617 second using v1.01-cache-2.11-cpan-0b5f733616e )