addEventListener('fetch', event => {
const url = new URL(event.request.url);
thisProxyServerUrlHttps = `${url.protocol}//${url.hostname}/`;
thisProxyServerUrl_hostOnly = url.host;
//console.log(thisProxyServerUrlHttps);
//console.log(thisProxyServerUrl_hostOnly);
event.respondWith(handleRequest(event.request))
})
const str = "/";
const lastVisitProxyCookie = "__PROXY_VISITEDSITE__";
const passwordCookieName = "__PROXY_PWD__";
const proxyHintCookieName = "__PROXY_HINT__";
const password = "";
const replaceUrlObj = "__location____"
var thisProxyServerUrlHttps;
var thisProxyServerUrl_hostOnly;
// const CSSReplace = ["https://", "http://"];
const proxyHintInjection = `
//---***========================================***---提示使用代理---***========================================***---
setTimeout(() => {
if (document.readyState === 'complete' || document.readyState === 'interactive') {
var hint = \`Warning: You are currently using a web proxy, the original link is \${window.location.pathname}. Please note that you are using a proxy, and do not log in to any website. Click to close this hint. 警告:您当前正在使用网络代理,原始链接为\${window.location.pathname}。请注意您正在使用代理,请勿登录任何网站。单击关闭此提示。\`;
console.log(1);
document.body.insertAdjacentHTML(
'afterbegin',
\`
\${hint}
\`
);
}else{
alert(\`Warning: You are currently using a web proxy, the original link is \${window.location.pathname}. Please note that you are using a proxy, and do not log in to any website.\`);
}
}, 3000);
`;
const httpRequestInjection = `
//---***========================================***---information---***========================================***---
var now = new URL(window.location.href);
var base = now.host;
var protocol = now.protocol;
var nowlink = protocol + "//" + base + "/";
var oriUrlStr = window.location.href.substring(nowlink.length);
var oriUrl = new URL(oriUrlStr);
var path = now.pathname.substring(1);
console.log("***************************----" + path);
if(!path.startsWith("http")) path = "https://" + path;
var original_host = path.substring(path.indexOf("://") + "://".length);
original_host = original_host.split('/')[0];
var mainOnly = path.substring(0, path.indexOf("://")) + "://" + original_host + "/";
//---***========================================***---通用func---***========================================***---
function changeURL(relativePath){
if(relativePath.startsWith("data:") || relativePath.startsWith("mailto:") || relativePath.startsWith("javascript:") || relativePath.startsWith("chrome") || relativePath.startsWith("edge")) return relativePath;
try{
if(relativePath && relativePath.startsWith(nowlink)) relativePath = relativePath.substring(nowlink.length);
if(relativePath && relativePath.startsWith(base + "/")) relativePath = relativePath.substring(base.length + 1);
if(relativePath && relativePath.startsWith(base)) relativePath = relativePath.substring(base.length);
}catch{
//ignore
}
try {
var absolutePath = new URL(relativePath, path).href;
absolutePath = absolutePath.replace(window.location.href, path);
absolutePath = absolutePath.replace(encodeURI(window.location.href), path);
absolutePath = absolutePath.replace(encodeURIComponent(window.location.href), path);
absolutePath = absolutePath.replace(nowlink, mainOnly);
absolutePath = absolutePath.replace(nowlink, encodeURI(mainOnly));
absolutePath = absolutePath.replace(nowlink, encodeURIComponent(mainOnly));
absolutePath = absolutePath.replace(nowlink, mainOnly.substring(0,mainOnly.length - 1));
absolutePath = absolutePath.replace(nowlink, encodeURI(mainOnly.substring(0,mainOnly.length - 1)));
absolutePath = absolutePath.replace(nowlink, encodeURIComponent(mainOnly.substring(0,mainOnly.length - 1)));
absolutePath = absolutePath.replace(base, original_host);
absolutePath = nowlink + absolutePath;
return absolutePath;
} catch (e) {
console.log(path + " " + relativePath);
return "";
}
}
//---***========================================***---注入网络---***========================================***---
function networkInject(){
//inject network request
var originalOpen = XMLHttpRequest.prototype.open;
var originalFetch = window.fetch;
XMLHttpRequest.prototype.open = function(method, url, async, user, password) {
url = changeURL(url);
console.log("R:" + url);
return originalOpen.apply(this, arguments);
};
window.fetch = function(input, init) {
var url;
if (typeof input === 'string') {
url = input;
} else if (input instanceof Request) {
url = input.url;
} else {
url = input;
}
url = changeURL(url);
console.log("R:" + url);
if (typeof input === 'string') {
return originalFetch(url, init);
} else {
const newRequest = new Request(url, input);
return originalFetch(newRequest, init);
}
};
console.log("NETWORK REQUEST METHOD INJECTED");
}
//---***========================================***---注入window.open---***========================================***---
function windowOpenInject(){
const originalOpen = window.open;
// Override window.open function
window.open = function (url, name, specs) {
let modifiedUrl = changeURL(url);
return originalOpen.call(window, modifiedUrl, name, specs);
};
console.log("WINDOW OPEN INJECTED");
}
//---***========================================***---注入append元素---***========================================***---
function appendChildInject(){
const originalAppendChild = Node.prototype.appendChild;
Node.prototype.appendChild = function(child) {
try{
if(child.src){
child.src = changeURL(child.src);
}
if(child.href){
child.href = changeURL(child.href);
}
}catch{
//ignore
}
return originalAppendChild.call(this, child);
};
console.log("APPEND CHILD INJECTED");
}
//---***========================================***---注入location---***========================================***---
class ProxyLocation {
constructor(originalLocation) {
this.originalLocation = originalLocation;
}
// 方法:重新加载页面
reload(forcedReload) {
this.originalLocation.reload(forcedReload);
}
// 方法:替换当前页面
replace(url) {
this.originalLocation.replace(changeURL(url));
}
// 方法:分配一个新的 URL
assign(url) {
this.originalLocation.assign(changeURL(url));
}
// 属性:获取和设置 href
get href() {
return oriUrlStr;
}
set href(url) {
this.originalLocation.href = changeURL(url);
}
// 属性:获取和设置 protocol
get protocol() {
return this.originalLocation.protocol;
}
set protocol(value) {
this.originalLocation.protocol = changeURL(value);
}
// 属性:获取和设置 host
get host() {
console.log("*host");
return original_host;
}
set host(value) {
console.log("*host");
this.originalLocation.host = changeURL(value);
}
// 属性:获取和设置 hostname
get hostname() {
console.log("*hostname");
return oriUrl.hostname;
}
set hostname(value) {
console.log("s hostname");
this.originalLocation.hostname = changeURL(value);
}
// 属性:获取和设置 port
get port() {
return oriUrl.port;
}
set port(value) {
this.originalLocation.port = value;
}
// 属性:获取和设置 pathname
get pathname() {
console.log("*pathname");
return oriUrl.pathname;
}
set pathname(value) {
console.log("*pathname");
this.originalLocation.pathname = value;
}
// 属性:获取和设置 search
get search() {
console.log("*search");
console.log(oriUrl.search);
return oriUrl.search;
}
set search(value) {
console.log("*search");
this.originalLocation.search = value;
}
// 属性:获取和设置 hash
get hash() {
return oriUrl.hash;
}
set hash(value) {
this.originalLocation.hash = value;
}
// 属性:获取 origin
get origin() {
return oriUrl.origin;
}
}
function documentLocationInject(){
Object.defineProperty(document, 'URL', {
get: function () {
return oriUrlStr;
},
set: function (url) {
document.URL = changeURL(url);
}
});
Object.defineProperty(document, '${replaceUrlObj}', {
get: function () {
return new ProxyLocation(window.location);
},
set: function (url) {
window.location.href = changeURL(url);
}
});
console.log("LOCATION INJECTED");
}
function windowLocationInject() {
Object.defineProperty(window, '${replaceUrlObj}', {
get: function () {
return new ProxyLocation(window.location);
},
set: function (url) {
window.location.href = changeURL(url);
}
});
console.log("WINDOW LOCATION INJECTED");
}
//---***========================================***---注入历史---***========================================***---
function historyInject(){
const originalPushState = History.prototype.pushState;
const originalReplaceState = History.prototype.replaceState;
History.prototype.pushState = function (state, title, url) {
var u = new URL(url, now.href).href;
return originalPushState.apply(this, [state, title, u]);
};
History.prototype.replaceState = function (state, title, url) {
console.log(nowlink);
console.log(url);
console.log(now.href);
var u = new URL(url, now.href).href;
console.log(u);
return originalReplaceState.apply(this, [state, title, u]);
};
console.log("HISTORY INJECTED");
}
//---***========================================***---Hook观察界面---***========================================***---
function obsPage() {
var yProxyObserver = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
traverseAndConvert(mutation);
});
});
var config = { attributes: true, childList: true, subtree: true };
yProxyObserver.observe(document.body, config);
console.log("OBSERVING THE WEBPAGE...");
}
function traverseAndConvert(node) {
if (node instanceof HTMLElement) {
removeIntegrityAttributesFromElement(node);
covToAbs(node);
node.querySelectorAll('*').forEach(function(child) {
removeIntegrityAttributesFromElement(child);
covToAbs(child);
});
}
}
function covToAbs(element) {
var relativePath = "";
var setAttr = "";
if (element instanceof HTMLElement && element.hasAttribute("href")) {
relativePath = element.getAttribute("href");
setAttr = "href";
}
if (element instanceof HTMLElement && element.hasAttribute("src")) {
relativePath = element.getAttribute("src");
setAttr = "src";
}
// Check and update the attribute if necessary
if (setAttr !== "" && relativePath.indexOf(nowlink) != 0) {
if (!relativePath.includes("*")) {
try {
var absolutePath = changeURL(relativePath);
console.log(absolutePath);
element.setAttribute(setAttr, absolutePath);
} catch (e) {
console.log(path + " " + relativePath);
}
}
}
}
function removeIntegrityAttributesFromElement(element){
if (element.hasAttribute('integrity')) {
element.removeAttribute('integrity');
}
}
//---***========================================***---Hook观察界面里面要用到的func---***========================================***---
function loopAndConvertToAbs(){
for(var ele of document.querySelectorAll('*')){
removeIntegrityAttributesFromElement(ele);
covToAbs(ele);
}
console.log("LOOPED EVERY ELEMENT");
}
function covScript(){ //由于observer经过测试不会hook添加的script标签,也可能是我测试有问题?
var scripts = document.getElementsByTagName('script');
for (var i = 0; i < scripts.length; i++) {
covToAbs(scripts[i]);
}
setTimeout(covScript, 3000);
}
//---***========================================***---操作---***========================================***---
networkInject();
windowOpenInject();
appendChildInject();
documentLocationInject();
windowLocationInject();
// historyInject();
// 这里实在无能为力不想改,可以pr一个
//---***========================================***---在window.load之后的操作---***========================================***---
window.addEventListener('load', () => {
loopAndConvertToAbs();
console.log("CONVERTING SCRIPT PATH");
obsPage();
covScript();
});
console.log("WINDOW ONLOAD EVENT ADDED");
//---***========================================***---在window.error的时候---***========================================***---
window.addEventListener('error', event => {
var element = event.target || event.srcElement;
if (element.tagName === 'SCRIPT') {
console.log("Found problematic script:", element);
if(element.alreadyChanged){
console.log("this script has already been injected, ignoring this problematic script...");
return;
}
// 调用 covToAbs 函数
removeIntegrityAttributesFromElement(element);
covToAbs(element);
// 创建新的 script 元素
var newScript = document.createElement("script");
newScript.src = element.src;
newScript.async = element.async; // 保留原有的 async 属性
newScript.defer = element.defer; // 保留原有的 defer 属性
newScript.alreadyChanged = true;
// 添加新的 script 元素到 document
document.head.appendChild(newScript);
console.log("New script added:", newScript);
}
}, true);
console.log("WINDOW CORS ERROR EVENT ADDED");
`;
const mainPage = `
I made this project because some extreme annoying network filter software in my school, which is notorious "Goguardian", and now it is open source at https://github.com/1234567Yang/cf-proxy-ex/.
- How to use this proxy:
Type the website you want to go to after the website's url, for example:
https://the current url/github.com
OR
https://the current url/https://github.com
- If your browser show 400 bad request, please clear your browser cookie
- Why I make this:
Because school blcok every website that I can find math / CS and other subjects' study material and question solutions. In the eyes of the school, China (and some other countries) seems to be outside the scope of this "world". They block access to server IP addresses in China and block access to Chinese search engines and video websites. Of course, some commonly used social software has also been blocked, which once made it impossible for me to send messages to my parents on campus. I don't think that's how it should be, so I'm going to fight it as hard as I can. I believe this will not only benefit myself, but a lot more people can get benefits.
- If this website is blocked by your school:
Contact me at help@wvusd.homes, and I will setup a new webpage.
- Limitation:
Although I tried my best to make every website proxiable, there still might be pages or resources that can not be load, and the most important part is that YOU SHOULD NEVER LOGIN ANY ACCOUNT VIA ONLINE PROXY.
Proxies that can bypass the school network blockade:
Traditional VPNs such as hide me.
Tor Browser, short for The Onion Router, is free and open-source software for enabling anonymous communication. It directs Internet traffic via a free, worldwide volunteer overlay network that consists of more than seven thousand relays. Using Tor makes it more difficult to trace a user's Internet activity.
v2RayN is a GUI client for Windows, support Xray core and v2fly core and others. You must subscribe to an airport to use it. For example, you can subscribe fly bird cloud.
Bypass Goguardian by proxy: You can buy a domain($1) and setup by yourself: how to setup a proxy. Unless Goguardian use white list mode, this can always work.
Too expensive? Never mind! There are a lot of free domains registration companies (for the first year of the domain) that do not need any credit card, search online!
Youtube video unblock: "Thanks" for Russia that they started to invade Ukraine and Google blocked the traffic from Russia, there are a LOT of mirror sites working. You can even setup one by yourself.
☭
`;
const redirectError = `
Error while redirecting: the website you want to access to may contain wrong redirect information, and we can not parse the info
`;
//new URL(请求路径, base路径).href;
async function handleRequest(request) {
//获取所有cookie
var siteCookie = request.headers.get('Cookie');
if (password != "") {
if(siteCookie != null && siteCookie != ""){
var pwd = getCook(passwordCookieName, siteCookie);
console.log(pwd);
if (pwd != null && pwd != "") {
if(pwd != password){
return getHTMLResponse("403 Forbidden
You do not have access to view this webpage.");
}
}else{
return getHTMLResponse("403 Forbidden
You do not have access to view this webpage.");
}
}else{
return getHTMLResponse("403 Forbidden
You do not have access to view this webpage.");
}
}
const url = new URL(request.url);
if(request.url.endsWith("favicon.ico")){
return Response.redirect("https://www.baidu.com/favicon.ico", 301);
}
//var siteOnly = url.pathname.substring(url.pathname.indexOf(str) + str.length);
var actualUrlStr = url.pathname.substring(url.pathname.indexOf(str) + str.length) + url.search + url.hash;
if (actualUrlStr == "") { //先返回引导界面
return getHTMLResponse(mainPage);
}
try {
var test = actualUrlStr;
if (!test.startsWith("http")) {
test = "https://" + test;
}
var u = new URL(test);
if (!u.host.includes(".")) {
throw new Error();
}
}
catch { //可能是搜素引擎,比如proxy.com/https://www.duckduckgo.com/ 转到 proxy.com/?q=key
var lastVisit;
if (siteCookie != null && siteCookie != "") {
lastVisit = getCook(lastVisitProxyCookie, siteCookie);
console.log(lastVisit);
if (lastVisit != null && lastVisit != "") {
//(!lastVisit.startsWith("http"))?"https://":"" +
//现在的actualUrlStr如果本来不带https:// 的话那么现在也不带,因为判断是否带protocol在后面
return Response.redirect(thisProxyServerUrlHttps + lastVisit + "/" + actualUrlStr, 301);
}
}
r