diff --git a/composer.json b/composer.json index a5c8ae9..6df43fe 100644 --- a/composer.json +++ b/composer.json @@ -1,13 +1,14 @@ { "authors": [ { "name": "Simon Walker", "email": "github@stwalkerster.co.uk" } ], "require": { "ext-json": "*", "ext-curl": "*", - "twbs/bootstrap": "v4.3.1" + "twbs/bootstrap": "v4.3.1", + "mlocati/ip-lib": "1.*" } } diff --git a/composer.lock b/composer.lock index a0ccd67..2301beb 100644 --- a/composer.lock +++ b/composer.lock @@ -1,72 +1,128 @@ { "_readme": [ "This file locks the dependencies of your project to a known state", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "content-hash": "42a04f416d1facd1a841e790f1a71f2b", + "content-hash": "014c4600270f10a859034b247a73f07f", "packages": [ + { + "name": "mlocati/ip-lib", + "version": "1.9.0", + "source": { + "type": "git", + "url": "https://github.com/mlocati/ip-lib.git", + "reference": "b844659e3b87a461d1a8fe8e3e374aa6c4a5d902" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/mlocati/ip-lib/zipball/b844659e3b87a461d1a8fe8e3e374aa6c4a5d902", + "reference": "b844659e3b87a461d1a8fe8e3e374aa6c4a5d902", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "require-dev": { + "ext-pdo_sqlite": "*", + "phpunit/dbunit": "^1.4 || ^2 || ^3 || ^4", + "phpunit/phpunit": "^4.8 || ^5.7 || ^6.5" + }, + "type": "library", + "autoload": { + "psr-4": { + "IPLib\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Michele Locati", + "email": "mlocati@gmail.com", + "homepage": "https://github.com/mlocati", + "role": "Author" + } + ], + "description": "Handle IPv4, IPv6 addresses and ranges", + "homepage": "https://github.com/mlocati/ip-lib", + "keywords": [ + "IP", + "address", + "addresses", + "ipv4", + "ipv6", + "matching", + "network", + "networking", + "range", + "subnet" + ], + "time": "2019-09-20T08:26:10+00:00" + }, { "name": "twbs/bootstrap", "version": "v4.3.1", "source": { "type": "git", "url": "https://github.com/twbs/bootstrap.git", "reference": "8fa0d3010112dca5dd6dd501173415856001ba8b" }, "dist": { "type": "zip", "url": "https://api.github.com/repos/twbs/bootstrap/zipball/8fa0d3010112dca5dd6dd501173415856001ba8b", "reference": "8fa0d3010112dca5dd6dd501173415856001ba8b", "shasum": "" }, "replace": { "twitter/bootstrap": "self.version" }, "type": "library", "extra": { "branch-alias": { "dev-master": "3.3.x-dev" } }, "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], "authors": [ { "name": "Jacob Thornton", "email": "jacobthornton@gmail.com" }, { "name": "Mark Otto", "email": "markdotto@gmail.com" } ], "description": "The most popular front-end framework for developing responsive, mobile first projects on the web.", "homepage": "https://getbootstrap.com/", "keywords": [ "JS", "css", "framework", "front-end", "mobile-first", "responsive", "sass", "web" ], "time": "2019-02-13T16:01:40+00:00" } ], "packages-dev": [], "aliases": [], "minimum-stability": "stable", "stability-flags": [], "prefer-stable": false, "prefer-lowest": false, "platform": { "ext-json": "*", "ext-curl": "*" }, "platform-dev": [] } diff --git a/index.php b/index.php index c6708ad..ac03964 100644 --- a/index.php +++ b/index.php @@ -1,152 +1,190 @@ - enwiki IPv4 rangeblock finder + Rangeblock finder -
-

enwiki IPv4 Rangeblock finder

-
-
- /> - -
-
+
+

Rangeblock finder

+
+
+ /> + +
+
"; + die(); } $cookieJar = tempnam("/tmp", "CURLCOOKIE"); $curlOpt = array( CURLOPT_COOKIEFILE => $cookieJar, CURLOPT_COOKIEJAR => $cookieJar, CURLOPT_RETURNTRANSFER => 1, CURLOPT_USERAGENT => 'RangeblockFinder/0.1 (+mailto:wikimedia@stwalkerster.co.uk)', ); const API_ENWIKI = 'https://en.wikipedia.org/w/api.php'; const API_METAWIKI = 'https://meta.wikimedia.org/w/api.php'; function apiQuery($base, array $params, array $substitutions, $post = false) { global $curlOpt; $usableParams = []; foreach ($params as $k => $v) { $val = $v; foreach ($substitutions as $kid => $repl) { $val = str_replace('{' . $kid . '}', $repl, $val); } $usableParams[$k] = $val; } $usableParams['format'] = 'json'; $queryString = http_build_query($usableParams); $url = $base; if (!$post) { $url .= '?' . $queryString; } $ch = curl_init(); curl_setopt_array($ch, $curlOpt); curl_setopt($ch, CURLOPT_URL, $url); if ($post) { curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $queryString); } $data = curl_exec($ch); if (curl_errno($ch)) { die('cURL Error: ' . curl_error($ch)); } return json_decode($data); } function countBlocks($target) { $enwikiGeneralQuery = [ 'action' => 'query', 'list' => 'logevents', 'letitle' => 'User:{0}', 'letype' => 'block', ]; $data = apiQuery(API_ENWIKI, $enwikiGeneralQuery, [$target]); return count($data->query->logevents); } function countGlobalBlocks($target) { $metaGeneralQuery = [ 'action' => 'query', 'list' => 'logevents', 'letitle' => 'User:{0}', 'letype' => 'gblblock', ]; $data = apiQuery(API_METAWIKI, $metaGeneralQuery, [$target]); return count($data->query->logevents); } -$ip = filter_var(trim($_POST['ip']), FILTER_VALIDATE_IP); -if($ip === false) { - die('Not a valid IP!'); -} +//$ip = filter_var(trim($_REQUEST['ip']), FILTER_VALIDATE_IP); +//if($ip === false) { +// echo '
Not a valid IP!
'; +// echo "
"; +// die(); +//} + +$address = Factory::addressFromString($_REQUEST['ip']); + -$ipBlocks = countBlocks($ip); -$ipGBlocks = countGlobalBlocks($ip); +$ipBlocks = countBlocks($address->toString()); +$ipGBlocks = countGlobalBlocks($address->toString()); ?> - + - - - + + + - - = 16; --$x) : - $mask = -1 << 32 - $x; - $lowerBound = $ipl & $mask; - $upperBound = $lowerBound | (~$mask); - $range = long2ip($lowerBound) . '/' . $x; - $ipBlocks = countBlocks($range); - $ipGBlocks = countGlobalBlocks($range); + +// var_dump(inet_pton('2603:9001:2C04:B21F:2C8F:B84F:224E:AD0B')); + +function writeTableRow($cidr, $lowerBound, $upperBound, $ipBlocks, $ipGBlocks) { +?> + + + + + + + +getAddressType() == Type::T_IPv4) { + /** @var IPv4 $v4Address */ + $v4Address = $address; + + for($x = 32; $x >= 16; --$x) { + $range = Subnet::fromString($v4Address->toString() . "/${x}"); + $ipBlocks = countBlocks($range); + $ipGBlocks = countGlobalBlocks($range); + + writeTableRow($x, $range->getStartAddress(), $range->getEndAddress(), $ipBlocks, $ipGBlocks); + } +} else if($address->getAddressType() == Type::T_IPv6) { + /** @var IPv6 $v6Address */ + $v6Address = $address; + + for($x = 128; $x >= 19; --$x) { + $range = Subnet::fromString($v6Address->toString() . "/${x}"); + $ipBlocks = countBlocks($range); + $ipGBlocks = countGlobalBlocks($range); + + writeTableRow($x, $range->getStartAddress(), $range->getEndAddress(), $ipBlocks, $ipGBlocks); + } +} + ?> - - - - - - - -
TargetCIDRUpper bound of rangeLocal block countGlobal block count
TargetCIDRUpper bound of rangeLocal block countGlobal block count
/32toString() ?> ">Local log + Local log ">Global log + Global log
/">">Local log + ">">Global log +
/">Local log - ">Global log -