| 1 | #!/usr/bin/env perl | ||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use Mojolicious::Lite -signatures; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use Mojo::Util qw(network_contains); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use NetAddr::IP (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Socket qw(AF_INET AF_INET6 inet_ntop getaddrinfo unpack_sockaddr_in unpack_sockaddr_in6); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 2 | get | |||||||||||||||||||||||||||
| 9 | '/' => {layout => 'pac'}, | ||||||||||||||||||||||||||||
| 10 | 'index'; | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 2 | get '/pac', [format => [qw(js)]], => 'pac'; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 0 | ▶ | post '/v1/gethostbyname' => sub ($c) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 15 | 0 | ▶ | return $c->render(text => 'Host missing.', status => 400) unless my $host = $c->param('host'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 16 | 0 | ▶ | return $c->render(text => "Invalid host: $host", status => 400) unless $host =~ /[A-Za-z:\.]/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 0 | $c->res->headers->cache_control('max-age=600'); | |||||||||||||||||||||||||||
| 19 | 0 | ▶ | return $c->render_later->host_to_ip_p($host)->then(sub ($addr) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 20 | 0 | ▶ | return $c->render(text => "No IP found for $host", status => 400) unless $addr; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | 0 | return $c->render(text => $addr); | |||||||||||||||||||||||||||
| 22 | })->catch( | ||||||||||||||||||||||||||||
| 23 | 0 | ▶ | sub ($err, @) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 24 | 0 | return $c->render(text => exception_to_text($err), status => 500); | |||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | 0 | ); | |||||||||||||||||||||||||||
| 27 | 2 | }; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 0 | ▶ | post '/v1/is-in-net' => sub ($c) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 30 | 0 | my ($ip, $net, $mask) = map { $c->param($_) } qw(ip net mask); | |||||||||||||||||||||||||||
| 31 | return $c->render(text => 'IP or Net is missing.', status => 400) | ||||||||||||||||||||||||||||
| 32 | 0 | ▶ | unless 2 == grep { $_ && $_ =~ /[:\.]/ } $ip, $net; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$_ && $_ =~ /[:\.]/u
MC/DC0%0/2
$_ && $_ =~ /[:\.]/u
$_
$_ =~ /[:\.]/u
Truth table0%0/3
A && B
A= $_B= $_ =~ /[:\.]/u
| |||||||||||||||||||||||||||||
| 33 | 0 | ▶ | return $c->render(text => 'Mask is invalid or missing.', status => 400) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$mask and $mask =~ /^\d{1,3}$/u
MC/DC0%0/2
$mask and $mask =~ /^\d{1,3}$/u
$mask
$mask =~ /^\d{1,3}$/u
Truth table0%0/3
A and B
A= $maskB= $mask =~ /^\d{1,3}$/u
| |||||||||||||||||||||||||||||
| 34 | unless $mask and $mask =~ /^\d{1,3}$/; | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 0 | $c->res->headers->cache_control('max-age=600'); | |||||||||||||||||||||||||||
| 37 | 0 | ▶ | return $c->render_later->host_to_ip_p($ip)->then(sub ($resolved) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 38 | 0 | ▶ | $c->render(text => $resolved && network_contains("$net/$mask", $resolved) ? 1 : 0); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$resolved and network_contains("$net/$mask", $resolved)
MC/DC0%0/2
$resolved and network_contains("$net/$mask", $resolved)
$resolved
network_contains("$net/$mask", $resolved)
Truth table0%0/3
A and B
A= $resolvedB= network_contains("$net/$mask", $resolved)
| |||||||||||||||||||||||||||||
| 39 | })->catch( | ||||||||||||||||||||||||||||
| 40 | 0 | ▶ | sub ($err, @) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 41 | 0 | $c->render(text => exception_to_text($err), status => 500); | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | 0 | ); | |||||||||||||||||||||||||||
| 44 | 2 | }; | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 2 | get '/v1/template' => {template => 'template'}; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 0 | ▶ | helper host_to_ip_p => sub ($c, $host) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 49 | 0 | ▶ | return Mojo::IOLoop->subprocess->run_p(sub (@) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||
| 50 | 0 | ▶ | local $SIG{ALRM} = sub { die 'Timeout!' }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 51 | 0 | alarm 2; | |||||||||||||||||||||||||||
| 52 | 0 | my ($err, @info) = getaddrinfo $host; | |||||||||||||||||||||||||||
| 53 | 0 | ▶ | return undef if $err; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 0 | @info = grep { $_->{family} & (AF_INET6 | AF_INET) } @info; | |||||||||||||||||||||||||||
| 56 | 0 | for my $item (@info) { | |||||||||||||||||||||||||||
| 57 | 0 | ▶ | $item->{pri} = $item->{family} == AF_INET6 ? 0 : 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | @$item{qw(port ip)} | ||||||||||||||||||||||||||||
| 59 | = $item->{family} == AF_INET6 | ||||||||||||||||||||||||||||
| 60 | ? unpack_sockaddr_in6($item->{addr}) | ||||||||||||||||||||||||||||
| 61 | 0 | ▶ | : unpack_sockaddr_in($item->{addr}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 0 | $item->{ip} = inet_ntop @$item{qw(family ip)}; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | # IPv4 before IPv6 | ||||||||||||||||||||||||||||
| 66 | 0 | @info = sort { $b->{pri} <=> $a->{pri} } @info; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 0 | ▶ | return @info && $info[0]{ip} || undef; | ||||||||||||||||||||||||||
|
Condition0%0/5
@info && $info[0]{'ip'} || undef
@info and $info[0]{"ip"}
MC/DC0%0/2
@info && $info[0]{'ip'} || undef
@info
$info[0]{"ip"}
Truth table0%0/3
A and B
A= @infoB= $info[0]{"ip"}
| |||||||||||||||||||||||||||||
| 69 | })->catch( | ||||||||||||||||||||||||||||
| 70 | 0 | ▶ | sub ($err, @) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 71 | 0 | ▶ | return $err =~ m!Timeout! ? undef : Mojo::Promise->reject($err); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | 0 | ); | |||||||||||||||||||||||||||
| 74 | 2 | }; | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 2 | ▶ | if (my $env_base = $ENV{PROXYFORURL_X_REQUEST_BASE}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 1 | ▶ | $env_base = '' unless $env_base =~ m!^http!; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 1 | ▶ | hook before_dispatch => sub ($c) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=13.1
| |||||||||||||||||||||||||||||
| 79 | 1 | ▶ | return unless my $base = $env_base || $c->req->headers->header('X-Request-Base'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$env_base || $c->req->headers->header('X-Request-Base')
MC/DC0%0/2
$env_base || $c->req->headers->header('X-Request-Base')
$env_base
$c->req->headers->header('X-Request-Base')
Truth table33%1/3
A || B
A= $env_baseB= $c->req->headers->header('X-Request-Base')
| |||||||||||||||||||||||||||||
| 80 | 1 | $c->req->url->base(Mojo::URL->new($base)); | |||||||||||||||||||||||||||
| 81 | 1 | }; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 2 | ▶ | app->renderer->paths([$ENV{PROXYFORURL_TEMPLATES} || 'templates']); | ||||||||||||||||||||||||||
|
Condition50%1/2
$ENV{'PROXYFORURL_TEMPLATES'} || 'templates'
MC/DC0%0/1
$ENV{'PROXYFORURL_TEMPLATES'} || 'templates'
$ENV{'PROXYFORURL_TEMPLATES'}
Truth table50%1/2
A
A= $ENV{'PROXYFORURL_TEMPLATES'}
| |||||||||||||||||||||||||||||
| 85 | 2 | ▶ | app->static->paths([$ENV{PROXYFORURL_PUBLIC} || 'public']); | ||||||||||||||||||||||||||
|
Condition50%1/2
$ENV{'PROXYFORURL_PUBLIC'} || 'public'
MC/DC0%0/1
$ENV{'PROXYFORURL_PUBLIC'} || 'public'
$ENV{'PROXYFORURL_PUBLIC'}
Truth table50%1/2
A
A= $ENV{'PROXYFORURL_PUBLIC'}
| |||||||||||||||||||||||||||||
| 86 | 2 | ▶ | app->config(brand_name => $ENV{PROXYFORURL_BRAND_NAME} || 'ProxyForURL'); | ||||||||||||||||||||||||||
|
Condition100%2/2
$ENV{'PROXYFORURL_BRAND_NAME'} || 'ProxyForURL'
MC/DC100%1/1
$ENV{'PROXYFORURL_BRAND_NAME'} || 'ProxyForURL'
$ENV{'PROXYFORURL_BRAND_NAME'}
Truth table100%2/2
A
A= $ENV{'PROXYFORURL_BRAND_NAME'}
| |||||||||||||||||||||||||||||
| 87 | 2 | ▶ | app->config(brand_url => $ENV{PROXYFORURL_BRAND_URL} || '/'); | ||||||||||||||||||||||||||
|
Condition100%2/2
$ENV{'PROXYFORURL_BRAND_URL'} || '/'
MC/DC100%1/1
$ENV{'PROXYFORURL_BRAND_URL'} || '/'
$ENV{'PROXYFORURL_BRAND_URL'}
Truth table100%2/2
A
A= $ENV{'PROXYFORURL_BRAND_URL'}
| |||||||||||||||||||||||||||||
| 88 | 2 | app->start; | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 0 | ▶ | sub exception_to_text ($err) { | ||||||||||||||||||||||||||
|
Subroutine
exception_to_text: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 91 | 0 | $err =~ s!\sat\s\S+.*?line.*!!s; | |||||||||||||||||||||||||||
| 92 | 0 | return $err; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | __DATA__ | ||||||||||||||||||||||||||||