| 1 | package Labyrinth::IPAddr; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use vars qw($VERSION @ISA %EXPORT_TAGS @EXPORT @EXPORT_OK); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 5 | $VERSION = '5.32'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =head1 NAME | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | Labyrinth::IPAddr - IP Address Functions for Labyrinth | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | use Labyrinth::IPAddr; | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | CheckIP(); | ||||||||||||||||||||||||||||
| 18 | BlockIP($who,$ipaddr); | ||||||||||||||||||||||||||||
| 19 | AllowIP($who,$ipaddr); | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | The IPAddr package contains generic functions used for verifying known IP | ||||||||||||||||||||||||||||
| 24 | addresses. Used to allow known safe address to use the site without hindrance | ||||||||||||||||||||||||||||
| 25 | and to refuse access to spammers. | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | Eventually this may be rewritten as a memcached stand-alone application, to be | ||||||||||||||||||||||||||||
| 28 | used across multiple sites. | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | =head1 EXPORT | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | CheckIP | ||||||||||||||||||||||||||||
| 33 | BlockIP | ||||||||||||||||||||||||||||
| 34 | AllowIP | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | =cut | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | # ------------------------------------- | ||||||||||||||||||||||||||||
| 39 | # Constants | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 5 | ▶ | use constant BLOCK => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | 5 | ▶ | use constant ALLOW => 2; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | # ------------------------------------- | ||||||||||||||||||||||||||||
| 45 | # Export Details | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 5 | require Exporter; | |||||||||||||||||||||||||||
| 48 | 5 | @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 49 | 5 | @EXPORT = ( qw( CheckIP BlockIP AllowIP BLOCK ALLOW) ); | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | # ------------------------------------- | ||||||||||||||||||||||||||||
| 52 | # Library Modules | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 5 | ▶ | use Labyrinth::Globals; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 55 | 5 | ▶ | use Labyrinth::DBUtils; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 56 | 5 | ▶ | use Labyrinth::Variables; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 5 | ▶ | use JSON::XS; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 59 | 5 | ▶ | use URI::Escape; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 60 | 5 | ▶ | use WWW::Mechanize; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | # ------------------------------------- | ||||||||||||||||||||||||||||
| 63 | # The Subs | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | =over 4 | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | =item CheckIP | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | Checks whether the current request sender IP address is know, and if so returns | ||||||||||||||||||||||||||||
| 72 | the classification. Return codes are: | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 0 - Unknown | ||||||||||||||||||||||||||||
| 75 | 1 - Blocked | ||||||||||||||||||||||||||||
| 76 | 2 - Allowed | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | =cut | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | ▶ | sub CheckIP { | ||||||||||||||||||||||||||
|
Subroutine
CheckIP: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 81 | 0 | ▶ | if($settings{blockurl}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 0 | my $res = _request($settings{blockurl},'check',$settings{ipaddr}); | |||||||||||||||||||||||||||
| 83 | 0 | ▶ | return $res && $res->{success} ? $res->{result} : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$res and $res->{"success"}
MC/DC0%0/2
$res and $res->{"success"}
$res
$res->{"success"}
Truth table0%0/3
A and B
A= $resB= $res->{"success"}
| |||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 0 | my @rows = $dbi->GetQuery('hash','FindIPAddress',$settings{ipaddr}); | |||||||||||||||||||||||||||
| 87 | 0 | ▶ | return @rows ? $rows[0]->{type} : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | =item BlockIP | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | Block current request sender IP address. | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | =cut | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 0 | ▶ | sub BlockIP { | ||||||||||||||||||||||||||
|
Subroutine
BlockIP: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||
| 98 | 0 | ▶ | my $who = shift || 'UNKNOWN'; | ||||||||||||||||||||||||||
|
Condition0%0/2
shift() || 'UNKNOWN'
MC/DC0%0/1
shift() || 'UNKNOWN'
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 99 | 0 | ▶ | my $ipaddr = shift || return; | ||||||||||||||||||||||||||
|
Condition0%0/2
shift() || (return)
MC/DC0%0/1
shift() || (return)
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 0 | ▶ | if($settings{blockurl}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 0 | my $res = _request($settings{blockurl},'block',$ipaddr,$who); | |||||||||||||||||||||||||||
| 103 | 0 | ▶ | return $res && $res->{success} ? $res->{result} : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$res and $res->{"success"}
MC/DC0%0/2
$res and $res->{"success"}
$res
$res->{"success"}
Truth table0%0/3
A and B
A= $resB= $res->{"success"}
| |||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 0 | ▶ | if(my @rows = $dbi->GetQuery('array','FindIPAddress',$ipaddr)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 0 | $dbi->DoQuery('SaveIPAddress',$who,1,$ipaddr); | |||||||||||||||||||||||||||
| 108 | } else { | ||||||||||||||||||||||||||||
| 109 | 0 | $dbi->DoQuery('AddIPAddress',$who,1,$ipaddr); | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 0 | return 1; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | =item AllowIP | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | Allow current request sender IP address. | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | =cut | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 0 | ▶ | sub AllowIP { | ||||||||||||||||||||||||||
|
Subroutine
AllowIP: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||
| 122 | 0 | ▶ | my $who = shift || 'UNKNOWN'; | ||||||||||||||||||||||||||
|
Condition0%0/2
shift() || 'UNKNOWN'
MC/DC0%0/1
shift() || 'UNKNOWN'
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 123 | 0 | ▶ | my $ipaddr = shift || return; | ||||||||||||||||||||||||||
|
Condition0%0/2
shift() || (return)
MC/DC0%0/1
shift() || (return)
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 0 | ▶ | if($settings{blockurl}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 0 | my $res = _request($settings{blockurl},'allow',$ipaddr,$who); | |||||||||||||||||||||||||||
| 127 | 0 | ▶ | return $res && $res->{success} ? $res->{result} : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$res and $res->{"success"}
MC/DC0%0/2
$res and $res->{"success"}
$res
$res->{"success"}
Truth table0%0/3
A and B
A= $resB= $res->{"success"}
| |||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 0 | ▶ | if(my @rows = $dbi->GetQuery('array','FindIPAddress',$ipaddr)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 0 | $dbi->DoQuery('SaveIPAddress',$who,2,$ipaddr); | |||||||||||||||||||||||||||
| 132 | } else { | ||||||||||||||||||||||||||||
| 133 | 0 | $dbi->DoQuery('AddIPAddress',$who,2,$ipaddr); | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 0 | return 1; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 0 | ▶ | sub _request { | ||||||||||||||||||||||||||
|
Subroutine
_request: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 140 | 0 | my $url = shift; | |||||||||||||||||||||||||||
| 141 | 0 | $url .= '/' . join('/', map { uri_escape_utf8($_) } @_); | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 0 | my $mech = WWW::Mechanize->new(); | |||||||||||||||||||||||||||
| 144 | 0 | $mech->get($url); | |||||||||||||||||||||||||||
| 145 | 0 | ▶ | if($mech->success()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | 0 | my $json = $mech->content(); | |||||||||||||||||||||||||||
| 147 | 0 | my $data = decode_json($json); | |||||||||||||||||||||||||||
| 148 | 0 | return $data; | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 0 | return; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 5 | 1; | |||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | __END__ | ||||||||||||||||||||||||||||