Line 0
Link Here
|
|
|
1 |
package Koha::Middleware::RealIP; |
2 |
|
3 |
# Copyright 2019 ByWater Solutions and the Koha Dev Team |
4 |
# |
5 |
# This file is part of Koha. |
6 |
# |
7 |
# Koha is free software; you can redistribute it and/or modify it |
8 |
# under the terms of the GNU General Public License as published by |
9 |
# the Free Software Foundation; either version 3 of the License, or |
10 |
# (at your option) any later version. |
11 |
# |
12 |
# Koha is distributed in the hope that it will be useful, but |
13 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
# GNU General Public License for more details. |
16 |
# |
17 |
# You should have received a copy of the GNU General Public License |
18 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
19 |
|
20 |
use Modern::Perl; |
21 |
|
22 |
use parent qw(Plack::Middleware); |
23 |
|
24 |
use C4::Context; |
25 |
|
26 |
use Net::Netmask; |
27 |
use Plack::Util::Accessor qw( trusted_proxy ); |
28 |
|
29 |
=head3 prepare_app |
30 |
|
31 |
This method generates and stores the list of trusted ip's as Netmask objects |
32 |
at the time Plack starts up, obviating the need to regerenate them on each request. |
33 |
|
34 |
=cut |
35 |
|
36 |
sub prepare_app { |
37 |
my $self = shift; |
38 |
$self->trusted_proxy( get_trusted_proxies() ); |
39 |
} |
40 |
|
41 |
=head3 call |
42 |
|
43 |
This method is called for each request, and will ensure the correct remote address |
44 |
is set in the REMOTE_ADDR environment variable. |
45 |
|
46 |
=cut |
47 |
|
48 |
sub call { |
49 |
my $self = shift; |
50 |
my $env = shift; |
51 |
|
52 |
if ( $env->{HTTP_X_FORWARDED_FOR} ) { |
53 |
my @trusted_proxy = @{ $self->trusted_proxy } if $self->trusted_proxy; |
54 |
|
55 |
if (@trusted_proxy) { |
56 |
my $addr = get_real_ip( $env->{REMOTE_ADDR}, $env->{HTTP_X_FORWARDED_FOR}, \@trusted_proxy ); |
57 |
$ENV{REMOTE_ADDR} = $addr; |
58 |
$env->{REMOTE_ADDR} = $addr; |
59 |
} |
60 |
} |
61 |
|
62 |
return $self->app->($env); |
63 |
} |
64 |
|
65 |
=head3 get_real_ip |
66 |
|
67 |
my $address = get_real_ip( $remote_addres, $x_forwarded_for_header ); |
68 |
|
69 |
This method takes the current remote address and the x-forwarded-for header string, |
70 |
determines the correct external ip address, and returns it. |
71 |
|
72 |
=cut |
73 |
|
74 |
sub get_real_ip { |
75 |
my ( $remote_addr, $header ) = @_; |
76 |
|
77 |
my @forwarded_for = $header =~ /([^,\s]+)/g; |
78 |
return $remote_addr unless @forwarded_for; |
79 |
|
80 |
my $trusted_proxies = get_trusted_proxies(); |
81 |
|
82 |
my @unconfirmed = ( @forwarded_for, $remote_addr ); |
83 |
|
84 |
my $real_ip; |
85 |
while (my $addr = pop @unconfirmed) { |
86 |
my $has_matched = 0; |
87 |
foreach my $netmask (@$trusted_proxies) { |
88 |
$has_matched++, last if $netmask->match($addr); |
89 |
} |
90 |
$real_ip = $addr, last unless $has_matched; |
91 |
} |
92 |
|
93 |
return $real_ip; |
94 |
} |
95 |
|
96 |
=head3 get_trusted_proxies |
97 |
|
98 |
This method returns an arrayref of Net::Netmask objects for all |
99 |
the trusted proxies given to Koha. |
100 |
|
101 |
=cut |
102 |
|
103 |
sub get_trusted_proxies { |
104 |
my $proxies_conf = C4::Context->config('koha_trusted_proxies'); |
105 |
return unless $proxies_conf; |
106 |
my @trusted_proxies_ip = split( / /, $proxies_conf ); |
107 |
my @trusted_proxies = map { Net::Netmask->new($_) } @trusted_proxies_ip; |
108 |
return \@trusted_proxies; |
109 |
} |
110 |
|
111 |
|
112 |
=head1 AUTHORS |
113 |
|
114 |
Kyle M Hall <kyle@bywatersolutions.com> |
115 |
|
116 |
=cut |
117 |
|
118 |
1; |