SixXS/TIC/Client.pm

filter: Statementstmt 33.5% 45 / 134 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 52.1% 12 / 23 Podpod 100% 7 / 7 totaltotal 31.2% 64 / 205 CC 22 SCAR 32.7
CC
22
Cov
26%
tic_command
90.0
connect
30.0
tunnel_info
20.0
CRAP 26.3
↑ Summary SixXS/TIC/Server.pm »
1 #!/usr/bin/perl
2  
3 package Net::SixXS::TIC::Client;
4  
5 1 ▶use v5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use version; our $VERSION = version->declare("v0.1.1");
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Digest::MD5 'md5_hex';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use POSIX 'uname';
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 ▶use Net::SixXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Net::SixXS::Data::Tunnel;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 has username => (
21 is => 'ro',
22 isa => 'Str',
23 required => 1,
24 );
25  
26 1 has password => (
27 is => 'ro',
28 isa => 'Str',
29 required => 1,
30 );
31  
32 1 has server => (
33 is => 'rw',
34 isa => 'Str',
35 required => 0,
36 default => 'tic.sixxs.net',
37 );
38  
39 1 has tic_socket => (
40 is => 'rw',
41 isa => 'IO::Socket::INET',
42 required => 0,
43 );
44  
45 has client_name => (
46 is => 'rw',
47 isa => 'Str',
48 required => 0,
49 0 ▶ default => sub { 'Net-SixXS' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
50 1 );
51  
52 has client_version => (
53 is => 'rw',
54 isa => 'Str',
55 required => 0,
56 0 ▶ default => sub { $Net::SixXS::VERSION.'' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
57 1 );
58  
59 has client_osname => (
60 is => 'rw',
61 isa => 'Str',
62 required => 0,
63 0 ▶ default => sub { my ($sysname, undef, $release) = uname; "$sysname/$release" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
64 1 );
65  
66 has diag => (
67 is => 'rw',
68 does => 'Net::SixXS::Diag',
69 required => 0,
70 0 ▶ default => sub { Net::SixXS::diag },
Subroutine __ANON__: not called CC=1 SCAR=6.9
71 1 );
72  
73 sub tic_resp_parse($ $)
74 0 ▶{
Subroutine tic_resp_parse: not called CC=2 SCAR=17.9
Pod documented
75 0 my ($self, $line) = @_;
76  
77 0 $line =~ s/[\r\n]*$//;
78 0 ▶ if ($line !~ /^([0-9][0-9][0-9])\s+(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($line =~ /^([0-9][0-9][0-9])\s+(.*)$/) 0 0 0
79 0 croak "Invalid TIC response line received: $line\n";
80 }
81 0 my ($code, $msg) = ($1, $2);
82 return {
83 0 success => int($code / 100) == 2,
84 code => $code,
85 msg => $msg,
86 };
87 }
88  
89 sub tic_command($ $)
90 0 ▶{
Subroutine tic_command: not called CC=9 SCAR=45.0
Pod documented
91 0 my ($self, $command) = @_;
92 0 my $s = $self->tic_socket;
93  
94 0 $self->debug("TIC: sending $command");
95 0 print $s "$command\n";
96 0 my $line = <$s>;
97 0 my $resp = $self->tic_resp_parse($line);
98 0 ▶ $self->debug("TIC: got a response with code $resp->{code} success ".($resp->{success}? 'true': 'false')." msg $resp->{msg}");
Branch: 1 branch
BranchTrueFalseTotal
$resp->{'success'} ? : 0 0 0
99 0 ▶ if (!$resp->{success}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($resp->{"success"}) 0 0 0
100 croak("The TIC server ".$self->server.
101 0 " refused the '$command' command: ".$resp->{msg}."\n");
102 }
103  
104 0 ▶ if ($resp->{code} == 201) {
Branch: 2 branches
BranchTrueFalseTotal
if ($resp->{'code'} == 201) { } 0 0 0
elsif ($resp->{'success'} and $resp->{'code'} != 200) { } 0 0 0
105 0 my @data;
106 0 ▶ while ($line = <$s>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = readline $s)) 0 0 0
107 0 $line =~ s/[\r\n]*$//;
108 0 ▶ if ($line =~ /^202\s+(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^202\s+(.*)/) 0 0 0
109 0 $resp->{msg} .= " ... $1";
110 0 last;
111 }
112 0 push @data, $line;
113 }
114 0 ▶ if (!defined $line) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $line) 0 0 0
115 0 die "The TIC server did not complete the response to '$command'\n";
116 }
117 0 $self->debug("returning ".scalar(@data)." lines of response");
118 0 $resp->{data} = \@data;
119 ▶ } elsif ($resp->{success} && $resp->{code} != 200) {
Condition0%0/3
$resp->{"success"} and $resp->{"code"} != 200
!ll&&!rl&&r
000
MC/DC0%0/2
$resp->{"success"} and $resp->{"code"} != 200
$resp->{"success"} $resp->{"code"} != 200
Truth table0%0/3
A and B
A= $resp->{"success"}B= $resp->{"code"} != 200
ABresult
0X0
100
111
120 0 die "FIXME: unexpected 'success' response from the TIC server: $line\n";
121 }
122 0 return $resp;
123 }
124  
125 sub connect($)
126 0 ▶{
Subroutine connect: not called CC=5 SCAR=34.0
Pod documented
127 0 my ($self) = @_;
128 0 my $server = $self->server;
129  
130 0 ▶ $self->disconnect if defined $self->tic_socket;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->tic_socket 0 0 0
131 0 $self->debug("TIC: connecting to $server:3874");
132 0 ▶ my $s = IO::Socket::INET->new(Proto => 'tcp', PeerHost => $server,
Branch: 1 branch
BranchTrueFalseTotal
my $s = "IO::Socket::INET"->new("Proto", "tcp", "PeerHost", $server, "PeerPort", 3874) or die "Could not connect to ${server}:3874: $!\n" 0 0 0
133 PeerPort => 3874) or
134 die "Could not connect to $server:3874: $!\n";
135 0 my $line = <$s>;
136 0 my $resp = $self->tic_resp_parse($line);
137 0 ▶ if (!$resp->{success}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($resp->{"success"}) 0 0 0
138 0 croak("The $server TIC server greeted us badly: $resp->{msg}\n");
139 }
140 0 $self->server($server);
141 0 $self->tic_socket($s);
142  
143 0 eval {
144 0 $self->tic_command('client TIC/draft-00 '.
145 $self->client_name.'/'.$self->client_version.' '.
146 $self->client_osname);
147 0 $self->tic_command('username '.$self->username);
148 0 $resp = $self->tic_command('challenge md5');
149 0 my $challenge = (split /\s+/, $resp->{msg})[-1];
150 0 $self->debug("Got a TIC challenge $challenge");
151 0 my $md5pass = md5_hex($self->password);
152 0 my $interm = "$challenge$md5pass";
153 0 my $md5resp = md5_hex($interm);
154 0 $self->debug("password '".$self->password."' md5 '$md5pass' intermediate '$interm' response '$md5resp'");
155 0 $self->tic_command("authenticate md5 $md5resp");
156 0 $self->debug("Wheee, it worked!");
157 };
158 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
159 0 my $msg = $@;
160 0 $self->disconnect;
161 0 die $msg;
162 }
163 }
164  
165 sub disconnect($)
166 0 ▶{
Subroutine disconnect: not called CC=3 SCAR=24.8
Pod documented
167 0 my ($self) = @_;
168 0 my $s = $self->tic_socket;
169
170 0 ▶ return unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 0 0 0
171 0 ▶ close $s or die "Could not close the TIC socket: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $s or die "Could not close the TIC socket: $!\n" 0 0 0
172 0 $self->{tic_socket} = undef;
173 }
174  
175 sub tunnels($)
176 0 ▶{
Subroutine tunnels: not called CC=3 SCAR=24.8
Pod documented
177 0 my ($self) = @_;
178
179 0 my $resp = $self->tic_command('tunnel list');
180 0 my %tunnels;
181 0 for (@{$resp->{data}}) {
182 0 ▶ if (!/^(T\w+)\s+(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/^(T\w+)\s+(.*)/) 0 0 0
183 0 die "Invalid 'tunnel list' response from ".
184 "the TIC server: $_\n";
185 }
186 0 $tunnels{$1} = $2;
187 }
188 0 return \%tunnels;
189 }
190  
191 sub tunnel_info($ $)
192 0 ▶{
Subroutine tunnel_info: not called CC=4 SCAR=30.0
Pod documented
193 0 my ($self, $tunnel) = @_;
194  
195 0 my $resp = $self->tic_command("tunnel show $tunnel");
196 0 my %data;
197 0 for (@{$resp->{data}}) {
198 0 ▶ if (!/^(\w[^:]+)\s*:\s*(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/^(\w[^:]+)\s*:\s*(.*)$/) 0 0 0
199 0 die "Invalid 'tunnel show' response from ".
200 "the TIC server: $_\n";
201 }
202 0 my ($k, $v) = ($1, $2);
203 0 ▶ if (exists $data{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data{$k}) 0 0 0
204 0 die "Duplicate key '$k' in the TIC server's ".
205 "'tunnel show' response: $_\n";
206 }
207 0 $data{$k} = $v;
208 }
209 0 return Net::SixXS::Data::Tunnel->from_json(\%data);
210 }
211  
212 sub debug($ $)
213 0 ▶{
Subroutine debug: not called CC=2 SCAR=17.9
Pod documented
214 0 my ($self, $msg) = @_;
215  
216 0 ▶ $self->diag->debug($msg) if $self->diag;
Branch: 1 branch
BranchTrueFalseTotal
if $self->diag 0 0 0
217 }
218  
219 1 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
220  
221 1 1;
222 __END__
↑ Summary SixXS/TIC/Server.pm »