From a99e7c54f40e5a5f8b9259a5ba86cfa5386fcbde Mon Sep 17 00:00:00 2001 From: Rhilip Date: Mon, 27 Apr 2020 14:13:53 +0800 Subject: [PATCH] feat(IpUtils): Rename to Ip and add isPublicIp function --- CHANGELOG.md | 11 +-- application/Controllers/TrackerController.php | 20 ++--- application/Middleware/IpBanMiddleware.php | 4 +- framework/Redis/BaseRedisConnection.php | 2 +- framework/Utils/Ip.php | 74 +++++++++++++++++++ framework/Utils/IpUtils.php | 61 --------------- 6 files changed, 93 insertions(+), 79 deletions(-) create mode 100644 framework/Utils/Ip.php delete mode 100644 framework/Utils/IpUtils.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 7c8388c..7c5ba40 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,8 @@ ## [Unreleased] ### Build -- **Composer:** remove unnecessary ext and some fix (e8b9adc) +- **Composer:** remove unnecessary ext and some fix (0729389) +- **Composer:** remove unnecessary ext and some fix (1235d86) - **PHP:** bump PHP major version to min 7.4 (d5b1ff1) ### Chore @@ -16,12 +17,12 @@ ### Feat - **Cache:** Allow ttl key for Redis Hash type (85a9587) -- **Request:** Move some Component prop to Request attributes (497bc38) +- **Request:** Move some Component prop to Request attributes (2f22b89) - **Requests:** Make Response Component extends from `Symfony\Component\HttpFoundation\Response` (dd001d2) - **Requests:** Make Request Component extends from `Symfony\Component\HttpFoundation\Request` (9cd715b) - **Torrent:** Per-add torrent edit (f06b342) - **Torrents:** Allow search in user's favour (or you can say bookmark) (c0aa627) -- **Validator:** Add magic function __get() as alias of getInput() (86e69f7) +- **Validator:** Add magic function __get() as alias of getInput() (3c23927) - **composer:** Add `nesbot/carbon` to deal with Date and Time (9e37c41) - **i18n:** Use symfony/translation and JSON format for locale (02cc251) - **layout:** Add anti-robots html meta tag (9c21e73) @@ -29,7 +30,7 @@ ### Fix - **API:** ApiMiddleware not work after change Response Component (6da82a7) - **Bencode:** Fix dict keys may not in sorted order (81f0783) -- **Composer:** Fix wrong composer.json format (3f17e03) +- **Composer:** Fix wrong composer.json format (2c4459c) - **Config:** Fix JSON type config return False (1129008) - **Cron:** Fix components lost in CronTabProcess (1ced4bf) - **Redis:** Fix wrong type of Redis call function hMset() to hMSet() (a163150) @@ -57,7 +58,7 @@ ### Style - **Bencode:** Separate Bencode library to `Rhilip/Bencode` (18cbfa1) -- **Request:** Move route as a part of attributes (aee4682) +- **Request:** Move route as a part of attributes (f63ed00) - **cs-fixer:** Update Composer config and php-cs-fixer whole project (e734812) - **gitignore:** Add .php_cs.cache to .gitignore (15a2a15) diff --git a/application/Controllers/TrackerController.php b/application/Controllers/TrackerController.php index dd0f3cb..e1b4a48 100644 --- a/application/Controllers/TrackerController.php +++ b/application/Controllers/TrackerController.php @@ -12,7 +12,7 @@ use App\Entity\User\UserRole; use App\Exceptions\TrackerException; -use Rid\Utils\IpUtils; +use Rid\Utils\Ip; use Rhilip\Bencode\Bencode; use Symfony\Component\HttpFoundation\Request; @@ -588,13 +588,13 @@ private function checkAnnounceFields(&$queries = []) // Get user ipv6 and ipv6_port data and store it in $queries['ipv6'] and $queries['ipv6_port'] if ($queries['ipv6']) { - if ($client = IpUtils::isEndPoint($queries['ipv6'])) { + if ($client = Ip::isEndPoint($queries['ipv6'])) { $queries['ipv6'] = $client['ip']; $queries['ipv6_port'] = $client['port']; } // Ignore all un-Native IPv6 address ( starting with FD or FC ; reserved IPv6 ) and IPv4-mapped-IPv6 address - if (!IpUtils::isPublicIPv6($queries['ipv6']) || strpos($queries['ipv6'], '.') !== false) { + if (!Ip::isPublicIPv6($queries['ipv6']) || strpos($queries['ipv6'], '.') !== false) { $queries['ipv6'] = $queries['ipv6_port'] = ''; } } @@ -602,9 +602,9 @@ private function checkAnnounceFields(&$queries = []) // If we can't get valid IPv6 address from `&ipv6=` // fail back to `&ip=` then the IPv6 format remote_ip if (!$queries['ipv6']) { - if ($queries['ip'] && IpUtils::isValidIPv6($queries['ip'])) { + if ($queries['ip'] && Ip::isValidIPv6($queries['ip'])) { $queries['ipv6'] = $queries['ip']; - } elseif (IpUtils::isValidIPv6($remote_ip)) { + } elseif (Ip::isValidIPv6($remote_ip)) { $queries['ipv6'] = $remote_ip; } if ($queries['ipv6']) { @@ -613,24 +613,24 @@ private function checkAnnounceFields(&$queries = []) } // Clean $queries['ip'] field and then store ipv4 data in it to make sure this field is IPv4-Only - if ($queries['ip'] && !IpUtils::isValidIPv4($queries['ip'])) { + if ($queries['ip'] && !Ip::isValidIPv4($queries['ip'])) { $queries['ip'] = ''; } // handle param `&ipv4=` like `&ipv6=` if ($queries['ipv4']) { - if ($client = IpUtils::isEndPoint($queries['ipv4'])) { - if (IpUtils::isValidIPv4($client['ip'])) { + if ($client = Ip::isEndPoint($queries['ipv4'])) { + if (Ip::isValidIPv4($client['ip'])) { $queries['ip'] = $client['ip']; $queries['port'] = $client['port']; } - } elseif (IpUtils::isValidIPv4($queries['ipv4'])) { + } elseif (Ip::isValidIPv4($queries['ipv4'])) { $queries['ip'] = $queries['ipv4']; } } // Fail back to remote_ip which in IPv4-format - if (!IpUtils::isPublicIPv4($queries['ip']) && IpUtils::isValidIPv4($remote_ip)) { + if (!Ip::isPublicIPv4($queries['ip']) && Ip::isValidIPv4($remote_ip)) { $queries['ip'] = $remote_ip; } diff --git a/application/Middleware/IpBanMiddleware.php b/application/Middleware/IpBanMiddleware.php index 9c2d802..e7a67f1 100644 --- a/application/Middleware/IpBanMiddleware.php +++ b/application/Middleware/IpBanMiddleware.php @@ -8,7 +8,7 @@ namespace App\Middleware; -use Rid\Utils\IpUtils; +use Rid\Utils\Ip; class IpBanMiddleware { @@ -18,7 +18,7 @@ public function handle($callable, \Closure $next) $ip = app()->request->getClientIp(); $ip_ban_list = app()->site->getBanIpsList(); - if (count($ip_ban_list) > 0 && IpUtils::checkIp($ip, $ip_ban_list)) { + if (count($ip_ban_list) > 0 && Ip::checkIp($ip, $ip_ban_list)) { return app()->response->setStatusCode(403); } diff --git a/framework/Redis/BaseRedisConnection.php b/framework/Redis/BaseRedisConnection.php index 49ab5c8..9431cd7 100644 --- a/framework/Redis/BaseRedisConnection.php +++ b/framework/Redis/BaseRedisConnection.php @@ -148,7 +148,7 @@ * @method array bzPopMin(string|array $key1, string|array $key2, int $timeout) * @method array zPopMax(string $key, int $count = 1) * @method array zPopMin(string $key, int $count = 1) - * @method int|bool hSet(string $key, string $hashKey, string|int $value) + * @method int|bool hSet(string $key, string $hashKey, string|mixed $value) * @method bool hSetNx(string $key, string $hashKey, string $value) * @method string hGet(string $key, string $hashKey) * @method int|bool hLen(string $key) diff --git a/framework/Utils/Ip.php b/framework/Utils/Ip.php new file mode 100644 index 0000000..f5e93fc --- /dev/null +++ b/framework/Utils/Ip.php @@ -0,0 +1,74 @@ + $ip, "port" => $port]; + } + } + return false; + } + + public static function isValidIP($ip, $flags = null) + { + return filter_var($ip, FILTER_VALIDATE_IP, $flags); + } + + public static function isValidIPv4($ip) + { + return self::isValidIP($ip, FILTER_FLAG_IPV4); + } + + public static function isValidIPv6($ip) + { + return self::isValidIP($ip, FILTER_FLAG_IPV6); + } + + public static function isPublicIp($ip, $flags = null) + { + /** + * FILTER_FLAG_NO_PRIV_RANGE: + * - Fails validation for the following private IPv4 ranges: 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16. + * - Fails validation for the IPv6 addresses starting with FD or FC. + * FILTER_FLAG_NO_RES_RANGE: + * - Fails validation for the following reserved IPv4 ranges: 0.0.0.0/8, 169.254.0.0/16, 127.0.0.0/8 and 240.0.0.0/4. + * - Fails validation for the following reserved IPv6 ranges: ::1/128, ::/128, ::ffff:0:0/96 and fe80::/10. + */ + return self::isValidIP($ip, $flags | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE); + } + + public static function isPublicIPv4($ip) + { + return self::isPublicIp($ip, FILTER_FLAG_IPV4); + } + + public static function isPublicIPv6($ip) + { + return self::isPublicIp($ip, FILTER_FLAG_IPV6); + } +} diff --git a/framework/Utils/IpUtils.php b/framework/Utils/IpUtils.php deleted file mode 100644 index 4364baf..0000000 --- a/framework/Utils/IpUtils.php +++ /dev/null @@ -1,61 +0,0 @@ - $ip, "port" => $port]; - } - } - return false; - } - - public static function isValidIP($ip) - { - return filter_var($ip, FILTER_VALIDATE_IP); - } - - public static function isValidIPv4($ip) - { - return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4); - } - - public static function isValidIPv6($ip) - { - return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6); - } - - public static function isPublicIPv4($ip) - { - return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE); - } - - public static function isPublicIPv6($ip) - { - return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE); - } -}