Skip to content

Commit 00de4f5

Browse files
committed
优化 whois 接口
1 parent 6eb5a39 commit 00de4f5

3 files changed

Lines changed: 55 additions & 38 deletions

File tree

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
### 🤖 **WHOIS 智能填充**
6767
- 🔍 一键查询域名信息
6868
- ⚡ 3秒自动填充全部字段
69-
- 🌐 支持 RDAP + WHOIS 双协议
69+
- 🌐 支持全球1000+域名后缀
7070
- 📊 95%+ 查询成功率
7171
- 💾 智能缓存避免重复查询
7272

@@ -469,7 +469,8 @@ git push origin main
469469
- `WHOIS_API_ID` = 你的 WHOIS API ID
470470
- `WHOIS_API_KEY` = 你的 WHOIS API 密钥
471471

472-
> 💡 WHOIS API 获取:访问 [apihz.cn](https://apihz.cn/) 注册并获取凭证
472+
> 💡 WHOIS API 获取:访问 [apihz.cn](https://apihz.cn/) 注册并获取凭证
473+
> 📖 接口文档:[全球域名WHOIS查询](https://apihz.cn/api/wangzhanwhoisall.html) - 支持1000+域名后缀
473474
474475
5. **重新部署**
475476
- Deployments → Redeploy
@@ -530,9 +531,9 @@ git push origin main
530531
<summary><b>Q: WHOIS 查询准确吗?</b></summary>
531532

532533
**A**: 非常准确!
533-
-使用成熟的 WHOIS/RDAP 协议
534-
- ✅ 95%+ 常见域名查询成功率
535-
-支持 .com/.net/.org/.cn 等主流 TLD
534+
-使用 apihz.cn 全球域名 WHOIS API
535+
- ✅ 95%+ 域名查询成功率
536+
-支持全球1000+域名后缀(.com/.net/.org/.cn/.io/.dev/.ai 等)
536537
- ✅ 查询结果可以手动修改
537538

538539
</details>
@@ -623,7 +624,7 @@ MIT License © 2025 [MaydayV](https://github.com/MaydayV)
623624
### 🤖 **Intelligent WHOIS Auto-Fill**
624625
- 🔍 One-click domain info query
625626
- ⚡ Auto-fill in 3 seconds
626-
- 🌐 RDAP + WHOIS dual protocols
627+
- 🌐 Support 1000+ domain suffixes globally
627628
- 📊 95%+ success rate
628629
- 💾 Smart caching
629630

app/api/whois/route.ts

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { NextRequest, NextResponse } from 'next/server';
22
import { getSessionFromHeader, validateSession } from '@/lib/auth';
33

4-
// WHOIS 查询 API - 使用 apihz.cn 格式化接口实现
4+
// WHOIS 查询 API - 使用 apihz.cn whoisall 接口实现(支持全球1000+域名后缀)
55
export async function GET(request: NextRequest) {
66
const authHeader = request.headers.get('authorization');
77
const session = getSessionFromHeader(authHeader);
@@ -48,7 +48,7 @@ export async function GET(request: NextRequest) {
4848
console.log(`🔍 Querying WHOIS for: ${domain}`);
4949
const startTime = Date.now();
5050

51-
// 使用 apihz.cn WHOIS API(支持顶级域名格式化查询
51+
// 使用 apihz.cn WHOIS API(支持全球1000+域名后缀查询
5252
const apiId = process.env.WHOIS_API_ID;
5353
const apiKey = process.env.WHOIS_API_KEY;
5454

@@ -57,7 +57,7 @@ export async function GET(request: NextRequest) {
5757
}
5858

5959
// 先尝试获取最优接口地址
60-
let apiEndpoint = 'https://cn.apihz.cn/api/wangzhan/whois.php';
60+
let apiEndpoint = 'https://cn.apihz.cn/api/wangzhan/whoisall.php';
6161

6262
try {
6363
const optimalResponse = await fetch('https://api.apihz.cn/getapi.php', {
@@ -66,15 +66,17 @@ export async function GET(request: NextRequest) {
6666
if (optimalResponse.ok) {
6767
const optimalUrl = await optimalResponse.text();
6868
if (optimalUrl && optimalUrl.startsWith('http')) {
69-
apiEndpoint = optimalUrl.trim();
69+
// 替换接口路径为 whoisall.php
70+
apiEndpoint = optimalUrl.trim().replace('/whois.php', '/whoisall.php');
7071
console.log(`📡 Using optimal endpoint: ${apiEndpoint}`);
7172
}
7273
}
7374
} catch (err) {
7475
console.log('⚠️ Could not fetch optimal endpoint, using default domain endpoint');
7576
}
7677

77-
const whoisUrl = `${apiEndpoint}?id=${apiId}&key=${apiKey}&domain=${domain}`;
78+
// type=1 表示优先使用缓存,type=2 直接查询官方
79+
const whoisUrl = `${apiEndpoint}?id=${apiId}&key=${apiKey}&domain=${domain}&type=1`;
7880
const whoisResponse = await fetch(whoisUrl, {
7981
headers: {
8082
'User-Agent': 'DomainManagement/1.0',
@@ -95,33 +97,23 @@ export async function GET(request: NextRequest) {
9597
throw new Error(whoisData.msg || 'WHOIS lookup failed');
9698
}
9799

98-
console.log(`✅ WHOIS query completed in ${queryTime}s using apihz.cn`);
100+
console.log(`✅ WHOIS query completed in ${queryTime}s using apihz.cn (whoisall)`);
99101

100-
// 解析API返回的数据
101-
const registrarId = mapRegistrarToId(whoisData.zcname || '');
102+
// 解析原始 WHOIS 文本数据
103+
const whoisText = whoisData.whois || '';
104+
const parsedData = parseWhoisText(whoisText);
102105

103-
// 收集所有非空的 nameservers
104-
const nameServers = [];
105-
for (let i = 1; i <= 7; i++) {
106-
const ns = whoisData[`ns${i}`];
107-
if (ns) {
108-
nameServers.push(ns.toLowerCase());
109-
}
110-
}
106+
const registrarId = mapRegistrarToId(parsedData.registrar || '');
111107

112108
const responseData = {
113-
domain: whoisData.domain || domain.toUpperCase(),
109+
domain: parsedData.domain || domain.toUpperCase(),
114110
registrar: registrarId,
115-
registrationDate: whoisData.addtime || null,
116-
expiryDate: whoisData.endtime || null,
117-
registrarName: whoisData.zcname || '',
118-
nameServers: nameServers,
111+
registrationDate: parsedData.creationDate || null,
112+
expiryDate: parsedData.expirationDate || null,
113+
registrarName: parsedData.registrar || '',
114+
nameServers: parsedData.nameServers || [],
119115
queryTime,
120-
source: 'apihz.cn',
121-
// 额外信息
122-
handle: whoisData.handle,
123-
status: whoisData.status,
124-
dnssec: whoisData.dnssec,
116+
source: 'apihz.cn-whoisall',
125117
};
126118

127119
// 缓存结果
@@ -172,46 +164,65 @@ export async function GET(request: NextRequest) {
172164
}
173165

174166

175-
// 解析 WHOIS 原始文本
167+
// 解析 WHOIS 原始文本(支持全球多种格式)
176168
function parseWhoisText(whoisText: string) {
177169
const lines = whoisText.split('\n');
178170
const data: any = {};
179171

180-
// 提取关键信息的正则表达式
172+
// 提取关键信息的正则表达式(扩展支持更多格式)
181173
const patterns = {
174+
domain: [
175+
/Domain Name:\s*(.+)/i,
176+
/:\s*(.+)/i,
177+
],
182178
registrar: [
183179
/Registrar:\s*(.+)/i,
184180
/Sponsoring Registrar:\s*(.+)/i,
185181
/Registrar Name:\s*(.+)/i,
186182
/:\s*(.+)/i,
183+
/:\s*(.+)/i,
187184
],
188185
creationDate: [
189186
/Creation Date:\s*(.+)/i,
190187
/Created On:\s*(.+)/i,
191188
/Created:\s*(.+)/i,
192189
/Registration Time:\s*(.+)/i,
190+
/Registration Date:\s*(.+)/i,
193191
/:\s*(.+)/i,
194192
/:\s*(.+)/i,
195193
],
196194
expirationDate: [
197195
/Registry Expiry Date:\s*(.+)/i,
196+
/Registrar Registration Expiration Date:\s*(.+)/i,
198197
/Expiration Date:\s*(.+)/i,
198+
/Expiration Time:\s*(.+)/i,
199199
/Expires On:\s*(.+)/i,
200200
/Expiry Date:\s*(.+)/i,
201+
/Expire Date:\s*(.+)/i,
201202
/:\s*(.+)/i,
202203
/:\s*(.+)/i,
203204
],
204205
nameServers: [
205206
/Name Server:\s*(.+)/i,
206207
/nserver:\s*(.+)/i,
207208
/DNS:\s*(.+)/i,
209+
/Nameserver:\s*(.+)/i,
208210
],
209211
};
210212

211213
// 解析每一行
212214
for (const line of lines) {
213215
const trimmedLine = line.trim();
214-
if (!trimmedLine) continue;
216+
if (!trimmedLine || trimmedLine.startsWith('%') || trimmedLine.startsWith('#')) continue;
217+
218+
// 域名
219+
for (const pattern of patterns.domain) {
220+
const match = trimmedLine.match(pattern);
221+
if (match && !data.domain) {
222+
data.domain = match[1].trim().toUpperCase();
223+
break;
224+
}
225+
}
215226

216227
// 注册商
217228
for (const pattern of patterns.registrar) {
@@ -253,7 +264,11 @@ function parseWhoisText(whoisText: string) {
253264
const match = trimmedLine.match(pattern);
254265
if (match) {
255266
if (!data.nameServers) data.nameServers = [];
256-
data.nameServers.push(match[1].trim().toLowerCase());
267+
const ns = match[1].trim().toLowerCase();
268+
// 去重
269+
if (!data.nameServers.includes(ns)) {
270+
data.nameServers.push(ns);
271+
}
257272
}
258273
}
259274
}

env.example

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ ACCESS_PASSWORD=your_password_here
1414
# -----------------------------------------------------------------------------
1515
# 🔍 WHOIS API 配置 | WHOIS API Configuration (必填 Required)
1616
# -----------------------------------------------------------------------------
17-
# apihz.cn WHOIS API 凭证
17+
# apihz.cn WHOIS API 凭证 - 使用 whoisall 接口
1818
# 注册地址: https://apihz.cn/
19-
# 文档地址: https://apihz.cn/api/wangzhanwhois.html
19+
# 文档地址: https://apihz.cn/api/wangzhanwhoisall.html
20+
# 支持全球1000+域名后缀(.com/.net/.org/.cn/.io/.dev 等)
2021

2122
# 开发者 ID (在用户中心查看)
2223
# Developer ID (check in user center)

0 commit comments

Comments
 (0)