line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
=head1 NAME |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
Cli::Command - Base class for commands |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=head1 DESCRIPTION |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
Base class for the individual CLI commands. A command takes care of (possible) input arguments and performs actions accordingly. |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=head1 SYNOPSIS |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
A command must implement at least |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
package Wireguard::WGmeta::Cli::Commands::YourCommand; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
use experimental 'signatures'; |
16
|
|
|
|
|
|
|
use parent 'Wireguard::WGmeta::Cli::Commands::Command'; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
# is called from the router |
19
|
|
|
|
|
|
|
sub entry_point($self) { |
20
|
|
|
|
|
|
|
... |
21
|
|
|
|
|
|
|
} |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
# show cmd specific help |
24
|
|
|
|
|
|
|
sub cmd_help($self) { |
25
|
|
|
|
|
|
|
... |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
=head1 METHODS |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
=cut |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
package Wireguard::WGmeta::Cli::Commands::Command; |
34
|
1
|
|
|
1
|
|
494
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
33
|
|
35
|
1
|
|
|
1
|
|
5
|
use warnings FATAL => 'all'; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
40
|
|
36
|
1
|
|
|
1
|
|
6
|
use experimental 'signatures'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
5
|
|
37
|
|
|
|
|
|
|
|
38
|
1
|
|
|
1
|
|
545
|
use Wireguard::WGmeta::Utils; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
73
|
|
39
|
1
|
|
|
1
|
|
8
|
use constant WIREGUARD_HOME => '/etc/wireguard/'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
477
|
|
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
=head2 new(@input_arguments) |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
Creates a new Command instance. The following data is accessible through C<$self> after calling: |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
my $commandline_args = $self->{input_args}; # reference to array of input arguments |
46
|
|
|
|
|
|
|
my $wireguard_home = $self->{wireguard_home}; # Path to wireguard home dir |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
Please also take note of the effect of environment vars: L |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
B |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
=over 1 |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=item |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
C<@input_arguments> List of input args (except C) |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=back |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
B |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
An instance of a Command |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=cut |
65
|
|
|
|
|
|
|
|
66
|
5
|
|
|
5
|
1
|
10
|
sub new($class, @input_arguments) { |
|
5
|
|
|
|
|
11
|
|
|
5
|
|
|
|
|
42
|
|
|
5
|
|
|
|
|
12
|
|
67
|
5
|
|
|
|
|
15
|
my $self = { |
68
|
|
|
|
|
|
|
'input_args' => \@input_arguments |
69
|
|
|
|
|
|
|
}; |
70
|
|
|
|
|
|
|
# check if env var is available |
71
|
5
|
50
|
|
|
|
17
|
if (defined($ENV{'WIREGUARD_HOME'})) { |
72
|
5
|
|
|
|
|
16
|
$self->{wireguard_home} = $ENV{'WIREGUARD_HOME'}; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
else { |
75
|
0
|
|
|
|
|
0
|
$self->{wireguard_home} = WIREGUARD_HOME; |
76
|
|
|
|
|
|
|
} |
77
|
5
|
|
|
|
|
11
|
$self->{wg_meta} = undef; |
78
|
5
|
|
|
|
|
11
|
bless $self, $class; |
79
|
5
|
|
|
|
|
27
|
return $self; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
#@returns Wireguard::WGmeta::Wrapper::Config |
83
|
20
|
|
|
20
|
0
|
34
|
sub wg_meta($self) { |
|
20
|
|
|
|
|
26
|
|
|
20
|
|
|
|
|
34
|
|
84
|
|
|
|
|
|
|
# (sort of) singleton |
85
|
20
|
100
|
|
|
|
45
|
unless (defined $self->{wg_meta}){ |
86
|
5
|
|
|
|
|
30
|
$self->{wg_meta} = Wireguard::WGmeta::Wrapper::Config->new($self->{wireguard_home}); |
87
|
|
|
|
|
|
|
} |
88
|
20
|
|
|
|
|
92
|
return $self->{wg_meta}; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=head2 entry_point() |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
Method called from L |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
=cut |
96
|
0
|
|
|
0
|
1
|
0
|
sub entry_point($self) { |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
97
|
0
|
|
|
|
|
0
|
die 'Please instantiate the actual implementation'; |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=head2 cmd_help() |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
Stub for specific cmd help. Is expected to terminate the command by calling C |
103
|
|
|
|
|
|
|
and print the help content directly to I. |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=cut |
106
|
0
|
|
|
0
|
1
|
0
|
sub cmd_help($self) { |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
107
|
0
|
|
|
|
|
0
|
die 'Please instantiate the actual implementation'; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=head2 check_privileges() |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
Check if the user has r/w access to C. |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
B |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
Exception if the user has insufficient privileges . |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=cut |
119
|
5
|
|
|
5
|
1
|
11
|
sub check_privileges($self) { |
|
5
|
|
|
|
|
7
|
|
|
5
|
|
|
|
|
10
|
|
120
|
5
|
50
|
|
|
|
124
|
if (not -w $self->{wireguard_home}) { |
121
|
0
|
|
|
|
|
0
|
my $username = getpwuid($<); |
122
|
0
|
|
|
|
|
0
|
die "Insufficient privileges - `$username` has no rw permissions to `$self->{wireguard_home}`. You probably forgot `sudo`"; |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
21
|
|
|
21
|
|
39
|
sub _retrieve_or_die($self, $ref_array, $idx) { |
|
21
|
|
|
|
|
32
|
|
|
21
|
|
|
|
|
30
|
|
|
21
|
|
|
|
|
33
|
|
|
21
|
|
|
|
|
30
|
|
127
|
21
|
|
|
|
|
32
|
my @arr = @{$ref_array}; |
|
21
|
|
|
|
|
55
|
|
128
|
21
|
50
|
|
|
|
39
|
eval {return $arr[$idx]} or $self->cmd_help(); |
|
21
|
|
|
|
|
93
|
|
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
2
|
|
|
2
|
|
4
|
sub _unknown_attr_handler($attribute, $value) { |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
3
|
|
132
|
2
|
|
|
|
|
5
|
my $prefix = substr $attribute, 0, 1; |
133
|
2
|
100
|
|
|
|
23
|
die "`$attribute` is unknown, please add `+` as prefix to add it" unless $prefix eq '+'; |
134
|
1
|
|
|
|
|
5
|
return (substr $attribute, 1), $value; |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
1; |