|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace SamuelMwangiW\Linode\Support; |
| 13 | + |
| 14 | +class IpUtils |
| 15 | +{ |
| 16 | + private static array $checkedIps = []; |
| 17 | + |
| 18 | + /** |
| 19 | + * This class should not be instantiated. |
| 20 | + */ |
| 21 | + private function __construct() |
| 22 | + { |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * Checks if an IPv4 or IPv6 address is contained in the list of given IPs or subnets. |
| 27 | + * |
| 28 | + * @param string|array $ips List of IPs or subnets (can be a string if only a single one) |
| 29 | + */ |
| 30 | + public static function checkIp(string $requestIp, string|array $ips): bool |
| 31 | + { |
| 32 | + if (!\is_array($ips)) { |
| 33 | + $ips = [$ips]; |
| 34 | + } |
| 35 | + |
| 36 | + $method = substr_count($requestIp, ':') > 1 ? 'checkIp6' : 'checkIp4'; |
| 37 | + |
| 38 | + foreach ($ips as $ip) { |
| 39 | + if (self::$method($requestIp, $ip)) { |
| 40 | + return true; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + return false; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Compares two IPv4 addresses. |
| 49 | + * In case a subnet is given, it checks if it contains the request IP. |
| 50 | + * |
| 51 | + * @param string $ip IPv4 address or subnet in CIDR notation |
| 52 | + * |
| 53 | + * @return bool Whether the request IP matches the IP, or whether the request IP is within the CIDR subnet |
| 54 | + */ |
| 55 | + public static function checkIp4(string $requestIp, string $ip): bool |
| 56 | + { |
| 57 | + $cacheKey = $requestIp.'-'.$ip; |
| 58 | + if (isset(self::$checkedIps[$cacheKey])) { |
| 59 | + return self::$checkedIps[$cacheKey]; |
| 60 | + } |
| 61 | + |
| 62 | + if (!filter_var($requestIp, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV4)) { |
| 63 | + return self::$checkedIps[$cacheKey] = false; |
| 64 | + } |
| 65 | + |
| 66 | + if (str_contains($ip, '/')) { |
| 67 | + [$address, $netmask] = explode('/', $ip, 2); |
| 68 | + |
| 69 | + if ('0' === $netmask) { |
| 70 | + return self::$checkedIps[$cacheKey] = filter_var($address, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV4); |
| 71 | + } |
| 72 | + |
| 73 | + if ($netmask < 0 || $netmask > 32) { |
| 74 | + return self::$checkedIps[$cacheKey] = false; |
| 75 | + } |
| 76 | + } else { |
| 77 | + $address = $ip; |
| 78 | + $netmask = 32; |
| 79 | + } |
| 80 | + |
| 81 | + if (false === ip2long($address)) { |
| 82 | + return self::$checkedIps[$cacheKey] = false; |
| 83 | + } |
| 84 | + |
| 85 | + return self::$checkedIps[$cacheKey] = 0 === substr_compare(sprintf('%032b', ip2long($requestIp)), sprintf('%032b', ip2long($address)), 0, $netmask); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Compares two IPv6 addresses. |
| 90 | + * In case a subnet is given, it checks if it contains the request IP. |
| 91 | + * |
| 92 | + * @author David Soria Parra <dsp at php dot net> |
| 93 | + * |
| 94 | + * @see https://github.com/dsp/v6tools |
| 95 | + * |
| 96 | + * @param string $ip IPv6 address or subnet in CIDR notation |
| 97 | + * |
| 98 | + * @throws \RuntimeException When IPV6 support is not enabled |
| 99 | + */ |
| 100 | + public static function checkIp6(string $requestIp, string $ip): bool |
| 101 | + { |
| 102 | + $cacheKey = $requestIp.'-'.$ip; |
| 103 | + if (isset(self::$checkedIps[$cacheKey])) { |
| 104 | + return self::$checkedIps[$cacheKey]; |
| 105 | + } |
| 106 | + |
| 107 | + if (!((\extension_loaded('sockets') && \defined('AF_INET6')) || @inet_pton('::1'))) { |
| 108 | + throw new \RuntimeException('Unable to check Ipv6. Check that PHP was not compiled with option "disable-ipv6".'); |
| 109 | + } |
| 110 | + |
| 111 | + if (str_contains($ip, '/')) { |
| 112 | + [$address, $netmask] = explode('/', $ip, 2); |
| 113 | + |
| 114 | + if ('0' === $netmask) { |
| 115 | + return (bool) unpack('n*', @inet_pton($address)); |
| 116 | + } |
| 117 | + |
| 118 | + if ($netmask < 1 || $netmask > 128) { |
| 119 | + return self::$checkedIps[$cacheKey] = false; |
| 120 | + } |
| 121 | + } else { |
| 122 | + $address = $ip; |
| 123 | + $netmask = 128; |
| 124 | + } |
| 125 | + |
| 126 | + $bytesAddr = unpack('n*', @inet_pton($address)); |
| 127 | + $bytesTest = unpack('n*', @inet_pton($requestIp)); |
| 128 | + |
| 129 | + if (!$bytesAddr || !$bytesTest) { |
| 130 | + return self::$checkedIps[$cacheKey] = false; |
| 131 | + } |
| 132 | + |
| 133 | + for ($i = 1, $ceil = ceil($netmask / 16); $i <= $ceil; ++$i) { |
| 134 | + $left = $netmask - 16 * ($i - 1); |
| 135 | + $left = ($left <= 16) ? $left : 16; |
| 136 | + $mask = ~(0xFFFF >> $left) & 0xFFFF; |
| 137 | + if (($bytesAddr[$i] & $mask) != ($bytesTest[$i] & $mask)) { |
| 138 | + return self::$checkedIps[$cacheKey] = false; |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + return self::$checkedIps[$cacheKey] = true; |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Anonymizes an IP/IPv6. |
| 147 | + * |
| 148 | + * Removes the last byte for v4 and the last 8 bytes for v6 IPs |
| 149 | + */ |
| 150 | + public static function anonymize(string $ip): string |
| 151 | + { |
| 152 | + $wrappedIPv6 = false; |
| 153 | + if (str_starts_with($ip, '[') && str_ends_with($ip, ']')) { |
| 154 | + $wrappedIPv6 = true; |
| 155 | + $ip = substr($ip, 1, -1); |
| 156 | + } |
| 157 | + |
| 158 | + $packedAddress = inet_pton($ip); |
| 159 | + if (4 === \strlen($packedAddress)) { |
| 160 | + $mask = '255.255.255.0'; |
| 161 | + } elseif ($ip === inet_ntop($packedAddress & inet_pton('::ffff:ffff:ffff'))) { |
| 162 | + $mask = '::ffff:ffff:ff00'; |
| 163 | + } elseif ($ip === inet_ntop($packedAddress & inet_pton('::ffff:ffff'))) { |
| 164 | + $mask = '::ffff:ff00'; |
| 165 | + } else { |
| 166 | + $mask = 'ffff:ffff:ffff:ffff:0000:0000:0000:0000'; |
| 167 | + } |
| 168 | + $ip = inet_ntop($packedAddress & inet_pton($mask)); |
| 169 | + |
| 170 | + if ($wrappedIPv6) { |
| 171 | + $ip = '['.$ip.']'; |
| 172 | + } |
| 173 | + |
| 174 | + return $ip; |
| 175 | + } |
| 176 | +} |
0 commit comments