-
Notifications
You must be signed in to change notification settings - Fork 81
/
JSNetProxy.js
84 lines (72 loc) · 2.24 KB
/
JSNetProxy.js
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
const _hostRegex = new RegExp( /(https?:\/\/[^/]+)/,"i");
window._replaceHost = function(str){
if (! str)
return str;
if (window._NoTilde)
str = str.replace("~","_")
return str.replace(_hostRegex,window._ProxyBase);
}
window.nv_XMLHttpRequest = new Proxy(XMLHttpRequest, {
construct: function (target, args) {
const originalRequest = new target();
const prototypeDescriptors = Object.getOwnPropertyDescriptors(
target.prototype
)
for (const propertyName in prototypeDescriptors) {
Reflect.defineProperty(
originalRequest,
propertyName,
prototypeDescriptors[propertyName]
)
}
return new Proxy(originalRequest, {
get: (target, name, trap) => {
if (typeof target[name] === 'function') {
return (...args) => {
switch (name) {
case 'open':
if (args.length > 1)
args[1] = window._replaceHost(args[1]);
break;
default:
break;
}
return target[name].apply(target, args);
}
}
return target[name];
},
set: function (target, prop, value) {
Reflect.set(target, prop, value) // or target[prop] = value
return true;
},
})
}
})
var oReq = new XMLHttpRequest();
window.nv_fetch = new Proxy(window.fetch, {
apply: function (target, that, args) {
if (args.length > 0 && args[0])
if (typeof args[0] !== 'string' && args[0].url){
const newUrl = window._replaceHost( args[0].url );
if (newUrl != args[0].url)
args[0] = new Request(newUrl, args[0]);
}
else
args[0] = window._replaceHost(args[0].toString());
return target.apply(that, args);
},
});
window.XMLHttpRequest = window.nv_XMLHttpRequest;
window.fetch = window.nv_fetch;
window.oldAppendChild = Element.prototype.appendChild;
Element.prototype.appendChild = function() {
if (arguments.length > 0) {
if ( (arguments[0]?.tagName == "SCRIPT" || arguments[0]?.tagName == "IMG") && arguments[0].src)
arguments[0].src = window._replaceHost(arguments[0].src);
else if ( arguments[0]?.tagName == "DIV" && arguments[0].style?.backgroundImage?.startsWith("url"))
arguments[0].style.backgroundImage = window._replaceHost(arguments[0].style?.backgroundImage);
}
return window.oldAppendChild.apply(this, arguments);
};
console.log("PROXY IN PLACE");