lib/Wireguard/WGmeta/Wrapper/Config.pm

filter: Statementstmt 91.4% 224 / 245 Branchbran 74.1% 86 / 116 Conditioncond 79.1% 19 / 24 MC/DCmcdc 42.8% 6 / 14 Subroutinesub 93.0% 40 / 43 Podpod 88.0% 22 / 25 totaltotal 85.0% 397 / 467 CC 86 SCAR 22.0
CC
86
Cov
85%
set
20.0
may_reload_from_disk
19.9
commit
11.1
CRAP 9.0
« lib/Wireguard/WGmeta/Parser/Conf.pm ↑ Summary lib/Wireguard/WGmeta/Validator.pm »
1 =pod
2  
3 =head1 NAME
4  
5 WGmeta::Wrapper::Config - Class for interfacing the wireguard configuration
6  
7 =head1 SYNOPSIS
8  
9 use Wireguard::WGmeta::Wrapper::Config;
10 my $wg_meta = Wireguard::WGmeta::Wrapper::Config->new('<path to wireguard configuration>');
11  
12 =head1 DESCRIPTION
13  
14 This class provides wrapper-functions around a wireguard configuration parsed by L<Wireguard::WGmeta::Parser::Middleware> which
15 allow to edit, add and remove interfaces and peers.
16  
17 =head1 CONCURRENCY
18  
19 Please refer to L<Wireguard::WGmeta::Wrapper::ConfigT>
20  
21 =head1 EXAMPLES
22  
23 use Wireguard::WGmeta::Wrapper::Config;
24 my $wg-meta = Wireguard::WGmeta::Wrapper::Config->new('<path to wireguard configuration>');
25  
26 # set an attribute (non wg-meta attributes forwarded to the original `wg set` command)
27 wg_meta->set('wg0', 'WG_0_PEER_A_PUBLIC_KEY', '<attribute_name>', '<attribute_value>');
28  
29 # set an alias for a peer
30 wg_meta->set('wg0', 'WG_0_PEER_A_PUBLIC_KEY', 'alias', 'some_fancy_alias');
31  
32 # disable peer (this comments out the peer in the configuration file)
33 wg_meta->disable('wg0', 'some_fancy_alias');
34  
35 # write config (if parameter is set to True, the config is overwritten, if set to False the resulting file is suffixed with '.not_applied'
36 wg_meta->commit(1);
37  
38 =head1 METHODS
39  
40 =cut
41  
42 5 ▶use v5.20.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 package Wireguard::WGmeta::Wrapper::Config;
44 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 5 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
47 5 ▶use Wireguard::WGmeta::Wrapper::Bridge;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 5 ▶use Wireguard::WGmeta::Parser::Middleware;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 5 ▶use Wireguard::WGmeta::ValidAttributes;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 5 ▶use Wireguard::WGmeta::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 5 ▶use Wireguard::WGmeta::Parser::Conf qw(INTERNAL_KEY_PREFIX);
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 5 our $VERSION = "0.3.4"; # do not change manually, this variable is updated when calling make
54  
55 5 ▶use constant FALSE => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 5 ▶use constant TRUE => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 =head3 new($wireguard_home [, $wg_meta_prefix, $wg_meta_disabled_prefix, $custom_attributes])
59  
60 Creates a new instance of this class.
61  
62 B<Parameters>
63  
64 =over 1
65  
66 =item *
67  
68 C<$wireguard_home> Path to Wireguard configuration files. Make sure the path ends with a `/`.
69  
70 =item *
71  
72 C<[$wg_meta_prefix]> A custom wg-meta comment prefix, has to begin with either `;` or `#`.
73 It is recommended to not change this setting, especially in a already deployed installation.
74  
75 =item *
76  
77 C<[$wg_meta_disabled_prefix]> A custom prefix for the commented out (disabled) sections,
78 has to begin with either `;` or `#` and must not be equal with C<$wg_meta_prefix>! (This is enforced and an exception is thrown if violated)
79 It is recommended to not change this setting, especially in an already deployed installation.
80  
81 =item *
82  
83 C<[$not_applied_suffix]> Suffix to add if C<commit()> is set to not override an existing config.
84  
85 =item *
86  
87 C<[$custom_attributes]> A reference to a hash defining custom attributes. Expects the following structure:
88  
89 {
90 'attr_key' => {
91 'validator' => 'Ref to validation function'
92 },
93 'example' => {
94 'validator' => sub ($attr, $value) {
95 return ($attr eq 'example') ? 1 : 0;
96 }
97 },
98 ...
99 }
100  
101 =back
102  
103 B<Returns>
104  
105 An instance of WGmeta::Wrapper::Config
106  
107 =cut
108 10 ▶sub new($class, $wireguard_home, $wg_meta_prefix = '#+', $wg_meta_disabled_prefix = '#-', $not_applied_suffix = '.not_applied', $custom_attributes = undef) {
Subroutine new: called CC=7 SCAR=19.7
Pod documented
109  
110 10 ▶ if ($wg_meta_prefix eq $wg_meta_disabled_prefix) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wg_meta_prefix eq $wg_meta_disabled_prefix) 0 10 10
111 0 die '`$wg_meta_prefix` and `$wg_meta_disabled_prefix` have to be different';
112 }
113  
114 10 ▶ my $self = {
Branch: 1 branch
BranchTrueFalseTotal
defined $custom_attributes ? : 1 9 10
115 'wireguard_home' => $wireguard_home,
116 'wg_meta_prefix' => $wg_meta_prefix,
117 'wg_meta_disabled_prefix' => $wg_meta_disabled_prefix,
118 'not_applied_suffix' => $not_applied_suffix,
119 'n_conf_files' => {},
120 'parsed_config' => {},
121 'reload_listeners' => {},
122 'custom_attributes' => defined $custom_attributes ? $custom_attributes : {}
123 };
124  
125 10 _read_configs_from_folder2($self);
126  
127 10 bless $self, $class;
128 10 return $self;
129 }
130  
131 10 ▶sub _read_configs_from_folder2($self) {
Subroutine _read_configs_from_folder2: called CC=2 SCAR=6.9
132 10 my ($all_dot_conf, $count) = get_all_conf_files($self->{wireguard_home});
133 10 for my $possible_config_path (@{$all_dot_conf}) {
134 40 my $interface = $possible_config_path;
135 40 $interface =~ s/^\/|\\|.*\/|.*\\|.conf$//g;
136 40 may_reload_from_disk($self, $interface, TRUE, TRUE, TRUE);
137 }
138 }
139  
140 =head3 set($interface, $identifier, $attribute, $value [, $unknown_callback])
141  
142 Sets a value on a specific interface section. If C<attribute_value> == C<$value> this sub is essentially a No-Op.
143  
144 B<Parameters>
145  
146 =over 1
147  
148 =item *
149  
150 C<$interface> Valid interface identifier (e.g 'wg0')
151  
152 =item *
153  
154 C<$identifier> Either an interface name, an alias or public-key of a peer
155  
156 =item *
157  
158 C<$attribute> Attribute name (case does matter!)
159  
160 =item *
161  
162 C<[$unknown_callback = undef]> A reference to a callback function which is fired when a previously unknown attribute is set.
163 Expected signature:
164  
165 sub my_unknown_callback($attribute, $value) {
166 # Handling of this particular case
167 return $attribute, $value;
168 }
169  
170 If not defined, a warning is emitted
171  
172 =back
173  
174 B<Raises>
175  
176 Exception if:
177  
178 =over 1
179  
180 =item *
181  
182 Value is not defined
183  
184 =item *
185  
186 Interface is invalid
187  
188 =item *
189  
190 Identifier is invalid (also if alias translation fails)
191  
192 =item *
193  
194 Attribute is not valid for target section (Interface, Peer)
195  
196 =item *
197  
198 Validation for the attribute value fails
199  
200 =back
201  
202 B<Returns>
203  
204 None
205  
206 =cut
207 41 ▶sub set($self, $interface, $identifier, $attribute, $value, $unknown_callback = undef) {
Subroutine set: called CC=19 SCAR=30.0
Pod documented
208 # Assertions
209 41 ▶ die "Undefined value for `$attribute` in interface `$interface` NOT SET" unless defined($value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 41 41
210 41 ▶ die "Invalid interface name `$interface`" unless $self->is_valid_interface($interface);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_valid_interface($interface) 0 41 41
211 41 $identifier = $self->try_translate_alias($interface, $identifier);
212 41 ▶ die "Invalid identifier `$identifier` for interface `$interface`" unless $self->is_valid_identifier($interface, $identifier);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_valid_identifier($interface, $identifier) 0 41 41
213 41 my $attr_type = get_attr_type($attribute);
214 41 ▶ if ($interface eq $identifier) {
Branch: 1 branch
BranchTrueFalseTotal
if ($interface eq $identifier) { } 6 35 41
215 # We have an interface
216 6 ▶ die "Attribute `$attribute` it not valid for the interface section" if $attr_type == ATTR_TYPE_IS_WG_ORIG_PEER;
Branch: 1 branch
BranchTrueFalseTotal
if $attr_type == 14 1 5 6
217 }
218 else {
219 35 ▶ die "Attribute `$attribute` is not valid for a peer section" if $attr_type == ATTR_TYPE_IS_WG_ORIG_INTERFACE or $attr_type == ATTR_TYPE_IS_WG_QUICK;
Branch: 1 branch
BranchTrueFalseTotal
if $attr_type == 13 or $attr_type == 12 1 34 35
Condition66%2/3
$attr_type == 13 or $attr_type == 12
l!l&&r!l&&!r
1034
MC/DC50%1/2
$attr_type == 13 or $attr_type == 12
$attr_type == 13 $attr_type == 12
Truth table66%2/3
A or B
A= $attr_type == 13B= $attr_type == 12
ABresult
1X1
011
000
220 }
221  
222 # skip if same value
223 39 ▶ if (exists $self->{parsed_config}{$interface}{$identifier}{$attribute} && $self->{parsed_config}{$interface}{$identifier}{$attribute} eq $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"parsed_config"}{$interface}{$identifier}{$attribute} and $self->{"parsed_config"}{$interface}{$identifier}{$attribute} eq $value) 0 39 39
Condition66%2/3
exists $self->{"parsed_config"}{$interface}{$identifier}{$attribute} and $self->{"parsed_config"}{$interface}{$identifier}{$attribute} eq $value
!ll&&!rl&&r
25140
MC/DC0%0/2
exists $self->{"parsed_config"}{$interface}{$identifier}{$attribute} and $self->{"parsed_config"}{$interface}{$identifier}{$attribute} eq $value
exists $self->{"parsed_config"}{$interface}{$identifier}{$attribute} $self->{"parsed_config"}{$interface}{$identifier}{$attribute} eq $value
Truth table66%2/3
A and B
A= exists $self->{"parsed_config"}{$interface}{$identifier}{$attribute}B= $self->{"parsed_config"}{$interface}{$identifier}{$attribute} eq $value
ABresult
0X0
100
111
224 0 return;
225 }
226  
227 # Call attribute validation function
228 39 ▶ die "Invalid attribute value `$value` for `$attribute`" unless $self->attr_value_is_valid($attribute, $value);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->attr_value_is_valid($attribute, $value) 1 38 39
229  
230 38 ▶ unless (exists $self->{parsed_config}{$interface}{$identifier}{$attribute}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"parsed_config"}{$interface}{$identifier}{$attribute}) 25 13 38
231  
232 25 ▶ if (not exists $self->{parsed_config}{$interface}{INTERNAL_KEY_PREFIX . 'observed_wg_meta_attrs'}{$attribute}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"parsed_config"}{$interface}{"int_observed_wg_meta_attrs"}{$attribute}) 23 2 25
233 23 ▶ if (exists KNOWN_ATTRIBUTES->{$attribute}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists {address => {in_config_name => 'Address', type => 12, validator => sub ($input) { package Wireguard::WGmeta::Validator; BEGIN {${^WARNING_BITS} = "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"} no feature 'current_sub', 'evalbytes', 'fc', 'say', 'state', 'switch', 'unicode_strings', 'unicode_eval'; return 1; } }, alias => {in_config_name => 'Alias', type => 10, validator => sub ($input) { package Wireguard::WGmeta::Validator; BEGIN {${^WARNING_BITS} = "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"} no feature 'current_sub', 'evalbytes', 'fc', 'say', 'state', 'switch', 'unicode_strings', 'unicode_eval'; return 1; } }, allowed-ips => {in_config_name => 'AllowedIPs', type => 14, validator => sub ($input) { package Wireguard::WGmeta::Validator; BEGIN {${^WARNING_BITS} = "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"} no feature 'current_sub', 'evalbytes', 'fc', 'say', 'state', 'switch', 'unicode_strings', 'unicode_eval'; return 1; } }, checksum => {in_config_name => 'Checksum', type => 10, validator => sub ($input) { package Wireguard::WGmeta::Validator; BEGIN {${^WARNING_BITS} = "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"} no feature 'current_sub', 'evalbytes', 'fc', 'say', 'state', 'switch', 'unicode_strings', 'unicode_eval'; return 1; } }, disabled => {in_config_name => 'Disabled', type => 10, validator => sub ($input) { package Wireguard::WGmeta::Validator; BEGIN {${^WARNING_BITS} = "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"} no feature 'current_sub', 'evalbytes', 'fc', 'say', 'state', 'switch', 'unicode_strings', 'unicode_eval'; return 1; } }, dns => {in_config_name => 'DNS', type => 12, validator => sub ($input) { package Wireguard::WGmeta::Validator; BEGIN {${^WARNING_BITS} = "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"} no feature 'current_sub', 'evalbytes', 'fc', 'say', 'state', 'switch', 'unicode_strings', 'unicode_eval'; return 1; } }, endpoint => {in_config_name => 'Endpoint', type => 14, validator => sub ($input) { package Wireguard::WGmeta::Validator; BEGIN {${^WARNING_BITS} = "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"} no feature 'current_sub', 'evalbytes', 'fc', 'say', 'state', 'switch', 'unicode_strings', 'unicode_eval'; return 1; } }, fwmark => {in_config_name => 'Fwmark', type => 13, validator => sub ($input) { package Wireguard::WGmeta::Validator; BEGIN {${^WARNING_BITS} = "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"} no feature 'current_sub', 'evalbytes', 'fc', 'say', 'state', 'switch', 'unicode_strings', 'unicode_eval'; return 1; } }, listen-port => {in_config_name => 'ListenPort', type => 13, validator => sub ($input) { package Wireguard::WGmeta::Validator; BEGIN {${^WARNING_BITS} = "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"} no feature 'current_sub', 'evalbytes', 'fc', 'say', 'state', 'switch', 'unicode_strings', 'unicode_eval'; return &looks_like_number($input); } }, mtu => {in_config_name => 'MTU', type => 12, validator => sub ($input) { package Wireguard::WGmeta::Validator; BEGIN {${^WARNING_BITS} = "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"} no feature 'current_sub', 'evalbytes', 'fc', 'say', 'state', 'switch', 'unicode_strings', 'unicode_eval'; return 1; } }, persistent-keepalive => {in_config_name => 'PersistentKeepalive', type => 14, validator => sub ($input) { package Wireguard::WGmeta::Validator; BEGIN {${^WARNING_BITS} = "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"} no feature 'current_sub', 'evalbytes', 'fc', 'say', 'state', 'switch', 'unicode_strings', 'unicode_eval'; return 1; } }, post-down => {in_config_name => 'PostDown', type => 12, validator => sub ($input) { package Wireguard::WGmeta::Validator; BEGIN {${^WARNING_BITS} = "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"} no feature 'current_sub', 'evalbytes', 'fc', 'say', 'state', 'switch', 'unicode_strings', 'unicode_eval'; return 1; } }, post-up => {in_config_name => 'PostUP', type => 12, validator => sub ($input) { package Wireguard::WGmeta::Validator; BEGIN {${^WARNING_BITS} = "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"} no feature 'current_sub', 'evalbytes', 'fc', 'say', 'state', 'switch', 'unicode_strings', 'unicode_eval'; return 1; } }, pre-down => {in_config_name => 'PreDown', type => 12, validator => sub ($input) { package Wireguard::WGmeta::Validator; BEGIN {${^WARNING_BITS} = "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"} no feature 'current_sub', 'evalbytes', 'fc', 'say', 'state', 'switch', 'unicode_strings', 'unicode_eval'; return 1; } }, pre-up => {in_config_name => 'PreUp', type => 12, validator => sub ($input) { package Wireguard::WGmeta::Validator; BEGIN {${^WARNING_BITS} = "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"} no feature 'current_sub', 'evalbytes', 'fc', 'say', 'state', 'switch', 'unicode_strings', 'unicode_eval'; return 1; } }, preshared-key => {in_config_name => 'PresharedKey', type => 14, validator => sub ($input) { package Wireguard::WGmeta::Validator; BEGIN {${^WARNING_BITS} = "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"} no feature 'current_sub', 'evalbytes', 'fc', 'say', 'state', 'switch', 'unicode_strings', 'unicode_eval'; return 1; } }, private-key => {in_config_name => 'PrivateKey', type => 13, validator => sub ($input) { package Wireguard::WGmeta::Validator; BEGIN {${^WARNING_BITS} = "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"} no feature 'current_sub', 'evalbytes', 'fc', 'say', 'state', 'switch', 'unicode_strings', 'unicode_eval'; return 1; } }, public-key => {in_config_name => 'PublicKey', type => 14, validator => sub ($input) { package Wireguard::WGmeta::Validator; BEGIN {${^WARNING_BITS} = "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"} no feature 'current_sub', 'evalbytes', 'fc', 'say', 'state', 'switch', 'unicode_strings', 'unicode_eval'; return 1; } }, save-config => {in_config_name => 'SaveConfig', type => 12, validator => sub ($input) { package Wireguard::WGmeta::Validator; BEGIN {${^WARNING_BITS} = "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"} no feature 'current_sub', 'evalbytes', 'fc', 'say', 'state', 'switch', 'unicode_strings', 'unicode_eval'; return 1; } }, table => {in_config_name => 'Table', type => 12, validator => sub ($input) { package Wireguard::WGmeta::Validator; BEGIN {${^WARNING_BITS} = "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"} no feature 'current_sub', 'evalbytes', 'fc', 'say', 'state', 'switch', 'unicode_strings', 'unicode_eval'; return 1; } }}->{$attribute}) { } 14 9 23
elsif (exists $self->{'custom_attributes'}{$attribute}) { } 1 8 9
234 # we have to first occurrence of a known but yet unseen wg-meta attribute
235 14 ▶ $self->{parsed_config}{$interface}{INTERNAL_KEY_PREFIX . 'observed_wg_meta_attrs'}{$attribute} = 1 if KNOWN_ATTRIBUTES->{$attribute}{type} == ATTR_TYPE_IS_WG_META;
Branch: 1 branch
BranchTrueFalseTotal
if ${my $v = {"address" => {"in_config_name" => "Address","type" => 12,"validator" => sub { die sprintf("Too many arguments for subroutine at %s line %d.\n", (caller)[1, 2]) unless @_ <= 1; die sprintf("Too few arguments for subroutine at %s line %d.\n", (caller)[1, 2]) unless @_ >= 1; my $input = $_[0]package Wireguard::WGmeta::Validator; BEGIN {${^WARNING_BITS} = "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"} use strict; use feature 'signatures'; return 1;}},"alias" => {"in_config_name" => "Alias","type" => 10,"validator" => do{my $o}},"allowed-ips" => {"in_config_name" => "AllowedIPs","type" => 14,"validator" => do{my $o}},"checksum" => {"in_config_name" => "Checksum","type" => 10,"validator" => do{my $o}},"disabled" => {"in_config_name" => "Disabled","type" => 10,"validator" => do{my $o}},"dns" => {"in_config_name" => "DNS","type" => 12,"validator" => do{my $o}},"endpoint" => {"in_config_name" => "Endpoint","type" => 14,"validator" => do{my $o}},"fwmark" => {"in_config_name" => "Fwmark","type" => 13,"validator" => do{my $o}},"listen-port" => {"in_config_name" => "ListenPort","type" => 13,"validator" => sub { die sprintf("Too many arguments for subroutine at %s line %d.\n", (caller)[1, 2]) unless @_ <= 1; die sprintf("Too few arguments for subroutine at %s line %d.\n", (caller)[1, 2]) unless @_ >= 1; my $input = $_[0]package Wireguard::WGmeta::Validator; BEGIN {${^WARNING_BITS} = "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"} use strict; use feature 'signatures'; return looks_like_number $input;}},"mtu" => {"in_config_name" => "MTU","type" => 12,"validator" => do{my $o}},"persistent-keepalive" => {"in_config_name" => "PersistentKeepalive","type" => 14,"validator" => do{my $o}},"post-down" => {"in_config_name" => "PostDown","type" => 12,"validator" => do{my $o}},"post-up" => {"in_config_name" => "PostUP","type" => 12,"validator" => do{my $o}},"pre-down" => {"in_config_name" => "PreDown","type" => 12,"validator" => do{my $o}},"pre-up" => {"in_config_name" => "PreUp","type" => 12,"validator" => do{my $o}},"preshared-key" => {"in_config_name" => "PresharedKey","type" => 14,"validator" => do{my $o}},"private-key" => {"in_config_name" => "PrivateKey","type" => 13,"validator" => do{my $o}},"public-key" => {"in_config_name" => "PublicKey","type" => 14,"validator" => do{my $o}},"save-config" => {"in_config_name" => "SaveConfig","type" => 12,"validator" => do{my $o}},"table" => {"in_config_name" => "Table","type" => 12,"validator" => do{my $o}}};$v->{"alias"}{"validator"} = $v->{"address"}{"validator"};$v->{"allowed-ips"}{"validator"} = $v->{"address"}{"validator"};$v->{"checksum"}{"validator"} = $v->{"address"}{"validator"};$v->{"disabled"}{"validator"} = $v->{"address"}{"validator"};$v->{"dns"}{"validator"} = $v->{"address"}{"validator"};$v->{"endpoint"}{"validator"} = $v->{"address"}{"validator"};$v->{"fwmark"}{"validator"} = $v->{"address"}{"validator"};$v->{"mtu"}{"validator"} = $v->{"address"}{"validator"};$v->{"persistent-keepalive"}{"validator"} = $v->{"address"}{"validator"};$v->{"post-down"}{"validator"} = $v->{"address"}{"validator"};$v->{"post-up"}{"validator"} = $v->{"address"}{"validator"};$v->{"pre-down"}{"validator"} = $v->{"address"}{"validator"};$v->{"pre-up"}{"validator"} = $v->{"address"}{"validator"};$v->{"preshared-key"}{"validator"} = $v->{"address"}{"validator"};$v->{"private-key"}{"validator"} = $v->{"address"}{"validator"};$v->{"public-key"}{"validator"} = $v->{"address"}{"validator"};$v->{"save-config"}{"validator"} = $v->{"address"}{"validator"};$v->{"table"}{"validator"} = $v->{"address"}{"validator"}; \$v}->{$attribute}{"type"} == 10 4 10 14
236 }
237 elsif (exists $self->{custom_attributes}{$attribute}) {
238 # we have a registered custom attribute
239 1 $self->{parsed_config}{$interface}{INTERNAL_KEY_PREFIX . 'observed_wg_meta_attrs'}{$attribute} = 1
240 }
241 else {
242 # we have a completely new, unknown attribute
243 8 ▶ if (defined $unknown_callback) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $unknown_callback) { } 8 0 8
244 8 ($attribute, $value) = &{$unknown_callback}($attribute, $value);
245 }
246 else {
247 0 warn "Attribute `$attribute` was previously not known on interface `$interface`";
248 }
249 7 $self->{parsed_config}{$interface}{INTERNAL_KEY_PREFIX . 'observed_wg_meta_attrs'}{$attribute} = 1;
250 }
251 }
252 # the attribute does not (yet) exist in the configuration, lets add it to the list
253 24 push @{$self->{parsed_config}{$interface}{$identifier}{INTERNAL_KEY_PREFIX . 'order'}}, $attribute;
254 }
255 37 ▶ if ($attribute eq 'alias') {
Branch: 1 branch
BranchTrueFalseTotal
if ($attribute eq "alias") 8 29 37
256 8 $self->_update_alias_map($interface, $identifier, $value);
257 }
258 36 $self->{parsed_config}{$interface}{$identifier}{$attribute} = $value;
259 36 $self->_set_changed($interface);
260 }
261  
262 =head3 attr_value_is_valid($attribute, $value, $ref_valid_attrs)
263  
264 Simply calls the C<validate()> function defined in L<Wireguard::WGmeta::Validator> or C<$custom_attributs>
265  
266 B<Parameters>
267  
268 =over 1
269  
270 =item
271  
272 C<$attribute> Attribute name
273  
274 =item
275  
276 C<$value> Attribute value
277  
278 =back
279  
280 B<Returns>
281  
282 True if validation was successful (or no validator function present), False if not.
283  
284 =cut
285 39 ▶sub attr_value_is_valid($self, $attribute, $value) {
Subroutine attr_value_is_valid: called CC=3 SCAR=11.0
Pod documented
286 39 ▶ return &{KNOWN_ATTRIBUTES->{$attribute}{validator}}($value) if exists KNOWN_ATTRIBUTES->{$attribute};
Branch: 1 branch
BranchTrueFalseTotal
if exists ${my $v = {"address" => {"in_config_name" => "Address","type" => 12,"validator" => sub { die sprintf("Too many arguments for subroutine at %s line %d.\n", (caller)[1, 2]) unless @_ <= 1; die sprintf("Too few arguments for subroutine at %s line %d.\n", (caller)[1, 2]) unless @_ >= 1; my $input = $_[0]package Wireguard::WGmeta::Validator; BEGIN {${^WARNING_BITS} = "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"} use strict; use feature 'signatures'; return 1;}},"alias" => {"in_config_name" => "Alias","type" => 10,"validator" => do{my $o}},"allowed-ips" => {"in_config_name" => "AllowedIPs","type" => 14,"validator" => do{my $o}},"checksum" => {"in_config_name" => "Checksum","type" => 10,"validator" => do{my $o}},"disabled" => {"in_config_name" => "Disabled","type" => 10,"validator" => do{my $o}},"dns" => {"in_config_name" => "DNS","type" => 12,"validator" => do{my $o}},"endpoint" => {"in_config_name" => "Endpoint","type" => 14,"validator" => do{my $o}},"fwmark" => {"in_config_name" => "Fwmark","type" => 13,"validator" => do{my $o}},"listen-port" => {"in_config_name" => "ListenPort","type" => 13,"validator" => sub { die sprintf("Too many arguments for subroutine at %s line %d.\n", (caller)[1, 2]) unless @_ <= 1; die sprintf("Too few arguments for subroutine at %s line %d.\n", (caller)[1, 2]) unless @_ >= 1; my $input = $_[0]package Wireguard::WGmeta::Validator; BEGIN {${^WARNING_BITS} = "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"} use strict; use feature 'signatures'; return looks_like_number $input;}},"mtu" => {"in_config_name" => "MTU","type" => 12,"validator" => do{my $o}},"persistent-keepalive" => {"in_config_name" => "PersistentKeepalive","type" => 14,"validator" => do{my $o}},"post-down" => {"in_config_name" => "PostDown","type" => 12,"validator" => do{my $o}},"post-up" => {"in_config_name" => "PostUP","type" => 12,"validator" => do{my $o}},"pre-down" => {"in_config_name" => "PreDown","type" => 12,"validator" => do{my $o}},"pre-up" => {"in_config_name" => "PreUp","type" => 12,"validator" => do{my $o}},"preshared-key" => {"in_config_name" => "PresharedKey","type" => 14,"validator" => do{my $o}},"private-key" => {"in_config_name" => "PrivateKey","type" => 13,"validator" => do{my $o}},"public-key" => {"in_config_name" => "PublicKey","type" => 14,"validator" => do{my $o}},"save-config" => {"in_config_name" => "SaveConfig","type" => 12,"validator" => do{my $o}},"table" => {"in_config_name" => "Table","type" => 12,"validator" => do{my $o}}};$v->{"alias"}{"validator"} = $v->{"address"}{"validator"};$v->{"allowed-ips"}{"validator"} = $v->{"address"}{"validator"};$v->{"checksum"}{"validator"} = $v->{"address"}{"validator"};$v->{"disabled"}{"validator"} = $v->{"address"}{"validator"};$v->{"dns"}{"validator"} = $v->{"address"}{"validator"};$v->{"endpoint"}{"validator"} = $v->{"address"}{"validator"};$v->{"fwmark"}{"validator"} = $v->{"address"}{"validator"};$v->{"mtu"}{"validator"} = $v->{"address"}{"validator"};$v->{"persistent-keepalive"}{"validator"} = $v->{"address"}{"validator"};$v->{"post-down"}{"validator"} = $v->{"address"}{"validator"};$v->{"post-up"}{"validator"} = $v->{"address"}{"validator"};$v->{"pre-down"}{"validator"} = $v->{"address"}{"validator"};$v->{"pre-up"}{"validator"} = $v->{"address"}{"validator"};$v->{"preshared-key"}{"validator"} = $v->{"address"}{"validator"};$v->{"private-key"}{"validator"} = $v->{"address"}{"validator"};$v->{"public-key"}{"validator"} = $v->{"address"}{"validator"};$v->{"save-config"}{"validator"} = $v->{"address"}{"validator"};$v->{"table"}{"validator"} = $v->{"address"}{"validator"}; \$v}->{$attribute} 27 12 39
287 12 ▶ return &{$self->{custom_attributes}{$attribute}{validator}}($value) if exists $self->{custom_attributes}{$attribute};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"custom_attributes"}{$attribute} 1 11 12
288 11 return 1;
289 }
290  
291 8 ▶sub _update_alias_map($self, $interface, $identifier, $alias) {
Subroutine _update_alias_map: called CC=2 SCAR=6.9
292 8 ▶ unless (exists $self->{parsed_config}{$interface}{INTERNAL_KEY_PREFIX . 'alias_map'}{$alias}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'parsed_config'}{$interface}{'int_alias_map'}{$alias}) { } 1 7 8
293 7 $self->{parsed_config}{$interface}{INTERNAL_KEY_PREFIX . 'alias_map'}{$alias} = $identifier;
294 }
295 else {
296 1 die "Alias `$alias` is already defined on interface `$interface`";
297 }
298 }
299  
300  
301 =head3 disable($interface, $identifier)
302  
303 Disables an interface/peer and setting the wg-meta attribute `Disabled` to C<1>.
304  
305 B<Parameters>
306  
307 =over 1
308  
309 =item *
310  
311 C<$interface> Valid interface name (e.g 'wg0').
312  
313 =item *
314  
315 C<$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,
316 its again the interface name.
317  
318 =back
319  
320 B<Returns>
321  
322 None
323  
324 =cut
325 3 ▶sub disable($self, $interface, $identifier,) {
Subroutine disable: called CC=1 SCAR=0.0
Pod documented
326 3 $self->_toggle($interface, $identifier, TRUE);
327 }
328  
329 =head3 enable($interface, $identifier)
330  
331 Inverse method if L</disable($interface, $identifier)>
332  
333 =cut
334 7 ▶sub enable($self, $interface, $identifier) {
Subroutine enable: called CC=1 SCAR=0.0
Pod documented
335 7 $self->_toggle($interface, $identifier, FALSE);
336 }
337  
338 # internal toggle method (DRY)
339 10 ▶sub _toggle($self, $interface, $identifier, $enable) {
Subroutine _toggle: called CC=1 SCAR=0.0
340 10 $identifier = $self->try_translate_alias($interface, $identifier);
341 # we can bypass an "expensive" set() here
342 10 $self->{parsed_config}{$interface}{$identifier}{'disabled'} = $enable;
343 10 $self->_set_changed($interface);
344 }
345  
346 =head3 is_valid_interface($interface)
347  
348 Checks if an interface name is valid (present in parsed config)
349  
350 B<Parameters>
351  
352 =over 1
353  
354 =item
355  
356 C<$interface> An interface name
357  
358 =back
359  
360 B<Returns>
361  
362 True if present, undef if not.
363  
364 =cut
365 59 ▶sub is_valid_interface($self, $interface) {
Subroutine is_valid_interface: called CC=1 SCAR=0.0
Pod documented
366 59 return (exists $self->{parsed_config}{$interface});
367 }
368  
369  
370 =head3 is_valid_alias($interface, $alias)
371  
372 Simply checks if an alias is valid for a given interface
373  
374 =cut
375 2 ▶sub is_valid_alias($self, $interface, $alias) {
Subroutine is_valid_alias: called CC=1 SCAR=0.0
Pod documented
376 2 return exists $self->{parsed_config}{$interface}{INTERNAL_KEY_PREFIX . 'alias_map'}{$alias}
377 }
378  
379 =head3 is_valid_identifier($interface, $identifier)
380  
381 Checks if an identifier is valid for a given interface
382  
383 B<Parameters>
384  
385 =over 1
386  
387 =item
388  
389 C<$interface> An interface name
390  
391 =item
392  
393 C<$identifier> An identifier (no alias!)
394  
395 =back
396  
397 B<Returns>
398  
399 True if present, undef if not.
400  
401 =cut
402 51 ▶sub is_valid_identifier($self, $interface, $identifier) {
Subroutine is_valid_identifier: called CC=1 SCAR=0.0
Pod documented
403 51 return (exists $self->{parsed_config}{$interface}{$identifier});
404 }
405  
406 =head3 try_translate_alias($interface, $may_alias)
407  
408 Tries to translate an identifier (which may be an alias).
409 no exception is thrown on failure, instead the C<$may_alias> is returned.
410  
411 B<Parameters>
412  
413 =over 1
414  
415 =item
416  
417 C<$interface> A valid interface name
418  
419 =item
420  
421 C<$may_alias> An identifier which could be a valid alias for this interface
422  
423 =back
424  
425 B<Returns>
426  
427 If the alias is valid for the specified interface, the corresponding identifier is returned, else C<$may_alias>
428  
429 =cut
430 62 ▶sub try_translate_alias($self, $interface, $may_alias) {
Subroutine try_translate_alias: called CC=2 SCAR=6.9
Pod documented
431 62 ▶ if (exists $self->{parsed_config}{$interface}{INTERNAL_KEY_PREFIX . 'alias_map'}{$may_alias}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'parsed_config'}{$interface}{'int_alias_map'}{$may_alias}) { } 5 57 62
432 5 return $self->{parsed_config}{$interface}{INTERNAL_KEY_PREFIX . 'alias_map'}{$may_alias};
433 }
434 else {
435 57 return $may_alias;
436 }
437 }
438  
439 =head3 get_all_conf_files($wireguard_home)
440  
441 Returns a list of all files in C<$wireguard_home> matching I</.*\.conf$/>.
442  
443 B<Parameters>
444  
445 =over 1
446  
447 =item
448  
449 C<$wireguard_home> Path to a folder where wireguard configuration files are located
450  
451 =back
452  
453 B<Returns>
454  
455 A reference to a list with absolute paths to the config files (possibly empty)
456  
457 =cut
458 10 ▶sub get_all_conf_files($wireguard_home) {
Subroutine get_all_conf_files: called CC=2 SCAR=7.4
Pod documented
459 10 my @config_files = read_dir($wireguard_home, qr/.*\.conf$/);
460 10 ▶ if (@config_files == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@config_files == 0) 0 10 10
461 0 die "No matching interface configuration(s) in " . $wireguard_home;
462 }
463 10 my $count = @config_files;
464 10 return \@config_files, $count;
465 }
466  
467  
468 =head3 commit([$is_hot_config = FALSE, $no_checksum = FALSE])
469  
470 Writes down the parsed config to the wireguard configuration folder
471  
472 B<Parameters>
473  
474 =over 1
475  
476 =item
477  
478 C<[$is_hot_config = FALSE])> If set to TRUE, the existing configuration is overwritten (and possibly existing, not applied configs are deleted). Otherwise,
479 the suffix '.not_applied' is appended to the filename
480  
481 =item
482  
483 C<[$no_checksum = FALSE])> If set to TRUE, no checksum is written
484  
485 =back
486  
487 B<Raises>
488  
489 Exception if: Folder or file is not writeable
490  
491 B<Returns>
492  
493 None
494  
495 =cut
496 7 ▶sub commit($self, $is_hot_config = FALSE, $no_checksum = FALSE) {
Subroutine commit: called CC=11 SCAR=24.0
Pod documented
497 7 for my $interface (keys %{$self->{parsed_config}}) {
498 14 ▶ if ($self->_has_changed($interface)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_has_changed($interface)) 7 7 14
499 7 my $new_config = create_wg_config2($self->{parsed_config}{$interface}, $self->{wg_meta_prefix}, $self->{wg_meta_disabled_prefix}, $no_checksum);
500 7 my $fh;
501 7 my $hot_path = $self->{wireguard_home} . $interface . '.conf';
502 7 my $safe_path = $self->{wireguard_home} . $interface . $self->{not_applied_suffix};
503 7 ▶ if ($is_hot_config == TRUE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_hot_config == 1) { } 6 1 7
504 6 ▶ open $fh, '>', $hot_path or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">", $hot_path or die $! 6 0 6
505 6 $self->{parsed_config}->{$interface}{INTERNAL_KEY_PREFIX . 'is_hot_config'} = 1;
506 }
507 else {
508 1 ▶ open $fh, '>', $safe_path or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">", $safe_path or die $! 1 0 1
509 1 $self->{parsed_config}->{$interface}{INTERNAL_KEY_PREFIX . 'is_hot_config'} = 0;
510 }
511 # write down to file
512 7 print $fh $new_config;
513 7 $self->_reset_changed($interface);
514 7 ▶ close $fh or die $!;
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die $! 7 0 7
515  
516 # if there is an not applied version around delete it (if is_hot_config = True)
517 7 ▶ if (-e $safe_path && $is_hot_config) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $safe_path and $is_hot_config) 1 6 7
Condition100%3/3
-e $safe_path and $is_hot_config
!ll&&!rl&&r
511
MC/DC100%2/2
-e $safe_path and $is_hot_config
-e $safe_path $is_hot_config
Truth table100%3/3
A and B
A= -e $safe_pathB= $is_hot_config
ABresult
0X0
100
111
518 1 unlink $safe_path;
519 }
520 # Notify listeners about a file change
521 7 $self->_call_reload_listeners($interface);
522 }
523 }
524 }
525  
526  
527 =head3 get_interface_list()
528  
529 Return a list of all interfaces.
530  
531 B<Returns>
532  
533 A list of all valid interface names. If no interfaces are available, an empty list is returned
534  
535 =cut
536 4 ▶sub get_interface_list($self) {
Subroutine get_interface_list: called CC=1 SCAR=0.0
Pod documented
537 4 return sort keys %{$self->{parsed_config}};
538 }
539  
540 =head3 get_interface_section($interface, $identifier)
541  
542 Returns a hash representing a section of a given interface
543  
544 B<Parameters>
545  
546 =over 1
547  
548 =item *
549  
550 C<$interface> Valid interface name
551  
552 =item *
553  
554 C<$identifier> Valid section identifier
555  
556 =back
557  
558 B<Returns>
559  
560 A hash containing the requested section. If the requested section/interface is not present, an empty hash is returned.
561  
562 =cut
563 3 ▶sub get_interface_section($self, $interface, $identifier) {
Subroutine get_interface_section: called CC=2 SCAR=7.2
Pod documented
564 3 $identifier = $self->try_translate_alias($interface, $identifier);
565 3 ▶ if (exists $self->{parsed_config}{$interface}{$identifier}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'parsed_config'}{$interface}{$identifier}) { } 3 0 3
566 3 my %r = %{$self->{parsed_config}{$interface}{$identifier}};
567 3 return %r;
568 }
569 else {
570 0 return ();
571 }
572 }
573  
574 =head3 get_section_list($interface)
575  
576 Returns a list of valid sections of an interface (ordered as in the original config file).
577  
578 B<Parameters>
579  
580 =over 1
581  
582 =item *
583  
584 C<$interface> A valid interface name
585  
586 =back
587  
588 B<Returns>
589  
590 A list of all sections of an interface. If interface is not present, an empty list is returned.
591  
592 =cut
593 5 ▶sub get_section_list($self, $interface) {
Subroutine get_section_list: called CC=2 SCAR=6.9
Pod documented
594 5 ▶ if ($self->is_valid_interface($interface)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_valid_interface($interface)) { } 4 1 5
595 4 return @{$self->{parsed_config}{$interface}{INTERNAL_KEY_PREFIX . 'section_order'}};
596 }
597 else {
598 1 return ();
599 }
600 }
601  
602 0 ▶sub get_wg_meta_prefix($self) {
Subroutine get_wg_meta_prefix: not called CC=1 SCAR=6.9
Pod undocumented
603 0 return $self->{wg_meta_prefix};
604 }
605  
606 0 ▶sub get_disabled_prefix($self) {
Subroutine get_disabled_prefix: not called CC=1 SCAR=6.9
Pod undocumented
607 0 return $self->{wg_meta_disabled_prefix};
608 }
609  
610 =head3 add_interface($interface_name, $ip_address, $listen_port, $private_key)
611  
612 Adds a (minimally configured) interface. If more attributes are needed, please set them using the C<set()> method.
613  
614 B<Caveat:> No validation is performed on the values!
615  
616 B<Parameters>
617  
618 =over 1
619  
620 =item *
621  
622 C<$interface_name> A new interface name, must be unique.
623  
624 =item *
625  
626 C<$ip_address> A string describing the ip net(s) (e.g '10.0.0.0/24, fdc9:281f:04d7:9ee9::2/64')
627  
628 =item *
629  
630 C<$listen_port> The listen port for this interface.
631  
632 =item *
633  
634 C<$private_key> A private key for this interface
635  
636 =back
637  
638 B<Raises>
639  
640 An exception if the interface name already exists.
641  
642 B<Returns>
643  
644 None
645  
646 =cut
647 1 ▶sub add_interface($self, $interface_name, $ip_address, $listen_port, $private_key) {
Subroutine add_interface: called CC=2 SCAR=7.0
Pod documented
648 1 ▶ if ($self->is_valid_interface($interface_name)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_valid_interface($interface_name)) 0 1 1
649 0 die "Interface `$interface_name` already exists";
650 }
651 1 my %interface = (
652 'address' => $ip_address,
653 'listen-port' => $listen_port,
654 'private-key' => $private_key,
655 INTERNAL_KEY_PREFIX . 'type' => 'Interface',
656 INTERNAL_KEY_PREFIX . 'order' => [ 'address', 'listen-port', 'private-key' ]
657 );
658 1 $self->{parsed_config}{$interface_name}{$interface_name} = \%interface;
659 1 $self->{parsed_config}{$interface_name}{INTERNAL_KEY_PREFIX . 'alias_map'} = {};
660 1 $self->{parsed_config}{$interface_name}{INTERNAL_KEY_PREFIX . 'section_order'} = [ $interface_name ];
661 1 $self->{parsed_config}{$interface_name}{checksum} = 'none';
662 1 $self->{parsed_config}{$interface_name}{INTERNAL_KEY_PREFIX . 'mtime'} = 0.0;
663 1 $self->{parsed_config}{$interface_name}{INTERNAL_KEY_PREFIX . 'config_path'} = $self->{wireguard_home} . $interface_name . '.conf';
664 1 $self->{parsed_config}{$interface_name}{has_changed} = 1;
665  
666 }
667  
668 =head3 add_peer($interface, $ip_address, $public_key [, $alias, $preshared_key])
669  
670 Adds a peer to an exiting interface.
671  
672 B<Parameters>
673  
674 =over 1
675  
676 =item *
677  
678 C<$interface> A valid interface.
679  
680 =item *
681  
682 C<$ip_address> A string describing the ip-address(es) of this this peer.
683  
684 =item *
685  
686 C<$public_key> Public-key for this interface. This becomes the identifier of this peer.
687  
688 =item *
689  
690 C<[$preshared_key]> Optional argument defining the psk.
691  
692 =item *
693  
694 C<[$alias]> Optional argument defining an alias for this peer (wg-meta)
695  
696 =back
697  
698 B<Raises>
699  
700 An exception if either the interface is invalid, the alias is already assigned or the public-key is
701 already present on an other peer.
702  
703 B<Returns>
704  
705 A tuple consisting of the iface private-key and listen port
706  
707 =cut
708 5 ▶sub add_peer($self, $interface, $ip_address, $public_key, $alias = undef, $preshared_key = undef) {
Subroutine add_peer: called CC=7 SCAR=20.2
Pod documented
709 # generate new key pair if not defined
710 5 ▶ if ($self->is_valid_interface($interface)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_valid_interface($interface)) { } 5 0 5
711 5 ▶ if ($self->is_valid_identifier($interface, $public_key)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_valid_identifier($interface, $public_key)) 0 5 5
712 0 die "An interface with this public-key already exists on `$interface`";
713 }
714 # generate peer config
715 5 my %peer = ();
716 5 $self->{parsed_config}{$interface}{$public_key} = \%peer;
717 5 $self->set($interface, $public_key, 'public-key', $public_key);
718 5 $self->set($interface, $public_key, 'allowed-ips', $ip_address);
719 5 ▶ if (defined $alias) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $alias) 4 1 5
720 4 $self->set($interface, $public_key, 'alias', $alias);
721 }
722 5 ▶ if (defined $preshared_key) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $preshared_key) 0 5 5
723 0 $self->set($interface, $public_key, 'preshared-key', $preshared_key);
724 }
725 5 $self->enable($interface, $public_key);
726 # set type to to Peer
727 5 $self->{parsed_config}{$interface}{$public_key}{INTERNAL_KEY_PREFIX . 'type'} = 'Peer';
728 # add section to global section list
729 5 push @{$self->{parsed_config}{$interface}{INTERNAL_KEY_PREFIX . 'section_order'}}, $public_key;
730 5 return $self->{parsed_config}{$interface}{$interface}{'private-key'}, $self->{parsed_config}{$interface}{$interface}{'listen-port'};
731 }
732 else {
733 0 die "Invalid interface `$interface`";
734 }
735 }
736  
737 =head3 remove_peer($interface, $identifier)
738  
739 Removes a peer (identified by it's public key or alias) from an interface.
740  
741 B<Parameters>
742  
743 =over 1
744  
745 =item
746  
747 C<$interface> A valid interface name
748  
749 =item
750  
751 C<$identifier> A valid identifier (or an alias)
752  
753 =back
754  
755 B<Raises>
756  
757 Exception if interface or identifier is invalid
758  
759 B<Returns>
760  
761 None
762  
763 =cut
764 2 ▶sub remove_peer($self, $interface, $identifier) {
Subroutine remove_peer: called CC=5 SCAR=16.4
Pod documented
765 2 ▶ if ($self->is_valid_interface($interface)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_valid_interface($interface)) { } 2 0 2
766 2 $identifier = $self->try_translate_alias($interface, $identifier);
767 2 ▶ if ($self->is_valid_identifier($interface, $identifier)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_valid_identifier($interface, $identifier)) { } 2 0 2
768  
769 # delete section
770 2 delete $self->{parsed_config}{$interface}{$identifier};
771  
772 # delete from section list
773 2 $self->{parsed_config}{$interface}{INTERNAL_KEY_PREFIX . 'section_order'} = [ grep {$_ ne $identifier} @{$self->{parsed_config}{$interface}{INTERNAL_KEY_PREFIX . 'section_order'}} ];
774  
775 # decrease peer count
776 2 $self->{parsed_config}{$interface}{INTERNAL_KEY_PREFIX . 'n_peers'}--;
777  
778 # delete alias (if exists)
779 2 ▶ while (my ($alias, $a_identifier) = each %{$self->{parsed_config}{$interface}{INTERNAL_KEY_PREFIX . 'alias_map'}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"parsed_config"}{$interface}{"int_alias_map"};}) 3 2 5
780 3 ▶ if ($a_identifier eq $identifier) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a_identifier eq $identifier) 2 1 3
781 2 delete $self->{parsed_config}{$interface}{INTERNAL_KEY_PREFIX . 'alias_map'}{$alias};
782 }
783 }
784 2 $self->_set_changed($interface);
785 }
786 else {
787 0 die "Invalid identifier `$identifier` for `$interface`";
788 }
789 }
790 else {
791 0 die "Invalid interface `$interface`";
792 }
793 }
794  
795 =head3 remove_interface($interface [, $keep_file = FALSE])
796  
797 Removes an interface. This command deletes the config file immediately. I.e no rollback possible!
798  
799 B<Parameters>
800  
801 =over 1
802  
803 =item
804  
805 C<$interface> A valid interface name
806  
807 =back
808  
809 B<Raises>
810  
811 Exception if interface or identifier is invalid
812  
813 B<Returns>
814  
815 None
816  
817 =cut
818 3 ▶sub remove_interface($self, $interface) {
Subroutine remove_interface: called CC=4 SCAR=14.6
Pod documented
819 3 ▶ if ($self->is_valid_interface($interface)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_valid_interface($interface)) 3 0 3
820 # delete interface
821 3 delete $self->{parsed_config}{$interface};
822 3 ▶ if (-e "$self->{wireguard_home}$interface.conf") {
Branch: 1 branch
BranchTrueFalseTotal
if (-e qq[$self->{"wireguard_home"}$interface.conf]) 3 0 3
823 3 ▶ unlink "$self->{wireguard_home}$interface.conf" or warn "Could not delete `$self->{wireguard_home}$interface.conf` do you have the needed permissions?";
Branch: 1 branch
BranchTrueFalseTotal
unlink qq[$self->{"wireguard_home"}$interface.conf] or warn qq[Could not delete `$self->{"wireguard_home"}$interface.conf` do you have the needed permissions?] 3 0 3
824 }
825 3 $self->{n_conf_files}--;
826 }
827 }
828  
829 =head3 get_peer_count([$interface = undef])
830  
831 Returns the number of peers.
832  
833 B<Caveat:> Does return the count represented in the current (parsed) configuration state.
834  
835 B<Parameters>
836  
837 =over 1
838  
839 =item
840  
841 C<[$interface = undef]> If defined and valid, only return counts for this specific interface
842  
843 =back
844  
845 B<Returns>
846  
847 Number of peers
848  
849 =cut
850 3 ▶sub get_peer_count($self, $interface = undef) {
Subroutine get_peer_count: called CC=5 SCAR=16.1
Pod documented
851 3 ▶ if (defined $interface && $self->is_valid_interface($interface)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $interface and $self->is_valid_interface($interface)) { } 2 1 3
Condition66%2/3
defined $interface and $self->is_valid_interface($interface)
!ll&&!rl&&r
102
MC/DC50%1/2
defined $interface and $self->is_valid_interface($interface)
defined $interface $self->is_valid_interface($interface)
Truth table66%2/3
A and B
A= defined $interfaceB= $self->is_valid_interface($interface)
ABresult
0X0
100
111
852 2 return $self->{parsed_config}{$interface}{INTERNAL_KEY_PREFIX . 'n_peers'};
853 }
854 else {
855 1 my $count = 0;
856 1 for ($self->get_interface_list()) {
857 2 $count += $self->{parsed_config}{$_}{INTERNAL_KEY_PREFIX . 'n_peers'};
858 }
859 1 return $count;
860 }
861 }
862  
863 =head3 may_reload_from_disk($interface [, $new = FALSE])
864  
865 Method to reload an interface configuration from disk. Also useful to add an new (externally) created
866 interface on-the-fly. If a config file with a I<.not_applied> suffix is present (and its mtime is newer
867 than the original one), it is taken as source for reloading the configuration data.
868  
869 B<Parameters>
870  
871 =over 1
872  
873 =item *
874  
875 C<$interface> A valid interface name
876  
877 =item *
878  
879 C<[$new = FALSE]> If set to True, the parser looks at C<$wireguard_home> for this new interface config.
880  
881 =item *
882  
883 C<[$force = FALSE]> When set to True, the configuration is reloaded regardless of its mtime.
884  
885 =back
886  
887 B<Raises>
888  
889 Exception: If the interface is invalid (or the config file is not found)
890  
891 B<Returns>
892  
893 None, or undef if C<$new == True> and the interface is in fact not a wg config.
894  
895 =cut
896 149 ▶sub may_reload_from_disk($self, $interface, $new = FALSE, $force = FALSE, $_init = FALSE) {
Subroutine may_reload_from_disk: called CC=19 SCAR=29.9
Pod documented
897 149 my $config_path = $self->{wireguard_home} . $interface . '.conf';
898 # check if there is a newer, not applied version, if yes prefer this version
899 149 my $not_applied_path = $self->{wireguard_home} . $interface . $self->{not_applied_suffix};
900 149 ▶ if (-e $not_applied_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $not_applied_path) 1 148 149
901 1 ▶ if (get_mtime($not_applied_path) > get_mtime($config_path)) {
Branch: 1 branch
BranchTrueFalseTotal
if (get_mtime($not_applied_path) > get_mtime($config_path)) 1 0 1
902 1 $config_path = $not_applied_path;
903 }
904 }
905 149 ▶ if ($new == FALSE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new == 0) { } 54 95 149
906 # do not use is_valid_interface() here otherwise there is a risk of infinite recursion (in a concurrent environment)
907 54 ▶ if (exists $self->{parsed_config}{$interface}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'parsed_config'}{$interface}) { } 54 0 54
908 # we only reload if the on-disk version is newer than our local one
909 # There is however one exception: The local config is based on a not applied version and this file somehow
910 # unexpectedly deleted (e.g by a sysadmin..)
911 54 my $on_disk_mtime = get_mtime($config_path);
912 my $unexpected_delete = (exists $self->{parsed_config}{$interface}{INTERNAL_KEY_PREFIX . 'is_hot_config'}
913 && $self->{parsed_config}{$interface}{INTERNAL_KEY_PREFIX . 'is_hot_config'} == 0
914 54 ▶ && $self->{parsed_config}{$interface}{INTERNAL_KEY_PREFIX . 'mtime'} > $on_disk_mtime);
Condition83%5/6
exists $self->{'parsed_config'}{$interface}{'int_is_hot_config'} && $self->{'parsed_config'}{$interface}{'int_is_hot_config'} == 0 && $self->{'parsed_config'}{$interface}{'int_mtime'} > $on_disk_mtime
!ll&&!rl&&r
5310
exists $self->{"parsed_config"}{$interface}{"int_is_hot_config"} and $self->{"parsed_config"}{$interface}{"int_is_hot_config"} == 0
!ll&&!rl&&r
1521
MC/DC0%0/3
exists $self->{'parsed_config'}{$interface}{'int_is_hot_config'} && $self->{'parsed_config'}{$interface}{'int_is_hot_config'} == 0 && $self->{'parsed_config'}{$interface}{'int_mtime'} > $on_disk_mtime
exists $self->{"parsed_config"}{$interface}{"int_is_hot_config"} $self->{"parsed_config"}{$interface}{"int_is_hot_config"} == 0 $self->{'parsed_config'}{$interface}{'int_mtime'} > $on_disk_mtime
Truth table75%3/4
A and B && C
A= exists $self->{"parsed_config"}{$interface}{"int_is_hot_config"}B= $self->{"parsed_config"}{$interface}{"int_is_hot_config"} == 0C= $self->{'parsed_config'}{$interface}{'int_mtime'} > $on_disk_mtime
ABCresult
0XX0
10X0
1100
1111
915  
916 54 ▶ if ($force || $unexpected_delete || $self->{parsed_config}{$interface}{INTERNAL_KEY_PREFIX . 'mtime'} < $on_disk_mtime) {
Branch: 1 branch
BranchTrueFalseTotal
if ($force or $unexpected_delete or $self->{"parsed_config"}{$interface}{"int_mtime"} < $on_disk_mtime) 12 42 54
Condition83%5/6
$force or $unexpected_delete or $self->{"parsed_config"}{$interface}{"int_mtime"} < $on_disk_mtime
l!l&&r!l&&!r
11142
$force or $unexpected_delete
l!l&&r!l&&!r
1053
MC/DC66%2/3
$force or $unexpected_delete or $self->{"parsed_config"}{$interface}{"int_mtime"} < $on_disk_mtime
$force $unexpected_delete $self->{"parsed_config"}{$interface}{"int_mtime"} < $on_disk_mtime
Truth table75%3/4
A or B or C
A= $forceB= $unexpected_deleteC= $self->{"parsed_config"}{$interface}{"int_mtime"} < $on_disk_mtime
ABCresult
1XX1
01X1
0011
0000
917 12 my $contents = read_file($config_path);
918 12 $self->{parsed_config}{$interface} = parse_wg_config2($contents, $interface, $self->{wg_meta_prefix}, $self->{wg_meta_disabled_prefix}, FALSE);
919 12 $self->{parsed_config}{$interface}{INTERNAL_KEY_PREFIX . 'config_path'} = $config_path;
920 12 $self->{parsed_config}{$interface}{INTERNAL_KEY_PREFIX . 'mtime'} = get_mtime($config_path);
921 12 ▶ $self->{parsed_config}{$interface}{INTERNAL_KEY_PREFIX . 'is_hot_config'} = ($config_path =~ /$self->{not_applied_suffix}/) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$config_path =~ /$self->{'not_applied_suffix'}/u ? : 1 11 12
922 12 ▶ $self->_call_reload_listeners($interface) if $_init == FALSE;
Branch: 1 branch
BranchTrueFalseTotal
if $_init == 0 12 0 12
923 }
924 }
925 else {
926 0 die "Invalid interface $interface - if this is a new interface, set `\$new` to True";
927 }
928 }
929 # We have a completely new interface
930 else {
931 95 ▶ if (-e $config_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $config_path) { } 95 0 95
932 95 my $contents = read_file($config_path);
933 95 my $maybe_new_config = parse_wg_config2($contents, $interface, $self->{wg_meta_prefix}, $self->{wg_meta_disabled_prefix}, FALSE);
934 95 ▶ if (defined $maybe_new_config) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $maybe_new_config) { } 21 74 95
935 21 $self->{n_conf_files}++;
936 21 $self->{parsed_config}{$interface} = $maybe_new_config;
937 21 $self->{parsed_config}{$interface}{INTERNAL_KEY_PREFIX . 'config_path'} = $config_path;
938 21 $self->{parsed_config}{$interface}{INTERNAL_KEY_PREFIX . 'mtime'} = get_mtime($config_path);
939 21 ▶ $self->{parsed_config}{$interface}{INTERNAL_KEY_PREFIX . 'is_hot_config'} = ($config_path =~ /$self->{not_applied_suffix}/) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$config_path =~ /$self->{'not_applied_suffix'}/u ? : 0 21 21
940 21 ▶ $self->_call_reload_listeners($interface) if $_init == FALSE;;
Branch: 1 branch
BranchTrueFalseTotal
if $_init == 0 1 20 21
941 }
942 else {
943 74 return undef;
944 }
945 }
946 else {
947 0 die "The interface $interface was not found in $self->{wireguard_home}";
948 }
949  
950 }
951  
952 }
953  
954 # internal method to create a configuration file (this method exists primarily for testing purposes)
955 4 ▶sub create_config($self, $interface, $plain = FALSE) {
Subroutine create_config: called CC=2 SCAR=6.9
Pod undocumented
956 return create_wg_config2(
957 $self->{parsed_config}{$interface},
958 $self->{wg_meta_prefix},
959 $self->{wg_meta_disabled_prefix},
960 4 $plain = $plain)
961 }
962  
963 39 ▶sub _has_changed($self, $interface) {
Subroutine _has_changed: called CC=1 SCAR=0.0
964 39 return exists $self->{parsed_config}{$interface}{has_changed};
965 }
966  
967 48 ▶sub _set_changed($self, $interface) {
Subroutine _set_changed: called CC=1 SCAR=0.0
968 48 $self->{parsed_config}{$interface}{has_changed} = 1;
969 }
970  
971 17 ▶sub _reset_changed($self, $interface) {
Subroutine _reset_changed: called CC=2 SCAR=7.6
972 17 ▶ delete $self->{parsed_config}{$interface}{has_changed} if (exists $self->{parsed_config}{$interface}{has_changed});
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"parsed_config"}{$interface}{"has_changed"} 17 0 17
973 }
974  
975 =head3 register_on_reload_listener($ref_handler, $handler_id [, $ref_listener_args = []])
976  
977 Register your callback handlers for the C<reload_from_disk()> event here. Your handler is called
978 B<after> the reload happened, is blocking and exceptions are caught in an C<eval{};> environment.
979  
980 B<Parameters>
981  
982 =over 1
983  
984 =item
985  
986 C<$ref_handler> Reference to a handler function. The following signature is expected:
987  
988 sub my_handler_function($interface, $ref_list_args){
989 ...
990 }
991  
992 =item
993  
994 C<$handler_id> An identifier for you handler function. Must be unique!
995  
996 =item
997  
998 C<[$ref_listener_args = []]> A reference to an argument list for your handler function
999  
1000 =back
1001  
1002 B<Returns>
1003  
1004 None, exception if C<$handler_id> is already present.
1005  
1006 =cut
1007 1 ▶sub register_on_reload_listener($self, $ref_handler, $handler_id, $ref_listener_args = []) {
Subroutine register_on_reload_listener: called CC=3 SCAR=11.7
Pod documented
1008 1 ▶ unless ($self->{reload_listeners}{$handler_id}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'reload_listeners'}{$handler_id}) { } 0 1 1
1009 1 my $listener_data = {
1010 'handler' => $ref_handler,
1011 'args' => $ref_listener_args
1012 };
1013 1 $self->{reload_listeners}{$handler_id} = $listener_data;
1014 }
1015 else {
1016 0 die "Handler id $handler_id already present";
1017 }
1018  
1019 }
1020  
1021 =head3 remove_on_reload_listener($handler_id)
1022  
1023 Removes a reload callback handler by it's C<$handler_id>.
1024  
1025 B<Parameters>
1026  
1027 =over 1
1028  
1029 =item
1030  
1031 C<$handler_id> A valid handler id
1032  
1033 =back
1034  
1035 B<Returns>
1036  
1037 1 on success, undef on failure.
1038  
1039 =cut
1040 0 ▶sub remove_on_reload_listener($self, $handler_id) {
Subroutine remove_on_reload_listener: not called CC=2 SCAR=17.9
Pod documented
1041 0 ▶ if (exists $self->{reload_listeners}{$handler_id}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'reload_listeners'}{$handler_id}) { } 0 0 0
1042 0 delete $self->{reload_listeners}{$handler_id};
1043 0 return 1;
1044 }
1045 else {
1046 0 return undef;
1047 }
1048 }
1049  
1050 30 ▶sub _call_reload_listeners($self, $interface) {
Subroutine _call_reload_listeners: called CC=3 SCAR=11.3
1051 30 for my $listener_id (keys %{$self->{reload_listeners}}) {
1052 1 eval {
1053 1 &{$self->{reload_listeners}{$listener_id}{handler}}($interface, $self->{reload_listeners}{$listener_id}{args});
1054 };
1055 1 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
1056 0 warn "Call to reload_listener $listener_id failed: $@";
1057 }
1058 }
1059 }
1060  
1061  
1062 5 1;
« lib/Wireguard/WGmeta/Parser/Conf.pm ↑ Summary lib/Wireguard/WGmeta/Validator.pm »