| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Wireguard::WGmeta::Cli::Commands::Set; |
|
2
|
1
|
|
|
1
|
|
8
|
use strict; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
35
|
|
|
3
|
1
|
|
|
1
|
|
6
|
use warnings FATAL => 'all'; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
75
|
|
|
4
|
1
|
|
|
1
|
|
9
|
use experimental 'signatures'; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
15
|
|
|
5
|
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
100
|
use parent 'Wireguard::WGmeta::Cli::Commands::Command'; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
5
|
|
|
7
|
1
|
|
|
1
|
|
80
|
use Wireguard::WGmeta::Wrapper::Config; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
25
|
|
|
8
|
1
|
|
|
1
|
|
5
|
use constant TRUE => 1; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
69
|
|
|
9
|
1
|
|
|
1
|
|
7
|
use constant FALSE => 0; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
708
|
|
|
10
|
|
|
|
|
|
|
|
|
11
|
3
|
|
|
3
|
1
|
7
|
sub entry_point($self) { |
|
|
3
|
|
|
|
|
5
|
|
|
|
3
|
|
|
|
|
6
|
|
|
12
|
3
|
50
|
|
|
|
17
|
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
|
|
|
|
|
15
|
$self->check_privileges(); |
|
18
|
|
|
|
|
|
|
# would be very nice if we can set a type hint here...possible? |
|
19
|
3
|
|
|
|
|
15
|
$self->_run_command(); |
|
20
|
|
|
|
|
|
|
} |
|
21
|
|
|
|
|
|
|
} |
|
22
|
|
|
|
|
|
|
|
|
23
|
3
|
|
|
3
|
|
5
|
sub _run_command($self) { |
|
|
3
|
|
|
|
|
7
|
|
|
|
3
|
|
|
|
|
4
|
|
|
24
|
3
|
|
|
|
|
14
|
my $interface = $self->_retrieve_or_die($self->{input_args}, 0); |
|
25
|
3
|
|
|
|
|
19
|
my $offset = -1; |
|
26
|
3
|
|
|
|
|
7
|
my $cur_start = 0; |
|
27
|
3
|
|
|
|
|
5
|
my @input_args = @{$self->{input_args}}; |
|
|
3
|
|
|
|
|
12
|
|
|
28
|
3
|
|
|
|
|
5
|
for my $value (@{$self->{input_args}}) { |
|
|
3
|
|
|
|
|
10
|
|
|
29
|
28
|
100
|
|
|
|
59
|
if ($value eq 'peer') { |
|
30
|
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
# if there is just one peer we skip here |
|
32
|
5
|
100
|
|
|
|
11
|
if ($offset != 0) { |
|
33
|
3
|
|
|
|
|
27
|
$self->_apply_change_set($interface, @input_args[$cur_start .. $offset]); |
|
34
|
2
|
|
|
|
|
7
|
$cur_start = $offset; |
|
35
|
|
|
|
|
|
|
} |
|
36
|
|
|
|
|
|
|
} |
|
37
|
27
|
|
|
|
|
37
|
$offset++; |
|
38
|
|
|
|
|
|
|
} |
|
39
|
2
|
|
|
|
|
8
|
$self->_apply_change_set($interface, @input_args[$cur_start .. $offset]); |
|
40
|
2
|
50
|
|
|
|
10
|
if (defined $ENV{IS_TESTING}) { |
|
41
|
|
|
|
|
|
|
# omit header |
|
42
|
2
|
|
|
|
|
5
|
$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
|
|
9
|
sub _apply_change_set($self, $interface, @change_set) { |
|
|
5
|
|
|
|
|
7
|
|
|
|
5
|
|
|
|
|
10
|
|
|
|
5
|
|
|
|
|
13
|
|
|
|
5
|
|
|
|
|
7
|
|
|
54
|
5
|
|
|
|
|
10
|
my $offset = 1; |
|
55
|
5
|
|
|
|
|
9
|
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
|
|
|
|
|
10
|
$identifier = $self->_retrieve_or_die(\@change_set, 2); |
|
59
|
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
# try to resolve alias |
|
61
|
4
|
|
|
|
|
19
|
$identifier = $self->wg_meta->try_translate_alias($interface, $identifier); |
|
62
|
|
|
|
|
|
|
|
|
63
|
4
|
|
|
|
|
7
|
$offset += 2; |
|
64
|
|
|
|
|
|
|
} |
|
65
|
|
|
|
|
|
|
else { |
|
66
|
1
|
|
|
|
|
3
|
$identifier = $interface; |
|
67
|
|
|
|
|
|
|
} |
|
68
|
5
|
|
|
|
|
16
|
my @value_keys = splice @change_set, $offset; |
|
69
|
|
|
|
|
|
|
|
|
70
|
5
|
50
|
|
|
|
15
|
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
|
|
|
|
|
10
|
my %args; |
|
75
|
5
|
|
|
|
|
8
|
my $idx = 0; |
|
76
|
|
|
|
|
|
|
|
|
77
|
5
|
|
|
|
|
15
|
while ($idx < @value_keys) { |
|
78
|
8
|
|
|
|
|
21
|
$args{$value_keys[$idx]} = $value_keys[$idx + 1]; |
|
79
|
8
|
|
|
|
|
18
|
$idx += 2; |
|
80
|
|
|
|
|
|
|
} |
|
81
|
|
|
|
|
|
|
# print "Got command set: |
|
82
|
|
|
|
|
|
|
# interface: $interface |
|
83
|
|
|
|
|
|
|
# ident: $identifier |
|
84
|
|
|
|
|
|
|
# attrs: @value_keys |
|
85
|
|
|
|
|
|
|
# "; |
|
86
|
|
|
|
|
|
|
|
|
87
|
5
|
|
|
|
|
15
|
$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
|
|
10
|
sub _set_values($self, $interface, $identifier, $ref_hash_values) { |
|
|
5
|
|
|
|
|
6
|
|
|
|
5
|
|
|
|
|
8
|
|
|
|
5
|
|
|
|
|
9
|
|
|
|
5
|
|
|
|
|
7
|
|
|
|
5
|
|
|
|
|
8
|
|
|
96
|
5
|
|
|
|
|
8
|
for my $key (keys %{$ref_hash_values}) { |
|
|
5
|
|
|
|
|
18
|
|
|
97
|
8
|
|
|
|
|
29
|
$self->wg_meta->set($interface, $identifier, $key, $ref_hash_values->{$key}, \&Wireguard::WGmeta::Cli::Commands::Command::_unknown_attr_handler); |
|
98
|
|
|
|
|
|
|
} |
|
99
|
|
|
|
|
|
|
} |
|
100
|
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
1; |