rc.update_urltables 2.24 KB
Newer Older
1
#!/usr/local/bin/php
Ad Schellevis's avatar
Ad Schellevis committed
2
<?php
3

Ad Schellevis's avatar
Ad Schellevis committed
4 5
require_once("config.inc");
require_once("util.inc");
6
require_once("pfsense-utils.inc");
Ad Schellevis's avatar
Ad Schellevis committed
7

8
if (!isset($config['aliases']['alias'])) {
9 10
    // No aliases
    exit;
Ad Schellevis's avatar
Ad Schellevis committed
11 12
}

13
// Gather list of urltable / geoip aliases
Ad Schellevis's avatar
Ad Schellevis committed
14 15
$todo = array();
foreach ($config['aliases']['alias'] as $alias) {
16
    if (preg_match('/urltable/i', $alias['type'])) {
17 18 19
        $todo[] = $alias;
    } elseif ($alias['type'] == 'geoip') {
        $todo[] = $alias;
20
    }
Ad Schellevis's avatar
Ad Schellevis committed
21 22 23
}

if (count($todo) > 0) {
24 25 26 27 28 29 30 31 32 33 34 35
    log_error("{$argv[0]}: Starting up.");

    if ($argv[1] != "now") {
        // Wait a little before updating.
        $wait = mt_rand(5, 60);
        log_error("{$argv[0]}: Sleeping for {$wait} seconds.");
        sleep($wait);
    }

    log_error("{$argv[0]}: Starting URL table alias updates");

    $filter_reload = false;
36 37
    foreach ($todo as $alias) {
        if (preg_match('/urltable/i', $alias['type'])) {
38
            log_error("{$argv[0]}: start update {$alias['name']} ");
39 40 41 42 43 44 45 46 47 48
            $r = process_alias_urltable($alias['name'], $alias['url'], $alias['updatefreq']);
            if ($r == 1) {
                if ($alias['type'] == "urltable") {
                    exec("/sbin/pfctl -t " . escapeshellarg($alias['name']) . " -T replace -f /var/db/aliastables/" . escapeshellarg($alias['name']) . ".txt 2>&1", $result);
                    log_error("{$argv[0]}: Updated {$alias['name']} content from {$alias['url']}: ". $result[count($result)-1]);
                } else {
                    $filter_reload = true;
                }
            } elseif ($r == -1) {
                log_error("{$argv[0]}: {$alias['name']} does not need updating.");
49
            } else {
50
                log_error("{$argv[0]}: ERROR: could not update {$alias['name']} content from {$alias['url']}");
51
            }
52
        } elseif ($alias['type'] == 'geoip') {
53 54
            download_alias_geoip($alias);
            // load alias content
55 56
            exec("/sbin/pfctl -t " . escapeshellarg($alias['name']) . " -T replace -f /var/db/aliastables/" . escapeshellarg($alias['name']) . ".txt 2>&1", $result);
            log_error("{$argv[0]}: Updated {$alias['name']} content from geoip database: ". $result[count($result)-1]);
57 58 59 60 61 62
        }
    }

    if ($filter_reload) {
        configd_run("filter reload");
    }
Ad Schellevis's avatar
Ad Schellevis committed
63
}