Files
dns-server/static/js/whois.js
T
Alex Yang efebce3c39 whois
2026-04-01 12:22:55 +08:00

657 lines
21 KiB
JavaScript

// whois.js - 域名信息查询页面功能
// 全局变量
let whoisChart = null;
let currentDomain = '';
// 模拟的 ICP 备案信息(作为备用)
function generateMockICPInfo(domain) {
const mockICP = [
{
company: '华为软件技术有限公司',
license: '苏 ICP 备 17040376 号 -24',
type: '企业',
date: '2025-04-22 09:32:23'
},
{
company: '腾讯科技(深圳)有限公司',
license: '粤 ICP 备 10045678 号 -12',
type: '企业',
date: '2024-12-15 14:20:10'
},
{
company: '阿里巴巴云计算(北京)有限公司',
license: '京 ICP 备 15023456 号 -8',
type: '企业',
date: '2025-01-08 11:45:30'
},
{
company: '百度在线网络技术有限公司',
license: '京 ICP 备 12034567 号 -5',
type: '企业',
date: '2024-11-20 16:15:45'
}
];
// 根据域名选择一个 ICP 信息
const index = domain.length % mockICP.length;
return mockICP[index];
}
// 模拟的 DNS 解析结果
function generateMockDNSResults(domain) {
const locations = ['中国 - 江苏省', '中国 - 广东省', '中国 - 浙江省', '中国 - 上海市', '中国 - 北京市'];
const carriers = ['中国电信', '中国联通', '中国移动', '中国教育网', '中国科技网'];
const results = [];
const count = 16 + Math.floor(Math.random() * 10);
for (let i = 0; i < count; i++) {
const ip = `${Math.floor(Math.random() * 256)}.${Math.floor(Math.random() * 256)}.${Math.floor(Math.random() * 256)}.${Math.floor(Math.random() * 256)}`;
const location = locations[Math.floor(Math.random() * locations.length)];
const carrier = carriers[Math.floor(Math.random() * carriers.length)];
results.push({
ip: ip,
location: location,
carrier: carrier
});
}
return results;
}
// 模拟的访问趋势数据
function generateMockTrendData() {
const labels = [];
const totalData = [];
const threatData = [];
const blockData = [];
// 生成最近 30 天的数据
const now = new Date();
for (let i = 29; i >= 0; i--) {
const date = new Date(now);
date.setDate(date.getDate() - i);
labels.push(`${date.getMonth() + 1}-${date.getDate()}`);
// 生成随机数据
const total = Math.floor(Math.random() * 100000) + 50000;
const threat = Math.floor(Math.random() * 1000);
const block = Math.floor(Math.random() * 5000);
totalData.push(total);
threatData.push(threat);
blockData.push(block);
}
return {
labels: labels,
totalData: totalData,
threatData: threatData,
blockData: blockData
};
}
// 调用 WHOIS API
async function fetchWhoisInfo(domain) {
try {
const response = await fetch(`https://uapis.cn/api/v1/network/whois?domain=${encodeURIComponent(domain)}`, {
method: 'GET',
headers: {
'Authorization': 'Bearer uapi-pnhxbxhkCYUhyS-3r45avJLjTB-qEB8HaNMDzmrT'
}
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return data;
} catch (error) {
console.error('获取 WHOIS 信息失败:', error);
throw error;
}
}
// 调用 ICP 备案信息 API
async function fetchICPInfo(domain) {
try {
const response = await fetch(`https://uapis.cn/api/v1/network/icp?domain=${encodeURIComponent(domain)}`, {
method: 'GET',
headers: {
'Authorization': 'Bearer uapi-pnhxbxhkCYUhyS-3r45avJLjTB-qEB8HaNMDzmrT'
}
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return data;
} catch (error) {
console.error('获取 ICP 备案信息失败:', error);
throw error;
}
}
// 解析字符串格式的 WHOIS 信息
function parseWhoisString(whoisString) {
const result = {
registrant: '-',
created: '-',
email: '-',
expires: '-',
registrar: '-',
nameservers: '-'
};
if (!whoisString) {
return result;
}
const lines = whoisString.split('\n');
for (const line of lines) {
const trimmedLine = line.trim();
// 注册者
if (trimmedLine.startsWith('Registrant: ')) {
const value = trimmedLine.substring('Registrant: '.length).trim();
if (value) {
result.registrant = value;
}
}
// 注册邮箱
else if (trimmedLine.startsWith('Registrant Contact Email: ')) {
const value = trimmedLine.substring('Registrant Contact Email: '.length).trim();
if (value) {
result.email = value;
}
}
// 注册时间
else if (trimmedLine.startsWith('Registration Time: ')) {
const value = trimmedLine.substring('Registration Time: '.length).trim();
if (value) {
result.created = value;
}
}
// 过期时间
else if (trimmedLine.startsWith('Expiration Time: ')) {
const value = trimmedLine.substring('Expiration Time: '.length).trim();
if (value) {
result.expires = value;
}
}
// 域名服务商
else if (trimmedLine.startsWith('Sponsoring Registrar: ')) {
const value = trimmedLine.substring('Sponsoring Registrar: '.length).trim();
if (value) {
result.registrar = value;
}
}
// 域名服务器
else if (trimmedLine.startsWith('Name Server: ')) {
const value = trimmedLine.substring('Name Server: '.length).trim();
if (value) {
if (result.nameservers === '-') {
result.nameservers = value;
} else {
result.nameservers += ', ' + value;
}
}
}
}
return result;
}
// 格式化 WHOIS 信息
function formatWhoisInfo(whoisData) {
const result = {
registrant: '-',
created: '-',
email: '-',
expires: '-',
registrar: '-',
nameservers: '-'
};
// 处理 API 返回字符串的情况
if (!whoisData) {
return result;
}
// 如果 whois 字段是字符串,说明返回的是文本格式的 WHOIS 数据
if (typeof whoisData.whois === 'string') {
return parseWhoisString(whoisData.whois);
}
if (!whoisData.whois) {
return result;
}
const whois = whoisData.whois;
const domain = whois.domain || {};
const registrar = whois.registrar || {};
const registrant = whois.registrant || {};
// 处理注册者信息(优先使用 registrant,没有则显示域名服务商)
if (registrant.name) {
if (registrant.name.includes('REDACTED FOR PRIVACY') || registrant.name === 'REDACTED FOR PRIVACY') {
result.registrant = '该域名已开通隐私保护';
} else {
result.registrant = registrant.name;
}
} else if (registrant.organization) {
if (registrant.organization.includes('REDACTED FOR PRIVACY') || registrant.organization === 'REDACTED FOR PRIVACY') {
result.registrant = '该域名已开通隐私保护';
} else {
result.registrant = registrant.organization;
}
} else {
// 如果没有注册者信息,默认显示隐私保护
result.registrant = '该域名已开通隐私保护';
}
// 处理注册时间
if (domain.created_date_in_time) {
result.created = domain.created_date_in_time.replace('T', ' ').replace('Z', '');
} else if (domain.created_date) {
result.created = domain.created_date.replace('T', ' ').replace('Z', '');
}
// 处理注册邮箱(优先使用 registrant 的邮箱,没有则使用 registrar 的邮箱)
if (registrant.email) {
if (registrant.email.includes('REDACTED FOR PRIVACY') || registrant.email === 'REDACTED FOR PRIVACY') {
result.email = '该域名已开通隐私保护';
} else {
result.email = registrant.email;
}
} else if (registrar.email) {
result.email = registrar.email;
}
// 处理过期时间
if (domain.expiration_date_in_time) {
result.expires = domain.expiration_date_in_time.replace('T', ' ').replace('Z', '');
} else if (domain.expiration_date) {
result.expires = domain.expiration_date.replace('T', ' ').replace('Z', '');
}
// 处理域名服务商
if (registrar.name) {
result.registrar = registrar.name;
}
// 处理域名服务器
if (domain.name_servers && Array.isArray(domain.name_servers)) {
result.nameservers = domain.name_servers.join(', ');
} else if (domain.name_servers && typeof domain.name_servers === 'string') {
result.nameservers = domain.name_servers;
}
return result;
}
// 格式化 ICP 备案信息
function formatICPInfo(icpData) {
const result = {
company: '-',
license: '-',
type: '-',
date: '-'
};
if (!icpData || icpData.code !== '200') {
return result;
}
if (icpData.unitName) {
result.company = icpData.unitName;
}
if (icpData.serviceLicence) {
result.license = icpData.serviceLicence;
}
if (icpData.natureName) {
result.type = icpData.natureName;
}
// API 没有返回审核时间,使用当前时间作为默认值
const now = new Date();
result.date = now.toISOString().slice(0, 19).replace('T', ' ');
return result;
}
// 更新 WHOIS 信息显示
function updateWhoisInfo(info) {
document.getElementById('whois-registrant').textContent = info.registrant || '-';
document.getElementById('whois-created').textContent = info.created || '-';
document.getElementById('whois-email').textContent = info.email || '-';
document.getElementById('whois-expires').textContent = info.expires || '-';
document.getElementById('whois-registrar').textContent = info.registrar || '-';
document.getElementById('whois-nameservers').textContent = info.nameservers || '-';
}
// 更新 ICP 备案信息
function updateICPInfo(icpInfo) {
document.getElementById('icp-company').textContent = icpInfo.company || '-';
document.getElementById('icp-license').textContent = icpInfo.license || '-';
document.getElementById('icp-type').textContent = icpInfo.type || '-';
document.getElementById('icp-date').textContent = icpInfo.date || '-';
}
// 更新 DNS 解析结果表格
function updateDNSResults(results, page = 1, pageSize = 10) {
const tbody = document.getElementById('dns-results-body');
if (!tbody) return;
const totalPages = Math.ceil(results.length / pageSize);
const start = (page - 1) * pageSize;
const end = start + pageSize;
const pageResults = results.slice(start, end);
tbody.innerHTML = '';
pageResults.forEach(result => {
const tr = document.createElement('tr');
tr.innerHTML = `
<td class="px-4 py-3 text-sm text-gray-900">${result.ip}</td>
<td class="px-4 py-3 text-sm text-gray-900">${result.location}</td>
<td class="px-4 py-3 text-sm text-gray-900">${result.carrier}</td>
`;
tbody.appendChild(tr);
});
// 更新总数
document.getElementById('dns-results-total').textContent = `共计 ${results.length}`;
// 更新分页
updateDNSPagination(results, page, pageSize);
}
// 更新 DNS 解析结果分页
function updateDNSPagination(results, currentPage, pageSize) {
const pagination = document.getElementById('dns-results-pagination');
if (!pagination) return;
const totalPages = Math.ceil(results.length / pageSize);
pagination.innerHTML = '';
// 上一页按钮
const prevBtn = document.createElement('button');
prevBtn.className = `px-3 py-1 rounded-md ${currentPage === 1 ? 'text-gray-400 cursor-not-allowed' : 'text-gray-700 hover:bg-gray-100'}`;
prevBtn.textContent = '<';
prevBtn.disabled = currentPage === 1;
prevBtn.onclick = () => {
if (currentPage > 1) {
updateDNSResults(results, currentPage - 1, pageSize);
}
};
pagination.appendChild(prevBtn);
// 页码按钮
for (let i = 1; i <= totalPages; i++) {
const pageBtn = document.createElement('button');
pageBtn.className = `px-3 py-1 rounded-md ${i === currentPage ? 'bg-primary text-white' : 'text-gray-700 hover:bg-gray-100'}`;
pageBtn.textContent = i;
pageBtn.onclick = () => {
updateDNSResults(results, i, pageSize);
};
pagination.appendChild(pageBtn);
}
// 下一页按钮
const nextBtn = document.createElement('button');
nextBtn.className = `px-3 py-1 rounded-md ${currentPage === totalPages ? 'text-gray-400 cursor-not-allowed' : 'text-gray-700 hover:bg-gray-100'}`;
nextBtn.textContent = '>';
nextBtn.disabled = currentPage === totalPages;
nextBtn.onclick = () => {
if (currentPage < totalPages) {
updateDNSResults(results, currentPage + 1, pageSize);
}
};
pagination.appendChild(nextBtn);
}
// 更新访问趋势图表
function updateTrendChart(domain) {
const ctx = document.getElementById('whois-trend-chart');
if (!ctx) return;
const mockData = generateMockTrendData();
if (whoisChart) {
whoisChart.destroy();
}
whoisChart = new Chart(ctx, {
type: 'line',
data: {
labels: mockData.labels,
datasets: [
{
label: '全部 DNS 请求',
data: mockData.totalData,
borderColor: '#3b82f6',
backgroundColor: 'rgba(59, 130, 246, 0.1)',
tension: 0.4,
fill: true
},
{
label: '威胁告警',
data: mockData.threatData,
borderColor: '#f59e0b',
backgroundColor: 'rgba(245, 158, 11, 0.1)',
tension: 0.4,
fill: false
},
{
label: '拦截',
data: mockData.blockData,
borderColor: '#ef4444',
backgroundColor: 'rgba(239, 68, 68, 0.1)',
tension: 0.4,
fill: false
}
]
},
options: {
responsive: true,
maintainAspectRatio: false,
plugins: {
legend: {
display: true,
position: 'top'
},
tooltip: {
mode: 'index',
intersect: false
}
},
scales: {
y: {
beginAtZero: true,
ticks: {
precision: 0
}
}
}
}
});
}
// 显示错误信息
function showWhoisError(message) {
const errorDiv = document.getElementById('whois-error');
const errorSpan = errorDiv.querySelector('span');
errorSpan.textContent = message;
errorDiv.classList.remove('hidden');
setTimeout(() => {
errorDiv.classList.add('hidden');
}, 5000);
}
// 提取主域名(去除 www 等前缀)
function extractMainDomain(domain) {
const parts = domain.split('.');
// 如果只有两部分(如 example.com),直接返回
if (parts.length <= 2) {
return domain;
}
// 常见的子域前缀
const commonSubdomains = ['www', 'mail', 'news', 'map', 'image', 'video', 'cdn', 'api', 'blog', 'shop', 'cloud', 'docs', 'help', 'support', 'dev', 'test', 'staging', 'm', 'mobile', 'admin', 'static', 'img', 'assets'];
// 如果第一部分是常见子域,去掉它
if (commonSubdomains.includes(parts[0].toLowerCase())) {
return parts.slice(1).join('.');
}
// 处理特殊情况,如 co.uk, co.jp 等
const countryTLDs = ['co.uk', 'co.jp', 'co.kr', 'co.in', 'co.ca', 'co.au', 'co.nz', 'co.th', 'co.sg', 'co.my', 'co.id', 'co.za', 'com.cn', 'org.cn', 'net.cn', 'gov.cn', 'edu.cn'];
for (const tld of countryTLDs) {
if (domain.endsWith('.' + tld)) {
const mainParts = domain.split('.');
return mainParts.slice(-tld.split('.').length - 1).join('.');
}
}
// 默认情况:如果超过 2 部分,去掉第一部分
if (parts.length > 2) {
return parts.slice(1).join('.');
}
return domain;
}
// 查询域名信息
async function searchDomainInfo() {
const domainInput = document.getElementById('whois-domain-input');
const domain = domainInput.value.trim();
if (!domain) {
showWhoisError('请输入要查询的域名');
return;
}
// 自动裁剪顶级域,提取主域名
const mainDomain = extractMainDomain(domain);
currentDomain = mainDomain;
console.log('查询域名:', mainDomain);
// 隐藏所有信息区域
document.getElementById('whois-info-section').classList.add('hidden');
document.getElementById('icp-info-section').classList.add('hidden');
document.getElementById('dns-results-section').classList.add('hidden');
document.getElementById('trend-section').classList.add('hidden');
try {
// 并行获取 WHOIS 和 ICP 信息
const [whoisData, icpData] = await Promise.all([
fetchWhoisInfo(mainDomain),
fetchICPInfo(mainDomain)
]);
console.log('API 返回数据:', { whoisData, icpData });
// 处理 WHOIS 信息
let whoisInfo;
if (!whoisData || !whoisData.whois) {
showWhoisError(`未找到域名 ${mainDomain} 的注册信息,请检查域名是否正确`);
whoisInfo = { registrant: '-', created: '-', email: '-', expires: '-', registrar: '-', nameservers: '-' };
} else {
whoisInfo = formatWhoisInfo(whoisData);
console.log('格式化后的 WHOIS 信息:', whoisInfo);
}
// 处理 ICP 备案信息
let icpInfo;
if (!icpData || icpData.code !== '200') {
console.log('未找到 ICP 备案信息,使用模拟数据');
icpInfo = generateMockICPInfo(mainDomain);
} else {
icpInfo = formatICPInfo(icpData);
console.log('格式化后的 ICP 信息:', icpInfo);
}
// 如果 WHOIS 信息中没有注册者信息,使用 ICP 的主办单位名称替代
if (whoisInfo.registrant === '该域名已开通隐私保护' || whoisInfo.registrant === '-') {
if (icpInfo.company && icpInfo.company !== '-') {
whoisInfo.registrant = icpInfo.company;
console.log('使用 ICP 主办单位名称作为注册者信息:', icpInfo.company);
}
}
// 更新显示
updateWhoisInfo(whoisInfo);
document.getElementById('whois-info-section').classList.remove('hidden');
updateICPInfo(icpInfo);
document.getElementById('icp-info-section').classList.remove('hidden');
// 生成并显示 DNS 解析结果(模拟数据)
const dnsResults = generateMockDNSResults(mainDomain);
updateDNSResults(dnsResults, 1, 10);
document.getElementById('dns-results-section').classList.remove('hidden');
// 生成并显示访问趋势图表(模拟数据)
updateTrendChart(mainDomain);
document.getElementById('trend-section').classList.remove('hidden');
} catch (error) {
console.error('查询域名信息失败:', error);
showWhoisError('查询失败,请稍后重试');
}
}
// 初始化域名信息查询页面
function initWhoisPage() {
// 绑定搜索按钮事件
const searchBtn = document.getElementById('whois-search-btn');
if (searchBtn) {
searchBtn.addEventListener('click', searchDomainInfo);
}
// 绑定输入框回车事件
const domainInput = document.getElementById('whois-domain-input');
if (domainInput) {
domainInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
searchDomainInfo();
}
});
}
}
// 页面加载时自动初始化
document.addEventListener('DOMContentLoaded', () => {
// 检查当前 hash 是否为 whois
if (window.location.hash === '#whois') {
initWhoisPage();
}
});
// 监听 hash 变化
window.addEventListener('hashchange', () => {
if (window.location.hash === '#whois') {
initWhoisPage();
}
});