Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Onedrive+OneManager+Heroku+CFWorkers #19

Open
dalaolala opened this issue Aug 30, 2020 · 0 comments
Open

Onedrive+OneManager+Heroku+CFWorkers #19

dalaolala opened this issue Aug 30, 2020 · 0 comments

Comments

@dalaolala
Copy link
Owner

准备工作:
1, Onedrive,或Sharepoint
这个大家有的,各种A1,E3,E5,世纪。(Sharepoint默认25T)
2, OneManager,Github地址:https://github.com/qkqpttgf/OneManager-php
基于各种无服务器的平台,比如Heroku、腾讯SCF、阿里FC、华为FG、百度CFC,配置都保存在其环境变量中,当然也可以装在VPS与空间里,不过为了一致,程序不固定储存onedrive的目录结构,缓存最多1小时
3, Heroku,最好需要注册2个帐号,注册与登录时需要魔法,平时操作时不需要
OneManager配置是保存在环境变量中的,所以不需要一直监控保活,Heroku免费套餐每月550小时,550小时/30天=18.33小时,每天只要访问时间段空出来24-18=6小时没有人访问,这一个月其实就足够使用了,但有些人的网站可能访问人比较多所以还没到月底就用光了。
当然,heroku也可绑卡达到1000小时,同时还能自定义域名。但,SSL却比较麻烦。
4, CF workers,免费的就好 部署参考 这里

部署程序、安装程序、绑定网盘:
1, 将OneManager一键部署到Heroku
2, 访问给定的herokuapp域名,正式安装好程序
3, 安装好后登录,在设置中绑定Onedrive或Sharepoint
4, 在设置中做些设定
5, 换一个Heroku帐号再安装一次(时长够用就无所谓)

重点:
在CF Workers中填入以下代码

// 单日
const SingleDay = '111.herokuapp.com'
// 双日
const DoubleDay = '222.herokuapp.com'
// 一切给CF代理,true/false
const CFproxy = true

// 由于heroku不绑卡不能自定义域名,就算绑卡后https也不方便
// 另外免费套餐每月550小时,有些人不够用
// 于是在CF Workers使用此代码,分单双日拉取不同heroku帐号下的相同网页
// 只改上面,下面不用动

addEventListener('fetch', event => {
    let nd = new Date();
    if (nd.getDate()%2) {
        host = SingleDay
    } else {
        host = DoubleDay
    }
    if (!CFproxy) {
        let url=new URL(event.request.url);
        if (url.protocol == 'http:') {
            url.protocol = 'https:'
            response = Response.redirect(url.href);
            event.respondWith( response );
        } else {
            url.hostname=host;
            let request=new Request(url,event.request);
            event.respondWith( fetch(request) )
        }
    } else {
        event.respondWith( fetchAndApply(event.request) );
    }
})

async function fetchAndApply(request) {
    let response = null;
    let url = new URL(request.url);
    if (url.protocol == 'http:') {
        url.protocol = 'https:'
        response = Response.redirect(url.href);
        return response;
    }
    url.host = host;

    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', url.host);
    new_request_headers.set('Referer', request.url);

    let original_response = await fetch(url.href, {
        method: method,
        body: body,
        headers: new_request_headers
    });

    response = new Response(original_response.body, {
        status: original_response.status,
        headers: original_response.headers
    })

    return response;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant