Giter VIP home page Giter VIP logo

cf-herokus's Introduction

CF-Herokus

Heroku free plan is only providing 550 hours usage per month. In this case, multiple Heroku instances can cover the usages for whole month.

This script is just to let Cloudflare easily proxy to multiple Heroku instances based on odd number days or plural number days.

Demo Site

This demo site is to list a onedrive content through web page. https://myod.51sec.eu.org

There are two Heroku Apps behind above URL.

Related post

https://blog.51sec.org/2021/04/deploy-onemanager-to-heroku-and-bypass.html

Steps

1. create Multiple Herohu apps

Basically, you will need two Heroku apps. For example, I created my first one as : https://myod1.herokuapp.com/ and my second one as : https://myod2.herokuapp.com/ Since both apps are used to list files at onedrive, they are basically showing same content.

2. Create a new Cloudflare Worker

Then you will use Cloudflare workers to rotate the access to those two apps using the javascript in this project. Create a new worker:

3. Paste the code into left script panel,save and deploy



4. Option step: create your own dns record and add a route to your worker.

This is an optional steps. If you do not have your own domain, you can still use subdomain from workers.dev. such as my example one: https://myod.51sec1.workers.dev If you got your own domain, you can visit your site using your own subdomain: https://myod.51sec.eu.org

Video

You can check video at https://youtu.be/aJ0SEUSeDZ4 for how to use Cloudflare workers.

Code



// odd days
const SingleDay = 'abc.herokuapp.com'
// plural days
const DoubleDay = 'xyz.herokuapp.com'
// Using CF to do porxy? true/false
const CFproxy = true

// Heroku only has 550 hours/month for free plan by default. 
// This CloudFlare Workers code can let use different Heroku app based on odd or even number's day. 
// Please change above code for your Heroku's app in either SingleDay or Doubleday parameter. 

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;
}



cf-herokus's People

Contributors

51sec avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

cf-herokus's Issues

How to use this?

Hello;
seems like a really handy script, but i wonder how to use it?

For example i have two workers as
worker1.domain.workers.dev
worker2.domain.workers.dev
so now where to put your script code? i dont have custom domain btw.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.