Mojolicious-Plugin-ClientIP
    
    
  
  
  
view release on metacpan or search on metacpan
{
   "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" : {
      "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec",
    
  
  
  ---
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:
    
  
  
  [](https://travis-ci.org/sixapart/Mojolicious-Plugin-ClientIP)
# 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
        @{$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
    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
    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
    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
    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.726 second using v1.01-cache-2.11-cpan-a1d94b6210f )