// To be deployed to Cloudflare Workers // https://workers.cloudflare.com/ const PEER_URL = "https://service-6memx4r1-1251114981.sh.apigw.tencentcs.com/release/water-meter"; addEventListener("fetch", (event) => { event.respondWith( handleRequest(event.request).catch( (err) => new Response(err.stack, { status: 500 }) ) ); }); /** * Many more examples available at: * https://developers.cloudflare.com/workers/examples * @param {Request} request * @returns {Promise} */ async function handleRequest(request) { const { pathname, origin } = new URL(request.url); switch (pathname) { case '/callback': return handleCallback(request); case '/': return new Response(`Usage: curl -L4 ${origin} (If you seen this, it indicates your client does not follow HTTP redirections.)` , { status: 302, headers: { Location: PEER_URL } }); default: return new Response(`Usage: curl -L4 ${origin}`); } } async function handleCallback(request) { const { searchParams: params } = new URL(request.url); const intraIp = params.get("ip"); console.log(new URL(request.url)) if (!intraIp) { return new Response("Wrong callback parameter" + request.url + intraIp, { status: 400 }); } // if (!request.headers.get("Referer").includes((new URL(PEER_URL).host))) { // return new Response("Invalid referer", { status: 400 }) // } const interIp = request.headers.get("CF-Connecting-IP"); let message; if (intraIp === interIp) { const geo = await getGeo(interIp); const notice = geo.country === "China" ? "你正在境内正常访问互联网。" : "现已识别到你属于境外势力。"; message = `${callpolice(3)}${notice}${callpolice(3)} 你的IP地址是:${interIp}(${geo.organization})。`; } else { const intraGeo = await getGeo(intraIp); const interGeo = await getGeo(interIp); if (intraGeo.country !== "China" || interGeo.country === "China") { return new Response("Unexpected network environment", { status: 400 }); } message = `${callpolice(24)} 🚨🚨你正在使用非法定信道访问国际互联网!!!🚨🚨 ${callpolice(24)} 你的境内IP地址是:${intraIp}(${intraGeo.organization}), 你的境外IP地址是:${interIp}(${interGeo.organization})。 ${callpolice(24)} `; } return new Response(message); } async function getGeo(ip) { const resp = await fetch("https://api.ip.sb/geoip/" + ip); return await resp.json() } function callpolice(n, symbols = "🚔👮🚓🚨") { symbols = [...symbols]; // Inspired by https://stackoverflow.com/a/68379909/5488616 return Array.from(Array(n).keys()).map(() => symbols[Math.floor(Math.random() * symbols.length)]).join(""); }