Files
Pake/bin/utils/ip_addr.ts

35 lines
961 B
TypeScript
Vendored
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { exec } from 'child_process';
import { promisify } from 'util';
import dns from 'dns';
const resolve = promisify(dns.resolve);
async function isChinaDomain(domain: string): Promise<boolean> {
try {
// 解析域名为IP地址
const [ip] = await resolve(domain);
return await isChinaIP(ip);
} catch (error) {
// 域名无法解析返回false
return false;
}
}
async function isChinaIP(ip: string): Promise<boolean> {
return new Promise((resolve, reject) => {
exec(`ping -c 1 -w 1 ${ip}`, (error, stdout, stderr) => {
if (error) {
// 命令执行出错返回false
resolve(false);
} else {
// 解析输出信息,提取延迟值
const match = stdout.match(/time=(\d+\.\d+) ms/);
const latency = match ? parseFloat(match[1]) : 0;
// 判断延迟是否超过100ms
resolve(latency > 100);
}
});
});
}
export { isChinaDomain, isChinaIP };