/** * IP SENTINEL - Cloudflare Worker * 玩偶学长 (DollSenior) 定制版 * 默认显示英文,如修改中文界面,请搜索 const [lang, setLang] = useState('en'); 把zh改为en即可 */ export default { async fetch(request, env, ctx) { const url = new URL(request.url); // === 1. 从 Cloudflare 环境变量读取配置 === const config = { pageTitle: env.TITLE || "IP SENTINEL | DollSenior", githubRepo: env.GITHUB || "https://github.com/wanouxuezhang/ipSentinel", ownerName: env.NAME || "DollSenior", ownerNameCN: env.NAMECN || "玩偶学长", ownerShort: env.SHORT || "DollSenior", ownerShortCN: env.SHORTCN || "玩偶🧸", footerText: env.DIBUEN || "IP SENTINEL · DollSenior Edition", footerTextCN: env.DIBUCN || "IP SENTINEL · 玩偶学长" }; // === PWA 配置: manifest.json === if (url.pathname === "/manifest.json") { return new Response(JSON.stringify({ "name": config.pageTitle, "short_name": "IP Check", "start_url": "/", "display": "standalone", "background_color": "#020617", "theme_color": "#020617", "orientation": "portrait-primary", "icons": [ { "src": "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%2322d3ee' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z'/%3E%3C/svg%3E", "type": "image/svg+xml", "sizes": "any" } ] }), { headers: { "content-type": "application/json" } }); } // === PWA 配置: sw.js === if (url.pathname === "/sw.js") { return new Response(`self.addEventListener('install',e=>self.skipWaiting());self.addEventListener('activate',e=>e.waitUntil(self.clients.claim()));self.addEventListener('fetch',e=>{});`, { headers: { "content-type": "application/javascript" } }); } // === 获取 IP 数据 === const cf = request.cf || {}; const headers = request.headers; const clientIp = headers.get("cf-connecting-ip") || headers.get("x-forwarded-for") || "127.0.0.1"; const initData = { source: "Cloudflare Edge", ip: clientIp, country: cf.country || "Unknown", city: cf.city || "Unknown City", region: cf.region || "", isp: cf.asOrganization || "Unknown ISP", asn: cf.asn ? "AS" + cf.asn : "Unknown ASN", lat: Number(cf.latitude) || 0, lon: Number(cf.longitude) || 0, colo: cf.colo || "UNK", timezone: cf.timezone || "UTC", httpProtocol: request.cf?.httpProtocol || "HTTP/2", tlsVersion: request.cf?.tlsVersion || "TLS 1.3", userAgent: headers.get("user-agent") || "Unknown" }; return new Response(renderHtml(initData, config), { headers: { 'content-type': 'text/html;charset=UTF-8', 'Content-Security-Policy': "default-src 'self' 'unsafe-inline' 'unsafe-eval' blob: data: *; connect-src *; img-src * data: https:; style-src * 'unsafe-inline'; font-src *;" }, }); }, }; function renderHtml(initData, config) { return ` ${config.pageTitle}
`; }