Removed IPAddress methods only used by the blacklist.

This commit is contained in:
flash 2023-01-05 17:15:26 +00:00
parent 05766f00e0
commit f7fcd3230a
5 changed files with 1 additions and 98 deletions

View file

@ -3,7 +3,6 @@ namespace Misuzu;
use Misuzu\AuthToken;
use Misuzu\Config\CfgType;
use Misuzu\Net\IPAddress;
use Misuzu\Users\User;
use Misuzu\Users\UserNotFoundException;
use Misuzu\Users\UserAuthSession;

View file

@ -1,7 +1,6 @@
<?php
namespace Misuzu;
use Misuzu\Net\IPAddress;
use Misuzu\Users\User;
use Misuzu\Users\UserLoginAttempt;
use Misuzu\Users\UserSession;
@ -18,7 +17,6 @@ if(UserSession::hasCurrent()) {
$twofactor = !empty($_POST['twofactor']) && is_array($_POST['twofactor']) ? $_POST['twofactor'] : [];
$notices = [];
$ipAddress = IPAddress::remote();
$remainingAttempts = UserLoginAttempt::remaining();
try {

View file

@ -2,7 +2,6 @@
namespace Misuzu;
use InvalidArgumentException;
use Misuzu\Net\IPAddress;
use Misuzu\Users\User;
use Misuzu\Users\UserNotFoundException;
use Misuzu\Users\UserWarning;

View file

@ -1,24 +1,10 @@
<?php
namespace Misuzu\Net;
use Exception;
use InvalidArgumentException;
use GeoIp2\Exception\AddressNotFoundException;
final class IPAddress {
public const VERSION_UNKNOWN = 0;
public const VERSION_4 = 4;
public const VERSION_6 = 6;
private const SIZES = [
self::VERSION_4 => 4,
self::VERSION_6 => 16,
];
public const DEFAULT_V4 = '127.1';
public const DEFAULT_V6 = '::1';
public static function remote(string $fallback = self::DEFAULT_V6): string {
public static function remote(string $fallback = '::1'): string {
return $_SERVER['REMOTE_ADDR'] ?? $fallback;
}
@ -32,81 +18,4 @@ final class IPAddress {
return $fallback;
}
}
public static function rawWidth(int $version): int {
return isset(self::SIZES[$version]) ? self::SIZES[$version] : 0;
}
public static function detectStringVersion(string $address): int {
if(filter_var($address, FILTER_VALIDATE_IP) !== false) {
if(filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) !== false)
return self::VERSION_6;
if(filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false)
return self::VERSION_4;
}
return self::VERSION_UNKNOWN;
}
public static function detectRawVersion(string $address): int {
$addressLength = strlen($address);
foreach(self::SIZES as $version => $length) {
if($length === $addressLength)
return $version;
}
return self::VERSION_UNKNOWN;
}
public static function cidrToRaw(string $cidr): ?array {
if(strpos($cidr, '/') !== false) {
[$subnet, $mask] = explode('/', $cidr, 2);
} else {
$subnet = $cidr;
}
try {
$subnet = inet_pton($subnet);
} catch(Exception $ex) {
return null;
}
$mask = empty($mask) ? null : (int)$mask;
return compact('subnet', 'mask');
}
public static function matchCidr(string $address, string $cidr): bool {
$address = inet_pton($address);
$cidr = self::cidrToRaw($cidr);
return self::matchCidrRaw($address, $cidr['subnet'], $cidr['mask']);
}
public static function matchCidrRaw(string $address, string $subnet, ?int $mask = null): bool {
$version = self::detectRawVersion($subnet);
if($version === self::VERSION_UNKNOWN)
return false;
$bits = self::SIZES[$version] * 8;
if(empty($mask))
$mask = $bits;
if($mask < 1 || $mask > $bits || $version !== self::detectRawVersion($subnet))
return false;
for($i = 0; $i < ceil($mask / 8); $i++) {
$byteMask = (0xFF00 >> max(0, min(8, $mask - ($i * 8)))) & 0xFF;
$addressByte = ord($address[$i]) & $byteMask;
$subnetByte = ord($subnet[$i]) & $byteMask;
if($addressByte !== $subnetByte)
return false;
}
return true;
}
}

View file

@ -1,2 +0,0 @@
<?php
echo "bruno";