From 59423165bd3a2ec3216f2a368d23840644d2f9bd Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sat, 28 Aug 2021 11:46:48 +0800 Subject: [PATCH] feat: description of special IPv4 --- backend/getInfo.php | 42 ++---------- backend/qqwry.php | 2 +- backend/specialIP.php | 155 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 162 insertions(+), 37 deletions(-) create mode 100644 backend/specialIP.php diff --git a/backend/getInfo.php b/backend/getInfo.php index 4c22d6d..cdca586 100644 --- a/backend/getInfo.php +++ b/backend/getInfo.php @@ -5,10 +5,12 @@ include("qqwry.php"); include("ipinfo.php"); include("ipip.php"); include("cityCN.php"); +include("specialIP.php"); function getIPInfo($ip) { + // TODO: format ipv6 $specialInfo = checkSpecial($ip); // 检查是否为特殊IP段 - if (isset($specialInfo)) { + if ($specialInfo !== null) { $info['ip'] = $ip; $info['as'] = null; $info['city'] = null; @@ -16,9 +18,9 @@ function getIPInfo($ip) { $info['country'] = null; $info['timezone'] = null; $info['loc'] = null; - $info['isp'] = $specialInfo['en']; - $info['scope'] = null; - $info['detail'] = $specialInfo['cn']; + $info['isp'] = $specialInfo['descEn']; + $info['scope'] = $specialInfo['scope']; + $info['detail'] = $specialInfo['descCn']; } else { $IPIP = new IPDB('ipipfree.ipdb'); $addr = $IPIP->getDistrict($ip); // 获取IPIP.net数据 @@ -105,38 +107,6 @@ function getIPInfo($ip) { return $info; } -function checkSpecial($ip) { // 检查特殊IP地址并返回说明 - if ('::1' === $ip) { - $info['en'] = 'localhost IPv6 access'; - $info['cn'] = '本地IPv6地址'; - } - if (stripos($ip, 'fe80:') === 0) { - $info['en'] = 'link-local IPv6 access'; - $info['cn'] = '链路本地IPv6地址'; - } - if (strpos($ip, '127.') === 0) { - $info['en'] = 'localhost IPv4 access'; - $info['cn'] = '本地IPv4地址'; - } - if (strpos($ip, '10.') === 0) { - $info['en'] = 'private IPv4 access'; - $info['cn'] = '私有IPv4地址'; - } - if (preg_match('/^172\.(1[6-9]|2\d|3[01])\./', $ip) === 1) { - $info['en'] = 'private IPv4 access'; - $info['cn'] = '私有IPv4地址'; - } - if (strpos($ip, '192.168.') === 0) { - $info['en'] = 'private IPv4 access'; - $info['cn'] = '私有IPv4地址'; - } - if (strpos($ip, '169.254.') === 0) { - $info['en'] = 'link-local IPv4 access'; - $info['cn'] = '链路本地IPv4地址'; - } - return isset($info) ? $info : null; -} - function tryCIDR($beginIP, $endIP) { // 给定IP范围,尝试计算CIDR $tmp = ip2long($endIP) - ip2long($beginIP) + 1; if (pow(2, intval(log($tmp, 2))) == $tmp) { // 判断是否为2的整数次方 diff --git a/backend/qqwry.php b/backend/qqwry.php index 17860c1..a90fcad 100644 --- a/backend/qqwry.php +++ b/backend/qqwry.php @@ -26,7 +26,7 @@ class QQWry { private $recordNum; // 总记录条数 private $formatPort = '1602'; // 数据格式化分析接口 - public function __construct($fileName) { // 构造函数 + public function __construct($fileName = 'qqwry.dat') { // 构造函数 $this->fp = fopen($fileName, 'rb'); $this->firstRecord = $this->read4byte(); $this->lastRecord = $this->read4byte(); diff --git a/backend/specialIP.php b/backend/specialIP.php new file mode 100644 index 0000000..6e6baaa --- /dev/null +++ b/backend/specialIP.php @@ -0,0 +1,155 @@ + '0.0.0.0/8', // 0.0.0.0 - 0.255.255.255 + 'desc' => 'Self Identification' + ], + [ + 'scope' => '10.0.0.0/8', // 10.0.0.0 - 10.255.255.255 + 'desc' => 'Private Use Networks' + ], + [ + 'scope' => '100.64.0.0/10', // 100.64.0.0 - 100.127.255.255 + 'desc' => 'Shared Address Space' + ], + [ + 'scope' => '127.0.0.0/8', // 127.0.0.0 - 127.255.255.255 + 'desc' => 'Loopback' + ], + [ + 'scope' => '169.254.0.0/16', // 169.254.0.0 - 169.254.255.255 + 'desc' => 'Link Local' + ], + [ + 'scope' => '172.16.0.0/12', // 172.16.0.0 - 172.31.255.255 + 'desc' => 'Private Use Networks' + ], + [ + 'scope' => '192.0.0.0/29', // 192.0.0.0 - 192.0.0.7 + 'desc' => 'IPv4 Service Continuity Prefix' + ], + [ + 'scope' => '192.0.0.8/32', // 192.0.0.8 + 'desc' => 'IPv4 dummy address' + ], + [ + 'scope' => '192.0.0.9/32', // 192.0.0.9 + 'desc' => 'Port Control Protocol Anycast' + ], + [ + 'scope' => '192.0.0.10/32', // 192.0.0.10 + 'desc' => 'Traversal Using Relays around NAT Anycast' + ], + [ + 'scope' => '192.0.0.170/32', // 192.0.0.170 + 'desc' => 'NAT64/DNS64 Discovery' + ], + [ + 'scope' => '192.0.0.171/32', // 192.0.0.171 + 'desc' => 'NAT64/DNS64 Discovery' + ], + [ + 'scope' => '192.0.0.0/24', // 192.0.0.0 - 192.0.0.255 + 'desc' => 'IETF Protocol Assignments' + ], + [ + 'scope' => '192.0.2.0/24', // 192.0.2.0 - 192.0.2.255 + 'desc' => 'TEST-NET-1' + ], + // [ + // 'scope' => '192.31.196.0/24', // 192.31.196.0 - 192.31.196.255 + // 'desc' => 'AS112-v4' + // ], + [ + 'scope' => '192.52.193.0/24', // 192.52.193.0 - 192.52.193.255 + 'desc' => 'AMT' + ], + [ + 'scope' => '192.88.99.0/24', // 192.88.99.0 - 192.88.99.255 + 'desc' => 'Deprecated (6to4 Relay Anycast)' + ], + [ + 'scope' => '192.168.0.0/16', // 192.168.0.0 - 192.168.255.255 + 'desc' => 'Private Use Networks' + ], + // [ + // 'scope' => '192.175.48.0/24', // 192.175.48.0 - 192.175.48.255 + // 'desc' => 'Direct Delegation AS112 Service' + // ], + [ + 'scope' => '198.18.0.0/15', // 198.18.0.0 - 198.19.255.255 + 'desc' => 'Benchmarking' + ], + [ + 'scope' => '198.51.100.0/24', // 198.51.100.0 - 198.51.100.255 + 'desc' => 'TEST-NET-2' + ], + [ + 'scope' => '203.0.113.0/24', // 203.0.113.0 - 203.0.113.255 + 'desc' => 'TEST-NET-3' + ], + [ + 'scope' => '224.0.0.0/4', // 224.0.0.0 - 239.255.255.255 + 'desc' => 'IPv4 Class D for Multicasting' + ], + [ + 'scope' => '255.255.255.255/32', // 255.255.255.255 + 'desc' => 'Limited Broadcast' + ], + [ + 'scope' => '240.0.0.0/4', // 240.0.0.0 - 255.255.255.255 + 'desc' => 'IPv4 Class E Reserved' + ] +); + +function cidrToRange($cidr) { // CIDR转IP段 + $cidr = explode('/', $cidr); + $range['start'] = long2ip((ip2long($cidr[0])) & ((-1 << (32 - (int)$cidr[1])))); + $range['end'] = long2ip((ip2long($range['start'])) + pow(2, (32 - (int)$cidr[1])) - 1); + return $range; +} + +function checkSpecialIPv4($ip) { // 检查特殊IPv4地址 + global $specialIPv4; + $ipv4 = ip2long($ip); + foreach ($specialIPv4 as $special) { + $range = cidrToRange($special['scope']); + if ($ipv4 >= ip2long($range['start']) && $ipv4 <= ip2long($range['end'])) { + $detail = (new QQWry)->getDetail($ip); + return array( + 'scope' => $special['scope'], + 'descEn' => $special['desc'], + 'descCn' => $detail['dataA'] . $detail['dataB'] + ); + } + } + return null; // 非特殊地址 +} + +function checkSpecialIPv6($ip) { // 检查特殊IPv6地址 + // TODO: More IPv6 range + if ('::1' === $ip) { + $info['scope'] = '::1/128'; + $info['en'] = 'localhost IPv6 access'; + $info['cn'] = '本地IPv6地址'; + } + if (stripos($ip, 'fe80:') === 0) { + $info['scope'] = 'fe80::/16'; + $info['en'] = 'link-local IPv6 access'; + $info['cn'] = '链路本地IPv6地址'; + } + return isset($info) ? $info : null; +} + +function checkSpecial($ip) { // 检查特殊IP地址并返回说明 + if (filter_var($ip, \FILTER_VALIDATE_IP,\FILTER_FLAG_IPV4)) { // IPv4 + return checkSpecialIPv4($ip); + } else { + return checkSpecialIPv6($ip); + } +} + +?> \ No newline at end of file