aboutsummaryrefslogtreecommitdiffstats
path: root/lib/MGA/Mirrors/DB.pm
blob: 2e548471f8869444ba3507827e2447528796a4b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
package MGA::Mirrors::DB;

# $Id$

use strict;
use warnings;
use Config::IniFiles;
use URI;
use DBI;
use File::Temp qw(tempfile);
use Net::DNS;

sub configfile { '/etc/mga-mirrors.ini' }

sub new {
    my ($class) = @_;

    my $conf = (-f './mga-mirrors.ini')
        ? Config::IniFiles->new(-file => './mga-mirrors.ini')
        : Config::IniFiles->new(-file => configfile())
            or return;

    bless {
        conf => $conf,
    }, $class;
}

sub host_ips {
    my ($self, $hostname) = @_;

    my $resolver = Net::DNS::Resolver->new;
    my @addresses;
    foreach my $type (qw'A AAAA') {
        my $packet = $resolver->search($hostname, $type) or next;
        foreach ($packet->answer) {
            $_->type eq $type or next;
            push(@addresses, $_->address);
        }
    }
    @addresses;
}

sub db { 
    my ($self) = @_;
    if ($self->{db} && $self->{db}->ping) {
        return $self->{db};
    } else {
        my $db = DBI->connect(
            'dbi:Pg:' . $self->{conf}->val('db', 'pgconn', ''),
            $self->{conf}->val('db', 'user') || undef,
            $self->{conf}->val('db', 'password') || undef,
            {
                AutoCommit => 0,
                PrintError => 1,
            }
        ) or return;
        return $self->{db} = $db;
    }
}

sub DESTROY {
    my ($self) = @_;

    if ($self->{db}) {
        $self->{db}->disconnect;
    }
}

sub locate_ips {
    my ($self, @ips) = @_;

    my $find = $self->db->prepare(q{
        select countries.* from geoip 
        join countries on geoip.code = countries.code
        where ipmin <= $1 and ipmax >= $1
    });

    foreach (@ips) {
        $find->execute($_);
        my $res = $find->fetchrow_hashref;
        if ($res) {
            $find->finish;
            return $res;
        }
    }

    return;
}

sub protocol_list {
    my ($self) = @_;
    my $select = $self->db->prepare(q{
        select name from protocol order by name
    });
    $select->execute;
    return keys %{ $select->fetchall_hashref([ 'name' ]) }
}

sub bandwidth_name {
    my ($self, $value) = @_;
    $value or return;
    my $select = $self->db->prepare(q{
        select name from bandwidth where value = ?
    });
    $select->execute($value);
    my $res = $select->fetchrow_hashref;
    $select->finish;
    $res->{name}
}

sub bandwidth_list {
    my ($self) = @_;
    my $list = $self->db->prepare(q{
        select * from bandwidth order by value
    });
    $list->execute;
    return $list->fetchall_arrayref({});
}

sub country_info {
    my ($self, $code) = @_;
    my $list = $self->db->prepare(q{
        select * from countries where code = ?
    });
    $list->execute($code);
    my $res = $list->fetchrow_hashref;
    $list->finish;
    $res
}

sub country_list_with_mirror {
    my ($self) = @_;
    my $list = $self->db->prepare(q{
        select * from countries where code in (select country from hosts
        join toplevel_urls on toplevel_urls.hostname = hosts.hostname)
        order by name
    });
    $list->execute;
    return $list->fetchall_arrayref({});
}


sub country_list {
    my ($self) = @_;
    my $list = $self->db->prepare(q{
        select * from countries order by name
    });
    $list->execute;
    return $list->fetchall_arrayref({});
}

sub version_list {
    my ($self) = @_;
    my $list = $self->db->prepare(q{
        select * from distributions
    });
    $list->execute;
    return $list->fetchall_hashref([qw(version)]);
}

sub arch_list {
    my ($self, $version) = @_;
    my $list = $self->db->prepare(q{
        select * from distributions
        } . ($version
        ? q{ where version = ? }
        : q{})
        );
    $list->execute($version ? $version : ());
    return $list->fetchall_hashref([qw(arch)]);
}

sub mirror_level {
    my ($self, $hostname) = @_;
    my $minfo = $self->find_mirrors({
            hostname => $hostname
        });
    if (my $mir = @{ $minfo || []}[0]) {
        if ($mir->{syncfrom}) {
            return $self->_mirror_level($hostname);
        } else {
            return
        }
    } else {
        return;
    }
}

