Automatically update the IP address DBs (#2680)

* Periodically update the IP address databases

* Ignore false positive PHPStan error in `Config.php`

Co-Authored-By: Adam Daley <admdly@users.noreply.github.com>

* Limit 1 DB update per execution

This way these are hogging less time from the cron job & it slightly spreads out the load on Github's release servers.

---------

Co-authored-by: Adam Daley <admdly@users.noreply.github.com>
This commit is contained in:
Belle AerniandAdam Daley authored and GitHub committed 2025-04-24 01:20:00 -07:00
1 parent df333e32b9
commit 6478746dc6
5 files changed
+79

No files matched your search

+1
View File
@@ -22,6 +22,7 @@ class Config
public static function getConfig(): array
{
// @phpstan-ignore include.fileNotFound (False positive)
return include PATH_CONFIG;
}
+76
View File
@@ -17,6 +17,13 @@ use FOSSBilling\StandardsHelper;
use MaxMind\Db\Reader as MaxMindReader;
use MaxMind\Db\Reader\InvalidDatabaseException;
use PrinsFrank\Standards\Language\LanguageAlpha2;
use Symfony\Component\Filesystem\Exception\IOException;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
class Reader
{
@@ -39,6 +46,34 @@ class Reader
return PATH_LIBRARY . '/FOSSBilling/GeoIP/Databases/PDDL-ASN.mmdb';
}
/**
* Handles updating the built-in, default databases.
* The databases will only be updated if the files do not exist, or if they are over 7 days old.
* This will only update 1 database per call as it's intended to be run in the background and have the work spread out VS all at once.
*
* @throws IOException
*/
public static function updateDefaultDatabases(): bool
{
$databases = [
self::getCountryDatabase() => 'https://github.com/HostByBelle/IP-Geolocation-DB/releases/latest/download/cc0-both-country.mmdb',
self::getAsnDatabase() => 'https://github.com/HostByBelle/IP-Geolocation-DB/releases/latest/download/pddl-asn-both.mmdb',
];
foreach ($databases as $path => $url) {
if (self::shouldUpdate($path)) {
try {
self::downloadDb($path, $url);
return true;
} catch (\Exception $e) {
error_log('There was an error while updating the IP address database: ' . $e->getMessage());
}
}
}
return false;
}
/**
* Constructs a new GeoIP reader instance.
*
@@ -111,4 +146,45 @@ class Reader
return new ASN($record);
}
/**
* Checks if a database should be updated.
*
* @param string $path The path to the database
* @param int $maxAge The maximum age in seconds. The default is 7 days.
*
* @throws IOException
*/
private static function shouldUpdate(string $path, int $maxAge = 604800): bool
{
$fs = new Filesystem();
if (!$fs->exists($path)) {
return true;
}
$dbAge = time() - filemtime($path);
return $dbAge >= $maxAge;
}
/**
* Downloads a database file and saves it to the provided location.
*
* @return void
*
* @throws TransportExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface
* @throws ServerExceptionInterface
*/
private static function downloadDb(string $path, string $url)
{
$httpClient = HttpClient::create();
$response = $httpClient->request('GET', $url);
if ($response->getStatusCode() === 200) {
file_put_contents($path, $response->getContent());
} else {
throw new \Exception('Got a ' . $response->getStatusCode() . ' status code when attempting to download ' . $url);
}
}
}
+2
View File
@@ -13,6 +13,7 @@ namespace Box\Mod\System;
use FOSSBilling\Config;
use FOSSBilling\Environment;
use FOSSBilling\GeoIP\Reader;
use FOSSBilling\SentryHelper;
use FOSSBilling\Version;
use Pimple\Container;
@@ -1004,6 +1005,7 @@ class Service
public static function onBeforeAdminCronRun(\Box_Event $event)
{
$di = $event->getDi();
Reader::updateDefaultDatabases();
try {
// Prune the classmap to remove classes which are no longer on the disk or that have moved.