line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Wireguard::WGmeta::Cli::Commands::Set; |
2
|
1
|
|
|
1
|
|
7
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
33
|
|
3
|
1
|
|
|
1
|
|
6
|
use warnings FATAL => 'all'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
60
|
|
4
|
1
|
|
|
1
|
|
7
|
use experimental 'signatures'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
15
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
94
|
use parent 'Wireguard::WGmeta::Cli::Commands::Command'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
5
|
|
7
|
1
|
|
|
1
|
|
91
|
use Wireguard::WGmeta::Wrapper::Config; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
23
|
|
8
|
1
|
|
|
1
|
|
5
|
use constant TRUE => 1; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
66
|
|
9
|
1
|
|
|
1
|
|
7
|
use constant FALSE => 0; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
700
|
|
10
|
|
|
|
|
|
|
|
11
|
3
|
|
|
3
|
1
|
8
|
sub entry_point($self) { |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
6
|
|
12
|
3
|
50
|
|
|
|
16
|
if ($self->_retrieve_or_die($self->{input_args}, 0) eq 'help') { |
13
|
0
|
|
|
|
|
0
|
$self->cmd_help(); |
14
|
|
|
|
|
|
|
return |
15
|
0
|
|
|
|
|
0
|
} |
16
|
|
|
|
|
|
|
else { |
17
|
3
|
|
|
|
|
13
|
$self->check_privileges(); |
18
|
|
|
|
|
|
|
# would be very nice if we can set a type hint here...possible? |
19
|
3
|
|
|
|
|
13
|
$self->_run_command(); |
20
|
|
|
|
|
|
|
} |
21
|
|
|
|
|
|
|
} |
22
|
|
|
|
|
|
|
|
23
|
3
|
|
|
3
|
|
5
|
sub _run_command($self) { |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
5
|
|
24
|
3
|
|
|
|
|
10
|
my $interface = $self->_retrieve_or_die($self->{input_args}, 0); |
25
|
3
|
|
|
|
|
7
|
my $offset = -1; |
26
|
3
|
|
|
|
|
4
|
my $cur_start = 0; |
27
|
3
|
|
|
|
|
6
|
my @input_args = @{$self->{input_args}}; |
|
3
|
|
|
|
|
10
|
|
28
|
3
|
|
|
|
|
7
|
for my $value (@{$self->{input_args}}) { |
|
3
|
|
|
|
|
7
|
|
29
|
28
|
100
|
|
|
|
53
|
if ($value eq 'peer') { |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
# if there is just one peer we skip here |
32
|
5
|
100
|
|
|
|
12
|
if ($offset != 0) { |
33
|
3
|
|
|
|
|
14
|
$self->_apply_change_set($interface, @input_args[$cur_start .. $offset]); |
34
|
2
|
|
|
|
|
6
|
$cur_start = $offset; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
} |
37
|
27
|
|
|
|
|
44
|
$offset++; |
38
|
|
|
|
|
|
|
} |
39
|
2
|
|
|
|
|
10
|
$self->_apply_change_set($interface, @input_args[$cur_start .. $offset]); |
40
|
2
|
50
|
|
|
|
8
|
if (defined $ENV{IS_TESTING}) { |
41
|
|
|
|
|
|
|
# omit header |
42
|
2
|
|
|
|
|
8
|
$self->wg_meta->commit(1, 1); |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
else { |
45
|
0
|
|
|
|
|
0
|
$self->wg_meta->commit(1, 0); |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
# internal method to split commandline args into "change-sets". |
51
|
|
|
|
|
|
|
# This method is fully* compatible with the `wg set`-syntax. |
52
|
|
|
|
|
|
|
# *exception: remove |
53
|
5
|
|
|
5
|
|
7
|
sub _apply_change_set($self, $interface, @change_set) { |
|
5
|
|
|
|
|
8
|
|
|
5
|
|
|
|
|
10
|
|
|
5
|
|
|
|
|
10
|
|
|
5
|
|
|
|
|
9
|
|
54
|
5
|
|
|
|
|
7
|
my $offset = 1; |
55
|
5
|
|
|
|
|
8
|
my $identifier; |
56
|
5
|
100
|
|
|
|
16
|
if ($self->_retrieve_or_die(\@change_set, 1) eq 'peer') { |
57
|
|
|
|
|
|
|
# this could be either a public key or alias |
58
|
4
|
|
|
|
|
12
|
$identifier = $self->_retrieve_or_die(\@change_set, 2); |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
# try to resolve alias |
61
|
4
|
|
|
|
|
12
|
$identifier = $self->wg_meta->try_translate_alias($interface, $identifier); |
62
|
|
|
|
|
|
|
|
63
|
4
|
|
|
|
|
11
|
$offset += 2; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
else { |
66
|
1
|
|
|
|
|
3
|
$identifier = $interface; |
67
|
|
|
|
|
|
|
} |
68
|
5
|
|
|
|
|
13
|
my @value_keys = splice @change_set, $offset; |
69
|
|
|
|
|
|
|
|
70
|
5
|
50
|
|
|
|
18
|
if (@value_keys % 2 != 0) { |
71
|
0
|
|
|
|
|
0
|
die "Odd number of value/key-pairs"; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
# parse key/value - pairs into a hash |
74
|
5
|
|
|
|
|
9
|
my %args; |
75
|
5
|
|
|
|
|
8
|
my $idx = 0; |
76
|
|
|
|
|
|
|
|
77
|
5
|
|
|
|
|
12
|
while ($idx < @value_keys) { |
78
|
8
|
|
|
|
|
22
|
$args{$value_keys[$idx]} = $value_keys[$idx + 1]; |
79
|
8
|
|
|
|
|
17
|
$idx += 2; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
# print "Got command set: |
82
|
|
|
|
|
|
|
# interface: $interface |
83
|
|
|
|
|
|
|
# ident: $identifier |
84
|
|
|
|
|
|
|
# attrs: @value_keys |
85
|
|
|
|
|
|
|
# "; |
86
|
|
|
|
|
|
|
|
87
|
5
|
|
|
|
|
14
|
$self->_set_values($interface, $identifier, \%args); |
88
|
|
|
|
|
|
|
} |
89
|
0
|
|
|
0
|
1
|
0
|
sub cmd_help($self) { |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
90
|
0
|
|
|
|
|
0
|
print "Usage: wg-meta set [attr1 value1] [attr2 value2] [peer {alias|public-key}] [attr1 value1] [attr2 value2] ...\n\n" |
91
|
|
|
|
|
|
|
. "Notes:\n The interface aims to follow the official `wg set` specification\n" |
92
|
|
|
|
|
|
|
. "To introduce new attributes prefix them with `+`"; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
5
|
|
|
5
|
|
8
|
sub _set_values($self, $interface, $identifier, $ref_hash_values) { |
|
5
|
|
|
|
|
9
|
|
|
5
|
|
|
|
|
9
|
|
|
5
|
|
|
|
|
7
|
|
|
5
|
|
|
|
|
10
|
|
|
5
|
|
|
|
|
7
|
|
96
|
5
|
|
|
|
|
11
|
for my $key (keys %{$ref_hash_values}) { |
|
5
|
|
|
|
|
18
|
|
97
|
8
|
|
|
|
|
26
|
$self->wg_meta->set($interface, $identifier, $key, $ref_hash_values->{$key}, \&Wireguard::WGmeta::Cli::Commands::Command::_unknown_attr_handler); |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
1; |