Deploy OneManager to Heroku and Bypass Free Tier's Restrictions - NETSEC

Latest

Learning, Sharing, Creating

Cybersecurity Memo

Thursday, April 22, 2021

Deploy OneManager to Heroku and Bypass Free Tier's Restrictions

 I have been introduced a couple ways to display OneDrive's content, either using Cloudflare workers or using Heroku. You can find them from my previous posts:


Deploying OneManager to Heroku is simple and easy but there are two issues which will cause the OneManager will be down. 

  1. New accounts receive 550 free dyno hours and you can verify your identity with a credit card for an additional 450 hours.
  2. The app will be put into idle mode automatically, after 30 minutes of inactivity. It will take 1-2 minutes to wake it up when it receives access activity. 


To resolve those two issues, I developed following three steps to bypass them.

Demo site: https://myod.51sec.eu.org

Deploy Two OneManager into Two Heroku Accounts

Lets say, you created two Heroku accounts, and deployed two OneManager apps into them one by one with the process described in my previous post:

You got following two OneManager apps:

  1. https://myod1.herokuapp.com/
  2. https://myod2.herokuapp.com/

They are exactly same except the app name and app url is different. Both apps have added same OneDrive disk in it. 

The logic behind it to use two OneManager apps which not get us exceeding the 550 hours / month limits if we only activate one app every day and keep another one in idle. 


Create a Cloudflare Workers to Rotate Access to Those Two Apps


If just need to use cloudflare worker to use your own domain to route to heroku site, here is worker code

LispaddEventListener(
    "fetch",event => {
        let url=new URL(event.request.url);     
        url.hostname="myod1.herokuapp.com";  
        let request=new Request(url,event.request);    
        event. respondWith(       
            fetch(request)    
        )  
    }
)




Since we have two different myod sites, and now we need to tell CloudFlare Workers to only point to one app at one day and point to anther app at another day. 
Github project: https://github.com/51sec/CF-Herokus/
Basically, this workers code will get Cloudflare route the traffic to myod1 on odd number days and to myod2 on even number days. 

// odd days const SingleDay =
'myod1.herokuapp.com' // plural days const DoubleDay = 'myod2.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; }

Of course, do not forget to set up a DNS A record (myod.51sec.eu.org) and workers route to it.



Create two CRON Job

Since Heroku will put your APP in idle mode once there is no activity for 30 minutes, we will just need to create a CRON job to get the site every 30 minutes at online cron job website.  We will need to create two CRON jobs, one for odd number days to get myod1.herokuapp.com, and second one for even number days to get my od2.herokuapp.com.

Note: www.easycron.com  has 200 EPDs for free users, which is not enough to run for whole month, but you can easily create two easycron accounts to bypass this limitation. Strongly suggest to use https://cron-job.org/en/ instead. 





Done, that is all you will need to set up a OneManager at Heroku, which will make your site up all the time to end user when they using your Cloudflare dns to access your site. 

Note: For https://cron-job.org/en/ site, you will have to configure it to use head as request method, else you will get a failure message "Response data too big". Here is the configuration screenshot:



YouTube


Also I recorded videos to show the whole process:


References




1 comment:

  1. It only works with domain name?
    i mean if i dont have domain name so can i still do this trick to keep the site alive for whole month on heroku?

    ReplyDelete