can proxy FC/SCF/CFC/FG

pull/128/head
qkqpttgf 2020-09-30 18:51:46 +08:00 committed by GitHub
parent c4c2c20bce
commit f6f7c6bbac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 56 additions and 30 deletions

View File

@ -1,8 +1,12 @@
// odd, 单日 // odd, 单日
const SingleDay = 'aaa1.herokuapp.com' const SingleDay = 'https://aaa1.herokuapp.com'
// even, 双日 // even, 双日
const DoubleDay = 'bbb2.herokuapp.com' const DoubleDay = 'https://bbb2.herokuapp.com'
//const SingleDay = 'https://153xxxxx0.cn-hongkong.fc.aliyuncs.com/2016-08-15/proxy/onedrive/xxx/'
//const DoubleDay = 'https://153xxxxx0.cn-hongkong.fc.aliyuncs.com/2016-08-15/proxy/onedrive/xxx/'
// CF proxy all, 一切给CF代理true/false // CF proxy all, 一切给CF代理true/false
const CFproxy = true const CFproxy = true
@ -17,48 +21,70 @@ addEventListener('fetch', event => {
let url=new URL(event.request.url); let url=new URL(event.request.url);
if (url.protocol == 'http:') { if (url.protocol == 'http:') {
url.protocol = 'https:' url.protocol = 'https:'
response = Response.redirect(url.href); event.respondWith( Response.redirect(url.href) )
event.respondWith( response );
} else { } else {
let response = null;
let nd = new Date(); let nd = new Date();
if (nd.getDate()%2) { if (nd.getDate()%2) {
host = SingleDay host = SingleDay
} else { } else {
host = DoubleDay host = DoubleDay
} }
if (!CFproxy) { if (host.substr(0, 7)!='http://'&&host.substr(0, 8)!='https://') host = 'http://' + host;
url.hostname=host;
let request=new Request(url,event.request); response = fetchAndApply(host, event.request);
event.respondWith( fetch(request) )
} else { event.respondWith( response );
event.respondWith( fetchAndApply(event.request) );
}
} }
}) })
async function fetchAndApply(request) { async function fetchAndApply(host, request) {
let f_url = new URL(request.url);
let a_url = new URL(host);
let replace_path = a_url.pathname;
if (replace_path.substr(replace_path.length-1)!='/') replace_path += '/';
let replaced_path = '/';
let query = f_url.search;
let path = f_url.pathname;
if (host.substr(host.length-1)=='/') path = path.substr(1);
f_url.href = host + path + query;
let response = null; let response = null;
let url = new URL(request.url); if (!CFproxy) {
url.host = host; response = await fetch(request);
} else {
let method = request.method;
let body = request.body;
let request_headers = request.headers;
let new_request_headers = new Headers(request_headers);
new_request_headers.set('Host', f_url.host);
new_request_headers.set('Referer', request.url);
let method = request.method; response = await fetch(f_url.href, {
let body = request.body; method: method,
let request_headers = request.headers; body: body,
let new_request_headers = new Headers(request_headers); headers: new_request_headers
});
}
new_request_headers.set('Host', url.host); let out_headers = new Headers(response.headers);
new_request_headers.set('Referer', request.url); if (out_headers.get('Content-Disposition')=='attachment') out_headers.delete('Content-Disposition');
let out_body = null;
let contentType = out_headers.get('content-type');
if (contentType.includes("application/text")) {
out_body = await response.text();
while (out_body.includes(replace_path)) out_body = out_body.replace(replace_path, replaced_path);
} else if (contentType.includes("text/html")) {
out_body = await response.text();
while (out_body.includes(replace_path)) out_body = out_body.replace(replace_path, replaced_path);
} else {
out_body = await response.body;
}
let original_response = await fetch(url.href, { let out_response = new Response(out_body, {
method: method, status: response.status,
body: body, headers: out_headers
headers: new_request_headers
});
response = new Response(original_response.body, {
status: original_response.status,
headers: original_response.headers
}) })
return response; return out_response;
} }