大家好,我是新来的,今天分享一个基于Cloudflare自带的IP地理位置库,实现类似ip.sb
功能的脚本。
功能:显示IP地址、定位、ASN信息、你的User Agent和CF数据中心;
声明:借鉴了ip.sb
与ip.p3terx.com
测试: 因害怕被刷不提供测试
先上效果:
curl:
root@OVH:~# curl ip.YOUR_DOMAIN2607:5300:60:x:x:x:791e:a3bf # IP地址Beauharnois, Quebec, CA # 定位AS16276 / OVH Hosting # 运营商curl/7.88.1 # 你的User AgentYUL # CF 数据中心root@OVH:~# curl ip.YOUR_DOMAIN -4192.99.x.yL**al, Quebec, CAAS16276 / OVHcloudcurl/7.88.1YYZ
使用方法:在Workers & Pages中新建一个"Hello world"的Workers,然后复制粘贴进去替换即可.
上代码:
/** * Welcome to Cloudflare Workers! This is your first worker. * * - Run "npm run dev" in your terminal to start a development server * - Open a browser tab at http://localhost:8787/ to see your worker in action * - Run "npm run deploy" to publish your worker * * Learn more at https://developers.cloudflare.com/workers/ */addEventListener('fetch', event => { event.respondWith(handleRequest(event.request));});async function handleRequest(request) { const url = new URL(request.url); if (url.pathname === '/' || url.pathname === '/index.html') { const { continent,//洲 asn, country, tlsVersion, city, timezone,// Asia/Shanghai colo,// LHR 节点 region,// 省份 httpProtocol,// HTTP/3 regionCode,//GD asOrganization,// China Telecom } = request.cf const ip = request.headers.get("cf-connecting-ip") const ua = request.headers.get("user-agent") const addr = [city, region, country] .filter((value) => value !== null && value !== undefined && value !== "") .join(", "); const $body = `${ip}\n${addr}\nAS${asn} / ${asOrganization}\n\n${ua}\n${colo}\n` return new Response($body, { status: 200, headers: { "Content-Type": "text/plain;charset=UTF-8" }, }) } else if (url.pathname.match('/myipv4addr')) { const $body = `var ipv4addr= document.getElementById("ipv4addr"); ipv4addr.innerHTML='IP: ${request.headers.get("cf-connecting-ip")} via ${request.cf.colo}';\n` return new Response($body, { status: 200, headers: { "Content-Type": "text/plain;charset=UTF-8" }, }) } else if (url.pathname.match('/mycfedge')) { const $body = `var cfedge= document.getElementById("cfedge"); ipv4addr.innerHTML='${request.cf.colo}';\n` return new Response($body, { status: 200, headers: { "Content-Type": "text/plain;charset=UTF-8" }, }) } else if (url.pathname.match('/asn')) { return new Response(request.cf.asn, { status: 200, headers: { "Content-Type": "text/plain;charset=UTF-8" }, }) } else if (url.pathname.match('/colo')) { return new Response(request.cf.colo, { status: 200, headers: { "Content-Type": "text/plain;charset=UTF-8" }, }) } else if (url.pathname.match('/ip')) { return new Response(request.headers.get('CF-Connecting-IP'), { status: 200, headers: { "Content-Type": "text/plain;charset=UTF-8" }, }) } else if (url.pathname.match('/generate_204')) { return new Response(null, { status: 204 }) } else { return new Response('Not found', { status: 404 }); }}
欢迎使用,有问题请提出。