站长可以用另一种思路解决自家网站被屏蔽的问题

已收录   阅读次数: 2,023
2019-10-0618:00:19 发表评论
摘要

站长之前介绍过利用cloudflare workers搭建的镜像网站可以访问谷歌等知名网站的办法,具体可以参看之前的网站,或者在本站搜索谷歌就可以,于是站长想了想,是否可以也能让不能访问的域名,也能访问呢?站长决定试了试,发现是可以的,于是就有了今天这篇文章……

分享至:
站长可以用另一种思路解决自家网站被屏蔽的问题

开篇寄语

站长之前介绍过利用cloudflare workers搭建的镜像网站可以访问谷歌等知名网站的办法,具体可以参看之前的网站,或者在本站搜索谷歌就可以,于是站长想了想,是否可以也能让不能访问的域名,也能访问呢?站长决定试了试,发现是可以的,于是就有了今天这篇文章。

实现方法

// List of domains bind to your WorkersProxy.
const domain_list = ['https://cdn.reverse-proxy.live/', 'https://google.xasiimov.workers.dev/']

// Website you intended to retrieve for users.
const upstream = 'https://www.google.com/'

// Website you intended to retrieve for users using mobile devices.
const upstream_mobile = 'https://www.google.com/'

// Countries and regions where you wish to suspend your service.
const blocked_region = ['CN', 'KP', 'SY', 'PK', 'CU']

// IP addresses which you wish to block from using your service.
const blocked_ip_address = ['0.0.0.0', '127.0.0.1']

addEventListener('fetch', event => {
    event.respondWith(fetchAndApply(event.request));
})

async function fetchAndApply(request) {

    const region = request.headers.get('cf-ipcountry').toUpperCase();
    const ip_address = request.headers.get('cf-connecting-ip');
    const user_agent = request.headers.get('user-agent');
    let response = null;
    let url = request.url;

    if (await device_status(user_agent)){
        upstream_domain = upstream
    } else {
        upstream_domain = upstream_mobile
    }

    for(let domain of domain_list) {
        url = url.replace(domain, upstream_domain)
    };

    if (blocked_region.includes(region)) {
        response = new Response('Access denied: WorkersProxy is not available in your region yet.', {
            status: 403
        });
    } else if(blocked_ip_address.includes(ip_address)){
        response = new Response('Access denied: Your IP address is blocked by WorkersProxy.', {
            status: 403
        });
    } else{
        let method = request.method;
        let request_headers = request.headers;
        let new_request_headers = new Headers(request_headers)

        new_request_headers.set('origin', upstream_domain)
        new_request_headers.set('referer', upstream_domain)

        origin_response = await fetch(url, {
            method: method,
            headers: new_request_headers
        })
        
        let response_body = origin_response.body
        let response_headers = origin_response.headers
        let new_response_headers = new Headers(response_headers)
        let status = origin_response.status
        
        new_response_headers.set('access-control-allow-origin', '*')
        new_response_headers.set('access-control-allow-credentials', true)
        new_response_headers.delete('content-security-policy')
        new_response_headers.delete('content-security-policy-report-only')
        new_response_headers.delete('clear-site-data')

        response = new Response(response_body, {
            status,
            headers: response_headers
        })
        response = origin_response
    }
    return response;
}

async function device_status (userAgentInfo) {
    var Agents = ["Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod"];
    var flag = true;
    for (var v = 0; v < Agents.length; v++) {
        if (userAgentInfo.indexOf(Agents[v]) > 0) {
            flag = false;
            break;
        }
    }
    return flag;
}
  • const domain_list的域名换上左上方的域名,域名可以自定义,下方绑定文章有介绍,可以换到自家域名,前提是使用了cloudflare的cdn跳转服务;
  • 把大佬源码里的,“CN”删除。在const upstream = 'https://www.google.com/',换上自家域名,一样可以实现访问的目的;
  • 随后将IP解析到这个域名即可。
  • 我的微信
  • 微信扫一扫加好友
  • weinxin
  • 我的微信公众号
  • 扫描关注公众号
  • weinxin

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: