/**
* https://********************************.cloudflareworkers.com/
* https://beta.zhwiki.workers.dev/
**/
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(req) {
try{
var url = (new URL(req.url))
if(url.protocol != 'https:'){
url.protocol = 'https:'
return (new Response('', {status: 301, headers: {'Location': url.href}}))
}
var header = (new Headers(req.headers))
var ua = header.get('User-Agent')
var ual = ua.toLowerCase()
var host = url.hostname
var path = url.pathname
var argv = url.search
if(path == '/'){
return (new Response('', {status: 302, headers: {'Location': '/wiki/'}}))
}
if(path == '/w/api.php'){
return (new Response('Api error', {status: 403, statusText: 'Error : https://beta.zhwiki.workers.dev/ can\'t not use MediaWiki Api'}))
}
var res = await fetch(`https://zh.wikipedia.beta.wmflabs.org${path}${argv}`, {
method : req.method,
body: req.body
})
return (new Response(res.body, {status: res.status, headers: res.headers}))
} catch(err){
return (new Response(err, {status: 500}))
}
}