sub _mirror_level {
    my ($self, $hostname, @prev) = @_;
    return if (grep { $hostname eq $_ } @prev);
    my $minfo = $self->find_mirrors({
            hostname => $hostname
        });
    if (my $mir = @{ $minfo || []}[0]) {
        if ($mir->{syncfrom}) {
            my $res = $self->_mirror_level($mir->{syncfrom}, $hostname, @prev);
            return defined($res) ? $res + 1 : undef;
        } else {
            return 0;
        }
    } else {
        return 0;
    }
}


sub mirror_validity {
    my ($self, $uri) = @_;
    my $listf = $self->db->prepare(q{
        select * from global_files
    });
    $listf->execute;
    while (my $res = $listf->fetchrow_hashref) {
        my $furi = URI->new($uri . $res->{relpath});
        $self->_check_url($furi) or return;
    }

    1;
}

sub check_distributions {
    my ($self) = @_;

    my $uneeded_check = $self->db->prepare(q{
        select * from distributions_validity where
        lastcheck > now() - '2 hours'::interval
        });
    $uneeded_check->execute();
    my $uch = $uneeded_check->fetchall_hashref([ qw(urlskey distributionkey) ]);

    my $listd = $self->db->prepare(q{
        select * from toplevel_urls, distributions
        });

    my $addstatus = $self->db->prepare(q{
        insert into distributions_validity (urlskey, distributionkey, exists)
        values (?,?,?)
        });

    my $updstatus = $self->db->prepare(q{
        update distributions_validity set lastcheck = now(), exists = ?
        where urlskey = ? and distributionkey = ?
        });
    my $upd_lastok = $self->db->prepare(q{
        update distributions_validity set lastok = now()
        where urlskey = ? and distributionkey = ?
        });

    my %urls_status = ();

    my $updurl = $self->db->prepare(q{
        update toplevel_urls set lastcheck = now(), valid = ?
        where key = ?
    });
    my $upd_url_lastok = $self->db->prepare(q{
        update toplevel_urls set lastok = now()
        where key = ?
    });

    $listd->execute();
    while (my $res = $listd->fetchrow_hashref) {
        $uch->{$res->{key}}{$res->{dkey}} and next;
        my $url = $self->fmt_url($res);
        if (!exists($urls_status{$res->{key}})) {
            $self->update_host_ips($res->{hostname});
            my $ok = $self->mirror_validity($url);
            $updurl->execute($ok ? 1 : 0, $res->{key});
            $upd_url_lastok->execute($res->{key}) if ($ok);
            $urls_status{$res->{key}} = $ok;
        }
        my $exists;
        if ($urls_status{$res->{key}}) {
            my $furi = URI->new(join('/', $url, $res->{relpath}, $res->{relfile}));
            $exists = $self->_check_url($furi);
        }
        if ($updstatus->execute($exists ? 1 : 0, $res->{key}, $res->{dkey}) == 0) {
            $addstatus->execute($res->{key}, $res->{dkey}, $exists ? 1 : 0);
        }
        $upd_lastok->execute($res->{key}, $res->{dkey}) if ($exists);
        $self->db->commit;
    }
}

sub _check_url {
    my ($self, $furi) = @_;
    my ($fh, $filename) = tempfile();
    close($fh);
    my $cmd = 
    $furi->scheme =~ /^http|ftp$/ ? "wget -q --no-check-certificate -nv -t 1 -T 4 -O $filename " . $furi->as_string :
    $furi->scheme eq 'rsync' ? "rsync --timeout 4 -q " . $furi->as_string . " $filename" : '';
    my $ok = (system($cmd) == 0);
    unlink($filename);
    return $ok
}

sub get_protocol_info {
    my ($self, $protocol) = @_;
    my $get = $self->db->prepare(q{
        select * from protocol where name = ?
    });
    $get->execute($protocol);
    my $res = $get->fetchrow_hashref;
    $get->finish;
    $res;
}

