每日签到奶昔超市点数市场奶昔访达
12下一页
返回列表 发布新帖
查看: 1536|回复: 38

用Cloudflare Workers搭建自己的IP查询API(ipdata)

发表于 2025-5-18 21:10:38 | 查看全部 |阅读模式

欢迎注册论坛,享受更多奶昔会员权益!

您需要 登录 才可以下载或查看,没有账号?注册

×
demo:https://full-starling-79.deno.dev/1.1.1.1 (IP 可选, 不填默认本机 IP)返回示例:
  1. {
  2.   "ip": "1.1.1.1",
  3.   "is_eu": null,
  4.   "city": null,
  5.   "region": null,
  6.   "region_code": null,
  7.   "region_type": null,
  8.   "country_name": null,
  9.   "country_code": null,
  10.   "continent_name": null,
  11.   "continent_code": null,
  12.   "latitude": null,
  13.   "longitude": null,
  14.   "postal": null,
  15.   "calling_code": null,
  16.   "flag": null,
  17.   "emoji_flag": null,
  18.   "emoji_unicode": null,
  19.   "asn": {
  20.     "asn": "AS13335",
  21.     "name": "Cloudflare Inc",
  22.     "domain": "cloudflare.com",
  23.     "route": "1.1.1.0/24",
  24.     "type": "business"
  25.   },
  26.   "company": {
  27.     "name": "Cloudflare Inc",
  28.     "domain": "cloudflare.com",
  29.     "network": "1.1.1.0/24",
  30.     "type": "business"
  31.   },
  32.   "languages": null,
  33.   "currency": {
  34.     "name": null,
  35.     "code": null,
  36.     "symbol": null,
  37.     "native": null,
  38.     "plural": null
  39.   },
  40.   "time_zone": {
  41.     "name": null,
  42.     "abbr": null,
  43.     "offset": null,
  44.     "is_dst": null,
  45.     "current_time": null
  46.   },
  47.   "threat": {
  48.     "is_tor": false,
  49.     "is_vpn": false,
  50.     "is_icloud_relay": false,
  51.     "is_proxy": false,
  52.     "is_datacenter": false,
  53.     "is_anonymous": false,
  54.     "is_known_attacker": false,
  55.     "is_known_abuser": false,
  56.     "is_threat": false,
  57.     "is_bogon": false,
  58.     "blocklists": [],
  59.     "scores": {
  60.       "vpn_score": 22,
  61.       "proxy_score": 12,
  62.       "threat_score": 71,
  63.       "trust_score": 65
  64.     }
  65.   }
  66. }
复制代码

