用cloudflare的workers反向代理onemanager

用cloudflare的workers反向代理onemanager-清风博客

由于国内访问onedrive速度实在是不太理想,大多数地区下载可能就仅仅比某度盘好那么一点点,而世纪互联又太贵,所以只好用上cf的workers来给od加速一下。

首先,当然是注册一个自己的cf账号(Cloudflare官网),登录后点击左侧的workers。

用cloudflare的workers反向代理onemanager-清风博客

没使用过的需要你随意创建一个子域名,然后点击创建workers,并将workers里面原来的代码删除。

用cloudflare的workers反向代理onemanager-清风博客

将反生成代码复制到workers中,吧代码中的两个域名替换成你的onemenager域名(至于为什么有两个,这是因为heroku一个月只有550小时, 有些人用是不够的,所以奇偶天填写不同的域名,单日和双日访问不同的heroku,如果你的onemanager不是建立在heroku上的,就填写同一个域名)。

// odd, 单日
const SingleDay = 'https://xxx.herokuapp.com'
// even, 双日
const DoubleDay = 'https://xxx.herokuapp.com'

//const SingleDay = 'https://153xxxxx0.cn-hongkong.fc.aliyuncs.com/2016-08-15/proxy/onedrive/xxx/'
//const DoubleDay = 'https://153xxxxx0.cn-hongkong.fc.aliyuncs.com/2016-08-15/proxy/onedrive/xxx/'

// 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:'
        event.respondWith( Response.redirect(url.href) )
    } else {
        let response = null;
        let nd = new Date();
        if (nd.getDate()%2) {
            host = SingleDay
        } else {
            host = DoubleDay
        }
        if (host.substr(0, 7)!='http://'&&host.substr(0, 8)!='https://') host = 'http://' + host;

        response = fetchAndApply(host, event.request);

        event.respondWith( response );
    }
})

async function fetchAndApply(host, request) {
    let f_url = new URL(request.url);
    let a_url = new URL(host);
    let replace_path = a_url.pathname;
    if (replace_path.substr(replace_path.length-1)!='/') replace_path += '/';
    let replaced_path = '/';
    let query = f_url.search;
    let path = f_url.pathname;
    if (host.substr(host.length-1)=='/') path = path.substr(1);
    f_url.href = host + path + query;

    let response = null;
    if (!CFproxy) {
        response = await fetch(f_url, request);
    } else {
        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', f_url.host);
        new_request_headers.set('Referer', request.url);

        response = await fetch(f_url.href, {
            method: method,
            body: body,
            headers: new_request_headers
        });
    }

    let out_headers = new Headers(response.headers);
    if (out_headers.get('Content-Disposition')=='attachment') out_headers.delete('Content-Disposition');
    let out_body = null;
    let contentType = out_headers.get('Content-Type');
    if (contentType.includes("application/text")) {
        out_body = await response.text();
        while (out_body.includes(replace_path)) out_body = out_body.replace(replace_path, replaced_path);
    } else if (contentType.includes("text/html")) {
        out_body = await response.text();
        while (replace_path!='/'&&out_body.includes(replace_path)) out_body = out_body.replace(replace_path, replaced_path);
    } else {
        out_body = await response.body;
    }

    let out_response = new Response(out_body, {
        status: response.status,
        headers: out_headers
    })

    return out_response;
}

或者用下面的代码也一样:

// 替换成你OneDrive的网址
const upstream = 'xxx-my.sharepoint.com'
 
// 替换成你OneDrive的网址
const upstream_mobile = 'xxx-my.sharepoint.com'
 
// 下面的配置都不用动
const upstream_path = '/'
 
const blocked_region = ['KP', 'SY', 'PK', 'CU']
 
const blocked_ip_address = ['0.0.0.0', '127.0.0.1']
 
const https = true
 
const disable_cache = false
 
const replace_dict = {
    '$upstream': '$custom_domain',
    '//sunpma.com': ''
}
 
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 = new URL(request.url);
    let url_hostname = url.hostname;
 
    if (https == true) {
        url.protocol = 'https:';
    } else {
        url.protocol = 'http:';
    }
 
    if (await device_status(user_agent)) {
        var upstream_domain = upstream;
    } else {
        var upstream_domain = upstream_mobile;
    }
 
    url.host = upstream_domain;
    if (url.pathname == '/') {
        url.pathname = upstream_path;
    } else {
        url.pathname = upstream_path + url.pathname;
    }
 
    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('Host', upstream_domain);
        new_request_headers.set('Referer', url.protocol + '//' + url_hostname);
 
        let original_response = await fetch(url.href, {
            method: method,
            headers: new_request_headers
        })
 
        connection_upgrade = new_request_headers.get("Upgrade");
        if (connection_upgrade && connection_upgrade.toLowerCase() == "websocket") {
            return original_response;
        }
 
        let original_response_clone = original_response.clone();
        let original_text = null;
        let response_headers = original_response.headers;
        let new_response_headers = new Headers(response_headers);
        let status = original_response.status;
        
        if (disable_cache) {
            new_response_headers.set('Cache-Control', 'no-store');
        }
 
        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');
        
        if (new_response_headers.get("x-pjax-url")) {
            new_response_headers.set("x-pjax-url", response_headers.get("x-pjax-url").replace("//" + upstream_domain, "//" + url_hostname));
        }
        
        const content_type = new_response_headers.get('content-type');
        if (content_type != null && content_type.includes('text/html') && content_type.includes('UTF-8')) {
            original_text = await replace_response_text(original_response_clone, upstream_domain, url_hostname);
        } else {
            original_text = original_response_clone.body
        }
        
        response = new Response(original_text, {
            status,
            headers: new_response_headers
        })
    }
    return response;
}
 
async function replace_response_text(response, upstream_domain, host_name) {
    let text = await response.text()
 
    var i, j;
    for (i in replace_dict) {
        j = replace_dict[i]
        if (i == '$upstream') {
            i = upstream_domain
        } else if (i == '$custom_domain') {
            i = host_name
        }
 
        if (j == '$upstream') {
            j = upstream_domain
        } else if (j == '$custom_domain') {
            j = host_name
        }
 
        let re = new RegExp(i, 'g')
        text = text.replace(re, j);
    }
    return text;
}
 
 
async function device_status(user_agent_info) {
    var agents = ["Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod"];
    var flag = true;
    for (var v = 0; v < agents.length; v++) {
        if (user_agent_info.indexOf(agents[v]) > 0) {
            flag = false;
            break;
        }
    }
    return flag;
}

然后访问此workers的域名就完成了。(如果你的od是世纪互联,请将下图的true改成false)

用cloudflare的workers反向代理onemanager-清风博客

如果你的onemanager同时添加了国际版和世纪互联,那建议分别反代onemanager和onedrive

首先,还是按照上文的方式反代om(并且将下图中的true改成false)。

用cloudflare的workers反向代理onemanager-清风博客

然后,用同样的方式反代onedrive,代码里面的域名改成你od的下载域名(xxxx-my.sharepoint.com)。
最后,将workers的域名填到onemanager的后台’domainforproxy’处

用cloudflare的workers反向代理onemanager-清风博客
温馨提示:本文最后更新于2022-04-13 15:44:28,某些文章具有时效性,若有错误或已失效,请在下方留言或联系清风#
© 版权声明
THE END
文章不错?点个赞呗!
点赞49 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容