This repository has been archived by the owner on May 30, 2022. It is now read-only.
forked from tindy2013/now-subconverter
-
Notifications
You must be signed in to change notification settings - Fork 359
cloudflare worker脚本
zhongfly edited this page Aug 16, 2020
·
1 revision
blacklist = [ ];
whitelist = [ "^https://api.github.com" ]; // regexp for whitelisted url
var mytoken='token '
function isListed(uri,listing) {
var ret=false;
if (typeof uri == "string") {
listing.forEach((m)=>{
if (uri.match(m)!=null) ret=true;
});
} else { // decide what to do when Origin is null
ret=true; // true accepts null origins false rejects them.
}
return ret;
}
async function handleRequest(request) {
var origin_url = new URL(request.url);
var fetch_url = unescape(unescape(origin_url.search.substr(1)));
const { headers } = request
const contentType = headers.get('content-type')
if ((!isListed(fetch_url, blacklist)) && (isListed(fetch_url, whitelist))) {
var newRequestInit = {
headers: {
'User-Agent': 'cloudflare',
'Accept': 'application/vnd.github.v3+json',
},
}
if((request.method == 'POST')&&(contentType.includes('application/json'))){
var body = await request.json()
if ('token' in body) {
mytoken+=body.token
delete body.token
}
if ('inputs' in body) {
if ('message' in body.inputs) {
body.inputs.message = body.inputs.message.replace(/(\r\n\t|\n|\r\t)/gm,"");
body.inputs.message = body.inputs.message.replace(/(^\s*)|(\s*$)/g, "");
}
}
var newRequestInit = {
headers: {
'Authorization': mytoken,
'User-Agent': 'cloudflare',
'Accept': 'application/vnd.github.v3+json',
},
body: JSON.stringify(body),
}
}
const newRequest = new Request(
fetch_url,
new Request(request, newRequestInit),
)
try {
var response = await fetch(newRequest)
return response
} catch (e) {
return new Response(JSON.stringify({ error: e.message }), { status: 500 })
}
} else {
return new Response(
"403</br>\n" +
"fetch_url is not allowed</br>\n",
{
status: 403,
statusText: 'Forbidden',
headers: {
"Content-Type": "text/html"
}
});
}
}
addEventListener('fetch', async event => {
event.respondWith(handleRequest(event.request))
})