| 1 | package Net::Google::SafeBrowsing3::MySQL; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use base 'Net::Google::SafeBrowsing3::DBI'; | ||
| 7 | |||
| 8 | use Carp; | ||
| 9 | use DBI; | ||
| 10 | use List::Util qw(first); | ||
| 11 | |||
| 12 | |||
| 13 | our $VERSION = '0.2'; | ||
| 14 | |||
| 15 | |||
| 16 | |||
| 17 | =head1 NAME | ||
| 18 | |||
| 19 | Net::Google::SafeBrowsing3::MySQL - MySQL as back-end storage for the Google Safe Browsing v3 database | ||
| 20 | |||
| 21 | =head1 SYNOPSIS | ||
| 22 | |||
| 23 | use Net::Google::SafeBrowsing3::MySQL; | ||
| 24 | |||
| 25 | my $storage = Net::Google::SafeBrowsing3::MySQL->new(host => '127.0.0.1', database => 'GoogleSafeBrowsingv3'); | ||
| 26 | ... | ||
| 27 | $storage->close(); | ||
| 28 | |||
| 29 | =head1 DESCRIPTION | ||
| 30 | |||
| 31 | This is an implementation of L<Net::Google::SafeBrowsing3::Storage> using MySQL. | ||
| 32 | |||
| 33 | =cut | ||
| 34 | |||
| 35 | |||
| 36 | =head1 CONSTRUCTOR | ||
| 37 | |||
| 38 | =over 4 | ||
| 39 | |||
| 40 | =back | ||
| 41 | |||
| 42 | =head2 new() | ||
| 43 | |||
| 44 | Create a Net::Google::SafeBrowsing3::MySQL object | ||
| 45 | |||
| 46 | my $storage = Net::Google::SafeBrowsing3::MySQL->new( | ||
| 47 | host => '127.0.0.1', | ||
| 48 | database => 'GoogleSafeBrowsingv3', | ||
| 49 | username => 'foo', | ||
| 50 | password => 'bar' | ||
| 51 | ); | ||
| 52 | |||
| 53 | Arguments | ||
| 54 | |||
| 55 | =over 4 | ||
| 56 | |||
| 57 | =item host | ||
| 58 | |||
| 59 | Required. MySQL host name | ||
| 60 | |||
| 61 | =item database | ||
| 62 | |||
| 63 | Required. MySQL database name to connect to. | ||
| 64 | |||
| 65 | =item username | ||
| 66 | |||
| 67 | Required. MySQL username. | ||
| 68 | |||
| 69 | =item password | ||
| 70 | |||
| 71 | Required. MySQL password. | ||
| 72 | |||
| 73 | =item port | ||
| 74 | |||
| 75 | Optional. MySQL port number to connect to. | ||
| 76 | |||
| 77 | =item keep_all | ||
| 78 | |||
| 79 | Optional. Set to 1 to keep old information (such as expiring full hashes) in the database. 0 (delete) by default. | ||
| 80 | |||
| 81 | =back | ||
| 82 | |||
| 83 | |||
| 84 | =cut | ||
| 85 | |||
| 86 | sub new { | ||
| 87 | my ($class, %args) = @_; | ||
| 88 | |||
| 89 | my $self = { # default arguments | ||
| 90 | host => '127.0.0.1', | ||
| 91 | database => 'GoogleSafeBrowsingv3', | ||
| 92 | port => 3306, | ||
| 93 | keep_all => 0, | ||
| 94 | |||
| 95 | %args, | ||
| 96 | }; | ||
| 97 | |||
| 98 | bless $self, $class or croak "Can't bless $class: $!"; | ||
| 99 | |||
| 100 | |||
| 101 | $self->init(); | ||
| 102 | |||
| 103 | return $self; | ||
| 104 | } | ||
| 105 | |||
| 106 | =head1 PUBLIC FUNCTIONS | ||
| 107 | |||
| 108 | =over 4 | ||
| 109 | |||
| 110 | See L<Net::Google::SafeBrowsing3::Storage> for a complete list of public functions. | ||
| 111 | |||
| 112 | =back | ||
| 113 | |||
| 114 | =head2 close() | ||
| 115 | |||
| 116 | Cleanup old full hashes, and close the connection to the database. | ||
| 117 | |||
| 118 | $storage->close(); | ||
| 119 | |||
| 120 | |||
| 121 | =cut | ||
| 122 | |||
| 123 | sub init { | ||
| 124 | my ($self, %args) = @_; | ||
| 125 | |||
| 126 | $self->{dbh} = DBI->connect("DBI:mysql:database=" . $self->{database} . ";host=" . $self->{host} . ";port=" . $self->{port}, $self->{username}, $self->{password}, {'RaiseError' => 1}); | ||
| 127 | |||
| 128 | my @tables = $self->{dbh}->tables; | ||
| 129 | |||
| 130 | if (! defined first { $_ =~ '`updates`' } @tables) { | ||
| 131 | $self->create_table_updates(); | ||
| 132 | } | ||
| 133 | if (! defined first { $_ =~ '`a_chunks`' } @tables) { | ||
| 134 | $self->create_table_a_chunks(); | ||
| 135 | } | ||
| 136 | if (! defined first { $_ =~ '`s_chunks`' } @tables) { | ||
| 137 | $self->create_table_s_chunks(); | ||
| 138 | } | ||
| 139 | if (! defined first { $_ =~ '`full_hashes`' } @tables) { | ||
| 140 | $self->create_table_full_hashes(); | ||
| 141 | } | ||
| 142 | if (! defined first { $_ =~ '`full_hashes_errors`' } @tables) { | ||
| 143 | $self->create_table_full_hashes_errors(); | ||
| 144 | } | ||
| 145 | } | ||
| 146 | |||
| 147 | |||
| 148 | sub create_table_updates { | ||
| 149 | my ($self, %args) = @_; | ||
| 150 | |||
| 151 | my $schema = qq{ | ||
| 152 | CREATE TABLE updates ( | ||
| 153 | last INT NOT NULL DEFAULT '0', | ||
| 154 | wait INT NOT NULL DEFAULT '0', | ||
| 155 | errors INT NOT NULL DEFAULT '1800', | ||
| 156 | list VARCHAR( 50 ) NOT NULL | ||
| 157 | ); | ||
| 158 | }; # Need to handle errors | ||
| 159 | |||
| 160 | $self->{dbh}->do($schema); | ||
| 161 | |||
| 162 | my $index = qq{ | ||
| 163 | CREATE UNIQUE INDEX list_unique ON updates (list); | ||
| 164 | }; | ||
| 165 | $self->{dbh}->do($index); | ||
| 166 | } | ||
| 167 | |||
| 168 | sub create_table_a_chunks { | ||
| 169 | my ($self, %args) = @_; | ||
| 170 | |||
| 171 | my $schema = qq{ | ||
| 172 | CREATE TABLE a_chunks ( | ||
| 173 | prefix VARBINARY( 32 ), | ||
| 174 | num INT NOT NULL, | ||
| 175 | list VARCHAR( 25 ) NOT NULL | ||
| 176 | ); | ||
| 177 | }; | ||
| 178 | |||
| 179 | $self->{dbh}->do($schema); | ||
| 180 | |||
| 181 | my $index = qq{ | ||
| 182 | CREATE INDEX a_chunks_num_list ON a_chunks ( | ||
| 183 | num, | ||
| 184 | list | ||
| 185 | ); | ||
| 186 | }; | ||
| 187 | # $self->{dbh}->do($index); | ||
| 188 | |||
| 189 | $index = qq{ | ||
| 190 | CREATE UNIQUE INDEX a_chunks_unique ON a_chunks ( | ||
| 191 | prefix, | ||
| 192 | num, | ||
| 193 | list | ||
| 194 | ); | ||
| 195 | }; | ||
| 196 | $self->{dbh}->do($index); | ||
| 197 | } | ||
| 198 | |||
| 199 | sub create_table_s_chunks { | ||
| 200 | my ($self, %args) = @_; | ||
| 201 | |||
| 202 | my $schema = qq{ | ||
| 203 | CREATE TABLE s_chunks ( | ||
| 204 | prefix VARBINARY( 32 ), | ||
| 205 | num INT NOT NULL, | ||
| 206 | add_num INT DEFAULT 0, | ||
| 207 | list VARCHAR( 25 ) NOT NULL | ||
| 208 | ); | ||
| 209 | }; | ||
| 210 | |||
| 211 | $self->{dbh}->do($schema); | ||
| 212 | |||
| 213 | |||
| 214 | my $index = qq{ | ||
| 215 | CREATE INDEX s_chunks_num ON s_chunks ( | ||
| 216 | num | ||
| 217 | ); | ||
| 218 | }; | ||
| 219 | # $self->{dbh}->do($index); | ||
| 220 | |||
| 221 | $index = qq{ | ||
| 222 | CREATE INDEX s_chunks_num_list ON s_chunks ( | ||
| 223 | num, | ||
| 224 | list | ||
| 225 | ); | ||
| 226 | }; | ||
| 227 | # $self->{dbh}->do($index); | ||
| 228 | |||
| 229 | $index = qq{ | ||
| 230 | CREATE UNIQUE INDEX s_chunks_unique ON s_chunks ( | ||
| 231 | prefix, | ||
| 232 | num, | ||
| 233 | add_num, | ||
| 234 | list | ||
| 235 | ); | ||
| 236 | }; | ||
| 237 | $self->{dbh}->do($index); | ||
| 238 | } | ||
| 239 | |||
| 240 | sub create_table_full_hashes { | ||
| 241 | my ($self, %args) = @_; | ||
| 242 | |||
| 243 | my $schema = qq{ | ||
| 244 | CREATE TABLE full_hashes ( | ||
| 245 | id INT AUTO_INCREMENT PRIMARY KEY, | ||
| 246 | hash VARBINARY( 32 ), | ||
| 247 | list VARCHAR( 25 ), | ||
| 248 | end INT Default '0', | ||
| 249 | type TINYINT Default '0' | ||
| 250 | ); | ||
| 251 | }; | ||
| 252 | |||
| 253 | $self->{dbh}->do($schema); | ||
| 254 | |||
| 255 | my $index = qq{ | ||
| 256 | CREATE UNIQUE INDEX hash ON full_hashes ( | ||
| 257 | hash, | ||
| 258 | list | ||
| 259 | ); | ||
| 260 | }; | ||
| 261 | $self->{dbh}->do($index); | ||
| 262 | } | ||
| 263 | |||
| 264 | sub create_table_full_hashes_errors { | ||
| 265 | my ($self, %args) = @_; | ||
| 266 | |||
| 267 | my $schema = qq{ | ||
| 268 | CREATE TABLE full_hashes_errors ( | ||
| 269 | id INT AUTO_INCREMENT PRIMARY KEY, | ||
| 270 | errors INT Default '0', | ||
| 271 | prefix VARBINARY( 32 ), | ||
| 272 | timestamp INT Default '0' | ||
| 273 | ); | ||
| 274 | }; | ||
| 275 | |||
| 276 | $self->{dbh}->do($schema); | ||
| 277 | } | ||
| 278 | |||
| 279 | |||
| 280 | sub add_chunks_s { | ||
| 281 | my ($self, %args) = @_; | ||
| 282 | my $chunknum = $args{chunknum} || 0; | ||
| 283 | my $chunks = $args{chunks} || []; | ||
| 284 | my $list = $args{'list'} || ''; | ||
| 285 | |||
| 286 | $self->{add_s_chunk} ||= $self->{dbh}->prepare('INSERT IGNORE INTO s_chunks (prefix, num, add_num, list) VALUES (?, ?, ?, ?)'); | ||
| 287 | my $add = $self->{add_s_chunk}; | ||
| 288 | $self->{dbh}->{AutoCommit} = 0; | ||
| 289 | |||
| 290 | foreach my $chunk (@$chunks) { | ||
| 291 | $add->execute( $chunk->{prefix}, $chunknum, $chunk->{add_chunknum}, $list ); | ||
| 292 | } | ||
| 293 | |||
| 294 | if (scalar @$chunks == 0) { # keep empty chunks | ||
| 295 | $add->execute( '', $chunknum, '', $list ); | ||
| 296 | } | ||
| 297 | |||
| 298 | $self->{dbh}->commit; | ||
| 299 | $self->{dbh}->{AutoCommit} = 1; | ||
| 300 | } | ||
| 301 | |||
| 302 | sub add_chunks_a { | ||
| 303 | my ($self, %args) = @_; | ||
| 304 | my $chunknum = $args{chunknum} || 0; | ||
| 305 | my $chunks = $args{chunks} || []; | ||
| 306 | my $list = $args{'list'} || ''; | ||
| 307 | |||
| 308 | $self->{add_a_chunk} ||= $self->{dbh}->prepare('INSERT IGNORE INTO a_chunks (prefix, num, list) VALUES (?, ?, ?)'); | ||
| 309 | my $add = $self->{add_a_chunk}; | ||
| 310 | $self->{dbh}->{AutoCommit} = 0; | ||
| 311 | |||
| 312 | foreach my $chunk (@$chunks) { | ||
| 313 | $add->execute( $chunk->{prefix}, $chunknum, $list ); | ||
| 314 | } | ||
| 315 | |||
| 316 | if (scalar @$chunks == 0) { # keep empty chunks | ||
| 317 | $add->execute( '', $chunknum, $list ); | ||
| 318 | } | ||
| 319 | |||
| 320 | $self->{dbh}->commit; | ||
| 321 | $self->{dbh}->{AutoCommit} = 1; | ||
| 322 | } | ||
| 323 | |||
| 324 | =head1 CHANGELOG | ||
| 325 | |||
| 326 | =over 4 | ||
| 327 | |||
| 328 | =item 0.1 | ||
| 329 | |||
| 330 | Initial release. | ||
| 331 | |||
| 332 | =back | ||
| 333 | |||
| 334 | =head1 SEE ALSO | ||
| 335 | |||
| 336 | See L<Net::Google::SafeBrowsing3> for handling Google Safe Browsing v2. | ||
| 337 | |||
| 338 | See L<Net::Google::SafeBrowsing3::Storage> for the list of public functions. | ||
| 339 | |||
| 340 | See L<Net::Google::SafeBrowsing3::Sqlite> for a back-end using Sqlite. | ||
| 341 | |||
| 342 | Google Safe Browsing v3 API: L<https://developers.google.com/safe-browsing/developers_guide_v3> | ||
| 343 | |||
| 344 | |||
| 345 | =head1 AUTHOR | ||
| 346 | |||
| 347 | Julien Sobrier, E<lt>julien@sobrier.netE<gt> | ||
| 348 | |||
| 349 | =head1 COPYRIGHT AND LICENSE | ||
| 350 | |||
| 351 | Copyright (C) 2015 by Julien Sobrier | ||
| 352 | |||
| 353 | This library is free software; you can redistribute it and/or modify | ||
| 354 | it under the same terms as Perl itself, either Perl version 5.8.8 or, | ||
| 355 | at your option, any later version of Perl 5 you may have available. | ||
| 356 | |||
| 357 | |||
| 358 | =cut | ||
| 359 | |||
| 360 | 1; |