Create CFWorkers.js

pull/128/head
qkqpttgf 2020-09-15 15:21:53 +08:00 committed by GitHub
parent acaeb5d3a0
commit e08ee5ab99
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 63 additions and 0 deletions

63
CFWorkers.js Normal file
View File

@ -0,0 +1,63 @@
// odd, 单日
const SingleDay = 'aaa1.herokuapp.com'
// even, 双日
const DoubleDay = 'bbb2.herokuapp.com'
// 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:'
response = Response.redirect(url.href);
event.respondWith( response );
}
let nd = new Date();
if (nd.getDate()%2) {
host = SingleDay
} else {
host = DoubleDay
}
if (!CFproxy) {
url.hostname=host;
let request=new Request(url,event.request);
event.respondWith( fetch(request) )
} else {
event.respondWith( fetchAndApply(event.request) );
}
})
async function fetchAndApply(request) {
let response = null;
let url = new URL(request.url);
url.host = host;
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', url.host);
new_request_headers.set('Referer', request.url);
let original_response = await fetch(url.href, {
method: method,
body: body,
headers: new_request_headers
});
response = new Response(original_response.body, {
status: original_response.status,
headers: original_response.headers
})
return response;
}