53 lines
1.5 KiB
Markdown
53 lines
1.5 KiB
Markdown
# cf-ip-geo (Cloudflare Workers)
|
||
|
||
一个基于 **Cloudflare Workers** 的轻量 HTTP API:返回访问者的 **公网 IP** 与 Cloudflare 边缘侧注入的 **地理/网络信息**(`request.cf`)。
|
||
|
||
> 公网 IP 优先取 `CF-Connecting-IP` 请求头;地理信息来自 `request.cf`(本地 dev 环境可能为空)。
|
||
|
||
## Endpoints
|
||
|
||
- `GET /`:首页文档(附带一个 `GET /api?pretty=1` 的实时示例)
|
||
- `GET /api`:返回 JSON(支持 `?pretty=1`)
|
||
- `GET /ipv4`:返回 JSON(仅允许 IPv4 连接;否则 400,支持 `?pretty=1`)
|
||
- `GET /ipv6`:返回 JSON(仅允许 IPv6 连接;否则 400,支持 `?pretty=1`)
|
||
|
||
> 所有 API 路径默认启用 CORS:`Access-Control-Allow-Origin: *`
|
||
|
||
## Examples
|
||
|
||
```bash
|
||
curl https://<your-domain>/api?pretty=1
|
||
curl -4 https://<your-domain>/ipv4?pretty=1
|
||
curl -6 https://<your-domain>/ipv6?pretty=1
|
||
```
|
||
|
||
> 说明:是否走 IPv4/IPv6 由客户端网络与 DNS 决定(浏览器常用 Happy Eyeballs 策略),服务端无法把一次 IPv4 请求强制切到 IPv6;命令行测试请使用 `curl -6`。
|
||
|
||
## Response (/api)
|
||
|
||
```json
|
||
{
|
||
"ip": "203.0.113.10",
|
||
"ipVersion": "ipv4",
|
||
"userAgent": "...",
|
||
"geo": {
|
||
"country": "CN",
|
||
"region": "Beijing",
|
||
"regionCode": "BJ",
|
||
"city": "Beijing",
|
||
"postalCode": "",
|
||
"timezone": "Asia/Shanghai",
|
||
"continent": "AS",
|
||
"latitude": "39.90",
|
||
"longitude": "116.40"
|
||
},
|
||
"network": {
|
||
"asn": 4134,
|
||
"asOrganization": "CHINANET",
|
||
"colo": "HKG",
|
||
"httpProtocol": "HTTP/2"
|
||
},
|
||
"ts": "2026-01-27T12:34:56.000Z"
|
||
}
|
||
```
|