Skip to content

Commit

Permalink
Remove enum usage in ip2asn related stuff
Browse files Browse the repository at this point in the history
There just isn't enough benefit of using it and using WeakMap for this
seems a bit overkill.
  • Loading branch information
nanaya committed Jan 14, 2025
1 parent 69d48c8 commit 8f4f6f4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 38 deletions.
14 changes: 0 additions & 14 deletions app/Enums/Ip.php

This file was deleted.

18 changes: 9 additions & 9 deletions app/Libraries/Ip2AsnUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,26 @@

namespace App\Libraries;

use App\Enums\Ip;
use App\Singletons\Ip2Asn;
use Log;

class Ip2AsnUpdater
{
public static function getDbPath(Ip $version): string
public static function getDbPath(string $version): string
{
return database_path("ip2asn/{$version->value}.tsv");
return database_path("ip2asn/{$version}.tsv");
}

public static function getIndexPath(Ip $version): string
public static function getIndexPath(string $version): string
{
return database_path("ip2asn/{$version->value}.idx");
return database_path("ip2asn/{$version}.idx");
}

public function run(?callable $logger = null): void
{
foreach (Ip::cases() as $version) {
foreach (Ip2Asn::IP as $version) {
$prefixedLogger = function (string $message) use ($logger, $version): void {
$prefixedMessage = "[{$version->value}] $message";
$prefixedMessage = "[{$version}] $message";

if (isset($logger)) {
$logger($prefixedMessage);
Expand All @@ -39,7 +39,7 @@ public function run(?callable $logger = null): void
}
}

private function update(Ip $version, callable $logger): void
private function update(string $version, callable $logger): void
{
$logger('Checking db for updates');

Expand All @@ -60,7 +60,7 @@ private function update(Ip $version, callable $logger): void

if ($newDb) {
$logger('Db file is outdated. Downloading');
$tsv = gzdecode(file_get_contents("https://iptoasn.com/data/ip2asn-{$version->value}.tsv.gz"));
$tsv = gzdecode(file_get_contents("https://iptoasn.com/data/ip2asn-{$version}.tsv.gz"));
} else {
$tsv = file_get_contents($dbPath);
}
Expand Down
28 changes: 13 additions & 15 deletions app/Singletons/Ip2Asn.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,26 @@

namespace App\Singletons;

use App\Enums\Ip;
use App\Libraries\Ip2AsnUpdater;
use Exception;
use WeakMap;

class Ip2Asn
{
private WeakMap $count;
private WeakMap $dbFh;
private WeakMap $index;
const IP = [
'v4' => 'v4',
'v6' => 'v6',
];

private array $count;
private array $dbFh;
private array $index;

public function __construct()
{
$this->count = new WeakMap();
$this->dbFh = new WeakMap();
$this->index = new WeakMap();

foreach (Ip::cases() as $version) {
foreach (static::IP as $version) {
$this->dbFh[$version] = fopen(Ip2AsnUpdater::getDbPath($version), 'r');
$index = file_get_contents(Ip2AsnUpdater::getIndexPath($version));
if ($this->dbFh[$version] === false || $index === false) {
throw new Exception("failed opening ip2asn {$version} database or index");
throw new \Exception("failed opening ip2asn {$version} database or index");
}
$this->index[$version] = $index;

Expand All @@ -41,15 +39,15 @@ public function lookup(string $ip): string
{
switch (true) {
case filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false:
return $this->lookupByVersion(Ip::V4, $ip);
return $this->lookupByVersion(static::IP['v4'], $ip);
case filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) !== false:
return $this->lookupByVersion(Ip::V6, $ip);
return $this->lookupByVersion(static::IP['v6'], $ip);
}

return '0';
}

private function lookupByVersion(Ip $version, string $ip): string
private function lookupByVersion(string $version, string $ip): string
{
$dbFh = $this->dbFh[$version];
$index = $this->index[$version];
Expand Down

0 comments on commit 8f4f6f4

Please sign in to comment.