From a311de6acf87490c48c59b9c58027d7a1e43973d Mon Sep 17 00:00:00 2001 From: flashwave Date: Thu, 5 Jan 2023 20:03:26 +0000 Subject: [PATCH] Updates to GeoIP lookup stuff. --- misuzu.php | 3 +- src/GeoIP/GeoIPHelper.php | 77 +++++++++++++++++++++++++++++++++++++++ src/MszContext.php | 8 ++++ src/Net/GeoIP.php | 28 -------------- src/Net/IPAddress.php | 20 +++++----- 5 files changed, 97 insertions(+), 39 deletions(-) create mode 100644 src/GeoIP/GeoIPHelper.php delete mode 100644 src/Net/GeoIP.php diff --git a/misuzu.php b/misuzu.php index 6e29b89..6367ff1 100644 --- a/misuzu.php +++ b/misuzu.php @@ -7,7 +7,6 @@ use Index\Data\ConnectionFailedException; use Index\Data\DbTools; use Misuzu\Config\CfgType; use Misuzu\Config\DbConfig; -use Misuzu\Net\GeoIP; use Misuzu\Net\IPAddress; use Misuzu\Users\User; use Misuzu\Users\UserNotFoundException; @@ -139,7 +138,7 @@ if(!is_readable(MSZ_STORAGE) || !is_writable(MSZ_STORAGE)) { exit; } -GeoIP::init($cfg->getValue('geoip.database', CfgType::T_STR, '/var/lib/GeoIP/GeoLite2-Country.mmdb')); +IPAddress::init($ctx); if(!MSZ_DEBUG) { $twigCacheDirSfx = GitInfo::hash(true); diff --git a/src/GeoIP/GeoIPHelper.php b/src/GeoIP/GeoIPHelper.php new file mode 100644 index 0000000..8aecef6 --- /dev/null +++ b/src/GeoIP/GeoIPHelper.php @@ -0,0 +1,77 @@ +config = $config; + } + + public function hasDbPath(string $type): bool { + return array_key_exists($type, $this->readers) + || $this->config->hasValue('db.' . $type); + } + + public function getDbPath(string $type): string { + $type = 'db.' . $type; + if(!$this->config->hasValue($type)) + throw new RuntimeException('Not database path has been configured for $type.'); + return $this->config->getValue($type, CfgType::T_STR); + } + + public function getReader(string $type): Reader { + if(!array_key_exists($type, $this->readers)) + return $this->readers[$type] = new Reader($this->getDbPath($type)); + return $this->readers[$type]; + } + + public function getASN(IPAddress|string $ipAddress): Asn { + if($ipAddress instanceof IPAddress) + $ipAddress = $ipAddress->getCleanAddress(); + + if($this->hasDbPath(self::ASN)) + return $this->getReader(self::ASN)->asn($ipAddress); + + throw new RuntimeException('There was no database available that could satisfy this request.'); + } + + public function getCityOrCountry(IPAddress|string $ipAddress): City|Country { + if($ipAddress instanceof IPAddress) + $ipAddress = $ipAddress->getCleanAddress(); + + if($this->hasDbPath(self::CITY)) + return $this->getReader(self::CITY)->city($ipAddress); + if($this->hasDbPath(self::COUNTRY)) + return $this->getReader(self::COUNTRY)->country($ipAddress); + + throw new RuntimeException('There was no database available that could satisfy this request.'); + } + + public function getIsoCountryCode(IPAddress|string $ipAddress, string $fallback = 'XX'): string { + try { + return $this->getCityOrCountry($ipAddress)->country->isoCode ?? $fallback; + } catch(AddressNotFoundException $ex) { + return $fallback; + } catch(RuntimeException $ex) { + return $fallback; + } + } +} diff --git a/src/MszContext.php b/src/MszContext.php index 2210031..c69c00f 100644 --- a/src/MszContext.php +++ b/src/MszContext.php @@ -3,6 +3,7 @@ namespace Misuzu; use Misuzu\Template; use Misuzu\Config\IConfig; +use Misuzu\GeoIP\GeoIPHelper; use Misuzu\SharpChat\SharpChatRoutes; use Misuzu\Users\Users; use Misuzu\Twitter\TwitterClient; @@ -20,6 +21,7 @@ class MszContext { private IConfig $config; private Users $users; private HttpFx $router; + private ?GeoIPHelper $geoIP = null; public function __construct(IDbConnection $dbConn, IConfig $config) { $this->dbConn = $dbConn; @@ -44,6 +46,12 @@ class MszContext { return $this->users; }*/ + public function getGeoIP(): GeoIPHelper { + if($this->geoIP === null) + return $this->geoIP = new GeoIPHelper($this->config->scopeTo('geoip')); + return $this->geoIP; + } + public function createTwitterClient(): TwitterClient { return TwitterClient::create($this->config->scopeTo('twitter')); } diff --git a/src/Net/GeoIP.php b/src/Net/GeoIP.php deleted file mode 100644 index 8df751a..0000000 --- a/src/Net/GeoIP.php +++ /dev/null @@ -1,28 +0,0 @@ -country($ipAddress); - } -} diff --git a/src/Net/IPAddress.php b/src/Net/IPAddress.php index 848f705..d58862d 100644 --- a/src/Net/IPAddress.php +++ b/src/Net/IPAddress.php @@ -1,17 +1,19 @@ country->isoCode ?? $fallback; - } catch(AddressNotFoundException $e) { - return $fallback; - } + public static function init(MszContext $ctx): void { + self::$context = $ctx; + } + + public static function country(string $address, string $fallback = 'XX'): string { + return self::$context->getGeoIP()->getIsoCountryCode($address, $fallback); } }