anti-AD/tools/valid-addr.php

116 lines
3.6 KiB
PHP
Raw Normal View History

2019-12-14 10:45:22 +08:00
<?php
2019-12-14 16:23:37 +08:00
/**
* address dns checker
*
* @file valid-addr.php
* @date 2019-12-14
* @author gently
*
*/
set_time_limit(0);
2019-12-14 10:45:22 +08:00
2019-12-14 19:27:05 +08:00
error_reporting(7);
2019-12-14 16:23:37 +08:00
2019-12-14 19:27:05 +08:00
if(PHP_SAPI != 'cli'){
2019-12-14 16:23:37 +08:00
die('nothing.');
}
$src_file = '';
$file_num = 'unknown';
2019-12-14 19:27:05 +08:00
try{
2019-12-14 16:23:37 +08:00
$file = $argv[1];
$src_file = dirname(__DIR__) . '/origin-files/' . $file;
2019-12-14 19:27:05 +08:00
list(, $file_num) = explode('_', $file);
}catch(Exception $e){
2019-12-14 16:23:37 +08:00
echo "get args failed.", $e->getMessage(), "\n";
die(0);
}
2019-12-14 10:45:22 +08:00
2019-12-14 19:27:05 +08:00
if(empty($src_file) || !is_file($src_file)){
2019-12-14 16:23:37 +08:00
echo 'src_file:', $src_file, ' is not found.';
die(0);
}
define('START_TIME', microtime(true));
2019-12-14 19:27:05 +08:00
define('LIB_DIR', dirname(__DIR__) . '/lib');
2019-12-14 16:23:37 +08:00
define('CHINA_LIST', dirname(__DIR__) . '/origin-files/china-list_' . $file_num . '.php');
define('DEAD_HORSE', dirname(__DIR__) . '/origin-files/dead-horse_' . $file_num . '.php');
//http://download.ip2location.com/lite/IP2LOCATION-LITE-DB1.BIN.ZIP
2019-12-14 19:27:05 +08:00
require_once LIB_DIR . '/IP2Location.php';
require_once LIB_DIR . '/Net/DNS2.php';
2019-12-14 10:45:22 +08:00
2019-12-14 16:23:37 +08:00
$china_list = is_file(CHINA_LIST) ? require CHINA_LIST : array();
$dead_horse = is_file(DEAD_HORSE) ? require DEAD_HORSE : array();
2019-12-14 10:45:22 +08:00
2019-12-14 16:23:37 +08:00
//大中华区
$CHINA_AREA = array('CN' => true, 'HK' => true, 'MO' => true, 'TW' => true);
2019-12-14 10:45:22 +08:00
2019-12-14 19:27:05 +08:00
$db = new \IP2Location\Database(LIB_DIR.'/databases/IP2LOCATION-LITE-DB1.BIN', \IP2Location\Database::MEMORY_CACHE);
2019-12-14 16:23:37 +08:00
$r = new Net_DNS2_Resolver(array('nameservers' => array('223.5.5.5', '223.6.6.6', '119.29.29.29')));
2019-12-14 10:45:22 +08:00
2019-12-14 16:23:37 +08:00
$src_fp = fopen($src_file, 'r');
2019-12-14 10:45:22 +08:00
2019-12-15 10:20:25 +08:00
$cnt = 0;
2019-12-14 19:27:05 +08:00
while(!feof($src_fp)){
2019-12-14 16:23:37 +08:00
$row = fgets($src_fp, 512);
2019-12-15 10:20:25 +08:00
echo '[', date('m-d H:i:s'), '],', ++$cnt, "\n";
2019-12-14 19:27:05 +08:00
if(empty($row)){
2019-12-14 16:23:37 +08:00
continue;
}
2019-12-14 10:45:22 +08:00
2019-12-14 19:27:05 +08:00
if(preg_match('/^address=\/(.+)?\/$/', $row, $matches)){
try{
2019-12-14 16:23:37 +08:00
$result = $r->query($matches[1], 'A');
$result = $result->answer;
2019-12-14 19:27:05 +08:00
if(is_array($result) && count($result) > 0){
2019-12-14 16:23:37 +08:00
//find the A record
$a_record = null;
2019-12-14 19:27:05 +08:00
foreach($result as $res){
if($res->type == 'A'){
2019-12-14 16:23:37 +08:00
$a_record = $res;
break;
}
}
2019-12-14 10:45:22 +08:00
2019-12-14 19:27:05 +08:00
if(!$a_record){
2019-12-14 16:23:37 +08:00
$dead_horse[$matches[1]]['empty']++;
continue;
}
$records = $db->lookup($a_record->address, \IP2Location\Database::ALL);
2019-12-14 19:27:05 +08:00
if(array_key_exists($records['countryCode'], $CHINA_AREA) && !array_key_exists($matches[1], $china_list)){
2019-12-14 16:23:37 +08:00
$china_list[$matches[1]] = $records['countryCode'];//$records['countryName'];
}
2019-12-14 19:27:05 +08:00
}else{
2019-12-14 16:23:37 +08:00
$dead_horse[$matches[1]]['empty']++;
}
2019-12-14 19:27:05 +08:00
}catch(Net_DNS2_Exception $e){
if($e->getCode() == 3){
2019-12-14 19:15:13 +08:00
$dead_horse[$matches[1]]['dead']++;//3=dns记录不存在
2019-12-14 19:27:05 +08:00
}elseif($e->getCode() == 2){
2019-12-14 19:15:13 +08:00
$dead_horse[$matches[1]]['problem']++;//2=查询失败dns服务器没有返回正确记录
}elseif($e->getCode() == 203){
$dead_horse[$matches[1]]['timeout']++;//203=查询超时
2019-12-14 19:27:05 +08:00
}else{
2019-12-14 16:23:37 +08:00
echo date('m-d H:i:s'), "[", $matches[1], "]", $e->getMessage(), ",code:", $e->getCode(), "\n";
}
2019-12-14 10:45:22 +08:00
}
}
}
2019-12-14 16:23:37 +08:00
2019-12-14 19:27:05 +08:00
try{
2019-12-14 16:23:37 +08:00
$dead_horse = "<?php \nreturn " . var_export($dead_horse, true) . ';';
$china_list = "<?php \nreturn " . var_export($china_list, true) . ';';
file_put_contents(DEAD_HORSE, $dead_horse);
file_put_contents(CHINA_LIST, $china_list);
2019-12-14 19:27:05 +08:00
}catch(Exception $e){
2019-12-14 16:23:37 +08:00
echo date('m-d H:i:s'), "write file failed:", $e->getMessage(), "\t", $e->getCode(), "\n";
}
2019-12-14 21:29:41 +08:00
echo 'Time cost:', microtime(true) - START_TIME, "s, at ", date('m-d H:i:s'), "\n";