Permalink
Browse files

Break NB::Network::IPFilter out into its own module tentativly named …

…Config::IPFilter
  • Loading branch information...
1 parent 89ea9c6 commit 5ee6323b1b432f0eca42b7e51ffb6a91f6cc9d98 @sanko committed Feb 1, 2011
View
@@ -26,6 +26,7 @@ my $mb = $class->new(
AnyEvent => 0,
'AnyEvent::Handle::Throttle' => '0.000002005',
'Bit::Vector' => 0,
+ 'Config::IPFilter' => 0,
'Digest::SHA' => 5.45,
Exporter => 0,
'File::Path' => 2.07,
@@ -37,6 +38,7 @@ my $mb = $class->new(
build_requires => {AnyEvent => 0,
'AnyEvent::Handle::Throttle' => '0.000002005',
'Bit::Vector' => 0,
+ 'Config::IPFilter' => 0,
'Digest::SHA' => 5.45,
Exporter => 0,
'File::Path' => 2.07,
View
@@ -8,8 +8,6 @@ lib/Net/BitTorrent.pm
lib/Net/BitTorrent/Callbacks.pod
lib/Net/BitTorrent/DHT.pm
lib/Net/BitTorrent/DHT/Standalone.pm
-lib/Net/BitTorrent/Network/IPFilter.pm
-lib/Net/BitTorrent/Network/IPFilter/Rule.pm
lib/Net/BitTorrent/Network/TCP.pm
lib/Net/BitTorrent/Network/UDP.pm
lib/Net/BitTorrent/Network/Utility.pm
@@ -61,9 +59,6 @@ README
t/10000_by_class/Net/BitTorrent.t
t/10000_by_class/Net/BitTorrent/DHT.t
t/10000_by_class/Net/BitTorrent/DHT/Standalone.t
-t/10000_by_class/Net/BitTorrent/Network/IPFilter.t
-t/10000_by_class/Net/BitTorrent/Network/IPFilter/Rule.t
-t/10000_by_class/Net/BitTorrent/Network/IPFilter_path.t
t/10000_by_class/Net/BitTorrent/Protocol/BEP03/Peer/Incoming.t
t/10000_by_class/Net/BitTorrent/Protocol/BEP03/Peer/Outgoing.t
t/10000_by_class/Net/BitTorrent/Storage/File.t
View
@@ -106,14 +106,14 @@ package Net::BitTorrent;
#
has 'ip_filter' => (is => 'ro',
- isa => 'Net::BitTorrent::Network::IPFilter',
+ isa => 'Config::IPFilter',
init_arg => undef,
builder => '_build_ip_filter'
);
sub _build_ip_filter {
- require Net::BitTorrent::Network::IPFilter;
- Net::BitTorrent::Network::IPFilter->new();
+ require Config::IPFilter;
+ Config::IPFilter->new();
}
# DHT (requires udp attribute)
@@ -100,8 +100,8 @@ passed to them. Note that this list is subject to change.
This alert is generated when an incoming connection is declined or an outgoing
connection attempt is aborted because the remote host was
-L<banned|Net::BitTorrent::Network::IPFilter/"is_banned"> by our local
-L<ipfilter|Net::BitTorrent::Network::IPFilter>. This is a C<debug>ging event.
+L<banned|Config::IPFilter/"is_banned"> by our local
+L<ipfilter|Config::IPFilter>. This is a C<debug>ging event.
In addition to the L<basic arguments|/basic_arguments>, this callback is
passed the following attributes:
@@ -119,7 +119,7 @@ This is either C<tcp4>, C<tcp6>, C<udp4>, or C<udp6>.
=item C<rule>
-This is the L<Net::BitTorrent::Network::IPFilter::Rule> object which caused
+This is the L<Config::IPFilter::Rule> object which caused
this peer to result in a banned state.
=back
@@ -43,14 +43,14 @@ package Net::BitTorrent::DHT::Standalone;
#
has 'ip_filter' => (is => 'ro',
- isa => 'Net::BitTorrent::Network::IPFilter',
+ isa => 'Config::IPFilter',
init_arg => undef,
builder => '_build_ip_filter'
);
sub _build_ip_filter {
- require Net::BitTorrent::Network::IPFilter;
- Net::BitTorrent::Network::IPFilter->new();
+ require Config::IPFilter;
+ Config::IPFilter->new();
}
sub _build_udp6 {
@@ -1,237 +0,0 @@
-package Net::BitTorrent::Network::IPFilter;
-{
- use Moose;
- use Moose::Util::TypeConstraints;
- use 5.010.000;
- our $MAJOR = 0; our $MINOR = 74; our $DEV = 13; our $VERSION = sprintf('%0d.%03d' . ($DEV ? (($DEV < 0 ? '' : '_') . '%03d') : ('')), $MAJOR, $MINOR, abs $DEV);
- use lib '../../../';
- use Net::BitTorrent::Network::Utility qw[:paddr];
- sub BUILD { 1; }
- has 'rules' => (
- isa => 'ArrayRef[Net::BitTorrent::Network::IPFilter::Rule]',
- is => 'ro',
- traits => ['Array'],
- init_arg => undef,
- default => sub { [] },
- handles => {
- add_rule => 'push',
- count_rules => 'count',
- is_empty => 'is_empty',
- get_rule => 'get',
- first_rule => 'first',
- grep_rules => 'grep',
- map_rules => 'map',
- sort_rules => 'sort',
- sort_rules_in_place => 'sort_in_place',
- shuffle_rules => 'shuffle',
- clear_rules => 'clear',
- insert_rule => 'insert',
- delete_rule => 'delete',
- push_rule => 'push',
- pop_rule => 'pop'
- }
- );
- around 'add_rule' => sub {
- my ($c, $s, $l, $u, $a, $d) = @_;
- $l = blessed $l? $l : sub {
- require Net::BitTorrent::Network::IPFilter::Rule;
- Net::BitTorrent::Network::IPFilter::Rule->new(lower => $l,
- upper => $u,
- access_level => $a,
- description => $d
- );
- }
- ->();
- return $c->($s, $l) ? $l : ();
- };
-
- sub load {
- my ($s, $path) = @_;
- open(my $IPFilter, '<', $path) || return;
- for my $line (<$IPFilter>) {
- next if $line =~ m[(?:^#|^$)];
- my ($range, $access_level, $desc)
- = ($line =~ m[^(.+-.+)\s*,\s*(\d+)\s*,\s*(.+)\s*$]);
- next if !$range;
- my ($start, $end) = ($range =~ m[^(.+)\s*-\s*(.+)\s*$]);
- $_ =~ s[\s][]g for $start, $end;
- $s->add_rule($start, $end, $access_level, $desc);
- }
- 1;
- }
-
- sub save {
- my ($s, $path) = @_;
- open(my $IPFilter, '>', $path) || return;
- for my $rule (
- $s->sort_rules(
- sub {
- $_[0]->lower cmp $_[1]->lower
- || $_[0]->upper cmp $_[1]->upper;
- }
- )
- )
- { syswrite $IPFilter, $rule->_as_string . "\n";
- }
- return close $IPFilter;
- }
-
- sub is_banned {
- my ($s, $ip) = @_;
- return $s->first_rule(
- sub {
- $_->in_range($ip) && $_->access_level < 127;
- }
- ) || ();
- }
-}
-1;
-
-=pod
-
-=head1 NAME
-
-Net::BitTorrent::Network::IPFilter - Simple, rule-based IP filter
-
-=head1 Description
-
- # Example of a "ipfilter.dat" file
- #
- # All entered IP ranges will be blocked in both directions. Be careful
- # what you enter here. Wrong entries may totally block access to the
- # network.
- #
- # Format:
- # IP-Range , Access Level , Description
- #
- # Access Levels:
- # 127 blocked
- # >=127 permitted
-
- 064.094.089.000 - 064.094.089.255 , 000 , Gator.com
-
-This entry will block the IPs from 064.094.089.000 to 064.094.089.255, i.e.
-L<Net::BitTorrent> will not connect to any IP in this range.
-
-Warning:
-
-The example above will block any connection to the specified IP-range. This
-may reduce the number of sources for downloads.
-
-At the moment only one access levels are implemented. A value at or below
-C<127> means that any connection-attempt is denied.
-
-=head2 Notes
-
-Remember...
-
-=over
-
-=item * Filtering will prevent up and downloading to the banned clients
-
-=item * Filtering is done at the protocol level so no connection to banned
-clients is ever established
-
-=back
-
-=head1 my $filter = Net::BitTorrent::Network::IPFilter->B<new>( )
-
-This constructs a new, empty object. There are currently no accepted
-arguments.
-
-=head1 $filter->B<add_rule>( $rule )
-
-This method adds a new L<range|Net::BitTorrent::Network::IPFilter::Rule> to
-the in-memory ipfilter.
-
-=head1 $filter->B<add_rule>( $lower, $upper, $access_level, $description )
-
-This method coerces the arguments into a new
-L<range|Net::BitTorrent::Network::IPFilter::Rule> which is the added to the
-in-memory ipfilter.
-
-=head1 $filter->B<count_rules>( )
-
-Returns how many L<rule|Net::BitTorrent::Network::IPFilter::Rule>s are loaded.
-
-=head1 $filter->B<is_empty>( )
-
-Returns a boolean value indicating whether or not there are any
-L<rule|Net::BitTorrent::Network::IPFilter::Rule>s loaded in the ipfilter.
-
-=head1 $filter->B<clear_rules>( )
-
-Deletes all L<rule|Net::BitTorrent::Network::IPFilter::Rule>s from the
-ipfilter.
-
-=head1 $filter->B<load>( $path )
-
-Slurps an ipfilter.dat-like file and adds the
-L<rule|Net::BitTorrent::Network::IPFilter::Rule>s found inside to the
-ipfilter.
-
-=head1 $filter->B<save>( $path )
-
-Stores the in-memory ipfilter to disk.
-
-=head1 $filter->B<is_banned>( $ip )
-
-Indicates whether or not C<$ip> is banned. If so, the
-L<rule|Net::BitTorrent::Network::IPFilter::Rule> in which it was found is
-returned. If not, a false value is returned. Currently, rules with an
-L<< access_level|Net::BitTorrent::Network::IPFilter::Rule/"$filter->B<access_level>( )" >>
-at or below C<127> are considered banned.
-
-=head1 IPv6 Support
-
-The standard ipfilter.dat only supports IPv4 addresses but
-L<Net::BitTorrent>'s current implementation supports IPv6 as well. Keep this
-in mind when L<storing|/save> an ipfilter.dat file to disk.
-
-=head1 See Also
-
-L<Emule Project's ipfilter.dat documentation|http://www.emule-project.net/home/perl/help.cgi?l=1&topic_id=142&rm=show_topic>
-
-=head1 Author
-
-=begin :html
-
-L<Sanko Robinson|http://sankorobinson.com/>
-<L<sanko@cpan.org|mailto://sanko@cpan.org>> -
-L<http://sankorobinson.com/|http://sankorobinson.com/>
-
-CPAN ID: L<SANKO|http://search.cpan.org/~sanko>
-
-=end :html
-
-=begin :text
-
-Sanko Robinson <sanko@cpan.org> - http://sankorobinson.com/
-
-CPAN ID: SANKO
-
-=end :text
-
-=head1 License and Legal
-
-Copyright (C) 2008-2010 by Sanko Robinson <sanko@cpan.org>
-
-This program is free software; you can redistribute it and/or modify it under
-the terms of
-L<The Artistic License 2.0|http://www.perlfoundation.org/artistic_license_2_0>.
-See the F<LICENSE> file included with this distribution or
-L<notes on the Artistic License 2.0|http://www.perlfoundation.org/artistic_2_0_notes>
-for clarification.
-
-When separated from the distribution, all original POD documentation is
-covered by the
-L<Creative Commons Attribution-Share Alike 3.0 License|http://creativecommons.org/licenses/by-sa/3.0/us/legalcode>.
-See the
-L<clarification of the CCA-SA3.0|http://creativecommons.org/licenses/by-sa/3.0/us/>.
-
-Neither this module nor the L<Author|/Author> is affiliated with BitTorrent,
-Inc.
-
-=for rcs $Id$
-
-=cut
Oops, something went wrong.

0 comments on commit 5ee6323

Please sign in to comment.