projects
/
spider.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (parent:
3b87cb9
)
rearrange badip files and code
author
Dirk Koopman
<djk@tobit.co.uk>
Sun, 8 Jan 2023 07:26:19 +0000
(07:26 +0000)
committer
Dirk Koopman
<djk@tobit.co.uk>
Sun, 8 Jan 2023 07:26:19 +0000
(07:26 +0000)
cmd/set/badip.pl
patch
|
blob
|
history
perl/DXCIDR.pm
patch
|
blob
|
history
diff --git
a/cmd/set/badip.pl
b/cmd/set/badip.pl
index 962fc641435cc65178169fa864ab11ea12ea5ee5..b53763569127f2505be6d73f3afcb3ac27ca8a13 100644
(file)
--- a/
cmd/set/badip.pl
+++ b/
cmd/set/badip.pl
@@
-13,12
+13,20
@@
my @out;
my @added;
my @in = split /\s+/, $line;
return (1, "set/badip: need IP, IP-IP or IP/24") unless @in;
my @added;
my @in = split /\s+/, $line;
return (1, "set/badip: need IP, IP-IP or IP/24") unless @in;
-for (@in) {
- eval{ DXCIDR::add($_); };
- return (1, "set/badip: $_ $@") if $@;
- push @added, $_;
+for my $ip (@in) {
+ my $r;
+ eval{ $r = DXCIDR::find($ip); };
+ return (1, "set/badip: $ip $@") if $@;
+ if ($r) {
+ push @out, "set/badip: $ip exists, not added";
+ next;
+ }
+ DXCIDR::add($ip);
+ push @added, $ip;
}
my $count = @added;
my $list = join ' ', @in;
}
my $count = @added;
my $list = join ' ', @in;
-push @out, "set/badip: added $count entries: $list";
+DXCIDR::clean_prep();
+DXCIDR::save();
+push @out, "set/badip: added $count entries: $list" if $count;
return (1, @out);
return (1, @out);
diff --git
a/perl/DXCIDR.pm
b/perl/DXCIDR.pm
index 2ef0d1937954dccd2f004bd5777e72a9da773e92..7cb13df0bf4ef7e7f870e99e1a785aa6dc0e0dc7 100644
(file)
--- a/
perl/DXCIDR.pm
+++ b/
perl/DXCIDR.pm
@@
-16,6
+16,7
@@
use DXUtil;
use DXLog;
use IO::File;
use File::Copy;
use DXLog;
use IO::File;
use File::Copy;
+
use Socket qw(AF_INET AF_INET6 inet_pton inet_ntop);
our $active = 0;
use Socket qw(AF_INET AF_INET6 inet_pton inet_ntop);
our $active = 0;
@@
-29,8
+30,7
@@
my $count6 = 0;
sub load
{
if ($active) {
sub load
{
if ($active) {
- $count4 = _load($ipv4, 4);
- $count6 = _load($ipv6, 6);
+ _load();
}
LogDbg('DXProt', "DXCIDR: loaded $count4 IPV4 addresses and $count6 IPV6 addresses");
return $count4 + $count6;
}
LogDbg('DXProt', "DXCIDR: loaded $count4 IPV4 addresses and $count6 IPV6 addresses");
return $count4 + $count6;
@@
-38,49
+38,51
@@
sub load
sub _fn
{
sub _fn
{
- return localdata($badipfn)
. ".$_[0]"
;
+ return localdata($badipfn);
}
sub _load
{
}
sub _load
{
- my $list = shift;
- my $sort = shift;
- my $fn = _fn($sort);
+ my $fn = _fn();
my $fh = IO::File->new($fn);
my $count = 0;
my $fh = IO::File->new($fn);
my $count = 0;
+
+ new();
if ($fh) {
while (<$fh>) {
chomp;
next if /^\s*\#/;
next unless /[\.:]/;
if ($fh) {
while (<$fh>) {
chomp;
next if /^\s*\#/;
next unless /[\.:]/;
-
$list->add_any
($_);
+
add
($_);
++$count;
}
$fh->close;
++$count;
}
$fh->close;
- $list->clean if $count;
- $list->prep_find;
} elsif (-r $fn) {
LogDbg('err', "DXCIDR: $fn not found ($!)");
}
} elsif (-r $fn) {
LogDbg('err', "DXCIDR: $fn not found ($!)");
}
+
+ clean_prep();
+
return $count;
}
sub _put
{
return $count;
}
sub _put
{
- my $list = shift;
- my $sort = shift;
- my $fn = _fn($sort);
+ my $fn = _fn();
my $r = rand;
my $fh = IO::File->new (">$fn.$r");
my $r = rand;
my $fh = IO::File->new (">$fn.$r");
+ my $count = 0;
if ($fh) {
if ($fh) {
- for ($
list
->list) {
+ for ($
ipv4->list, $ipv6
->list) {
$fh->print("$_\n");
$fh->print("$_\n");
+ ++$count;
}
move "$fn.$r", $fn;
} else {
LogDbg('err', "DXCIDR: cannot write $fn.$r $!");
}
}
move "$fn.$r", $fn;
} else {
LogDbg('err', "DXCIDR: cannot write $fn.$r $!");
}
+ return $count;
}
sub add
}
sub add
@@
-92,40
+94,35
@@
sub add
next if /^127\./;
next if /^::1$/;
if (/\./) {
next if /^127\./;
next if /^::1$/;
if (/\./) {
- if ($ipv4->find($ip)) {
- LogDbg('DXProt', "DXCIDR: Ignoring existing IPV4 $ip");
- next;
- }
$ipv4->add_any($ip);
++$count;
++$count4;
} elsif (/:/) {
$ipv4->add_any($ip);
++$count;
++$count4;
} elsif (/:/) {
- if ($ipv6->find($ip)) {
- LogDbg('DXProt', "DXCIDR: Ignoring existing IPV6 $ip");
- next;
- }
$ipv6->add_any($ip);
++$count;
++$count6;
LogDbg('DXProt', "DXCIDR: Added IPV6 $ip address");
}
}
$ipv6->add_any($ip);
++$count;
++$count6;
LogDbg('DXProt', "DXCIDR: Added IPV6 $ip address");
}
}
+ return $count;
+}
+
+sub clean_prep
+{
if ($ipv4 && $count4) {
if ($ipv4 && $count4) {
+ $ipv4->clean;
$ipv4->prep_find;
$ipv4->prep_find;
- _put($ipv4, 4);
}
if ($ipv6 && $count6) {
}
if ($ipv6 && $count6) {
+ $ipv6->clean;
$ipv6->prep_find;
$ipv6->prep_find;
- _put($ipv6, 6);
}
}
- return $count;
}
sub save
{
return 0 unless $active;
}
sub save
{
return 0 unless $active;
- _put($ipv4, 4) if $count4;
- _put($ipv6, 6) if $count6;
+ _put() if $count4 || $count6;
}
sub _sort
}
sub _sort
@@
-167,14
+164,18
@@
sub init
}
import Net::CIDR::Lite;
}
import Net::CIDR::Lite;
+ $active = 1;
- $ipv4 = Net::CIDR::Lite->new;
- $ipv6 = Net::CIDR::Lite->new;
+ new();
- $active = 1;
load();
}
load();
}
-
+sub new
+{
+ $ipv4 = Net::CIDR::Lite->new;
+ $ipv6 = Net::CIDR::Lite->new;
+ $count4 = $count6 = 0;
+}
1;
1;