getMessage(), "\n"; die(0); } if (empty($src_file) || !is_file($src_file)) { echo 'src_file:', $src_file, ' is not found.'; die(0); } define('START_TIME', microtime(true)); 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 require_once './IP2Location.php'; require_once './Net/DNS2.php'; $china_list = is_file(CHINA_LIST) ? require CHINA_LIST : array(); $dead_horse = is_file(DEAD_HORSE) ? require DEAD_HORSE : array(); //大中华区 $CHINA_AREA = array('CN' => true, 'HK' => true, 'MO' => true, 'TW' => true); $db = new \IP2Location\Database('./databases/IP2LOCATION-LITE-DB1.BIN', \IP2Location\Database::MEMORY_CACHE); $r = new Net_DNS2_Resolver(array('nameservers' => array('223.5.5.5', '223.6.6.6', '119.29.29.29'))); $src_fp = fopen($src_file, 'r'); while (!feof($src_fp)) { $row = fgets($src_fp, 512); if (empty($row)) { continue; } echo $row; if (preg_match('/^address=\/(.+)?\/$/', $row, $matches)) { try { $result = $r->query($matches[1], 'A'); $result = $result->answer; if (is_array($result) && count($result) > 0) { //find the A record $a_record = null; foreach ($result as $res) { if ($res->type == 'A') { $a_record = $res; break; } } if (!$a_record) { $dead_horse[$matches[1]]['empty']++; continue; } $records = $db->lookup($a_record->address, \IP2Location\Database::ALL); if (array_key_exists($records['countryCode'], $CHINA_AREA) && !array_key_exists($matches[1], $china_list)) { $china_list[$matches[1]] = $records['countryCode'];//$records['countryName']; } } else { $dead_horse[$matches[1]]['empty']++; } } catch (Net_DNS2_Exception $e) { //3=dns记录不存在 if ($e->getCode() == 3) { $dead_horse[$matches[1]]['dead']++; } else { echo date('m-d H:i:s'), "[", $matches[1], "]", $e->getMessage(), ",code:", $e->getCode(), "\n"; } } } } try { $dead_horse = "getMessage(), "\t", $e->getCode(), "\n"; } echo 'Time cost:', microtime(true) - START_TIME, "s\n";