1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
| const AllowedReferrer = ["cuojue.org"];
export const config = { runtime: "experimental-edge", };
export default function (req) { return response(req); }
async function senData(request, url, uuid, user_agent) { const encode = (data) => encodeURIComponent(decodeURIComponent(data));
const getReqHeader = (key) => request.headers.get(key); const getQueryString = (name) => url.searchParams.get(name);
const reqParameter = { headers: { Host: "www.google-analytics.com", "User-Agent": user_agent, Accept: getReqHeader("Accept"), "Accept-Language": getReqHeader("Accept-Language"), "Accept-Encoding": getReqHeader("Accept-Encoding"), "Cache-Control": "max-age=0", }, };
const pvData = `tid=${encode(getQueryString("ga"))}&cid=${uuid}&dl=${encode( getQueryString("dl") )}&uip=${getReqHeader("CF-Connecting-IP")}&ua=${user_agent}&dt=${encode( getQueryString("dt") )}&de=${encode(getQueryString("de"))}&dr=${encode( getQueryString("dr") )}&ul=${getQueryString("ul")}&sd=${getQueryString("sd")}&sr=${getQueryString( "sr" )}&vp=${getQueryString("vp")}`;
const perfData = `plt=${getQueryString("plt")}&dns=${getQueryString( "dns" )}&pdt=${getQueryString("pdt")}&rrt=${getQueryString( "rrt" )}&tcp=${getQueryString("tcp")}&srt=${getQueryString( "srt" )}&dit=${getQueryString("dit")}&clt=${getQueryString("clt")}`;
const pvUrl = `https://www.google-analytics.com/collect?v=1&t=pageview&${pvData}&z=${getQueryString( "z" )}`; const perfUrl = `https://www.google-analytics.com/collect?v=1&t=timing&${pvData}&${perfData}&z=${getQueryString( "z" )}`;
const response1 = await fetch(pvUrl, reqParameter); const response2 = await fetch(perfUrl, reqParameter); await response1.waitUntil(); await response2.waitUntil(); }
async function response(request) { const url = new URL(request.url);
const getReqHeader = (key) => request.headers.get(key);
const Referer = getReqHeader("Referer"); const user_agent = getReqHeader("User-Agent"); const ref_host = (() => { try { return new URL(Referer).hostname; } catch (e) { return ""; } })();
let needBlock = false;
needBlock = !ref_host || ref_host === "" || !user_agent || !url.search.includes("ga=UA-") ? true : false;
if ( typeof AllowedReferrer !== "undefined" && AllowedReferrer !== null && AllowedReferrer ) { let _AllowedReferrer = AllowedReferrer;
if (!Array.isArray(AllowedReferrer)) _AllowedReferrer = [_AllowedReferrer];
const rAllowedReferrer = new RegExp(_AllowedReferrer.join("|"), "g");
needBlock = !rAllowedReferrer.test(ref_host) ? true : false; console.log(_AllowedReferrer, rAllowedReferrer, ref_host); }
if (needBlock) { return new Response("403 Forbidden", { headers: { "Content-Type": "text/html" }, status: 403, statusText: "Forbidden", }); }
const getCookie = (name) => { const pattern = new RegExp("(^| )" + name + "=([^;]*)(;|$)"); const r = (getReqHeader("cookie") || "").match(pattern); return r !== null ? unescape(r[2]) : null; };
const createUuid = () => { let s = []; const hexDigits = "0123456789abcdef"; for (let i = 0; i < 36; i++) { s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1); } s[14] = "4"; s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); s[8] = s[13] = s[18] = s[23] = "-";
return s.join(""); };
const _uuid = getCookie("uuid"); const uuid = _uuid ? _uuid : createUuid();
senData(request, url, uuid, user_agent, Referer);
let response = new Response(null, { status: 204, statusText: "No Content", });
if (!_uuid) response.headers.set( "Set-Cookie", `uuid=${uuid}; Expires=${new Date( new Date().getTime() + 365 * 86400 * 30 * 1000 ).toGMTString()}; Path='/'; SameSite=None; Secure` );
return response; }
|