Files
cf-ip-geo/README.md
2026-03-11 20:11:49 +08:00

53 lines
1.5 KiB
Markdown
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.
# 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"
}
```