Skip to content
This repository has been archived by the owner on Nov 2, 2020. It is now read-only.

Commit

Permalink
feat(IpUtils): Rename to Ip and add isPublicIp function
Browse files Browse the repository at this point in the history
  • Loading branch information
Rhilip committed Apr 27, 2020
1 parent 511e79f commit a99e7c5
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 79 deletions.
11 changes: 6 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -16,20 +17,20 @@

### 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)

### 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)
Expand Down Expand Up @@ -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)

Expand Down
20 changes: 10 additions & 10 deletions application/Controllers/TrackerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -588,23 +588,23 @@ 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'] = '';
}
}

// If we can't get valid IPv6 address from `&ipv6=`
// fail back to `&ip=<IPv6>` 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']) {
Expand All @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions application/Middleware/IpBanMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace App\Middleware;

use Rid\Utils\IpUtils;
use Rid\Utils\Ip;

class IpBanMiddleware
{
Expand All @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion framework/Redis/BaseRedisConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
74 changes: 74 additions & 0 deletions framework/Utils/Ip.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
/**
* Created by PhpStorm.
* User: Rhilip
* Date: 2018/11/29
* Time: 10:37
*/

namespace Rid\Utils;

use Symfony\Component\HttpFoundation\IpUtils as HttpFoundationIpUtils;

/**
* Http utility functions.
*/
class Ip extends HttpFoundationIpUtils
{

/**
* @param $raw_ip
* @return array|bool
*/
public static function isEndPoint($raw_ip)
{
preg_match("/\[?([^\]]+)\]?:([0-9]+)/", $raw_ip, $data);

if ($data) {
$ip = self::isPublicIp($data[1]);
$port = $data[2];
if ($ip && $port) {
return ["ip" => $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);
}
}
61 changes: 0 additions & 61 deletions framework/Utils/IpUtils.php

This file was deleted.

0 comments on commit a99e7c5

Please sign in to comment.