line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
=head1 NAME |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
WGmeta::Wrapper::Bridge - Interface with shell using IPC::Open3 |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=head1 METHODS |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=cut |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
package Wireguard::WGmeta::Wrapper::Bridge; |
10
|
5
|
|
|
5
|
|
32
|
use strict; |
|
5
|
|
|
|
|
9
|
|
|
5
|
|
|
|
|
154
|
|
11
|
5
|
|
|
5
|
|
25
|
use warnings FATAL => 'all'; |
|
5
|
|
|
|
|
8
|
|
|
5
|
|
|
|
|
196
|
|
12
|
5
|
|
|
5
|
|
26
|
use experimental 'signatures'; |
|
5
|
|
|
|
|
9
|
|
|
5
|
|
|
|
|
36
|
|
13
|
|
|
|
|
|
|
|
14
|
5
|
|
|
5
|
|
426
|
use base 'Exporter'; |
|
5
|
|
|
|
|
10
|
|
|
5
|
|
|
|
|
846
|
|
15
|
|
|
|
|
|
|
our @EXPORT = qw(gen_keypair get_pub_key get_wg_show run_external); |
16
|
|
|
|
|
|
|
|
17
|
5
|
|
|
5
|
|
2412
|
use Symbol 'gensym'; |
|
5
|
|
|
|
|
3998
|
|
|
5
|
|
|
|
|
285
|
|
18
|
5
|
|
|
5
|
|
2524
|
use IPC::Open3; |
|
5
|
|
|
|
|
14199
|
|
|
5
|
|
|
|
|
276
|
|
19
|
|
|
|
|
|
|
|
20
|
5
|
|
|
5
|
|
35
|
use constant FALSE => 0; |
|
5
|
|
|
|
|
13
|
|
|
5
|
|
|
|
|
275
|
|
21
|
5
|
|
|
5
|
|
33
|
use constant TRUE => 1; |
|
5
|
|
|
|
|
10
|
|
|
5
|
|
|
|
|
2599
|
|
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
=head2 gen_keypair() |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
Runs the C command and returns a private and the corresponding public-key |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
B |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
Two strings: private-key and public-key |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
=cut |
33
|
0
|
|
|
0
|
1
|
|
sub gen_keypair() { |
|
0
|
|
|
|
|
|
|
34
|
0
|
0
|
|
|
|
|
unless (defined $ENV{WGmeta_NO_WG}){ |
35
|
0
|
|
|
|
|
|
my (@out_priv, undef) = run_external('wg genkey'); |
36
|
0
|
|
|
|
|
|
my (@out_pub, undef) = run_external('wg pubkey', $out_priv[0]); |
37
|
0
|
|
|
|
|
|
chomp @out_priv; |
38
|
0
|
|
|
|
|
|
chomp @out_pub; |
39
|
0
|
|
|
|
|
|
return $out_priv[0], $out_pub[0]; |
40
|
|
|
|
|
|
|
} |
41
|
0
|
|
|
|
|
|
return 'dummy_private_key', 'dummy_public_key'; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
=head2 get_pub_key($priv_key) |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
Runs C on C<$priv_key>. |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
B |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
=over 1 |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=item |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
C<$priv_key> A valid private key |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=back |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
B |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
A string containing the derived publickey. |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=cut |
62
|
0
|
|
|
0
|
0
|
|
sub get_pub_key($priv_key){ |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
63
|
0
|
0
|
|
|
|
|
unless (defined $ENV{WGmeta_NO_WG}){ |
64
|
0
|
|
|
|
|
|
my (@out, undef) = run_external('wg pubkey', $priv_key); |
65
|
0
|
|
|
|
|
|
chomp @out; |
66
|
0
|
|
|
|
|
|
return $out[0]; |
67
|
|
|
|
|
|
|
} |
68
|
0
|
|
|
|
|
|
return 'dummy_pubkey_from_privkey'; |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=head2 get_wg_show() |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
Runs C and captures the output into str_out and str_err. |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
B |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
Please refer to L |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
B |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
First array of STD_OUT |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=cut |
85
|
0
|
|
|
0
|
1
|
|
sub get_wg_show() { |
|
0
|
|
|
|
|
|
|
86
|
0
|
0
|
|
|
|
|
unless (defined $ENV{WGmeta_NO_WG}){ |
87
|
0
|
|
|
|
|
|
my $cmd = 'wg show all dump'; |
88
|
0
|
|
|
|
|
|
my (@out, undef) = run_external($cmd); |
89
|
0
|
|
|
|
|
|
chomp @out; |
90
|
0
|
|
|
|
|
|
return @out; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=head2 run_external($command_line [, $input, $soft_fail]) |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
Runs an external program and throws an exception (or a warning if C<$soft_fail> is true) if the return code is != 0 |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
B |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=over 1 |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=item * |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
C<$command_line> Complete commandline for the external program to execute. |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=item * |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
C<[$input = undef]> If defined, this is feed into STD_IN of C<$command_line>. |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=item * |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
C<[$soft_fail = FALSE]> If set to true, a warning is thrown instead of an exception |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=back |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
B |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
Exception if return code is not 0 (if C<$soft_fail> is set to true, just a warning) |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
B |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
Returns two lists with all lines of I and I |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=cut |
127
|
0
|
|
|
0
|
1
|
|
sub run_external($command_line, $input = undef, $soft_fail = FALSE) { |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
128
|
0
|
|
|
|
|
|
my $pid = open3(my $std_in, my $std_out, my $std_err = gensym, $command_line); |
129
|
0
|
0
|
|
|
|
|
if (defined($input)) { |
130
|
0
|
|
|
|
|
|
print $std_in $input; |
131
|
|
|
|
|
|
|
} |
132
|
0
|
|
|
|
|
|
close $std_in; |
133
|
0
|
|
|
|
|
|
my @output = <$std_out>; |
134
|
0
|
|
|
|
|
|
my @err = <$std_err>; |
135
|
0
|
|
|
|
|
|
close $std_out; |
136
|
0
|
|
|
|
|
|
close $std_err; |
137
|
|
|
|
|
|
|
|
138
|
0
|
|
|
|
|
|
waitpid($pid, 0); |
139
|
|
|
|
|
|
|
|
140
|
0
|
|
|
|
|
|
my $child_exit_status = $? >> 8; |
141
|
0
|
0
|
|
|
|
|
if ($child_exit_status != 0) { |
142
|
0
|
0
|
|
|
|
|
if ($soft_fail == TRUE) { |
143
|
0
|
|
|
|
|
|
warn "Command `$command_line` failed @err"; |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
else { |
146
|
0
|
|
|
|
|
|
die "Command `$command_line` failed @err"; |
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
} |
150
|
0
|
|
|
|
|
|
return @output, @err; |
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
1; |