Mojolicious-Plugin-ClientIP
view release on metacpan or search on metacpan
123456789101112{
"abstract"
:
"Get client's IP address from X-Forwarded-For"
,
"author"
: [
"ziguzagu <ziguzagu@cpan.org>"
],
"dynamic_config"
: 0,
"generated_by"
:
"Minilla/v3.1.9"
,
"license"
: [
"perl_5"
],
"meta-spec"
: {
123456789101112---
abstract:
"Get client's IP address from X-Forwarded-For"
author:
-
'ziguzagu <ziguzagu@cpan.org>'
build_requires:
Test::More:
'0.98'
configure_requires:
Module::Build::Tiny:
'0.035'
dynamic_config: 0
generated_by:
'Minilla/v3.1.9, CPAN::Meta::Converter version 2.150010'
license: perl
meta-spec:
12345678910111213141516171819202122232425262728293031323334353637[](https://travis-ci.org/sixapart/Mojolicious-Plugin-ClientIP)
# NAME
Mojolicious::Plugin::ClientIP - Get client's IP address from X-Forwarded-For
# SYNOPSIS
plugin
'ClientIP'
;
get
'/'
=>
sub
{
my
$c
=
shift
;
$c
->render(
text
=>
$c
->client_ip);
};
app->start;
# DESCRIPTION
Mojolicious::Plugin::ClientIP is a Mojolicious plugin to get an IP address looks like client, not proxy, from X-Forwarded-For header.
# METHODS
## client\_ip
Find a client IP address from X-Forwarded-For. Private network addresses in XFF are ignored by
default
. If the good IP address is not found, it returns Mojo::Transaction
#remote\_address.
# OPTIONS
## ignore
Specify IP list to be ignored
with
ArrayRef.
plugin
'ClientIP'
,
ignore
=> [
qw(192.0.2.1 192.0.2.16/28)
];
## private
lib/Mojolicious/Plugin/ClientIP.pm view on Meta::CPAN
151617181920212223242526272829303132333435
@{
$conf
->{ignore} || []}
]);
$app
->helper(
client_ip
=>
sub
{
my
(
$c
) =
@_
;
state
$key
=
'__plugin_clientip_ip'
;
return
$c
->stash(
$key
)
if
$c
->stash(
$key
);
my
$xff
=
$c
->req->headers->header(
'X-Forwarded-For'
) //
''
;
my
@candidates
=
reverse
grep
{
$_
}
split
/,\s*/,
$xff
;
my
$ip
=
$self
->_find(\
@candidates
) //
$c
->tx->remote_address;
$c
->stash(
$key
=>
$ip
);
return
$ip
;
});
}
sub
_find {
my
$self
=
shift
;
lib/Mojolicious/Plugin/ClientIP.pm view on Meta::CPAN
707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
join
''
,
map
{
unpack
(
'B8'
,
pack
(
'C'
,
$_
)) }
split
/\./,
$ip
;
}
1;
__END__
=encoding utf-8
=head1 NAME
Mojolicious::Plugin::ClientIP - Get client's IP address from X-Forwarded-For
=head1 SYNOPSIS
use Mojolicious::Lite;
plugin 'ClientIP';
get '/' => sub {
my $c = shift;
$c->render(text => $c->client_ip);
};
app->start;
=head1 DESCRIPTION
Mojolicious::Plugin::ClientIP is a Mojolicious plugin to get an IP address looks like client, not proxy, from X-Forwarded-For header.
=head1 METHODS
=head2 client_ip
Find a client IP address from X-Forwarded-For. Private network addresses in XFF are ignored by default. If the good IP address is not found, it returns Mojo::Transaction#remote_address.
=head1 OPTIONS
=head2 ignore
Specify IP list to be ignored with ArrayRef.
plugin 'ClientIP', ignore => [qw(192.0.2.1 192.0.2.16/28)];
=head2 private
t/01_get_client_ip.t view on Meta::CPAN
111213141516171819202122232425262728293031
get
'/'
=>
sub
{
my
$c
=
shift
;
$c
->render(
text
=>
$c
->client_ip);
};
app->start;
}
my
$web
= Test::Mojo->new;
my
$xff
=
'X-Forwarded-For'
;
$web
->get_ok(
'/'
)
->content_is(
'127.0.0.1'
);
$web
->get_ok(
'/'
, {
$xff
=>
'192.0.2.1'
})
->content_is(
'192.0.2.1'
);
subtest
'ignore private IPs in XFF as default'
=>
sub
{
$web
->get_ok(
'/'
, {
$xff
=>
'127.0.0.2'
})
->content_is(
'127.0.0.1'
);
t/02_ignore_ip.t view on Meta::CPAN
1112131415161718192021222324252627282930
get
'/'
=>
sub
{
my
$c
=
shift
;
$c
->render(
text
=>
$c
->client_ip);
};
app->start;
}
my
$web
= Test::Mojo->new;
my
$xff
=
'X-Forwarded-For'
;
$web
->get_ok(
'/'
)
->content_is(
'127.0.0.1'
);
$web
->get_ok(
'/'
, {
$xff
=>
'192.0.2.1'
})
->content_is(
'192.0.2.1'
);
$web
->get_ok(
'/'
, {
$xff
=>
'192.0.2.2'
})
->content_is(
'127.0.0.1'
);
t/03_ignore_ip_private.t view on Meta::CPAN
1314151617181920212223242526272829303132
get
'/'
=>
sub
{
my
$c
=
shift
;
$c
->render(
text
=>
$c
->client_ip);
};
app->start;
}
my
$web
= Test::Mojo->new;
my
$xff
=
'X-Forwarded-For'
;
$web
->get_ok(
'/'
)
->content_is(
'127.0.0.1'
);
$web
->get_ok(
'/'
, {
$xff
=>
'192.0.2.1'
})
->content_is(
'192.0.2.1'
);
$web
->get_ok(
'/'
, {
$xff
=>
'192.0.2.2'
})
->content_is(
'127.0.0.1'
);
( run in 0.268 second using v1.01-cache-2.11-cpan-5f2e87ce722 )