|
|
欢迎注册论坛,享受更多奶昔会员权益!
您需要 登录 才可以下载或查看,没有账号?注册
×
demo:https://full-starling-79.deno.dev/1.1.1.1 (IP 可选, 不填默认本机 IP)返回示例:
- {
- "ip": "1.1.1.1",
- "is_eu": null,
- "city": null,
- "region": null,
- "region_code": null,
- "region_type": null,
- "country_name": null,
- "country_code": null,
- "continent_name": null,
- "continent_code": null,
- "latitude": null,
- "longitude": null,
- "postal": null,
- "calling_code": null,
- "flag": null,
- "emoji_flag": null,
- "emoji_unicode": null,
- "asn": {
- "asn": "AS13335",
- "name": "Cloudflare Inc",
- "domain": "cloudflare.com",
- "route": "1.1.1.0/24",
- "type": "business"
- },
- "company": {
- "name": "Cloudflare Inc",
- "domain": "cloudflare.com",
- "network": "1.1.1.0/24",
- "type": "business"
- },
- "languages": null,
- "currency": {
- "name": null,
- "code": null,
- "symbol": null,
- "native": null,
- "plural": null
- },
- "time_zone": {
- "name": null,
- "abbr": null,
- "offset": null,
- "is_dst": null,
- "current_time": null
- },
- "threat": {
- "is_tor": false,
- "is_vpn": false,
- "is_icloud_relay": false,
- "is_proxy": false,
- "is_datacenter": false,
- "is_anonymous": false,
- "is_known_attacker": false,
- "is_known_abuser": false,
- "is_threat": false,
- "is_bogon": false,
- "blocklists": [],
- "scores": {
- "vpn_score": 22,
- "proxy_score": 12,
- "threat_score": 71,
- "trust_score": 65
- }
- }
- }
复制代码
源码:
- // deno-lint-ignore-file no-explicit-any
- import { serve } from "https://deno.land/std@0.224.0/http/server.ts";
- const IPDATA_API_KEY = "eca677b284b3bac29eb72f5e496aa9047f26543605efe99ff2ce35c9";
- async function fetchIpData(ip: string): Promise<any> {
- const url = `https://api.ipdata.co/${ip}?api-key=${IPDATA_API_KEY}`;
- const headers = {
- 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:138.0) Gecko/20100101 Firefox/138.0',
- 'Accept': '*/*',
- 'Accept-Language': 'en-US,en;q=0.5',
- 'Accept-Encoding': 'gzip, deflate, br, zstd',
- 'Referer': 'https://ipdata.co/',
- 'Origin': 'https://ipdata.co',
- 'DNT': '1',
- 'Sec-GPC': '1',
- 'Connection': 'keep-alive',
- 'Sec-Fetch-Dest': 'empty',
- 'Sec-Fetch-Mode': 'cors',
- 'Sec-Fetch-Site': 'same-site',
- 'Priority': 'u=0',
- 'Pragma': 'no-cache',
- 'Cache-Control': 'no-cache',
- };
- try {
- const response = await fetch(url, { headers });
- if (!response.ok) {
- return { error: `Error fetching data: ${response.status} ${response.statusText}` };
- }
- return await response.json();
- } catch (error: any) {
- return { error: `Fetch error: ${error.message}` };
- }
- }
- serve(async (req, info) => {
- const url = new URL(req.url);
- const pathname = url.pathname;
- if (pathname === "/") {
- // 获取客户端IP
- const clientIp = info.remoteAddr.hostname;
- if (clientIp === '127.0.0.1' || clientIp === '::1') {
- // 本地开发环境,可能无法获取真实IP
- return new Response(JSON.stringify({ message: "Running on localhost, cannot determine client IP reliably." }), {
- headers: { "content-type": "application/json" },
- status: 200,
- });
- }
- const ipData = await fetchIpData(clientIp);
- return new Response(JSON.stringify(ipData, null, 2), {
- headers: { "content-type": "application/json" },
- status: ipData.error ? 500 : 200,
- });
- } else if (pathname.startsWith("/")) {
- // 提取路径中的IP地址
- const parts = pathname.split("/");
- const ip = parts[1];
- // 简单的IP地址格式验证 (可以根据需要加强)
- if (!ip || !/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(ip)) {
- return new Response(JSON.stringify({ error: "Invalid IP address format" }), {
- headers: { "content-type": "application/json" },
- status: 400,
- });
- }
- const ipData = await fetchIpData(ip);
- return new Response(JSON.stringify(ipData, null, 2), {
- headers: { "content-type": "application/json" },
- status: ipData.error ? 500 : 200,
- });
- } else {
- return new Response("Not Found", { status: 404 });
- }
- });
复制代码 |
|