跳至主要內容

搭建常用镜像网站

白日梦想家yy...大约 1 分钟

搭建常用镜像网站

CloudFlare Worker搭建常用镜像网站,账户/密码需要解密
账户:bXlvcmFuZ2UyMDA5QHllYWgubmV0
密码:eXk5NDA1MTAh
模板代码,可能随时失效,复制到workers服务代码块中,使用时测试

const config = {  
    basic: {  
        upstream:'https://www.google.com/',  
    },  
    firewall: {  
        blockedRegion:['KP'],  
        blockedIPAddress:[],  
        scrapeShield:true,  
    },  
    routes: {  
    },  
    optimization: {  
        cacheEverything:false,  
        cacheTtl:5,  
        mirage:true,  
        polish:'off',  
        minify: {  
            javascript:true,  
            css:true,  
            html:true,  
        },  
    },  
};  
async function isMobile(userAgent){  
    const agents = ['Android','iPhone','SymbianoS','Windows Phone','iPad','iPod'];  
    return agents.any((agent) => userAgent.indexOf(agent) > 0);  
}  
async function fetchAndApply(request) {  
    const region = request.headers.get('cf-ipcountry') || '';  
    const ipAddress = request.headers.get('cf-connecting-ip') || '';  
    const userAgent = request.headers.get('user-agent') || '';  
  
    if (region  !== '' && config.firewall.blockedRegion.includes(region.toUpperCase())) {  
        return new Response(  
            'Access denied:booster.js is not available in your region.',  
            {  
                status:403,  
            },  
        );  
    } if  (ipAddress  !== '' && config.firewall.blockedIPAddress.includes(ipAddress)) {  
        return new Response(  
            'Access denied:Your IP address is blocked by booster.js.',  
            {  
                status:403,  
            },  
        );  
    }  
    const requestURL = new URL(request.url);  
    let upstreamURL = null;  
  
    if (userAgent && isMobile(userAgent) === true) {  
        upstreamURL = new URL(config.basic.mobileRedirect);  
    } else if (region && region.toUpperCase() in config.routes) {  
        upstreamURL = new URL(config.routes[region.toUpperCase()]);  
    }  else {  
        upstreamURL = new URL(config.basic.upstream);  
    }  
  
    requestURL.protocol = upstreamURL.protocol;  
    requestURL.host = upstreamURL.host;  
    requestURL.pathname = upstreamURL.pathname + requestURL.pathname;  
  
    let newRequests;  
    if (request.method === 'GET' || request.method === 'HEAD') {  
        newRequests = new Request(requestURL, {  
            cf: {  
                cacheEverything: config.optimization.cacheEverything,  
                cacheTtl: config.optimization.cacheTtl,  
                mirage: config.optimization.mirage,  
                polish: config.optimization.polish,  
                minify: config.optimization.minify,  
                scrapeShield: config.firewall.scrapeShield,  
            },  
            method: request.method,  
            headers: request.headers,  
        });  
    } else {  
        const  requestBody = await  request.text();  
        newRequests = new Request(requestURL,{  
            cf: {  
                cacheEverything: config.optimization.cacheEverything,  
                cacheTtl: config.optimization.cacheTtl,  
                mirage: config.optimization.mirage,  
                polish: config.optimization.polish,  
                minify: config.optimization.minify,  
                scrapeShield: config.firewall.scrapeShield,  
            },  
            method: request.method,  
            headers: request.headers,  
            body: requestBody,  
        })  
    }  
  
    const fetchedResponse = await  fetch(newRequests);  
  
    const modifiedResponseHeaders = new Headers(fetchedResponse.headers);  
    if (modifiedResponseHeaders.has('x-pjax-url')) {  
        const pjaxURL = new URL(modifiedResponseHeaders.get('x-pjax-url'));  
        pjaxURL.protocol = requestURL.protocol;  
        pjaxURL.host = requestURL.host;  
        pjaxURL.pathname = pjaxURL.path.replace(requestURL.pathname, '/');  
  
        modifiedResponseHeaders.set(  
            'x-pjax-url',  
            pjaxURL.href,  
        );  
    }  
  
    return  new  Response(  
        fetchedResponse.body,  
        {  
            headers: modifiedResponseHeaders,  
            status: fetchedResponse.status,  
            statusText: fetchedResponse.statusText,  
        },  
    );  
}  
  
addEventListener('fetch',(event) => {  
    event.respondWith(fetchAndApply((event.request)));  
});
上次编辑于:
贡献者: mygit