Mojolicious-Plugin-RemoteAddr
view release on metacpan or search on metacpan
0.01
First release.
0.02
Replaced "render_text" with "render". "render_text" was removed in Mojolicious 4.0
0.03
Add support for X-Forwarded-For
# Mojolicious
$self->plugin('RemoteAddr');
# In controller
my $ip = $self->remote_addr;
=head1 DESCRIPTION
L<Mojolicious::Plugin::RemoteAddr> adds simple helper "remote_addr" which returns an ip address of a remote host, It tries getting remote ip in different ways.
Firstly, it takes 'X-Real-IP' header. Secondly, it takes 'X-Forwarded-For' header. If they are empty it takes the ip from current request transaction.
=head1 CONFIG
=head2 order
Lookup order. Default is ['x-real-ip', 'x-forwarded-for', 'tx']
If you do not have reverse proxy then set order to ['tx'] to avoid ip-address spoofing.
Supported places:
=over 4
=item 'x-real-ip'
'X-Real-IP' request header
=item 'x-forwarded-for'
'X-Forwarded-For' request header
=item 'tx'
current request transaction
=back
=head1 HELPERS
=head2 remote_addr
lib/Mojolicious/Plugin/RemoteAddr.pm view on Meta::CPAN
$conf->{order} ||= ['x-real-ip', 'x-forwarded-for', 'tx'];
$app->helper( remote_addr => sub {
my $c = shift;
foreach my $place ( @{ $conf->{order} } ) {
if ( $place eq 'x-real-ip' ) {
my $ip = $c->req->headers->header('X-Real-IP');
return $ip if $ip;
} elsif ( $place eq 'x-forwarded-for' ) {
my $ip = $c->req->headers->header('X-Forwarded-For');
return $ip if $ip;
} elsif ( $place eq 'tx' ) {
my $ip = $c->tx->remote_address;
return $ip if $ip;
}
}
return;
});
}
lib/Mojolicious/Plugin/RemoteAddr.pm view on Meta::CPAN
# Mojolicious
$self->plugin('RemoteAddr');
# In controller
my $ip = $self->remote_addr;
=head1 DESCRIPTION
L<Mojolicious::Plugin::RemoteAddr> adds simple helper "remote_addr" which returns an ip address of a remote host, It tries getting remote ip in different ways.
Firstly, it takes 'X-Real-IP' header. Secondly, it takes 'X-Forwarded-For' header. If they are empty it takes the ip from current request transaction.
=head1 CONFIG
=head2 order
Lookup order. Default is ['x-real-ip', 'x-forwarded-for', 'tx']
If you do not have reverse proxy then set order to ['tx'] to avoid ip-address spoofing.
Supported places:
=over 4
=item 'x-real-ip'
'X-Real-IP' request header
=item 'x-forwarded-for'
'X-Forwarded-For' request header
=item 'tx'
current request transaction
=back
=head1 HELPERS
=head2 remote_addr
( run in 0.433 second using v1.01-cache-2.11-cpan-4e96b696675 )