]> jfr.im git - irc/SurrealServices/srsv.git/blame - branches/0.4.3/utils/blacklistLoader.pl
blacklistLoader can now use wget or curl, if curl is available
[irc/SurrealServices/srsv.git] / branches / 0.4.3 / utils / blacklistLoader.pl
CommitLineData
7b261bb8 1#!/usr/bin/perl
2
3# This file is part of SurrealServices.
4#
5# SurrealServices is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 2 of the License, or
8# (at your option) any later version.
9#
10# SurrealServices is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with SurrealServices; if not, write to the Free Software
17# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19# SurrealChat.net does not provide the Blacklist Data
20# is in no way associated with dronebl.org,
21# nor are we providing a license to download/use it.
22# Be sure to direct availability/accuracy/licensing questions to
23# http://dronebl.org/docs/howtouse
24
25use strict;
26use DBI;
27use Cwd 'abs_path';
28use File::Basename;
29
30use Cwd qw( abs_path getcwd );
31use File::Basename qw( dirname );
32BEGIN {
33 my %constants = (
34 CWD => getcwd(),
35 PREFIX => abs_path(dirname(abs_path($0)).'/../'),
36 );
37 require constant; import constant \%constants;
38}
39use lib PREFIX;
40
41#Date::Parse might not be on the user's system, so we ship our own copy.
42use Date::Parse;
43
44use SrSv::SimpleHash qw(readHash);
2d25814c 45use SrSv::Conf::sql;
7b261bb8 46use SrSv::Conf2Consts 'sql';
47
6751fb58 48my $srcname = 'http://dronebl.org/buildzone.do';
7b261bb8 49my $bindip = undef;
50my $unpackname = $srcname;
51my $diffname = $srcname.'.diff';
00bf607d 52my $agent = findAgent();
53
54sub findAgent {
55 my $agent;
56 my $ret = system('which curl');
57 if(($ret >> 8) == 0) {
58 # we prefer curl b/c it can handle gzip compression!
59 # we do IPv4 b/c either their IPv6 gateway or ours is SLOW
60 # UPDATE 2011/05: due to DDoS, IPv4 is swamped, IPv6 is only way!
61 $agent = 'curl --compressed --silent';
62 } else {
63 $agent = 'wget -q -O -';
64 }
65 return $agent;
66}
7b261bb8 67
68my $OPMDATA;
00bf607d 69unless(open $OPMDATA, '-|', "$agent $srcname") {
7b261bb8 70 print STDERR "FATAL: Processing failed.\n";
71 exit -1;
72}
73
74print "Connecting to database...\n";
75
76my $dbh;
77eval {
78 $dbh = DBI->connect("DBI:mysql:".sql_conf_mysql_db, sql_conf_mysql_user, sql_conf_mysql_pass,
79 { AutoCommit => 1, RaiseError => 1, PrintError => 1 })
80};
81
82if($@) {
83 print STDERR "FATAL: Can't connect to database:\n$@\n";
84 print STDERR "You must have SrSv properly setup before you attempt to use this helper script.\n\n";
85 exit -1;
86}
87
88print "Creating new table...\n";
89
90$dbh->do("DROP TABLE IF EXISTS `newopm`");
91$dbh->do(
7b728f77 92"CREATE TEMPORARY TABLE `newopm` (
7b261bb8 93 `ipnum` int(11) unsigned NOT NULL default 0,
94 `ipaddr` char(15) NOT NULL default '0.0.0.0',
95 `type` tinyint(3) NOT NULL default 0,
96 PRIMARY KEY (`ipnum`),
97 UNIQUE KEY `addrkey` (`ipaddr`)
7b728f77 98) Engine=Memory;"
7b261bb8 99);
100
101sub save2DB($@) {
102 my ($baseQuery, @rows) = @_;
103 $dbh->do("$baseQuery ".join(',', @rows));
104}
105
106sub processData() {
107 print "Inserting data... ";
108
109 $dbh->do("ALTER TABLE `newopm` DISABLE KEYS");
7b728f77 110 $dbh->do("LOCK TABLES `newopm` WRITE");
7b261bb8 111 my $type;
112 my $baseQuery = "REPLACE INTO `newopm` (ipnum, ipaddr, type) VALUES ";
113 my @rows;
114 my $count = 0;
115 while(my $x = <$OPMDATA>) {
116 chomp $x;
ddb4918f 117 if($x =~ /^:(\d{1,3}):/) {
7b261bb8 118 $type = $1;
119 } elsif($x =~ /^(\d+\.\d+\.\d+\.\d+)$/) {
120 next unless $type;
121 my $ipaddr = $1;
122 push @rows, '(INET_ATON('.$dbh->quote($ipaddr).'),'.$dbh->quote($ipaddr).','.$type.')';
123 $count++;
7b728f77 124 if(scalar(@rows) > 1000) {
7b261bb8 125 save2DB($baseQuery, @rows);
126 @rows = ();
127 }
128 }
129 }
130 die "No entries found\n" unless $count;
131
132 #rename($unpackname, $srcname.'.old');
133 save2DB($baseQuery, @rows) if scalar(@rows);
134
135 $dbh->do("UNLOCK TABLES");
136 $dbh->do("ALTER TABLE `newopm` ENABLE KEYS");
137}
138
139processData();
140close $OPMDATA;
141
142print "done.\nRemoving old table...\n";
143$dbh->do("DROP TABLE IF EXISTS `oldopm`");
7b728f77 144$dbh->do("ALTER TABLE opm ENGINE=InnoDB");
145$dbh->do("START TRANSACTION");
7b261bb8 146print "Renaming new table...\n";
7b728f77 147#$dbh->{RaiseError} = $dbh->{PrintError} = 0; # the following commands can fail, but are harmless.
148$dbh->do("TRUNCATE TABLE `opm`");
149$dbh->do("INSERT INTO opm SELECT * FROM newopm");
150$dbh->do("COMMIT");
7b261bb8 151
152print "Blacklist table update complete.\n";
153
154exit;