Removed IPAddress methods only used by the blacklist.
This commit is contained in:
parent
05766f00e0
commit
f7fcd3230a
5 changed files with 1 additions and 98 deletions
|
@ -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;
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
namespace Misuzu;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use Misuzu\Net\IPAddress;
|
||||
use Misuzu\Users\User;
|
||||
use Misuzu\Users\UserNotFoundException;
|
||||
use Misuzu\Users\UserWarning;
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,2 +0,0 @@
|
|||
<?php
|
||||
echo "bruno";
|
Loading…
Reference in a new issue