Skip to content

Commit

Permalink
拦截无用的统计请求
Browse files Browse the repository at this point in the history
  • Loading branch information
jianjianai committed Jun 6, 2024
1 parent e2c7724 commit f889e27
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
79 changes: 79 additions & 0 deletions src/ClientScript.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<script>
(() => {
//解析器
function pathOrUrl(value) {
if (!value) {
return undefined;
}
if ((typeof value) != "string") {
value = `${value};`
}
try {
return new URL(value);
} catch (e) {
if (value.startsWith("/")) {
let url = new URL(`${window.location.origin}${value}`)
return url
} else {
return new URL(`${window.location.href.substring(0, window.location.href.lastIndexOf("/"))}/${value}`);
}
}
}

//feth请求编辑
const myFetch = window.fetch;
window.fetch = (input, init) => {
let url = pathOrUrl(input);
if (url) {
if (url.hostname == "www.bing.com") {
url.hostname = window.location.hostname;
url.port = window.location.port;
url.protocol = window.location.protocol;
console.log("已拦重定向请求=>", input, url.toString());
return myFetch(url, init);
}
}
return myFetch(input, init);
}
//xml请求编辑
const myXMLHttpRequestOpen = window.XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function () {
let url = pathOrUrl(arguments[1]);
if (url) {
if(url.pathname=="/fd/ls/lsp.aspx"){
console.log("已拦截无用请求=>", arguments);
return;
}
}
console.log(arguments);
return myXMLHttpRequestOpen.apply(this, arguments);
}

//sendBeacon请求
const mySendBeacon = window.navigator.sendBeacon;
window.navigator.sendBeacon = (url, data)=>{
console.log("已拦截无用统计请求=>", url,data);
}

//img请求
const myImage = window.Image;
window.Image = new Proxy(myImage, {
construct(target, argumentsList) {
return new Proxy(new myImage(...argumentsList), {
set(target, property, value) {
const url = pathOrUrl(value);
if (url) {
if (url.pathname == "/fd/ls/l") {
console.log("已拦截无用请求=>", value);
return;
}
}
console.log(url,value);
target[property] = value;
}
});
}
})
console.log("注入成功!");
})();
</script>
12 changes: 12 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import { proxyLinkHttp } from "./proxyLink/proxyLinkHttp";
import { usIps } from './ips/usIps';
import ClientScript from './ClientScript.html';

const XForwardedForIP = usIps[Math.floor(Math.random()*usIps.length)][0];
console.log(XForwardedForIP)
Expand Down Expand Up @@ -217,6 +218,12 @@ export default {
// retBody = retBody.replace(/https?:\\\/\\\/copilot\.microsoft\.com(:[0-9]{1,6})?/g, `${porxyOrigin.replaceAll("/",`\\/`)}`);
// retBody = retBody.replaceAll(`"copilot.microsoft.com"`,`"${porxyHostName}"`);
// retBody = retBody.replaceAll(`"copilot.microsoft.com/"`,`"${porxyHostName}/"`);
const resUrl = new URL(res.url);

//特定页面注入脚本
if(resUrl.pathname=="/"){
retBody = injectionHtml(retBody);
}
config.body = retBody;
return config;
},
Expand Down Expand Up @@ -262,3 +269,8 @@ async function websocketPorxy(request: Request): Promise<Response> {
}) as any;
}

/** 注入脚本 */
function injectionHtml(html:string){
return html.replace("<head>",`<head>${ClientScript}`)
}

0 comments on commit f889e27

Please sign in to comment.