Mojolicious-Plugin-ClientIP

 view release on metacpan or  search on metacpan

META.json  view on Meta::CPAN

1
2
3
4
5
6
7
8
9
10
11
12
{
   "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" : {

META.yml  view on Meta::CPAN

1
2
3
4
5
6
7
8
9
10
11
12
---
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:

README.md  view on Meta::CPAN

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# NAME
 
Mojolicious::Plugin::ClientIP - Get client's IP address from X-Forwarded-For
 
# SYNOPSIS
 
    use Mojolicious::Lite;
 
    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

15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
        @{$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

70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
    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

11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
    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

11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
    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

13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
    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 )