Skip to content

如何追蹤測試ip庫內容的國家地區是正確的

Published: at 上午05:47

如何追蹤測試ip庫內容的國家地區是正確的 js

async function queryData() {
  const client = await pool.connect();
  try {
    const result =
      await client.query(`Select * from (SELECT ip_start, ip_end, continent, country_for_country, stateprov_code, 
        stateprovfor_cubdivision, district, city_for_city, zipcode, latitude, longitude, geoname_id, timezone_offset, 
        timezone_name, weather_code FROM public.dbip_location limit 10000000)  tt  ORDER BY RANDOM() limit 1000`);
    // console.log(result.rows);
    // SELECT * FROM  public.dbip_location  LIMIT 1 OFFSET FLOOR(RANDOM() * (SELECT COUNT(*) FROM public.dbip_location));
    let iplist = result.rows;
    iplist.forEach(async (ip, index) => {
      // console.log("queryIp : ", ip);
      axios
        .get(`https://ipinfo.io/${ip.ip_start}`)
        .then(response => {
          // console.log(response.data);
          checkIp(response.data, ip, index);
        })
        .catch(error => {
          console.log(error);
        });

      // 每個請求之間加入 1 秒的延遲
      await new Promise(resolve => setTimeout(resolve, 300));
    });
  } catch (error) {
    console.error(error);
  } finally {
    client.release();
  }
}

function checkIp(checkipObj, ip, index) {
  let countryDiff = false;
  let cityDiff = false;
  let regionDiff = false;
  if (checkipObj.country !== ip.country_for_country) {
    countryDiff = true;
  }
  if (checkipObj.city !== ip.city_for_city) {
    cityDiff = true;
  }
  if (checkipObj.region !== ip.stateprovfor_cubdivision) {
    regionDiff = true;
  }

  writeFile(`\n確認的第幾個ip = ${index}\nip: ${checkipObj.ip} : country:${
    countryDiff ? "不同" : "一樣"
  } , city:${cityDiff ? "不同" : "一樣"} , region:${
    regionDiff ? "不同" : "一樣"
  } 
購買ip庫的資料 : ${ip.ip_start} : country:${ip.country_for_country} , city:${
    ip.city_for_city
  } , region(stateprovfor_cubdivision):${ip.stateprovfor_cubdivision}
確認的ip的資料 : ${checkipObj.ip} : country:${checkipObj.country} , city:${
    checkipObj.city
  } , region(stateprovfor_cubdivision):${checkipObj.region}
==========================`);
}

function writeFile(str) {
  fs.appendFile("checkip.txt", str, error => {
    if (error) {
      console.error(error);
    } else {
      console.log("Data appended to file.");
    }
  });
}