源码:
  1. // deno-lint-ignore-file no-explicit-any
  2. import { serve } from "https://deno.land/std@0.224.0/http/server.ts";

  3. const IPDATA_API_KEY = "eca677b284b3bac29eb72f5e496aa9047f26543605efe99ff2ce35c9";

  4. async function fetchIpData(ip: string): Promise<any> {
  5.   const url = `https://api.ipdata.co/${ip}?api-key=${IPDATA_API_KEY}`;
  6.   const headers = {
  7.     'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:138.0) Gecko/20100101 Firefox/138.0',
  8.     'Accept': '*/*',
  9.     'Accept-Language': 'en-US,en;q=0.5',
  10.     'Accept-Encoding': 'gzip, deflate, br, zstd',
  11.     'Referer': 'https://ipdata.co/',
  12.     'Origin': 'https://ipdata.co',
  13.     'DNT': '1',
  14.     'Sec-GPC': '1',
  15.     'Connection': 'keep-alive',
  16.     'Sec-Fetch-Dest': 'empty',
  17.     'Sec-Fetch-Mode': 'cors',
  18.     'Sec-Fetch-Site': 'same-site',
  19.     'Priority': 'u=0',
  20.     'Pragma': 'no-cache',
  21.     'Cache-Control': 'no-cache',
  22.   };

  23.   try {
  24.     const response = await fetch(url, { headers });
  25.     if (!response.ok) {
  26.       return { error: `Error fetching data: ${response.status} ${response.statusText}` };
  27.     }
  28.     return await response.json();
  29.   } catch (error: any) {
  30.     return { error: `Fetch error: ${error.message}` };
  31.   }
  32. }

  33. serve(async (req, info) => {
  34.   const url = new URL(req.url);
  35.   const pathname = url.pathname;

  36.   if (pathname === "/") {
  37.     // 获取客户端IP
  38.     const clientIp = info.remoteAddr.hostname;
  39.     if (clientIp === '127.0.0.1' || clientIp === '::1') {
  40.       // 本地开发环境,可能无法获取真实IP
  41.       return new Response(JSON.stringify({ message: "Running on localhost, cannot determine client IP reliably." }), {
  42.         headers: { "content-type": "application/json" },
  43.         status: 200,
  44.       });
  45.     }
  46.     const ipData = await fetchIpData(clientIp);
  47.     return new Response(JSON.stringify(ipData, null, 2), {
  48.       headers: { "content-type": "application/json" },
  49.       status: ipData.error ? 500 : 200,
  50.     });
  51.   } else if (pathname.startsWith("/")) {
  52.     // 提取路径中的IP地址
  53.     const parts = pathname.split("/");
  54.     const ip = parts[1];

  55.     // 简单的IP地址格式验证 (可以根据需要加强)
  56.     if (!ip || !/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(ip)) {
  57.        return new Response(JSON.stringify({ error: "Invalid IP address format" }), {
  58.         headers: { "content-type": "application/json" },
  59.         status: 400,
  60.       });
  61.     }

  62.     const ipData = await fetchIpData(ip);
  63.     return new Response(JSON.stringify(ipData, null, 2), {
  64.       headers: { "content-type": "application/json" },
  65.       status: ipData.error ? 500 : 200,
  66.     });
  67.   } else {
  68.     return new Response("Not Found", { status: 404 });
  69.   }
  70. });
复制代码
爱生活,爱奶昔~
回复

使用道具 举报

发表于 2025-5-18 21:11:15 | 查看全部
总感觉 ipdata 这家不准。ipinfo 和 ip-api 都比他家的强
爱生活,爱奶昔~
发表于 2025-5-18 21:34:31 | 查看全部
thx up share.                                       
爱生活,爱奶昔~
发表于 2025-5-19 14:29:56 | 查看全部
看看怎么玩
爱生活,爱奶昔~
发表于 2025-5-19 17:35:03 | 查看全部
看看
爱生活,爱奶昔~
发表于 2025-5-19 17:56:08 | 查看全部
我来看看
爱生活,爱奶昔~
发表于 2025-5-20 18:12:47 | 查看全部
看看
爱生活,爱奶昔~
发表于 2025-5-20 18:40:14 来自手机 | 查看全部
看看
爱生活,爱奶昔~
发表于 2025-5-21 00:14:32 | 查看全部
看看,学习一下
爱生活,爱奶昔~
发表于 2025-5-25 08:08:31 来自手机 | 查看全部
学习一下
爱生活,爱奶昔~
发表于 2025-5-25 08:35:16 来自手机 | 查看全部
学习一下
爱生活,爱奶昔~
发表于 2025-5-26 10:21:36 | 查看全部
谢谢分享!!
爱生活,爱奶昔~
发表于 2025-5-27 21:38:25 | 查看全部
thx up share.
爱生活,爱奶昔~
发表于 2025-6-1 08:25:06 | 查看全部
giffgaff
爱生活,爱奶昔~
发表于 2025-6-1 08:47:20 | 查看全部
看看
爱生活,爱奶昔~
发表于 2025-6-1 11:43:34 | 查看全部
看看
爱生活,爱奶昔~
发表于 2025-6-9 08:02:00 来自手机 | 查看全部
看看隐藏
爱生活,爱奶昔~
发表于 2025-6-9 08:24:23 | 查看全部
进来学习一下新内容。
爱生活,爱奶昔~
发表于 2025-6-9 11:05:40 | 查看全部
看看
爱生活,爱奶昔~
发表于 2025-6-9 12:32:29 来自手机 | 查看全部
学习了
爱生活,爱奶昔~
您需要登录后才可以回帖 登录 | 注册

本版积分规则

© 2025 Nyarime 沪ICP备13020230号-1|沪公网安备 31010702007642号手机版小黑屋RSS
返回顶部 关灯 在本版发帖
快速回复 返回顶部 返回列表