diff --git a/cspell.json b/cspell.json index 471a6abe0..84df80d34 100644 --- a/cspell.json +++ b/cspell.json @@ -37,7 +37,8 @@ "jhurliman", "jambroseclarke", "Egulias", - "ICANN" + "ICANN", + "failovers" ], "ignoreWords": [ diff --git a/src/di.php b/src/di.php index d9d3e7b82..849a1629e 100644 --- a/src/di.php +++ b/src/di.php @@ -711,7 +711,7 @@ $di['license_server'] = function () use ($di) { * * @return \FOSSBilling\GeoIP\Reader */ -$di['geoip'] = fn (): FOSSBilling\GeoIP\Reader => new FOSSBilling\GeoIP\Reader(PATH_LIBRARY . '/FOSSBilling/GeoIP/Databases/CC0-Country.mmdb'); +$di['geoip'] = fn (): FOSSBilling\GeoIP\Reader => new FOSSBilling\GeoIP\Reader(); /* * @param void diff --git a/src/library/FOSSBilling/Fingerprint.php b/src/library/FOSSBilling/Fingerprint.php index f08bb0a84..78131c466 100644 --- a/src/library/FOSSBilling/Fingerprint.php +++ b/src/library/FOSSBilling/Fingerprint.php @@ -76,10 +76,14 @@ class Fingerprint 'source' => $_SERVER['HTTP_SEC_CH_UA_MOBILE'] ?? '', 'weight' => 2, ], - 'cloudFlareCountry' => [ - 'source' => $_SERVER['HTTP_CF_IPCOUNTRY'] ?? '', // "IP Geolocation" must be enabled under Cloudflare's "network" settings + 'geoIpCountry' => [ + 'source' => $this->getIpCountry() ?? '', 'weight' => 4, ], + 'asn' => [ + 'source' => $this->getIpAsn() ?? '', + 'weight' => 1, // Things like Cloudflare, VPNs, or failovers might cause the ASN to change. Otherwise this would be a really strong indicator. + ] ]; } @@ -140,7 +144,7 @@ class Fingerprint * Here we calculate how confident we are in our ability to fingerprint a device without causing issues for legitimate sessions. * The less confident we are, the more wrong the current fingerprint needs to be when compared against the session's before it is invalidated. * - * In the event that less that 70% of the possible values are in the fingerprint, we use the percentage off we are to calculate a higher percentage before failure. + * In the event that less that 70% of the possible values are in the fingerprint, we calculate a higher failure point for fingerprints due to reduced confidence. * For example: * If we have 13 possible fingerprint properties and only 9 are available, that's only 69% of the possible properties and the failure threshold will be calculated as follows. * Negative weight: (1 - 0.69) / 1.25 = `0.24` @@ -222,4 +226,31 @@ class Fingerprint 'userAgent' => $userAgent, ]; } + + private function getIpCountry() + { + // Use the CF header if it is set. + if (isset($_SERVER['HTTP_CF_IPCOUNTRY'])) { + return $_SERVER['HTTP_CF_IPCOUNTRY']; + } + + // Otherwise, instance the system's GeoIP reader and read the country from there. + try { + $reader = new GeoIP\Reader(); + return $reader->country($_SERVER['REMOTE_ADDR'])->name; + } catch (\Exception) { + return ''; + } + } + + private function getIpAsn() + { + try { + $asnDb = GeoIP\Reader::getAsnDatabase(); + $reader = new GeoIP\Reader($asnDb); + return $reader->asn($_SERVER['REMOTE_ADDR'])->asnNumber; + } catch (\Exception) { + return ''; + } + } } diff --git a/src/library/FOSSBilling/GeoIP/ASN.php b/src/library/FOSSBilling/GeoIP/ASN.php index 6b6aaf7df..9805aaf5f 100644 --- a/src/library/FOSSBilling/GeoIP/ASN.php +++ b/src/library/FOSSBilling/GeoIP/ASN.php @@ -14,8 +14,8 @@ namespace FOSSBilling\GeoIP; class ASN implements \JsonSerializable { - private readonly int|float $asnNumber; - private readonly string $asnOrg; + public readonly int|float $asnNumber; + public readonly string $asnOrg; public function __construct(array $asnRecord) { diff --git a/src/library/FOSSBilling/GeoIP/Reader.php b/src/library/FOSSBilling/GeoIP/Reader.php index b437f90b3..0ad14e1b6 100644 --- a/src/library/FOSSBilling/GeoIP/Reader.php +++ b/src/library/FOSSBilling/GeoIP/Reader.php @@ -23,17 +23,35 @@ class Reader private readonly MaxMindReader $reader; private readonly LanguageAlpha2 $language; + /** + * Returns the path to the system's `country` database + */ + public static function getCountryDatabase(): string + { + return PATH_LIBRARY . '/FOSSBilling/GeoIP/Databases/CC0-Country.mmdb'; + } + + /** + * Returns the path to the system's `asn` database + */ + public static function getAsnDatabase(): string + { + return PATH_LIBRARY . '/FOSSBilling/GeoIP/Databases/PDDL-ASN.mmdb'; + } + /** * Constructs a new GeoIP reader instance. * - * @param string $database a path to the database to load - * @param string $locale (Optional) the locale to use for country names. Defaults to the locale being used by FOSSBilling. + * @param string|null $database (Optional) a path to the database to load> Will default to the system's country database. + * @param string|null $locale (Optional) the locale to use for country names. Defaults to the locale being used by FOSSBilling. * * @throws \InvalidArgumentException for invalid database path or unknown arguments * @throws InvalidDatabaseException if the database is invalid or there is an error reading from it */ - public function __construct(string $database, ?string $locale = null) + public function __construct(?string $database = null, ?string $locale = null) { + $database ??= self::getCountryDatabase(); + $this->reader = new MaxMindReader($database); if ($locale === null) { diff --git a/src/modules/Security/Service.php b/src/modules/Security/Service.php index 7bd714211..e2fc92256 100644 --- a/src/modules/Security/Service.php +++ b/src/modules/Security/Service.php @@ -14,6 +14,7 @@ namespace Box\Mod\Security; use FOSSBilling\GeoIP\IncompleteRecord; use FOSSBilling\InformationException; use FOSSBilling\Interfaces\SecurityCheckInterface; +use FOSSBilling\GeoIP\Reader; class Service { @@ -135,7 +136,8 @@ class Service } try { - $asnReader = new \FOSSBilling\GeoIP\Reader(PATH_LIBRARY . '/FOSSBilling/GeoIP/Databases/PDDL-ASN.mmdb'); + $asnPath = Reader::getAsnDatabase(); + $asnReader = new Reader($asnPath); $asnInfo = $asnReader->asn($ip); } catch (IncompleteRecord) { $asnInfo = [];