NAME

Plack::Middleware::DNSBL - An IPv4 DNS Blacklist middleware for Plack

SYNOPSIS

The Plack::Middleware::DNSBL middleware provides a simple yet customizable way of blocking ill-intentionated requests from reaching your main application by using an external blacklist.

use Plack::Builder;
use Plack::Middleware::DNSBL;

my $app = sub { ... };

builder {
    enable 'DNSBL',
        blacklists => {
            'your-trusted-blacklist' => '$ip.your.trusted.blacklist',
            'ip-port-blacklist'      => '$ip.$port.ip-port.trusted.blacklist',
        };

    $app;
}

CONFIGURATIONS

blacklists
enable 'DNSBL',
    blacklists => {
        'blacklist-name-1' => 'blacklist-query-address',
        'blacklist-name-2' => 'blacklist-query-address',
        'blacklist-name-3' => 'blacklist-query-address',
        # ...
        'blacklist-name-n' => 'blacklist-query-address',
    };

The blacklists option specifies a hashref with all the blacklists' name and query address pairs. The query address will have every $ip and $port substrings replaced respectively by the $enviroment's reversed IPv4 address and server's port.

Therefore:

enable 'DNSBL',
    blacklists => {
        'my example blacklist' => '$ip.$port.blacklist.example.com', # single quotes!
    };

Will query 1.0.0.127.80.blacklist.example.com for IP 127.0.0.1 acessing over port 80.

blacklisted
enable 'DNSBL',
    blacklists => { ... },
    blacklisted => sub {
        my ($env, $blacklist, $is_cached) = @_;

        # Do some logging here

        if (!$is_cached && $blacklist eq 'blacklist name') {
            warn "$blacklist matched another address!";
        }

        if ($ENV{DEBUG} || $ENV{FRIENDLY}) {
            return [ 200, [ 'Content-type' => 'text/html' ], [
                "<html><body>",
                "<h1>Hello, buddy ($env->{REMOTE_ADDR})!</h1>",
                "<p>Looks like you're banned at $blacklist!</p>",
                "<p>Sorry :(</p>",
                "</body></html>",
            ] ];
        }

        [ 500, [ 'Content-type' => 'text/plain' ], [ "Die, spammer! ] ];
    };

The blacklisted option specifies a coderef that will be called at the first blacklist that detect this IP as flagged, returing immediately it's return value.

Defaults to:

sub { [ 500, [ 'Content-Type' => 'text/plain' ], [ '' ] ] }
cache, cache_time
enable 'DNSBL',
    blacklists => { ... },
    cache_time => '1h',
    cache      => $cache;

The cache option specifies an object which handles get and set methods for caching whether an IP is blacklisted or not. If this option is set, it expects cache_time to be a string that can be parsed by this object and contains how long should this data be cached. Defaults to '86400' (1 day).

resolver
my $my_resolver = Net::DNS::Resolver->new(
    nameservers => [ '10.1.1.128', '10.1.2.128' ],
    recurse     => 0,
    debug       => 1
);

builder {
    enable 'DNSBL',
        resolver   => $my_resolver,
        blacklists => { ... };
    $app;
};

A Net::DNS::Resolver object. Defaults to Net::DNS::Resolver->new.

SEE ALSO

Net::DNS::Resolver

WHITELISTING

There's no build-in way of whitelisting IPs or certain paths, however this can be quickly solved by using Plack::Builder's enable_if:

builder {
    enable_if {
        !$ENV{DEBUG} && $_[0]->{REMOTE_ADDR} ne '127.0.0.1'
    } 'DNSBL', ...;
    $app;
};

AUTHOR

Victor Franco, <victorfrancovl at gmail.com>

BUGS

Please report any bugs or feature requests to bug-plack-middleware-dnsbl at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Plack-Middleware-DNSBL. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

Patches welcome at https://www.github.com/vtfrvl/plack-middleware-dnsbl

COPYRIGHT

Copyright 2015 Victor Franco.

LICENSE

This program is free software; you can redistribute it and/or modify it under the terms of the the Artistic License (2.0). You may obtain a copy of the full license at:

http://www.perlfoundation.org/artistic_license_2_0

Any use, modification, and distribution of the Standard or Modified Versions is governed by this Artistic License. By using, modifying or distributing the Package, you accept this license. Do not use, modify, or distribute the Package, if you do not accept this license.

If your Modified Version has been derived from a Modified Version made by someone other than you, you are nevertheless required to ensure that your Modified Version complies with the requirements of this license.

This license does not grant you the right to use any trademark, service mark, tradename, or logo of the Copyright Holder.

This license includes the non-exclusive, worldwide, free-of-charge patent license to make, have made, use, offer to sell, sell, import and otherwise transfer the Package with respect to any patent claims licensable by the Copyright Holder that are necessarily infringed by the Package. If you institute patent litigation (including a cross-claim or counterclaim) against any party alleging that the Package constitutes direct or contributory patent infringement, then this Artistic License to you shall terminate on the date that such litigation is filed.

Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.