OneManager-php/CFWorkers.js

91 lines
3.1 KiB
JavaScript
Raw Permalink Normal View History

2020-09-15 07:21:53 +00:00
// odd, 单日
2020-09-30 10:51:46 +00:00
const SingleDay = 'https://aaa1.herokuapp.com'
2020-09-15 07:21:53 +00:00
// even, 双日
2020-09-30 10:51:46 +00:00
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/'
2020-09-15 07:21:53 +00:00
// CF proxy all, 一切给CF代理true/false
const CFproxy = true
// Used in cloudflare workers, odd or even days point to 2 heroku account.
// 由于heroku不绑卡不能自定义域名就算绑卡后https也不方便
// 另外免费套餐每月550小时有些人不够用
// 于是在CF Workers使用此代码分单双日拉取不同heroku帐号下的相同网页
// 只改上面,下面不用动
addEventListener('fetch', event => {
let url=new URL(event.request.url);
if (url.protocol == 'http:') {
url.protocol = 'https:'
2020-09-30 10:51:46 +00:00
event.respondWith( Response.redirect(url.href) )
2020-09-15 07:21:53 +00:00
} else {
2020-09-30 10:51:46 +00:00
let response = null;
2020-09-28 14:45:28 +00:00
let nd = new Date();
if (nd.getDate()%2) {
host = SingleDay
} else {
host = DoubleDay
}
2020-09-30 10:51:46 +00:00
if (host.substr(0, 7)!='http://'&&host.substr(0, 8)!='https://') host = 'http://' + host;
response = fetchAndApply(host, event.request);
event.respondWith( response );
2020-09-15 07:21:53 +00:00
}
})
2020-09-30 10:51:46 +00:00
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;
2020-09-15 07:21:53 +00:00
2020-09-30 10:51:46 +00:00
let response = null;
if (!CFproxy) {
2020-10-03 01:47:18 +00:00
response = await fetch(f_url, request);
2020-09-30 10:51:46 +00:00
} 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);
2020-09-15 07:21:53 +00:00
2020-09-30 10:51:46 +00:00
response = await fetch(f_url.href, {
method: method,
body: body,
headers: new_request_headers
});
}
2020-09-15 07:21:53 +00:00
2020-09-30 10:51:46 +00:00
let out_headers = new Headers(response.headers);
if (out_headers.get('Content-Disposition')=='attachment') out_headers.delete('Content-Disposition');
let out_body = null;
2020-10-03 01:47:18 +00:00
let contentType = out_headers.get('Content-Type');
2020-09-30 10:51:46 +00:00
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();
2020-10-03 03:28:56 +00:00
while (replace_path!='/'&&out_body.includes(replace_path)) out_body = out_body.replace(replace_path, replaced_path);
2020-09-30 10:51:46 +00:00
} else {
out_body = await response.body;
}
2020-09-15 07:21:53 +00:00
2020-09-30 10:51:46 +00:00
let out_response = new Response(out_body, {
status: response.status,
headers: out_headers
2020-09-15 07:21:53 +00:00
})
2020-09-30 10:51:46 +00:00
return out_response;
2020-09-15 07:21:53 +00:00
}