sub find_mirrors {
    my ($self, $filters, $key) = @_;

    my $query = q{
        select *,
        coalesce(hosts.latitude, countries.latitude) as latitude,
        coalesce(hosts.longitude, countries.longitude) as longitude
        from hosts
        join countries on countries.code = hosts.country
        where hosts.hostname in (select hostname from toplevel_urls %s)
        %s
    };

    my (@mvals, @uvals);
    my (@mw, @uw);
    if (keys %{ $filters || {}}) {
        foreach (keys %$filters) {
            $filters->{$_} or next;
            if (my $field = {
                hostname => 'hosts.hostname',
                country => 'countries.code',
                continent => 'countries.contienent_code',
            }->{$_}) {
                push(@mw, sprintf('%s = any(?)', $field));
                push(@mvals, ref $filters->{$_} ? $filters->{$_} : [ $filters->{$_} ]);
            }
            if (my $field = {
                protocol => 'protocol',
                valid => 'url.valid',
            }->{$_}) {
                push(@uw, sprintf('%s = any(?)', $field));
                push(@uvals, ref $filters->{$_} ? $filters->{$_} : [ $filters->{$_} ]);
            }
        }
    }
    my $list = $self->db->prepare(sprintf(
        $query,
        (@uw ? 'where ' . join(' and ', @uw) : ''),
        (@mw ? 'and '   . join(' and ', @mw) : ''),
    ));
    $list->execute(@uvals, @mvals);
    return $list->fetchall_arrayref({});
}

sub _find_urls {
    my ($self, $filters, $key) = @_;

    my $query = q{
        select toplevel_urls.* from toplevel_urls join
        hosts on hosts.hostname = toplevel_urls.hostname
    };
    my @vals;
    if (keys %{ $filters || {} }) {
        my @w;
        foreach (keys %$filters) {
            $filters->{$_} or next;
            my $field = {
                hostname => 'hosts.hostname',
                protocol => 'toplevel_urls.protocol',
            }->{$_} or next;

            push(@w, sprintf('%s = any(?)', $field));
            push(@vals, ref $filters->{$_} ? $filters->{$_} : [ $filters->{$_} ]);
        }
        $query .= ' where ' if (@w);
        $query .= join(' and ', @w);
    }
    my $list = $self->db->prepare($query);
    $list->execute(@vals);
    return $list->fetchall_arrayref({});
}

sub find_distributions {
    my ($self, $filters, $key) = @_;
    return [
        map { $_->{url} = $self->fmt_url($_); $_ }
        @{ $self->_find_distributions($filters) || []}]
}

sub _find_distributions {
    my ($self, $filters, $key) = @_;

    my $query = q{
    select *, toplevel_urls.path || distributions.relpath as path,
        coalesce(hosts.latitude, countries.latitude) as latitude,
        coalesce(hosts.longitude, countries.longitude) as longitude
    from hosts
    join toplevel_urls on toplevel_urls.hostname = hosts.hostname
    join distributions_validity on toplevel_urls.key = distributions_validity.urlskey
    join distributions on distributions.dkey = distributions_validity.distributionkey
    join countries on countries.code = hosts.country
    };

    my @vals;
    if (keys %{ $filters || {} }) {
        my @w;
        foreach (keys %$filters) {
            $filters->{$_} or next;
            my $field = {
                hostname => 'hosts.hostname',
                protocol => 'toplevel_urls.protocol',
                version  => 'distributions.version',
                arch     => 'distributions.arch',
                country  => 'hosts.country',
                exists   => 'distributions_validity.exists',
                public   => 'hosts.public',
            }->{$_} or next;

            push(@w, sprintf('%s = any(?)', $field));
            push(@vals, ref $filters->{$_} ? $filters->{$_} : [ $filters->{$_} ]);
        }
        $query .= ' where ' if (@w);
        $query .= join(' and ', @w);
    }
    my $list = $self->db->prepare($query);
    $list->execute(@vals);
    return $list->fetchall_arrayref({});
}

sub find_host_ip_overlap {
    my ($self, $hostname) = @_;

    my @addresses = $self->host_ips($hostname);

    my $list = $self->db->prepare(q{
        select * from ips where ip = any(?)
            and hostname != ?
    });
    $list->execute(\@addresses, $hostname);
    my $res = $list->fetchall_hashref('hostname');
    return keys %{ $res };
}

