Skip to content

Instantly share code, notes, and snippets.

@morontt
Last active November 10, 2024 20:23
Show Gist options
  • Save morontt/6aa4812deaa6cfc83a31af75819f8f60 to your computer and use it in GitHub Desktop.
Save morontt/6aa4812deaa6cfc83a31af75819f8f60 to your computer and use it in GitHub Desktop.
CIDR
<?php
class Cidr {
public string $raw;
public int $subnet;
public int $length;
public string $ip;
public function __construct(string $str)
{
$this->raw = $str;
[$ip, $mask] = explode('/', $str);
$this->ip = $ip;
$this->subnet = ip2long($ip) >> (32 - $mask);
$this->length = (int)$mask;
}
}
$fp = fopen('./data.txt', 'rb');
//$fp = fopen('./data_temp.txt', 'rb');
$data = fgets($fp);
fclose($fp);
$cidrs = [];
$excluded = [];
foreach(explode(",", $data) as $item) {
$str = trim($item);
if (!isset($excluded[$str])) {
$cidrs[] = new Cidr($str);
$excluded[$str] = false;
}
}
function contains(Cidr $a, Cidr $b): bool {
return (ip2long($b->ip) >> (32 - $a->length)) === $a->subnet;
}
function compare(Cidr $a, Cidr $b): void {
global $excluded;
if ($a->length === $b->length) {
if ($a->subnet === $b->subnet) {
$c = ($a->raw > $b->raw) ? $a : $b;
$excluded[$c->raw] = true;
}
return;
}
if ($a->length > $b->length) {
if (contains($b, $a)) {
$excluded[$a->raw] = true;
}
} else if (contains($a, $b)) {
$excluded[$b->raw] = true;
}
}
$len = count($cidrs);
for ($i = 0; $i < $len - 1; $i++) {
for ($j = $i + 1; $j < $len; $j++) {
compare($cidrs[$i], $cidrs[$j]);
}
}
/* echo "Excluded:\n";
foreach ($excluded as $key => $value) {
if ($value) {
echo $key . "\n";
}
} */
echo "Compact:\n";
foreach ($cidrs as $cidr) {
if (!$excluded[$cidr->raw]) {
echo $cidr->raw . "\n";
}
}
198.199.79.214/32,17.0.0.0/8,139.178.128.0/17,144.178.0.0/18,63.92.224.0/19,65.199.22.0/23,213.62.61.0/24,204.79.190.0/24,192.12.74.0/24,192.42.249.0/24,208.198.53.0/24,212.50.144.64/26,12.180.46.128/26,62.189.71.224/27,18.141.66.64/27,18.141.66.96/27,18.141.66.128/27,18.236.229.96/27,18.236.229.128/27,18.236.229.160/27,18.236.229.192/27,3.141.237.128/27,3.141.237.160/27,3.141.237.192/27,3.248.235.64/27,3.248.235.96/27,3.248.235.128/27,66.48.6.32/27,34.223.14.0/27,34.223.14.32/27,34.223.22.64/27,34.223.22.96/27,34.223.22.128/27,64.117.208.64/27,64.117.212.32/27,16.162.11.192/27,16.162.11.224/27,16.162.70.128/27,195.212.0.176/28,195.183.136.16/28,195.89.48.32/28,195.89.48.64/28,195.89.48.80/28,195.89.48.192/28,195.89.49.0/28,195.89.49.16/28,195.89.49.112/28,195.89.49.128/28,195.89.49.192/28,195.89.49.224/28,195.89.50.32/28,195.89.50.48/28,195.89.50.64/28,195.89.54.112/28,195.89.54.128/28,195.89.50.128/28,195.89.50.144/28,195.89.50.208/28,12.0.136.32/28,66.48.31.64/28,65.202.22.96/28,65.202.160.192/28,65.205.37.160/28,65.206.52.0/28,65.209.45.240/28,12.138.21.240/28,34.221.183.96/28,34.221.183.112/28,63.81.139.160/28,63.85.190.240/28,64.117.210.144/28,63.118.169.80/28,63.130.136.160/28,99.97.66.192/28,99.108.107.240/28,59.154.164.96/29,194.196.53.56/29,89.202.42.0/29,5.175.83.192/29,80.69.16.216/29,89.202.42.128/29,88.205.100.120/29,89.202.117.24/29,217.111.163.152/29,212.161.12.8/29,213.208.215.208/29,213.86.8.176/29,12.0.65.144/29,12.0.65.168/29,12.0.65.176/29,12.0.68.136/29,12.0.243.104/29,12.1.133.56/29,18.236.61.240/29,18.236.61.248/29,18.236.229.224/29,12.2.181.0/29,12.2.221.152/29,12.3.202.64/29,12.4.214.184/29,209.37.103.136/29,17.0.0.0/9,17.0.0.0/21,17.0.64.0/18,17.0.71.0/24,17.0.99.0/24,17.0.109.0/24,17.0.117.0/24,17.0.120.0/24,17.0.122.0/24,17.0.128.0/18,17.0.141.0/24,17.0.152.0/24,17.0.176.0/20,17.0.184.0/23,17.0.224.0/24,17.1.0.0/19,17.1.64.0/18,17.1.108.0/24,17.1.128.0/18,17.1.138.0/24,17.1.184.0/24,17.1.185.0/24,17.1.193.0/24,17.1.197.0/24,17.7.64.0/24,17.7.68.0/24,17.7.72.0/24,17.7.74.0/23,17.8.64.0/23,17.8.66.0/23,17.8.68.0/22,17.8.76.0/23,17.8.80.0/23,17.8.84.0/24,17.8.85.0/24,17.8.86.0/23,17.8.90.0/24,17.8.91.0/24,17.8.130.0/24,17.8.131.0/24,17.8.136.0/24,17.8.152.0/21,17.8.192.0/20,17.8.208.0/20,17.8.224.0/20,17.8.240.0/20,17.13.64.0/20,17.14.0.0/18,17.14.64.0/19,17.14.96.0/19,17.15.16.0/22,17.15.20.0/22,17.15.50.0/23,17.15.52.0/22,17.15.59.0/24,17.15.61.0/24,17.15.62.0/24,17.15.63.0/24,17.15.64.0/19,17.15.96.0/20,17.15.112.0/20,17.15.128.0/20,17.15.144.0/22,17.15.148.0/22,17.15.152.0/22,17.15.156.0/23,17.15.160.0/20,17.15.176.0/20,17.15.192.0/22,17.15.196.0/22,17.15.208.0/20,17.15.224.0/20,17.22.0.0/18,17.22.64.0/19,17.22.96.0/20,17.22.112.0/21,17.22.120.0/23,17.23.0.0/22,17.23.4.0/22,17.23.8.0/22,17.23.12.0/24,17.23.14.0/23,17.23.16.0/22,17.23.20.0/23,17.23.22.0/23,17.23.24.0/22,17.23.28.0/22,17.23.32.0/23,17.23.34.0/24,17.23.36.0/24,17.23.37.0/24,17.23.38.0/24,17.23.39.0/24,17.23.40.0/24,17.23.41.0/24,17.23.42.0/24,17.23.44.0/24,17.23.48.0/20,17.23.64.0/23,17.23.66.0/23,17.23.68.0/23,17.23.72.0/21,17.23.80.0/20,17.23.96.0/20,17.23.112.0/20,17.23.128.0/19,17.23.160.0/19,17.23.192.0/21,17.23.200.0/22,17.23.204.0/22,17.23.208.0/21,17.23.216.0/21,17.23.224.0/19,17.24.176.0/21,17.24.240.0/21,17.25.232.0/21,17.26.88.0/21,17.26.96.0/21,17.26.104.0/21,17.26.136.0/21,17.26.224.0/21,17.27.224.0/21,17.28.88.0/21,17.28.128.0/21,17.29.16.0/20,17.29.34.0/24,17.29.35.0/24,17.29.38.0/24,17.29.55.0/24,17.29.56.0/24,17.29.59.0/24,17.29.60.0/24,17.29.96.0/20,17.29.114.0/24,17.29.115.0/24,17.29.120.0/24,17.29.122.0/24,17.29.144.0/21,17.29.152.0/22,17.29.156.0/22,17.29.160.0/21,17.29.176.0/20,17.29.192.0/19,17.29.224.0/24,17.29.225.0/24,17.29.226.0/23,17.29.228.0/24,17.29.230.0/24,17.29.234.0/24,17.29.237.0/24,17.29.242.0/24,17.29.246.0/24,17.29.247.0/24,17.29.248.0/24,17.29.251.0/24,17.29.252.0/24,17.32.0.0/16,17.32.32.0/20,17.32.128.0/17,17.32.214.0/23,17.33.13.0/24,17.33.19.0/24,17.33.32.0/19,17.33.32.0/20,17.33.48.0/23,17.33.84.0/23,17.33.86.0/23,17.33.88.0/24,17.33.89.0/24,17.33.90.0/24,17.33.91.0/24,17.33.96.0/23,17.33.98.0/23,17.33.101.0/24,17.33.102.0/23,17.33.104.0/23,17.33.106.0/23,17.33.108.0/23,17.33.112.0/20,17.33.138.0/23,17.33.140.0/23,17.33.144.0/24,17.33.145.0/24,17.33.146.0/24,17.33.151.0/24,17.33.169.0/24,17.33.170.0/24,17.33.171.0/24,17.33.192.0/21,17.33.200.0/22,17.33.208.0/22,17.33.216.0/22,17.33.220.0/22,17.36.0.0/15,17.36.196.0/22,17.37.0.0/20,17.37.16.0/20,17.37.32.0/20,17.37.48.0/22,17.37.52.0/22,17.37.56.0/23,17.37.58.0/23,17.37.60.0/22,17.37.64.0/20,17.37.80.0/20,17.37.96.0/20,17.37.112.0/21,17.37.120.0/21,17.37.128.0/19,17.37.176.0/22,17.37.180.0/22,17.37.184.0/21,17.37.192.0/20,17.37.208.0/21,17.37.216.0/22,17.37.220.0/22,17.37.224.0/20,17.37.240.0/21,17.37.248.0/21,17.38.0.0/15,17.39.64.0/18,17.39.64.0/21,17.39.72.0/21,17.39.80.0/21,17.39.88.0/22,17.39.96.0/20,17.39.112.0/22,17.39.116.0/22,17.39.128.0/19,17.39.128.0/20,17.39.144.0/22,17.40.0.0/15,17.40.0.0/16,17.40.16.0/21,17.40.24.0/22,17.40.32.0/20,17.40.112.0/23,17.40.128.0/20,17.40.148.0/24,17.40.184.0/24,17.40.240.0/23,17.41.0.0/16,17.42.0.0/16,17.43.0.0/19,17.43.50.0/24,17.43.58.0/24,17.43.59.0/24,17.43.64.0/19,17.43.76.0/24,17.43.96.0/21,17.43.104.0/21,17.43.112.0/21,17.43.120.0/21,17.43.132.0/22,17.43.138.0/23,17.43.140.0/23,17.43.142.0/23,17.43.148.0/22,17.43.152.0/21,17.43.160.0/20,17.43.176.0/21,17.43.192.0/18,17.44.4.0/23,17.44.4.0/24,17.44.5.0/24,17.44.12.0/22,17.44.12.0/23,17.44.14.0/23,17.44.16.0/23,17.44.16.0/24,17.44.18.0/23,17.44.20.0/22,17.44.24.0/22,17.44.32.0/22,17.44.36.0/22,17.44.36.0/23,17.44.38.0/23,17.44.40.0/22,17.44.44.0/22,17.44.48.0/21,17.44.48.0/23,17.44.52.0/22,17.44.56.0/22,17.44.60.0/22,17.44.60.0/23,17.44.62.0/23,17.44.64.0/22,17.44.64.0/23,17.44.66.0/23,17.44.68.0/22,17.44.72.0/21,17.44.80.0/20,17.44.96.0/21,17.44.104.0/21,17.44.116.0/22,17.44.120.0/22,17.44.120.0/24,17.44.121.0/24,17.44.122.0/24,17.44.124.0/24,17.44.125.0/24,17.44.126.0/24,17.44.127.0/24,17.44.136.0/21,17.44.136.0/23,17.44.140.0/22,17.44.144.0/22,17.44.144.0/23,17.44.146.0/23,17.44.148.0/22,17.44.148.0/24,17.44.152.0/22,17.44.152.0/23,17.44.154.0/23,17.44.156.0/22,17.44.160.0/22,17.44.172.0/22,17.44.184.0/23,17.44.186.0/23,17.44.192.0/20,17.44.216.0/21,17.44.224.0/22,17.44.224.0/24,17.44.226.0/24,17.44.227.0/24,17.44.228.0/22,17.44.228.0/23,17.44.232.0/21,17.44.232.0/23,17.44.235.0/24,17.44.240.0/22,17.44.244.0/22,17.44.244.0/23,17.44.248.0/23,17.44.250.0/23,17.44.252.0/23,17.44.252.0/24,17.44.254.0/23,17.45.8.0/22,17.45.20.0/23,17.45.32.0/23,17.45.36.0/22,17.45.40.0/22,17.45.44.0/22,17.45.48.0/22,17.45.52.0/22,17.45.72.0/22,17.45.76.0/22,17.45.80.0/21,17.45.88.0/22,17.45.92.0/22,17.45.96.0/21,17.45.112.0/20,17.45.144.0/22,17.45.148.0/23,17.45.150.0/23,17.45.151.0/24,17.45.152.0/22,17.45.156.0/22,17.45.157.0/24,17.45.160.0/23,17.45.162.0/23,17.45.162.0/24,17.45.163.0/24,17.45.166.0/23,17.45.168.0/21,17.45.176.0/22,17.45.180.0/23,17.45.182.0/23,17.45.188.0/22,17.45.188.0/23,17.45.190.0/23,17.45.192.0/21,17.45.200.0/24,17.45.201.0/24,17.45.202.0/23,17.45.206.0/23,17.45.216.0/23,17.45.216.0/24,17.45.217.0/24,17.45.218.0/23,17.45.220.0/22,17.45.220.0/23,17.45.224.0/20,17.46.0.0/16,17.46.0.0/18,17.46.128.0/17,17.47.0.0/20,17.47.2.0/24,17.47.3.0/24,17.47.6.0/24,17.47.7.0/24,17.47.8.0/24,17.47.48.0/24,17.47.49.0/24,17.47.126.0/23,17.47.128.0/17,17.47.128.0/24,17.50.48.0/21,17.50.72.0/21,17.56.0.0/19,17.56.0.0/22,17.56.8.0/24,17.56.9.0/24,17.56.10.0/23,17.56.12.0/23,17.56.128.0/17,17.56.136.0/24,17.56.138.0/23,17.56.140.0/23,17.56.144.0/24,17.57.0.0/17,17.57.0.0/18,17.57.0.0/22,17.57.128.0/21,17.57.144.0/22,17.57.152.0/23,17.57.154.0/24,17.57.155.0/24,17.57.156.0/24,17.57.160.0/24,17.57.161.0/24,17.57.162.0/23,17.57.164.0/23,17.57.167.0/24,17.57.172.0/23,17.57.180.0/24,17.58.0.0/18,17.58.0.0/20,17.58.16.0/20,17.58.32.0/20,17.58.48.0/23,17.58.51.0/24,17.58.55.0/24,17.58.56.0/24,17.58.57.0/24,17.58.58.0/24,17.58.59.0/24,17.58.61.0/24,17.58.62.0/24,17.58.63.0/24,17.58.64.0/21,17.58.72.0/22,17.58.76.0/22,17.58.80.0/20,17.58.96.0/21,17.58.112.0/20,17.58.128.0/18,17.58.192.0/18,17.58.192.0/23,17.58.194.0/23,17.58.196.0/23,17.58.200.0/23,17.58.202.0/23,17.58.204.0/24,17.58.206.0/24,17.58.207.0/24,17.58.224.0/24,17.58.225.0/24,17.58.226.0/24,17.58.227.0/24,17.58.228.0/24,17.58.242.0/23,17.58.244.0/23,17.58.246.0/23,17.58.249.0/24,17.58.250.0/24,17.58.252.0/24,17.58.253.0/24,17.58.254.0/24,17.58.255.0/24,17.59.0.0/16,17.60.0.0/19,17.60.32.0/19,17.60.64.0/19,17.60.96.0/19,17.60.128.0/17,17.61.0.0/17,17.61.128.0/17,17.62.0.0/18,17.62.32.0/20,17.62.48.0/22,17.64.0.0/16,17.65.0.0/16,17.66.0.0/16,17.66.64.0/18,17.66.116.0/22,17.66.120.0/21,17.66.128.0/18,17.66.145.0/24,17.66.146.0/23,17.66.160.0/20,17.66.176.0/22,17.67.0.0/16,17.67.8.0/22,17.67.32.0/20,17.67.48.0/20,17.67.64.0/22,17.67.80.0/20,17.67.104.0/21,17.67.123.0/24,17.67.192.0/22,17.67.196.0/23,17.67.198.0/23,17.67.204.0/22,17.67.252.0/22,17.68.192.0/18,17.69.0.0/16,17.69.32.0/24,17.69.33.0/24,17.69.34.0/24,17.69.35.0/24,17.69.64.0/21,17.69.80.0/22,17.69.96.0/22,17.69.104.0/23,17.69.106.0/24,17.69.112.0/20,17.69.140.0/22,17.69.192.0/20,17.69.208.0/21,17.69.224.0/21,17.69.232.0/22,17.69.236.0/22,17.69.240.0/21,17.72.0.0/16,17.72.64.0/18,17.72.64.0/19,17.72.96.0/19,17.72.124.0/22,17.76.0.0/23,17.76.2.0/23,17.76.4.0/23,17.76.8.0/22,17.76.12.0/22,17.76.16.0/20,17.76.32.0/19,17.76.72.0/21,17.76.80.0/21,17.76.88.0/21,17.76.94.0/23,17.76.96.0/21,17.76.104.0/22,17.76.104.0/23,17.76.106.0/23,17.76.108.0/22,17.76.112.0/21,17.76.112.0/24,17.76.118.0/23,17.76.120.0/24,17.76.122.0/24,17.76.128.0/21,17.76.136.0/22,17.76.140.0/22,17.76.144.0/22,17.76.148.0/22,17.76.192.0/20,17.76.208.0/20,17.77.0.0/16,17.77.0.0/17,17.77.44.0/24,17.77.46.0/23,17.77.56.0/23,17.77.58.0/23,17.77.80.0/23,17.77.128.0/17,17.77.220.0/22,17.77.224.0/20,17.78.64.0/18,17.78.128.0/19,17.78.160.0/19,17.78.192.0/20,17.78.200.0/23,17.78.208.0/21,17.78.224.0/19,17.78.232.0/23,17.79.0.0/16,17.79.0.0/20,17.79.24.0/21,17.79.32.0/20,17.79.43.0/24,17.79.47.0/24,17.79.64.0/18,17.80.0.0/16,17.80.143.0/24,17.80.151.0/24,17.82.0.0/16,17.83.56.0/24,17.83.57.0/24,17.83.130.0/24,17.83.186.0/24,17.84.0.0/16,17.84.128.0/23,17.84.192.0/22,17.84.196.0/23,17.84.198.0/24,17.84.208.0/24,17.84.209.0/24,17.84.216.0/23,17.86.0.0/16,17.91.16.0/21,17.91.24.0/21,17.91.32.0/21,17.91.40.0/21,17.91.48.0/21,17.91.56.0/21,17.91.64.0/21,17.91.72.0/21,17.91.80.0/21,17.91.88.0/21,17.91.96.0/21,17.91.104.0/21,17.91.112.0/21,17.91.120.0/21,17.91.128.0/22,17.91.132.0/22,17.91.136.0/21,17.91.144.0/21,17.91.152.0/21,17.91.160.0/21,17.91.176.0/21,17.91.184.0/21,17.91.200.0/21,17.91.208.0/21,17.91.232.0/21,17.91.248.0/22,17.91.252.0/22,17.92.0.0/21,17.92.8.0/23,17.92.32.0/21,17.92.40.0/21,17.92.184.0/21,17.92.192.0/21,17.92.208.0/21,17.92.216.0/21,17.97.0.0/17,17.97.128.0/19,17.97.160.0/20,17.97.176.0/20,17.97.192.0/21,17.97.208.0/20,17.97.224.0/19,17.98.0.0/16,17.99.0.0/17,17.99.128.0/21,17.99.136.0/22,17.99.192.0/18,17.100.0.0/16,17.102.0.0/15,17.102.144.0/21,17.104.0.0/15,17.106.0.0/15,17.108.0.0/15,17.110.0.0/15,17.110.64.0/18,17.110.128.0/17,17.111.2.0/23,17.111.32.0/19,17.111.64.0/19,17.111.96.0/19,17.111.128.0/17,17.112.3.0/24,17.112.4.0/22,17.112.8.0/22,17.112.12.0/22,17.112.16.0/22,17.112.20.0/22,17.112.24.0/22,17.112.28.0/22,17.112.32.0/21,17.112.36.0/22,17.112.40.0/21,17.112.44.0/22,17.112.48.0/22,17.112.52.0/22,17.112.56.0/21,17.112.56.0/22,17.112.64.0/22,17.112.68.0/22,17.112.72.0/22,17.112.76.0/22,17.112.80.0/22,17.112.84.0/22,17.112.88.0/22,17.112.92.0/22,17.112.96.0/22,17.112.104.0/22,17.112.108.0/22,17.112.112.0/22,17.112.128.0/18,17.113.128.0/17,17.114.0.0/15,17.120.0.0/16,17.121.0.0/18,17.121.64.0/23,17.121.80.0/20,17.121.128.0/17,17.121.128.0/22,17.121.132.0/24,17.122.0.0/18,17.122.0.0/20,17.122.16.0/20,17.122.33.0/24,17.122.64.0/18,17.122.128.0/21,17.122.136.0/22,17.122.160.0/19,17.122.192.0/19,17.122.242.0/24,17.122.244.0/22,17.122.248.0/21,17.123.0.0/20,17.123.16.0/20,17.123.16.0/24,17.123.48.0/22,17.123.60.0/22,17.123.64.0/18,17.123.144.0/22,17.123.192.0/21,17.123.208.0/20,17.123.224.0/20,17.123.240.0/20,17.124.0.0/16,17.125.0.0/16,17.126.5.0/24,17.126.6.0/24,17.126.9.0/24,17.126.13.0/24,17.126.203.0/24,17.127.0.0/24,17.127.1.0/24,17.127.4.0/24,17.127.5.0/24,17.127.6.0/24,17.127.9.0/24,17.127.13.0/24,17.127.22.0/24,17.127.64.0/20,17.127.160.0/21,17.128.0.0/9,17.128.0.0/19,17.128.32.0/19,17.129.0.0/16,17.129.48.0/20,17.129.80.0/20,17.129.96.0/24,17.129.104.0/22,17.130.128.0/22,17.130.132.0/24,17.130.133.0/24,17.130.160.0/20,17.130.176.0/22,17.130.180.0/24,17.130.184.0/22,17.130.188.0/22,17.130.208.0/22,17.132.4.0/22,17.132.8.0/22,17.132.13.0/24,17.132.15.0/24,17.132.18.0/23,17.132.20.0/23,17.132.22.0/23,17.132.26.0/24,17.132.27.0/24,17.132.28.0/22,17.132.32.0/22,17.132.36.0/24,17.132.40.0/23,17.132.48.0/22,17.132.56.0/22,17.132.64.0/23,17.132.66.0/23,17.132.68.0/23,17.132.70.0/23,17.132.72.0/24,17.132.73.0/24,17.132.76.0/23,17.132.78.0/24,17.132.79.0/24,17.132.80.0/24,17.132.82.0/24,17.132.83.0/24,17.132.84.0/24,17.132.85.0/24,17.132.86.0/24,17.132.87.0/24,17.132.88.0/24,17.132.96.0/22,17.132.100.0/23,17.132.102.0/23,17.132.108.0/22,17.132.112.0/20,17.132.144.0/21,17.133.0.0/16,17.134.0.0/22,17.134.8.0/21,17.134.16.0/20,17.134.32.0/19,17.134.64.0/20,17.134.80.0/20,17.134.96.0/20,17.134.112.0/20,17.134.128.0/20,17.134.144.0/20,17.134.160.0/20,17.134.184.0/21,17.134.192.0/20,17.134.208.0/20,17.134.224.0/20,17.134.240.0/20,17.135.0.0/16,17.135.0.0/17,17.136.0.0/18,17.136.128.0/22,17.136.132.0/22,17.136.136.0/21,17.136.144.0/20,17.137.0.0/16,17.138.0.0/15,17.138.0.0/17,17.138.128.0/20,17.138.144.0/20,17.138.160.0/20,17.138.176.0/20,17.138.192.0/21,17.138.200.0/21,17.138.200.0/22,17.138.208.0/20,17.138.224.0/20,17.138.240.0/20,17.139.0.0/20,17.139.16.0/24,17.139.17.0/24,17.139.18.0/23,17.139.18.0/24,17.139.19.0/24,17.139.20.0/24,17.139.21.0/24,17.139.22.0/23,17.139.24.0/21,17.139.32.0/20,17.139.48.0/20,17.139.64.0/20,17.139.80.0/21,17.139.88.0/21,17.139.96.0/23,17.139.98.0/23,17.139.100.0/22,17.139.104.0/21,17.139.112.0/20,17.139.128.0/17,17.140.34.0/23,17.140.40.0/21,17.140.42.0/24,17.140.44.0/24,17.140.45.0/24,17.140.64.0/19,17.140.80.0/20,17.140.96.0/19,17.140.128.0/17,17.141.0.0/16,17.142.0.0/15,17.145.0.0/20,17.145.16.0/20,17.145.32.0/20,17.145.48.0/20,17.145.48.0/22,17.145.96.0/20,17.150.172.0/24,17.153.4.0/24,17.153.19.0/24,17.155.0.0/21,17.155.8.0/21,17.155.16.0/20,17.155.32.0/20,17.155.128.0/20,17.155.144.0/20,17.155.160.0/19,17.155.192.0/20,17.155.208.0/20,17.155.224.0/19,17.156.0.0/16,17.156.96.0/23,17.156.98.0/23,17.156.102.0/23,17.156.104.0/23,17.156.106.0/23,17.156.112.0/23,17.156.124.0/23,17.156.128.0/20,17.156.176.0/20,17.157.0.0/22,17.157.4.0/24,17.157.5.0/24,17.157.12.0/24,17.157.16.0/22,17.157.20.0/22,17.157.24.0/24,17.157.25.0/24,17.157.28.0/22,17.157.44.0/22,17.157.48.0/24,17.157.52.0/24,17.157.56.0/22,17.157.64.0/20,17.157.80.0/20,17.157.96.0/20,17.157.112.0/20,17.157.128.0/19,17.157.130.0/23,17.157.132.0/22,17.157.136.0/21,17.157.144.0/20,17.157.160.0/19,17.157.192.0/22,17.157.196.0/22,17.157.202.0/23,17.157.204.0/23,17.157.224.0/19,17.160.0.0/12,17.160.0.0/16,17.160.22.0/24,17.160.74.0/23,17.160.82.0/23,17.160.160.0/20,17.160.171.0/24,17.161.0.0/17,17.161.48.0/21,17.161.64.0/19,17.161.104.0/24,17.161.128.0/18,17.161.192.0/18,17.162.0.0/15,17.164.0.0/16,17.167.0.0/16,17.168.0.0/13,17.168.0.0/14,17.168.2.0/24,17.169.249.0/24,17.169.254.0/23,17.170.36.0/24,17.170.175.0/24,17.171.11.0/24,17.171.58.0/23,17.171.63.0/24,17.171.78.0/23,17.171.193.0/24,17.171.194.0/24,17.172.0.0/16,17.173.0.0/16,17.173.232.0/21,17.174.0.0/20,17.174.0.0/24,17.174.24.0/22,17.174.28.0/24,17.174.29.0/24,17.174.128.0/17,17.175.128.0/18,17.176.0.0/15,17.178.0.0/15,17.178.2.0/23,17.178.8.0/21,17.178.10.0/23,17.178.12.0/24,17.178.26.0/23,17.178.32.0/24,17.178.33.0/24,17.178.34.0/24,17.178.35.0/24,17.178.36.0/23,17.178.40.0/21,17.178.48.0/20,17.178.64.0/19,17.178.96.0/20,17.178.112.0/21,17.178.120.0/22,17.178.126.0/23,17.178.128.0/17,17.179.16.0/20,17.179.32.0/21,17.179.40.0/21,17.179.48.0/20,17.179.78.0/24,17.179.80.0/20,17.179.96.0/19,17.179.128.0/17,17.180.0.0/14,17.181.0.0/21,17.181.8.0/22,17.181.12.0/22,17.181.16.0/20,17.181.32.0/20,17.181.48.0/21,17.181.56.0/21,17.181.64.0/19,17.181.96.0/19,17.181.128.0/17,17.182.0.0/19,17.253.0.0/23,17.253.2.0/23,17.253.4.0/23,17.253.6.0/23,17.253.10.0/24,17.253.11.0/24,17.253.12.0/23,17.253.14.0/23,17.253.16.0/23,17.253.18.0/24,17.253.20.0/23,17.253.22.0/23,17.253.24.0/24,17.253.26.0/23,17.253.28.0/23,17.253.30.0/23,17.253.32.0/23,17.253.34.0/24,17.253.36.0/23,17.253.38.0/23,17.253.42.0/23,17.253.44.0/23,17.253.46.0/23,17.253.48.0/23,17.253.50.0/23,17.253.52.0/23,17.253.54.0/24,17.253.56.0/23,17.253.58.0/23,17.253.60.0/23,17.253.62.0/23,17.253.64.0/23,17.253.66.0/23,17.253.68.0/23,17.253.70.0/23,17.253.72.0/23,17.253.74.0/23,17.253.76.0/23,17.253.80.0/23,17.253.82.0/23,17.253.84.0/23,17.253.86.0/23,17.253.90.0/23,17.253.92.0/23,17.253.94.0/23,17.253.96.0/23,17.253.98.0/23,17.253.100.0/23,17.253.102.0/23,17.253.104.0/23,17.253.106.0/23,17.253.108.0/23,17.253.110.0/23,17.253.112.0/23,17.253.114.0/23,17.253.116.0/23,17.253.118.0/24,17.253.119.0/24,17.253.120.0/24,17.253.121.0/24,17.253.122.0/24,17.253.124.0/23
3.141.237.128/27,3.141.237.160/27,3.141.237.192/27,3.248.235.64/27,3.248.235.96/27,3.248.235.128/27,5.175.83.192/29,12.0.65.144/28,12.0.65.160/27,12.0.68.136/29,12.0.136.32/28,12.0.243.104/29,12.1.133.56/29,12.2.181.0/29,12.2.221.152/29,12.3.202.64/29,12.4.214.184/29,12.138.21.240/28,12.180.46.128/26,16.162.11.192/27,16.162.11.224/27,16.162.70.128/27,17.0.0.0/8,18.141.66.64/27,18.141.66.96/27,18.141.66.128/27,18.236.61.240/28,18.236.61.248/29,18.236.229.96/27,18.236.229.128/27,18.236.229.160/27,18.236.229.192/27,18.236.229.224/29,34.221.183.96/28,34.221.183.112/28,34.223.14.0/27,34.223.14.32/27,34.223.22.64/27,34.223.22.96/27,34.223.22.128/27,59.154.164.96/29,62.189.71.224/27,63.81.139.160/28,63.85.190.240/28,63.92.224.0/19,63.118.169.80/28,63.130.136.160/28,64.117.208.64/27,64.117.210.144/28,64.117.212.32/27,65.199.22.0/23,65.202.22.96/28,65.202.160.192/28,65.205.37.160/28,65.206.52.0/28,65.209.45.240/28,66.48.6.32/27,66.48.31.64/28,80.69.16.216/29,88.205.100.120/29,89.202.42.0/28,89.202.42.128/29,89.202.117.24/29,99.97.66.192/28,99.108.107.240/28,139.178.128.0/17,144.178.0.0/18,192.12.74.0/24,192.42.249.0/24,194.196.53.56/29,195.89.48.32/28,195.89.48.64/27,195.89.48.192/26,195.89.49.0/25,195.89.49.128/26,195.89.49.192/27,195.89.49.224/27,195.89.50.32/26,195.89.50.64/28,195.89.50.128/27,195.89.50.208/28,195.89.54.112/28,195.89.54.128/28,195.183.136.16/28,195.196.53.56/29,195.212.0.176/28,198.199.79.214/32,204.79.190.0/24,208.198.53.0/24,209.37.103.136/29,212.50.144.64/26,212.161.12.8/29,213.62.61.0/24,213.208.215.208/29,213.86.8.176/29,217.111.163.152/29
3.141.237.128/16,3.141.237.160/27,3.141.237.192/31,3.248.235.64/27,3.248.235.65/27,139.218.74.0/24,139.218.72.0/21
<?php
/**
* 195.89.49.91: FT
* 195.89.50.21: FT
* 195.89.49.87: FT
* 12.0.65.191: FT
*/
class IpFilter {
private array $cidrs;
public function __construct(string $cidrsStr)
{
$this->cidrs = array_map(fn($a) => trim($a), explode(',', $cidrsStr));
}
public function shouldRequestBeFiltered(string $clientIP): bool
{
if (!empty($this->getCIDRs())) {
foreach ($this->getCIDRs() as $cidr) {
if ($this->isIpMatchesCidr($clientIP, $cidr)) {
return true;
}
}
}
return false;
}
private function isIpMatchesCidr(string $clientIP, string $cidr): bool
{
[$subnet, $mask] = explode('/', $cidr);
return (ip2long($clientIP) >> (32 - $mask)) === (ip2long($subnet) >> (32 - $mask));
}
private function getCIDRs(): array
{
return $this->cidrs;
}
}
$fp = fopen('./data.txt', 'rb');
$s1 = fgets($fp);
$f1 = new IpFilter($s1);
//echo $s1;
$s2 = fgets($fp);
$f2 = new IpFilter($s2);
//echo $s2;
fclose($fp);
$i = 0;
$stop = false;
while ($i < 1e8 && !$stop) {
$ip = sprintf(
'%d.%d.%d.%d',
mt_rand(0, 255),
mt_rand(0, 255),
mt_rand(0, 255),
mt_rand(0, 255)
);
$r1 = $f1->shouldRequestBeFiltered($ip);
$r2 = $f2->shouldRequestBeFiltered($ip);
$stop = $r1 !== $r2;
echo $ip . ': ';
echo $r1 ? 'T' : 'F';
echo $r2 ? 'T' : 'F';
echo "\n";
$i++;
}
Compact:
198.199.79.214/32
17.0.0.0/8
139.178.128.0/17
144.178.0.0/18
63.92.224.0/19
65.199.22.0/23
213.62.61.0/24
204.79.190.0/24
192.12.74.0/24
192.42.249.0/24
208.198.53.0/24
212.50.144.64/26
12.180.46.128/26
62.189.71.224/27
18.141.66.64/27
18.141.66.96/27
18.141.66.128/27
18.236.229.96/27
18.236.229.128/27
18.236.229.160/27
18.236.229.192/27
3.141.237.128/27
3.141.237.160/27
3.141.237.192/27
3.248.235.64/27
3.248.235.96/27
3.248.235.128/27
66.48.6.32/27
34.223.14.0/27
34.223.14.32/27
34.223.22.64/27
34.223.22.96/27
34.223.22.128/27
64.117.208.64/27
64.117.212.32/27
16.162.11.192/27
16.162.11.224/27
16.162.70.128/27
195.212.0.176/28
195.183.136.16/28
195.89.48.32/28
195.89.48.64/28
195.89.48.80/28
195.89.48.192/28
195.89.49.0/28
195.89.49.16/28
195.89.49.112/28
195.89.49.128/28
195.89.49.192/28
195.89.49.224/28
195.89.50.32/28
195.89.50.48/28
195.89.50.64/28
195.89.54.112/28
195.89.54.128/28
195.89.50.128/28
195.89.50.144/28
195.89.50.208/28
12.0.136.32/28
66.48.31.64/28
65.202.22.96/28
65.202.160.192/28
65.205.37.160/28
65.206.52.0/28
65.209.45.240/28
12.138.21.240/28
34.221.183.96/28
34.221.183.112/28
63.81.139.160/28
63.85.190.240/28
64.117.210.144/28
63.118.169.80/28
63.130.136.160/28
99.97.66.192/28
99.108.107.240/28
59.154.164.96/29
194.196.53.56/29
89.202.42.0/29
5.175.83.192/29
80.69.16.216/29
89.202.42.128/29
88.205.100.120/29
89.202.117.24/29
217.111.163.152/29
212.161.12.8/29
213.208.215.208/29
213.86.8.176/29
12.0.65.144/29
12.0.65.168/29
12.0.65.176/29
12.0.68.136/29
12.0.243.104/29
12.1.133.56/29
18.236.61.240/29
18.236.61.248/29
18.236.229.224/29
12.2.181.0/29
12.2.221.152/29
12.3.202.64/29
12.4.214.184/29
209.37.103.136/29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment