line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
=pod |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=head1 NAME |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
WGmeta::Parser::Middleware - Middleware between the parser and wrapper class(es) |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 SYNOPSIS |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
use Wireguard::WGmeta::Parser::Middleware; |
10
|
|
|
|
|
|
|
use Wireguard::WGmeta::Util; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
# Parse a wireguard configuration file |
13
|
|
|
|
|
|
|
my $config_contents = read_file('/path/to/config.conf', 'interface_name'); |
14
|
|
|
|
|
|
|
my $parsed_config = parse_wg_config2($config_contents); |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
# And convert it to string representation again |
17
|
|
|
|
|
|
|
my $new_config_content = create_wg_config2($parsed_config); |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
=head1 DESCRIPTION |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
Acts as a middleware between L and L. Most importantly it |
22
|
|
|
|
|
|
|
implements the I and I callbacks of L. |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
=head1 METHODS |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
=cut |
27
|
|
|
|
|
|
|
package Wireguard::WGmeta::Parser::Middleware; |
28
|
5
|
|
|
5
|
|
38
|
use strict; |
|
5
|
|
|
|
|
11
|
|
|
5
|
|
|
|
|
180
|
|
29
|
5
|
|
|
5
|
|
25
|
use warnings FATAL => 'all'; |
|
5
|
|
|
|
|
33
|
|
|
5
|
|
|
|
|
188
|
|
30
|
5
|
|
|
5
|
|
28
|
use experimental qw(signatures); |
|
5
|
|
|
|
|
9
|
|
|
5
|
|
|
|
|
51
|
|
31
|
|
|
|
|
|
|
|
32
|
5
|
|
|
5
|
|
3008
|
use Wireguard::WGmeta::Parser::Conf qw(INTERNAL_KEY_PREFIX parse_raw_wg_config); |
|
5
|
|
|
|
|
15
|
|
|
5
|
|
|
|
|
332
|
|
33
|
5
|
|
|
5
|
|
2373
|
use Wireguard::WGmeta::ValidAttributes; |
|
5
|
|
|
|
|
15
|
|
|
5
|
|
|
|
|
365
|
|
34
|
5
|
|
|
5
|
|
1910
|
use Wireguard::WGmeta::Utils; |
|
5
|
|
|
|
|
14
|
|
|
5
|
|
|
|
|
360
|
|
35
|
|
|
|
|
|
|
|
36
|
5
|
|
|
5
|
|
36
|
use base 'Exporter'; |
|
5
|
|
|
|
|
11
|
|
|
5
|
|
|
|
|
5741
|
|
37
|
|
|
|
|
|
|
our @EXPORT = qw(parse_wg_config2 create_wg_config2); |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
our $VERSION = "0.3.2"; |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
=head3 parse_wg_config2($config_file_content, $interface_name [, $wg_meta_prefix, $disabled_prefix, $use_checksum]) |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
Using the I and I, this method enriches the parsed config by several artefacts: |
44
|
|
|
|
|
|
|
I, I, and I. Considering this minimal config example: |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
#+root_attr1 = value1 |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
[Interface] |
49
|
|
|
|
|
|
|
ListenPort = 12345 |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
[Peer] |
52
|
|
|
|
|
|
|
#+Alias = some_alias |
53
|
|
|
|
|
|
|
PublicKey = peer_1 |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
We end up with the following structure: |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
{ |
58
|
|
|
|
|
|
|
'root_attr1' => value1, |
59
|
|
|
|
|
|
|
INT_PREFIX.'root_order' => [ |
60
|
|
|
|
|
|
|
'root_attr1' |
61
|
|
|
|
|
|
|
], |
62
|
|
|
|
|
|
|
INT_PREFIX.'section_order' => [ |
63
|
|
|
|
|
|
|
'interface_1', |
64
|
|
|
|
|
|
|
'peer_1' |
65
|
|
|
|
|
|
|
], |
66
|
|
|
|
|
|
|
INT_PREFIX.'n_peers' => 1, |
67
|
|
|
|
|
|
|
INT_PREFIX.'observer_wg_meta_attrs => { |
68
|
|
|
|
|
|
|
'alias' => 1 |
69
|
|
|
|
|
|
|
}, |
70
|
|
|
|
|
|
|
INT_PREFIX.'alias_map => { |
71
|
|
|
|
|
|
|
'some_alias' => 'peer_1' |
72
|
|
|
|
|
|
|
}, |
73
|
|
|
|
|
|
|
'interface_name => 'interface_name', |
74
|
|
|
|
|
|
|
'interface_1 => { |
75
|
|
|
|
|
|
|
'listen-port' => 12345, |
76
|
|
|
|
|
|
|
INT_PREFIX.'type' => 'Interface', |
77
|
|
|
|
|
|
|
INT_PREFIX.'order' => [ |
78
|
|
|
|
|
|
|
'listen-port', |
79
|
|
|
|
|
|
|
] |
80
|
|
|
|
|
|
|
}, |
81
|
|
|
|
|
|
|
'peer_1' => { |
82
|
|
|
|
|
|
|
'alias' => 'some_alias', |
83
|
|
|
|
|
|
|
INT_PREFIX.'type => 'Peer', |
84
|
|
|
|
|
|
|
INT_PREFIX.'order => [ |
85
|
|
|
|
|
|
|
'alias', |
86
|
|
|
|
|
|
|
] |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
B |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=over 1 |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=item * |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
All attributes listed in L are referenced by their key. This means, if you want for |
97
|
|
|
|
|
|
|
example access I the key would be I. Any attribute not present in L |
98
|
|
|
|
|
|
|
is stored (and written back) as they appear in Config. |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=item * |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
This method can be used as stand-alone in conjunction with the L<> |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=item * |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
If the section is of type 'Peer' the identifier equals to its public-key, otherwise its the interface name again. |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=item * |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
wg-meta attributes are always prefixed with C<$wg_meta_prefix>. |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=back |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
B |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=over 1 |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=item * |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
C<$config_file_content> String containing the contents of a Wireguard configuration file. |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
=item * |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
C<$interface_name> Interface name |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=item * |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
C<[$wg_meta_prefix = '#+']> wg-meta prefix. Must start with '#' or ';' |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=item * |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
C<[$disabled_prefix = '#-']> disabled prefix. Must start with '#' or ';' |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=item * |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
C<[$use_checksum = TRUE]> If set to False, checksum is not checked |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=back |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
B |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
An exceptions if: |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=over 1 |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=item * |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
If the parser ends up in an invalid state (e.g a section without information). Or An alias is defined twice. |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=back |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
A warning: |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=over 1 |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=item * |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
On a checksum mismatch |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=back |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
B |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
A reference to a hash with the structure described above. Or if the configuration file is not a Wireguard configuration: undef. |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=cut |
169
|
116
|
|
|
116
|
1
|
330
|
sub parse_wg_config2($config_file_content, $interface_name, $wg_meta_prefix = '#+', $disabled_prefix = '#-', $use_checksum = 1) { |
|
116
|
|
|
|
|
223
|
|
|
116
|
|
|
|
|
232
|
|
|
116
|
|
|
|
|
211
|
|
|
116
|
|
|
|
|
195
|
|
|
116
|
|
|
|
|
177
|
|
|
116
|
|
|
|
|
230
|
|
170
|
|
|
|
|
|
|
|
171
|
116
|
100
|
|
|
|
542
|
return undef unless ($config_file_content =~ /\[Interface\]/); |
172
|
|
|
|
|
|
|
|
173
|
42
|
|
|
|
|
91
|
my %alias_map; |
174
|
|
|
|
|
|
|
my %observed_wg_meta_attrs; |
175
|
42
|
|
|
|
|
71
|
my $peer_count = 0; |
176
|
42
|
|
|
|
|
83
|
my $alias_to_consume; |
177
|
|
|
|
|
|
|
my $old_checksum; |
178
|
|
|
|
|
|
|
|
179
|
491
|
|
|
491
|
|
628
|
my $entry_handler = sub($raw_key, $raw_value, $is_wg_meta) { |
|
491
|
|
|
|
|
692
|
|
|
491
|
|
|
|
|
1103
|
|
|
491
|
|
|
|
|
727
|
|
|
491
|
|
|
|
|
665
|
|
180
|
491
|
|
|
|
|
781
|
my $final_key = $raw_key; |
181
|
491
|
|
|
|
|
692
|
my $final_value = $raw_value; |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
# Convert known Keys to attr-name style |
185
|
491
|
100
|
|
|
|
1429
|
$final_key = NAME_2_KEYS_MAPPING->{$raw_key} if exists NAME_2_KEYS_MAPPING->{$raw_key}; |
186
|
|
|
|
|
|
|
|
187
|
491
|
100
|
|
|
|
964
|
$observed_wg_meta_attrs{$final_key} = 1 if $is_wg_meta; |
188
|
|
|
|
|
|
|
# register alias to consume (if any) |
189
|
491
|
100
|
|
|
|
1019
|
$alias_to_consume = $raw_value if $raw_key eq 'Alias'; |
190
|
|
|
|
|
|
|
|
191
|
491
|
100
|
|
|
|
898
|
if ($raw_key eq 'Checksum') { |
192
|
17
|
|
|
|
|
31
|
$old_checksum = $raw_value; |
193
|
|
|
|
|
|
|
# discard old checksum |
194
|
17
|
|
|
|
|
89
|
return undef, undef, 1; |
195
|
|
|
|
|
|
|
} |
196
|
|
|
|
|
|
|
|
197
|
474
|
|
|
|
|
1280
|
return $final_key, $final_value, 0; |
198
|
42
|
|
|
|
|
302
|
}; |
199
|
|
|
|
|
|
|
|
200
|
115
|
|
|
115
|
|
174
|
my $new_section_handler = sub($identifier, $section_type, $is_active) { |
|
115
|
|
|
|
|
162
|
|
|
115
|
|
|
|
|
191
|
|
|
115
|
|
|
|
|
177
|
|
|
115
|
|
|
|
|
163
|
|
201
|
115
|
100
|
|
|
|
254
|
$peer_count++ if $section_type eq 'Peer'; |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
# Consume alias (if any) |
204
|
115
|
100
|
|
|
|
254
|
if (defined $alias_to_consume) { |
205
|
56
|
50
|
|
|
|
127
|
die "Alias `$alias_to_consume` is already defined on $interface_name" if exists $alias_map{$alias_to_consume}; |
206
|
56
|
|
|
|
|
126
|
$alias_map{$alias_to_consume} = $identifier; |
207
|
56
|
|
|
|
|
112
|
$alias_to_consume = undef; |
208
|
|
|
|
|
|
|
} |
209
|
|
|
|
|
|
|
|
210
|
115
|
100
|
|
|
|
318
|
return ($section_type eq 'Interface') ? $interface_name : $identifier; |
211
|
|
|
|
|
|
|
|
212
|
42
|
|
|
|
|
178
|
}; |
213
|
|
|
|
|
|
|
|
214
|
42
|
|
|
|
|
188
|
my $parsed_config = parse_raw_wg_config($config_file_content, $entry_handler, $new_section_handler, 0, $wg_meta_prefix, $disabled_prefix); |
215
|
42
|
|
|
|
|
102
|
$parsed_config->{INTERNAL_KEY_PREFIX . 'alias_map'} = \%alias_map; |
216
|
42
|
|
|
|
|
100
|
$parsed_config->{INTERNAL_KEY_PREFIX . 'n_peers'} = $peer_count; |
217
|
42
|
|
|
|
|
95
|
$parsed_config->{INTERNAL_KEY_PREFIX . 'interface_name'} = $interface_name; |
218
|
42
|
|
|
|
|
98
|
$parsed_config->{INTERNAL_KEY_PREFIX . 'observed_wg_meta_attrs'} = \%observed_wg_meta_attrs; |
219
|
|
|
|
|
|
|
|
220
|
42
|
100
|
100
|
|
|
163
|
if ($use_checksum == 1 && defined $old_checksum) { |
221
|
8
|
|
|
|
|
33
|
my $new_checksum = compute_md5_checksum(create_wg_config2($parsed_config, $wg_meta_prefix, $disabled_prefix, 1)); |
222
|
8
|
50
|
|
|
|
37
|
warn("Checksum mismatch `$interface_name` has been altered in the meantime") if not $new_checksum eq $old_checksum; |
223
|
|
|
|
|
|
|
} |
224
|
|
|
|
|
|
|
|
225
|
42
|
|
|
|
|
482
|
return $parsed_config; |
226
|
|
|
|
|
|
|
} |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
=head3 create_wg_config2($ref_interface_config [, $wg_meta_prefix, $disabled_prefix, $no_checksum]) |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
Turns a reference of interface-config hash (just a single interface!) back into a wireguard config. |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
B |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
=over 1 |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
=item * |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
C<$ref_interface_config> Reference to hash containing B interface config. |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
=item * |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
C<[$wg_meta_prefix = '#+']> Has to start with a '#' or ';' character and is ideally the |
243
|
|
|
|
|
|
|
same as in L |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
=item * |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
C<[$wg_meta_prefix = '#-']> Same restrictions as parameter C<$wg_meta_prefix> |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
=item * |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
C<[$no_checksum = FALSE]> If set to true, no header checksum is calculated and added to the output |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
=back |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
B |
256
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
A string, ready to be written down as a config file. |
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
=cut |
260
|
29
|
|
|
29
|
1
|
56
|
sub create_wg_config2($ref_interface_config, $wg_meta_prefix = '#+', $disabled_prefix = '#-', $no_checksum = 0) { |
|
29
|
|
|
|
|
57
|
|
|
29
|
|
|
|
|
65
|
|
|
29
|
|
|
|
|
57
|
|
|
29
|
|
|
|
|
52
|
|
|
29
|
|
|
|
|
46
|
|
261
|
29
|
|
|
|
|
67
|
my $new_config = ""; |
262
|
|
|
|
|
|
|
|
263
|
29
|
|
|
|
|
56
|
for my $identifier (@{$ref_interface_config->{INTERNAL_KEY_PREFIX . 'section_order'}}) { |
|
29
|
|
|
|
|
100
|
|
264
|
84
|
50
|
|
|
|
231
|
if (not ref($ref_interface_config->{$identifier}) eq 'HASH') { |
265
|
|
|
|
|
|
|
# We are in root section |
266
|
0
|
|
|
|
|
0
|
$new_config .= _write_line($identifier, $ref_interface_config->{$identifier}, '', $wg_meta_prefix); |
267
|
|
|
|
|
|
|
} |
268
|
|
|
|
|
|
|
else { |
269
|
|
|
|
|
|
|
# First lets check if the following section is active |
270
|
|
|
|
|
|
|
my $is_disabled = (exists $ref_interface_config->{$identifier}{'disabled'} |
271
|
84
|
100
|
100
|
|
|
374
|
and $ref_interface_config->{$identifier}{'disabled'} == 1) ? $disabled_prefix : ''; |
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
# Add [Interface] or [Peer] |
274
|
84
|
|
|
|
|
258
|
$new_config .= "\n$is_disabled" . "[$ref_interface_config->{$identifier}{INTERNAL_KEY_PREFIX . 'type'}]\n"; |
275
|
|
|
|
|
|
|
|
276
|
|
|
|
|
|
|
# Add config lines |
277
|
84
|
|
|
|
|
119
|
for my $attr_name (@{$ref_interface_config->{$identifier}{INTERNAL_KEY_PREFIX . 'order'}}) { |
|
84
|
|
|
|
|
216
|
|
278
|
|
|
|
|
|
|
|
279
|
362
|
100
|
|
|
|
718
|
my $is_wg_meta = (exists $ref_interface_config->{INTERNAL_KEY_PREFIX .'observed_wg_meta_attrs'}{$attr_name}) ? $wg_meta_prefix : ''; |
280
|
362
|
|
|
|
|
693
|
$new_config .= _write_line($attr_name, $ref_interface_config->{$identifier}{$attr_name}, $is_disabled, $is_wg_meta); |
281
|
|
|
|
|
|
|
} |
282
|
|
|
|
|
|
|
} |
283
|
|
|
|
|
|
|
} |
284
|
29
|
100
|
|
|
|
93
|
if ($no_checksum == 0) { |
285
|
10
|
|
|
|
|
37
|
return "#+Checksum = " . compute_md5_checksum($new_config) . "\n" . $new_config; |
286
|
|
|
|
|
|
|
} |
287
|
19
|
|
|
|
|
121
|
return $new_config; |
288
|
|
|
|
|
|
|
} |
289
|
|
|
|
|
|
|
|
290
|
|
|
|
|
|
|
# internal method to create on config line |
291
|
362
|
|
|
362
|
|
464
|
sub _write_line($attr_name, $attr_value, $is_disabled, $is_wg_meta) { |
|
362
|
|
|
|
|
514
|
|
|
362
|
|
|
|
|
487
|
|
|
362
|
|
|
|
|
511
|
|
|
362
|
|
|
|
|
487
|
|
|
362
|
|
|
|
|
470
|
|
292
|
362
|
|
|
|
|
547
|
my $cfg_line = ''; |
293
|
|
|
|
|
|
|
# if we have a comment |
294
|
362
|
100
|
|
|
|
764
|
if (substr($attr_name, 0, 7) eq 'comment') { |
295
|
9
|
|
|
|
|
23
|
$cfg_line .= $attr_value . "\n"; |
296
|
|
|
|
|
|
|
} |
297
|
|
|
|
|
|
|
else { |
298
|
353
|
100
|
|
|
|
817
|
my $inconfig_name = exists KNOWN_ATTRIBUTES->{$attr_name} ? KNOWN_ATTRIBUTES->{$attr_name}{in_config_name} : $attr_name; |
299
|
353
|
|
|
|
|
910
|
$cfg_line .= "$is_disabled$is_wg_meta$inconfig_name = $attr_value\n"; |
300
|
|
|
|
|
|
|
} |
301
|
362
|
|
|
|
|
897
|
return $cfg_line; |
302
|
|
|
|
|
|
|
} |
303
|
|
|
|
|
|
|
|
304
|
|
|
|
|
|
|
|
305
|
|
|
|
|
|
|
1; |