sub add_host_change_request {
    my ($self, $hostname, %info) = @_;

    my $reqid = join("", map { sprintf("%x",  int rand(256)) } (0..16));
    my $add = $self->db->prepare(sprintf(q{
            INSERT INTO hosts_ch_req(
            hostname, country, city, readonly, infourl, hostby, sponsorurl, 
            "comment", contact, syncfrom, public, bandwidth, latitude, longitude, 
            added, reqid)
            select *, '%s' from hosts where hostname = ?
            }, $reqid)
    );
    $add->execute( $hostname );
    my (@fields, @vals);
    while (my ($field, $val) = each(%info)) {
        push(@fields, $field);
        push(@vals,   $val || undef);
    }
    if (keys %info) {
        my $upd = $self->db->prepare(sprintf(q{
            update hosts_ch_req set %s where reqid = ?
        }, join(', ', map { "$_ = ?" } @fields)));
        $upd->execute(@vals, $reqid);
    }

    $self->db->commit;

    $reqid;
}

sub apply_change_request {
    my ($self, $reqid) = @_;

    my $findreq = $self->db->prepare(q{
        select * from hosts_ch_req where reqid = ?
        });
    $findreq->execute($reqid);
    my $res = $findreq->fetchrow_hashref;
    $findreq->finish;
    $res or return;

    my $hostname = $res->{hostname};
    delete $res->{hostname};
    delete $res->{reqid};

    $self->add_or_update_host($hostname, %{$res});
    my $del = $self->db->prepare(q{delete from hosts_ch_req where reqid = ?});
    $del->execute($reqid);
    $self->db->commit;
    $hostname
}

sub add_or_update_host {
    my ($self, $hostname, %info) = @_;

    my (@fields, @vals);
    while (my ($field, $val) = each(%info)) {
        push(@fields, $field);
        push(@vals, (defined($val) && $val ne '' ? $val : undef));
    }
    if (keys %info) {
        my $upd = $self->db->prepare(sprintf(q{
            update hosts set %s where hostname = ?
        }, join(', ', map { "$_ = ?" } @fields)));
        if ($upd->execute(@vals, $hostname) == 0) {
            my $add = $self->db->prepare(sprintf(q{
                insert into hosts (%s) values (%s)
                }, join(', ', (@fields, 'hostname')),
                join(',', ('?') x (scalar(@fields)+1))
            ));
            $add->execute(@vals, $hostname) or do {
                $self->db->rollback;
                return;
            };
        }
    }

    $self->update_host_ips($hostname);

    1;
}

sub add_or_update_url {
    my ($self, $uri) = @_;
    if (!ref $uri) {
        $uri = URI->new($uri);
    }

    my $update = $self->db->prepare(q{
        update toplevel_urls set path = ?, port = ?, valid = true
        where hostname = ? and protocol = ?
    });

    if ($update->execute(
            $uri->path, $uri->port == $uri->default_port ? undef : $uri->port,
            $uri->host, $uri->scheme
        ) == 0) {
        my $add = $self->db->prepare(q{
            insert into toplevel_urls (path, port, hostname, protocol)
            values (?,?,?,?)
            });
        $add->execute($uri->path, $uri->port == $uri->default_port ? undef : $uri->port,
            $uri->host, $uri->scheme) or do {
            $self->db->rollback;
            return;
        }
    }

    1;
}

sub update_host_ips {
    my ($self, $hostname) = @_;

    my @addresses = $self->host_ips($hostname);
    my $delete = $self->db->prepare(
        q{delete from ips where hostname = ?
            and ip != any(?)
        }
    );

    $delete->execute($hostname, [ @addresses ]);

    my $getip = $self->db->prepare(q{
        select 1 from ips where hostname = ? and ip = ?
    });
    my $addip = $self->db->prepare(q{
        insert into ips (hostname, ip) values (?,?)
    });
    foreach (@addresses) {
        if ($getip->execute($hostname, $_) == 0) {
            $addip->execute($hostname, $_);
        }
        $getip->finish;
    }
    
    1;
}

sub find_urls {
    my ($self, $filters, $key) = @_;
    return [
        map { $_->{url} = $self->fmt_url($_); $_ }
        @{ $self->_find_urls($filters) || []}]
}

sub fmt_url {
    my ($self, $dburl) = @_;

    $dburl->{path} =~ s:/+:/:g;
    my $uri = URI->new(
        sprintf('%s://%s%s',
            $dburl->{protocol},
            $dburl->{hostname},
            $dburl->{path} || '/',
        )        
    );
    $uri->port($dburl->{port});

    return $uri->as_string;
}

1;