📖 API 文档 ← 返回测试页面

基于 tcptest.cn 节点资源的批量 TCPing 测试服务。Base URL: /api

🔐 认证:所有 API(除 /api/health)必须携带 API Key,两种方式:
方式 1: 请求头  X-API-Key: your_key
方式 2: URL 参数 ?key=your_key

1. 获取节点列表

GET/api/nodes
获取全部可用探测节点(111 个,含名称/运营商/地区/类型)。响应会缓存 10 分钟。
GET /api/nodes

2. 同步批量测试

POST/api/tcping
提交测试并等待全部节点返回结果(适合程序调用)。节点数 × 目标数较多时耗时长,建议用流式接口。
POST /api/tcping
Content-Type: application/json

{
  "targets": "example.com:443\n1.1.1.1:443",   // 字符串或数组
  "nodes": "all",                               // "all"|"telecom"|"unicom"|"mobile"|"overseas"|节点key数组
  "port": 443,                                  // 默认端口(目标未带端口时生效)
  "timeoutMs": 120000                           // 可选,超时时间
}
nodes 取值说明
"all"全部 111 个节点
"telecom" / "unicom" / "mobile"电信 / 联通 / 移动
"overseas"港澳台 + 海外节点
[46, 399, ...]指定节点 key 数组

3. 流式测试(SSE,推荐)

POST/api/tcping/stream
SSE 流式返回,每收到一个节点结果实时推送一条 result 事件。请求参数与同步接口一致。
POST /api/tcping/stream
Accept: text/event-stream

事件流格式:
event: meta     → 测试元信息(目标数/节点列表)
event: status   → 连接状态
event: result   → 单个节点结果(含 _progress 进度)
event: done     → 全部完成
event: error    → 出错
JavaScript 调用示例:
const resp = await fetch('/api/tcping/stream', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({ targets: 'example.com:443', nodes: 'telecom', port: 443 })
});
const reader = resp.body.getReader();
// 逐块解析 SSE 事件...(前端页面即此实现)

4. 健康检查

GET/api/health
服务状态与当前并发数。
GET /api/health → {"status":"ok","activeTests":0}

curl 快速示例

# 测 3 个目标,全部节点(同步)
curl -s -X POST http://localhost:3000/api/tcping \
  -H 'Content-Type: application/json' \
  -H 'X-API-Key: your_key' \
  -d '{"targets":"example.com:443\n1.1.1.1","nodes":"all","port":443}'

# 只测电信节点,流式
curl -N -X POST http://localhost:3000/api/tcping/stream \
  -H 'Content-Type: application/json' \
  -H 'X-API-Key: your_key' \
  -d '{"targets":"example.com:443","nodes":"telecom","port":443}'