blib/lib/Plack/App/OpenVPN/Status.pm

filter: Statementstmt 96.6% 117 / 121 Branchbran 74.0% 40 / 54 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 14 / 14 Podpod 100% 4 / 4 totaltotal 89.9% 178 / 198 CC 31 SCAR 19.7
CC
31
Cov
89%
_adaptive_octets
10.9
_ovpn_status_v2_parse
10.2
_ovpn_status_v1_parse
10.0
CRAP 7.2
↑ Summary
1 package Plack::App::OpenVPN::Status;
2  
3 # ABSTRACT: Plack application to display the sessions of OpenVPN server
4  
5 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use feature ':5.10';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 ▶use parent 'Plack::Component';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Text::MicroTemplate;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Plack::Util::Accessor qw/renderer status_from custom_view/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 4 our $VERSION = '0.16'; # VERSION
15 4 our $AUTHORITY = 'cpan:CHIM'; # AUTHORITY
16  
17 #
18 # default view (uses Twitter Bootstrap v2.x.x layout)
19 8 ▶sub default_view {
Subroutine default_view: called CC=1 SCAR=0.0
Pod documented
20 8 <<'EOTMPL' }
21 % my $vars = $_[0];
22 <!DOCTYPE html>
23 <html>
24 <head>
25 <meta charset="utf-8">
26 <title>OpenVPN Status</title>
27 <link href="/static/bootstrap.min.css" rel="stylesheet">
28 </head>
29 <body>
30 <div class="container">
31 <div class="row">
32 <div class="page-header">
33 <h1>Active OpenVPN Sessions <small>Updated <%= $vars->{updated} %></small></h1>
34 </div>
35 </div>
36 <div class="row">
37 <p><code>Status Version #<%= $vars->{version} %></code></p>
38 % if (scalar @{$vars->{users}}) {
39 <table class="table table-bordered table-striped table-hover">
40 <thead>
41 <tr>
42 <th class="span2">Virtual address</th>
43 <th class="span2">Common name</th>
44 <th>Remote IP (port)</th>
45 <th>Recv (from)</th>
46 <th>Xmit (to)</th>
47 <th class="span3">Connected since</th>
48 </tr>
49 </thead>
50 <tbody>
51 % for my $user (@{$vars->{users}}) {
52 <tr>
53 <td><tt><%= $user->{'virtual'} %></tt></td>
54 <td><%= $user->{'common-name'} %></td>
55 <td><%= $user->{'remote-ip'} %> (<%= $user->{'remote-port'} %>)</td>
56 <td><%= $user->{'rx-bytes'} %></td>
57 <td><%= $user->{'tx-bytes'} %></td>
58 <td><%= $user->{'connected'} %></td>
59 </tr>
60 % }
61 </tbody>
62 </table>
63 % } else {
64 <div class="alert alert-block alert-info">
65 <h4>Attention!</h4>
66 There is no connected OpenVPN users.
67 </div>
68 % }
69 </div>
70 </div>
71 </body>
72 </html>
73 EOTMPL
74  
75 #
76 # some preparations
77 8 ▶sub prepare_app {
Subroutine prepare_app: called CC=3 SCAR=11.0
Pod documented
78 8 my ($self) = @_;
79  
80 8 my $t_view = $self->default_view;
81  
82 8 ▶ if ($self->custom_view) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->custom_view) 2 6 8
83 2 ▶ if (ref($self->custom_view) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->custom_view eq 'CODE') { } 1 1 2
84 1 $t_view = $self->custom_view->();
85 }
86 else {
87 1 Carp::croak "Parameter 'custom_view' must be a CODEREF";
88 }
89 }
90  
91 $self->renderer(
92 7 Text::MicroTemplate->new(
93 template => $t_view,
94 tag_start => '<%',
95 tag_end => '%>',
96 line_start => '%',
97 )->build
98 );
99 }
100  
101 #
102 # execute application
103 7 ▶sub call {
Subroutine call: called CC=4 SCAR=13.9
Pod documented
104 7 my ($self, $env) = @_;
105  
106 7 my ($body);
107  
108 7 ▶ unless ($self->status_from) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->status_from) { } 6 1 7
109 1 $body = "Error: OpenVPN status file is not set!";
110 }
111 else {
112 6 ▶ unless (-e $self->status_from || -r _) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $self->status_from or -r _) { } 5 1 6
Condition66%2/3
-e $self->status_from or -r _
l!l&&r!l&&!r
501
MC/DC50%1/2
-e $self->status_from or -r _
-e $self->status_from -r _
Truth table66%2/3
A or B
A= -e $self->status_fromB= -r _
ABresult
1X1
011
000
113 1 $body = "Error: OpenVPN status file '" . $self->status_from . "' does not exist or unreadable!";
114 }
115 else {
116 5 $body = $self->renderer->($self->openvpn_status);
117 }
118 }
119  
120 7 [ 200, [ 'Content-Type' => 'text/html; charset=utf-8' ], [ $body ] ];
121 }
122  
123 #
124 # parse OpenVPN status log
125 5 ▶sub openvpn_status {
Subroutine openvpn_status: called CC=2 SCAR=6.9
Pod documented
126 5 my ($self) = @_;
127  
128 5 my $lines;
129  
130 {
131 5 local $/ = undef;
132 5 ▶ open STATUS, '<' . $self->status_from or Carp::croak "Cannot open '" . $self->status_from . "'";
Branch: 1 branch
BranchTrueFalseTotal
open STATUS, '<' . $self->status_from or Carp::croak(q[Cannot open '] . $self->status_from . q[']) 5 0 5
133 5 $lines = <STATUS>;
134 5 close STATUS;
135 }
136  
137 5 my ($st_ver, $delim, $sub);
138  
139 # guess status file version
140 5 given ($lines) {
141 5 when (/TITLE,/) {
142 1 $st_ver = 2;
143 1 $delim = ',';
144 1 $sub = \&_ovpn_status_v2_parse;
145 }
146 4 when (/TITLE\t/) {
147 1 $st_ver = 3;
148 1 $delim = '\t';
149 1 $sub = \&_ovpn_status_v2_parse;
150 }
151 3 default {
152 3 $st_ver = 1;
153 3 $delim = ',';
154 3 $sub = \&_ovpn_status_v1_parse;
155 }
156 }
157  
158 5 $sub->($lines, $delim, $st_ver);
159 }
160  
161 # octets formatter
162 # http://en.wikipedia.org/wiki/Octet_%28computing%29
163 8 ▶sub _adaptive_octets {
Subroutine _adaptive_octets: called CC=7 SCAR=23.8
164 8 my ($octets) = @_;
165  
166 8 ▶ if ($octets > 1152921504606846976) { # exbioctet (Eio) = 2^60 octets
Branch: 6 branches
BranchTrueFalseTotal
if ($octets > 1152921504606846976) { } 0 8 8
elsif ($octets > 1125899906842624) { } 0 8 8
elsif ($octets > 1099511627776) { } 0 8 8
elsif ($octets > 1073741824) { } 0 8 8
elsif ($octets > 1048576) { } 7 1 8
elsif ($octets > 1024) { } 1 0 1
167 0 $octets = sprintf('%.6f Eio', $octets/1152921504606846976);
168 }
169 elsif ($octets > 1125899906842624) { # pebioctet (Pio) = 2^50 octets
170 0 $octets = sprintf('%.5f Pio', $octets/1125899906842624);
171 }
172 elsif ($octets > 1099511627776) { # tebioctet (Tio) = 2^40 octets
173 0 $octets = sprintf('%.4f Tio', $octets/1099511627776);
174 }
175 elsif ($octets > 1073741824) { # gibioctet (Gio) = 2^30 octets
176 0 $octets = sprintf('%.3f Gio', $octets/1073741824);
177 }
178 elsif ($octets > 1048576) { # mebioctet (Mio) = 2^20 octets
179 7 $octets = sprintf('%.2f Mio', $octets/1048576);
180 }
181 elsif ($octets > 1024) { # kibioctet (Kio) = 2^10 octets
182 1 $octets = sprintf('%.1f Kio', $octets/1024);
183 }
184  
185 8 $octets;
186 };
187  
188 #
189 # OpenVPN status file format version #1 parser
190 3 ▶sub _ovpn_status_v1_parse {
Subroutine _ovpn_status_v1_parse: called CC=10 SCAR=23.1
191 3 my ($lines, $delim, $version) = @_;
192  
193 3 my $vars = {};
194  
195 3 my ($users, $updated);
196  
197 3 for (split /\n/, $lines) {
198 28 ▶ next if /^$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^$/ 0 28 28
199 28 ▶ next if /^(OpenVPN|ROUTING TABLE|GLOBAL STATS|Max bcast|END)/;
Branch: 1 branch
BranchTrueFalseTotal
if /^(OpenVPN|ROUTING TABLE|GLOBAL STATS|Max bcast|END)/ 15 13 28
200  
201 13 my @line = split $delim, $_;
202 13 my $length = scalar(@line);
203  
204 13 ▶ $length == 2 && do {
Branch: 1 branch
BranchTrueFalseTotal
$length == 2 and do { next unless $line[0] =~ /^Updated/; $updated = $line[1]; next } 3 10 13
205 3 ▶ next unless $line[0] =~ /^Updated/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line[0] =~ /^Updated/ 0 3 3
206 3 $updated = $line[1];
207 3 next;
208 };
209  
210 10 ▶ $length == 5 && do {
Branch: 1 branch
BranchTrueFalseTotal
$length == 5 and do { next if $line[0] =~ /^Common Name/; () = split(/:/, $line[1], 3); $$users{$line[0]} = {"common-name", $line[0], "remote-ip", $ip, "remote-port", $port, "rx-bytes", _adaptive_octets($line[2]), "tx-bytes", _adaptive_octets($line[3]), "connected", $line[4]}; next } 5 5 10
211 5 ▶ next if $line[0] =~ /^Common Name/;
Branch: 1 branch
BranchTrueFalseTotal
if $line[0] =~ /^Common Name/ 3 2 5
212 2 my ($ip, $port) = split /:/, $line[1];
213 2 $users->{$line[0]} = {
214 'common-name' => $line[0],
215 'remote-ip' => $ip,
216 'remote-port' => $port,
217 'rx-bytes' => _adaptive_octets($line[2]),
218 'tx-bytes' => _adaptive_octets($line[3]),
219 'connected' => $line[4],
220 };
221 2 next;
222 };
223  
224 5 ▶ $length == 4 && do {
Branch: 1 branch
BranchTrueFalseTotal
$length == 4 and do { next if $line[0] =~ /^Virtual Address/; $$users{$line[1]}{"virtual"} = $line[0]; $$users{$line[1]}{"last-ref"} = $line[3]; next } 5 0 5
225 5 ▶ next if $line[0] =~ /^Virtual Address/;
Branch: 1 branch
BranchTrueFalseTotal
if $line[0] =~ /^Virtual Address/ 3 2 5
226 2 $users->{$line[1]}->{'virtual'} = $line[0];
227 2 $users->{$line[1]}->{'last-ref'} = $line[3];
228 2 next;
229 };
230 }
231  
232 $vars = {
233 'version' => $version,
234 'updated' => $updated,
235 3 'users' => [ map { $users->{$_} } keys %$users ],
236 };
237  
238 3 $vars;
239 }
240  
241 #
242 # OpenVPN status file format version #2 and #3 parser
243 2 ▶sub _ovpn_status_v2_parse {
Subroutine _ovpn_status_v2_parse: called CC=10 SCAR=23.2
244 2 my ($lines, $delim, $version) = @_;
245  
246 2 my $vars = {};
247  
248 2 my ($users, $updated);
249  
250 2 for (split /\n/, $lines) {
251 16 ▶ next if /^$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^$/ 0 16 16
252 16 ▶ next if /^(TITLE|HEADER|GLOBAL_STATS|END)/;
Branch: 1 branch
BranchTrueFalseTotal
if /^(TITLE|HEADER|GLOBAL_STATS|END)/ 10 6 16
253  
254 6 my @line = split $delim, $_;
255 6 my $length = scalar(@line);
256  
257 6 ▶ $length == 3 && do {
Branch: 1 branch
BranchTrueFalseTotal
$length == 3 and do { next unless $line[0] =~ /^TIME/; $updated = $line[1]; next } 2 4 6
258 2 ▶ next unless $line[0] =~ /^TIME/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line[0] =~ /^TIME/ 0 2 2
259 2 $updated = $line[1];
260 2 next;
261 };
262  
263 4 ▶ $length == 8 && do {
Branch: 1 branch
BranchTrueFalseTotal
$length == 8 and do { next unless $line[0] =~ /^CLIENT_LIST/; () = split(/:/, $line[2], 3); $$users{$line[1]} = {"common-name", $line[1], "remote-ip", $ip, "remote-port", $port, "rx-bytes", _adaptive_octets($line[4]), "tx-bytes", _adaptive_octets($line[5]), "connected", $line[6]}; next } 2 2 4
264 2 ▶ next unless $line[0] =~ /^CLIENT_LIST/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line[0] =~ /^CLIENT_LIST/ 0 2 2
265 2 my ($ip, $port) = split /:/, $line[2];
266 2 $users->{$line[1]} = {
267 'common-name' => $line[1],
268 'remote-ip' => $ip,
269 'remote-port' => $port,
270 'rx-bytes' => _adaptive_octets($line[4]),
271 'tx-bytes' => _adaptive_octets($line[5]),
272 'connected' => $line[6],
273 };
274 2 next;
275 };
276  
277 2 ▶ $length == 6 && do {
Branch: 1 branch
BranchTrueFalseTotal
$length == 6 and do { next unless $line[0] =~ /^ROUTING_TABLE/; $$users{$line[2]}{"virtual"} = $line[1]; $$users{$line[2]}{"last-ref"} = $line[4]; next } 2 0 2
278 2 ▶ next unless $line[0] =~ /^ROUTING_TABLE/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line[0] =~ /^ROUTING_TABLE/ 0 2 2
279 2 $users->{$line[2]}->{'virtual'} = $line[1];
280 2 $users->{$line[2]}->{'last-ref'} = $line[4];
281 2 next;
282 };
283 }
284  
285 $vars = {
286 'version' => $version,
287 'updated' => $updated,
288 2 'users' => [ map { $users->{$_} } keys %$users ],
289 };
290  
291 2 $vars;
292 }
293  
294 4 1; # End of Plack::App::OpenVPN::Status
295  
296 __END__
↑ Summary