Last active
November 4, 2021 08:55
-
-
Save Gowee/ed7a7e2a3093d542fa3ee4ea35a8249e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf8 -*- | |
# To be deployed to Tecent SCF | |
# https://console.cloud.tencent.com/scf/ | |
import json | |
def main_handler(event, context): | |
srcip = event['requestContext']['sourceIp'] | |
dest_url = "https://water-meter.bamboo.workers.dev/callback?ip=" + srcip | |
resp = { | |
"isBase64Encoded": False, | |
"statusCode": 302, | |
"headers": {"Location": dest_url, "Content-Type":"text/html"}, | |
"body": f'<html><body><h1>Redirecting</h1><p>To <a href="{dest_url}">there</a></p></body></html>' | |
} | |
return resp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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<Response>} | |
*/ | |
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(""); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment