File Coverage

script/proxyforurl
Criterion Covered Total %
statement 9 9 100.0
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 12 12 100.0


line stmt bran cond sub pod time code
1             #!/usr/bin/env perl
2 3     3   10379 use Mojolicious::Lite;
  3         362325  
  3         27  
3 3     3   83768 use NetAddr::IP ();
  3         71727  
  3         111  
4 3     3   29 use Socket 'inet_ntoa';
  3         9  
  3         2220  
5              
6             get '/' => 'index';
7             get '/pac', [format => [qw(js)]], => 'pac';
8              
9             get '/gethostbyname' => sub {
10             my $c = shift;
11             return $c->render(text => "Host missing.", status => 400) unless my $host = $c->param('host');
12             return $c->render(text => $host) unless $host =~ /[a-z]/;
13              
14             local $! = 0;
15             local $? = 0;
16             my @addr = gethostbyname($c->param('host'));
17             $! ||= $?;
18             return $c->render(text => "No IP found: $!", status => 400) if $!;
19              
20             @addr = map { inet_ntoa($_) } @addr[4 .. $#addr];
21             return $c->render(text => $addr[0]) if @addr;
22             return $c->render(text => "No IP found for $host.", status => 400);
23             },
24             'gethostbyname';
25              
26             get '/within' => sub {
27             my $c = shift;
28             my $ip = $c->param('ip');
29             my $net = $c->param('net');
30             my $mask = $c->param('mask');
31              
32             unless (2 == grep { $_ and $_ =~ /[:\.]/ } $ip, $net) {
33             return $c->render(text => "IP or Net is missing.", status => 400);
34             }
35             unless ($mask and $mask =~ /\d/) {
36             return $c->render(text => "Mask is invalid or missing.", status => 400);
37             }
38              
39             $ip = NetAddr::IP->new($c->param('ip'));
40             $net = NetAddr::IP->new(join '/', $net, $mask);
41             $c->render(text => $net->contains($ip) ? 1 : 0);
42             },
43             'within';
44              
45             hook before_dispatch => sub {
46             my $c = shift;
47             return unless my $base = $c->req->headers->header('X-Request-Base');
48             $c->req->url->base(Mojo::URL->new($base));
49             };
50              
51             app->start;
52              
53             __DATA__