Updated a few more things

This commit is contained in:
QuiteAFancyEmerald 2022-02-08 00:43:17 -08:00
parent c827e9f7e1
commit 73cdcb3186
47 changed files with 35805 additions and 1102 deletions

View file

@ -1,91 +1,117 @@
function createHttpRewriter(ctx = {}) {
const { overrideFunction, overrideConstructor, overrideAccessors } = require("./utils");
function createHttpRewriter(ctx) {
if (ctx.window.fetch) ctx.originalFn.fetch = ctx.window.fetch;
if (ctx.window.WebSocket && ctx.window.WebSocket.prototype) ctx.originalFn.WebSocket = ctx.window.WebSocket;
if (ctx.window.Request && ctx.window.Request.prototype) {
ctx.originalFn.Request = ctx.window.Request;
ctx.originalAccessors.RequestUrl = Object.getOwnPropertyDescriptor(ctx.window.Request.prototype, 'url');
};
if (ctx.window.Response && ctx.window.Response.prototype) {
ctx.originalAccessors.ResponseUrl = Object.getOwnPropertyDescriptor(ctx.window.Response.prototype, 'url');
};
if (ctx.window.XMLHttpRequest && ctx.window.XMLHttpRequest.prototype) {
ctx.originalFn.xhrOpen = ctx.window.XMLHttpRequest.prototype.open;
ctx.originalFn.xhrSend = ctx.window.XMLHttpRequest.prototype.send;
ctx.originalAccessors.xhrResponseUrl = Object.getOwnPropertyDescriptor(ctx.window.XMLHttpRequest.prototype, 'responseURL');
};
if (ctx.window.EventSource && ctx.window.EventSource.prototype) {
ctx.originalFn.EventSource = ctx.window.EventSource;
ctx.originalAccessors.EventSourceUrl = Object.getOwnPropertyDescriptor(ctx.window.EventSource.prototype, 'url');
};
if (ctx.window.open) ctx.originalFn.open = ctx.window.open;
function rewriteFetch() {
if (ctx.originalFn.Request) {
overrideConstructor(ctx.window, 'Request', (target, args) => {
if (args[0] instanceof ctx.window.Request) return;
if (args[0]) args[0] = ctx.url.wrap(args[0], { ...ctx.meta, flags: ['xhr'] });
return new target(...args);
});
overrideAccessors(ctx.window.Request.prototype, 'url', {
getter: (target, that) => ctx.url.unwrap(target.call(that), ctx.meta),
});
ctx.proxyToOriginalMp.set(Object.getOwnPropertyDescriptor(ctx.window.Request.prototype, 'url').get, ctx.originalAccessors.RequestUrl);
ctx.proxyToOriginalMp.set(ctx.window.Request, ctx.originalFn.Request);
};
if (ctx.originalAccessors.ResponseUrl) {
overrideAccessors(ctx.window.Response.prototype, 'url', {
getter: (target, that) => ctx.url.unwrap(target.call(that), ctx.meta),
});
ctx.proxyToOriginalMp.set(Object.getOwnPropertyDescriptor(ctx.window.Response.prototype, 'url').get, ctx.originalAccessors.ResponseUrl);
};
if (ctx.originalFn.fetch) {
overrideFunction(ctx.window, 'fetch', async (target, that, args) => {
if (args[0] instanceof ctx.window.Request) return target.apply(that, args);
if (args[0]) args[0] = ctx.url.wrap(args[0], { ...ctx.meta, flags: ['xhr'], query: new URL(args[0], ctx.location.href).origin != ctx.location.origin ? `origin=${ctx.codec.base64.encode(ctx.location.origin)}` : null });
return target.apply(that, args);
});
ctx.proxyToOriginalMp.set(ctx.window.fetch, ctx.originalFn.fetch);
};
return true;
};
function rewriteXhr() {
if (ctx.originalFn.xhrOpen) {
overrideFunction(ctx.window.XMLHttpRequest.prototype, 'open', (target, that, args) => {
if (args[1]) {
args[1] = ctx.url.wrap(args[1], { ...ctx.meta, flags: ['xhr'], query: new URL(args[1], ctx.location.href).origin != ctx.location.origin ? `origin=${ctx.codec.base64.encode(ctx.location.origin)}` : null });
};
return target.apply(that, args);
});
ctx.proxyToOriginalMp.set(ctx.window.XMLHttpRequest.prototype.open, ctx.originalFn.xhrOpen);
};
if (ctx.originalAccessors.xhrResponseUrl) {
overrideAccessors(ctx.window.XMLHttpRequest.prototype, 'responseURL', {
getter: (target, that) => {
const url = target.call(that);
return url ? ctx.url.unwrap(url, ctx.meta) : url;
},
});
ctx.proxyToOriginalMp.set(Object.getOwnPropertyDescriptor(ctx.window.XMLHttpRequest.prototype, 'responseURL').get, ctx.originalAccessors.xhrResponseUrl.get);
};
return true;
};
function rewriteWebSocket() {
if (ctx.originalFn.WebSocket) {
overrideConstructor(ctx.window, 'WebSocket', (target, args) => {
try {
let url = new URL(args[0]);
args[0] = ['wss:', 'ws:'].includes(url.protocol) ? ctx.url.wrap(url.href.replace('ws', 'http'), ctx.meta).replace('http', 'ws') + '?origin=' + ctx.codec.base64.encode(ctx.location.origin) : '';
} catch(e) {
args[0] = '';
};
return new target(...args);
});
};
};
function rewriteOpen() {
if (ctx.originalFn.open) {
overrideFunction(ctx.window, 'open', (target, that, args) => {
if (args[0]) args[0] = ctx.url.wrap(args[0], ctx.meta);
return target.apply(that, args);
});
ctx.proxyToOriginalMp.set(ctx.window.open, ctx.originalFn.open);
};
return true;
};
function rewriteEventSource() {
if (ctx.originalFn.EventSource) {
overrideConstructor(ctx.window, 'EventSource', (target, args) => {
if (args[0]) args[0] = ctx.url.wrap(args[0], ctx.meta);
return new target(...args);
});
overrideAccessors(ctx.window.EventSource.prototype, 'url', {
getter: (target, that) => ctx.url.unwrap(target.call(that), ctx.meta),
});
};
return true;
};
return function rewriteHttp() {
if (ctx.window.Request) {
const requestURL = Object.getOwnPropertyDescriptor(ctx.window.Request.prototype, 'url');
ctx.window.Request = new Proxy(ctx.window.Request, {
construct(target, args) {
if (args[0]) args[0] = ctx.url.wrap(args[0], { ...ctx.meta, flags: ['xhr'], })
return Reflect.construct(target, args);
},
});
Object.defineProperty(ctx.window.Request.prototype, 'url', {
get: new Proxy(requestURL.get, {
apply: (target, that, args) => {
var url = Reflect.apply(target, that, args);
return url ? ctx.url.unwrap(url, ctx.meta) : url;
},
}),
});
};
if (ctx.window.Response) {
const responseURL = Object.getOwnPropertyDescriptor(ctx.window.Response.prototype, 'url');
Object.defineProperty(ctx.window.Response.prototype, 'url', {
get: new Proxy(responseURL.get, {
apply: (target, that, args) => {
var url = Reflect.apply(target, that, args);
return url ? ctx.url.unwrap(url, ctx.meta) : url;
},
}),
});
};
if (ctx.window.open) {
ctx.window.open = new Proxy(ctx.window.open, {
apply: (target, that, args) => {
if (args[0]) args[0] = ctx.url.wrap(args[0], ctx.meta);
return Reflect.apply(target, that, args)
},
});
};
if (ctx.window.fetch) {
ctx.window.fetch = new Proxy(ctx.window.fetch, {
apply: (target, that, args) => {
if (args[0] instanceof ctx.window.Request) return Reflect.apply(target, that, args);
if (args[0]) args[0] = ctx.url.wrap(args[0], { ...ctx.meta, flags: ['xhr'], });
return Reflect.apply(target, that, args);
},
});
};
if (ctx.window.Navigator && ctx.window.Navigator.prototype.sendBeacon) {
ctx.window.Navigator.prototype.sendBeacon = new Proxy(ctx.window.Navigator.prototype.sendBeacon, {
apply: (target, that, args) => {
if (args[0]) ctx.url.wrap(args[0], { ...ctx.meta, flags: ['xhr'], });
return Reflect.apply(target, that, args);
},
});
};
if (ctx.window.XMLHttpRequest) {
const responseURL = Object.getOwnPropertyDescriptor(ctx.window.XMLHttpRequest.prototype, 'responseURL');
ctx.window.XMLHttpRequest.prototype.open = new Proxy(ctx.window.XMLHttpRequest.prototype.open, {
apply: (target, that, args) => {
if (args[1]) args[1] = ctx.url.wrap(args[1], { ...ctx.meta, flags: ['xhr'], });
return Reflect.apply(target, that, args);
},
});
Object.defineProperty(ctx.window.XMLHttpRequest.prototype, 'responseURL', {
get: new Proxy(responseURL.get, {
apply: (target, that, args) => {
const url = Reflect.apply(target, that, args);
return url ? ctx.url.unwrap(url, ctx.meta) : url;
},
}),
});
};
if (ctx.window.postMessage) {
ctx.window.postMessage = new Proxy(ctx.window.postMessage, {
apply: (target, that, args) => {
if (!ctx.serviceWorker && args[1]) args[1] = ctx.meta.origin;
return Reflect.apply(target, that, args);
},
});
};
if (ctx.window.WebSocket && ctx.config.ws) {
ctx.window.WebSocket = new Proxy(ctx.window.WebSocket, {
construct: (target, args) => {
if (args[0]) args[0] = ctx.url.wrap(args[0].toString().replace('ws', 'http'), ctx.meta).replace('http', 'ws') + '?origin=' + ctx.location.origin;
return Reflect.construct(target, args);
},
});
};
rewriteXhr();
rewriteFetch();
rewriteWebSocket();
rewriteOpen();
rewriteEventSource();
return true;
};
};