Bypassing CloudFlare's time-out of 100 seconds

13,738

Solution 1

In case anyone else has the same problem, I am posting how I finally got this working. I gave up trying to use AJAX to run the report, but instead ran the report via a Thread, but used AJAX to "poll" to check if the report had been created. What I did was basically as follows.

Note that I stripped a lot out of my code, e.g. security routines and error checking routines, just to give the basic framework.

I created a java class called ThreadMyReport

public class ThreadMyReport implements Runnable {

    String fileID = "";
    Date dateOfReport = null;

    public ThreadMyReport(Date dateOfReport) {
        this.fileID= "MyReport_" + UUID.randomUUID();
        this.dateOfReport = dateOfReport;
    }

    public void run() {
        int a = ReportMyReport.loadAndSaveMyReport(dateOfReport, fileID);
    }


    public String getFileID() {
        return fileID;
    }
}

All my original code to generate the report is found in ReportMyReport.loadAndSaveMyReport. When the report is finished, then it saves a file with fileName fileID on the server.

I then started a thread going to run the report

    ThreadMyReport a  = new ThreadMyReport(theDate);
    Thread theThread=new Thread(a);
    theThread.start();
    fileID=a.getFileID();

I then added a javascript routine to check via AJAX every second whether the file had been created, and if it had been created then to display the report.

<SCRIPT language="javascript">


    var myVar;
    myVar=setInterval(function (){
    $.post("post/post_checkReportReady_xml.jsp", {
       generatedReport: '<%=fileID%>'
    }, function(data) {
       if (data.indexOf("produced")>-1) {
           clearInterval(myVar);
           //display report
       }
       if (data.indexOf("failed")>-1) {
           clearInterval(myVar);
       }
    });

}, 1000);
        </SCRIPT>

The AJAX code looks like this:

     <%
    String result="";

    String generatedReport=(request.getParameter("generatedReport"));

    if(!"".equals(generatedReport)) {
        String fileName2="My directory/"+generatedReport+".xlsm"; 
        java.io.File f = new java.io.File(fileName2);
        if(f.exists()) { 
            result="produced";
        }
    }
}

%>
<%=result%>

Solution 2

Since cloudflare does not cache html or xhr, you can greycloud a subdomain but on the server use that as an alias. For example...

In CloudFlare dns:

  • www 123.123.123.123 = orange (protected)
  • ajax 123.123.123.123 = grey (dns only)

In your website control panel add ajax.mydomain.com as an alias.

Finally, in your code use the fqdn that hits your server bypassing cloudflare.

function ajaxReport() {
    var seconds = prompt("Please enter how many seconds you want the report to run", "5");
    $('#imgWaiting').show();
    $.post("//ajax.mydomain.com/post/post_ajaxReport.jsp",
  {
    theParam:seconds
  },function(data) {
    $('#imgWaiting').hide();
    window.location=data;
 });
}

This does expose your IP address. But there should be little to no performance penalty, depending on the content returned.

Share:
13,738
gordon613
Author by

gordon613

This user likes to keep an air of mystery about them

Updated on June 09, 2022

Comments

  • gordon613
    gordon613 almost 2 years

    I am attempting to AJAX-ify my reports in order to bypass the 100 seconds time-out that CloudFlare imposes on requests that run through its site.

    See Is it possible to increase CloudFlare time-out?

    I did the following:

    function ajaxReport() {
        var seconds = prompt("Please enter how many seconds you want the report to run", "5");
        $('#imgWaiting').show();
        $.post("post/post_ajaxReport.jsp",
      {
        theParam:seconds
      },function(data) {
        $('#imgWaiting').hide();
        window.location=data;
     });
    
    }
    

    and the following for post_ajaxReport.jsp

    <%
     int theParam=myFunctionToConvertStringToInt(request.getParameter("theParam"));
    int a=theParam/60;
    int b=theParam-a*60;
    String query="WAITFOR DELAY '00:"+a+":"+b+"';";
    double d=myCustomCodeToRunQuery(query);
    String fileName=createReport();
    %>
    <%=fileName%>
    

    The code worked great for under 100 seconds. But did not work for over 100 seconds.

    Any ideas?

    UPDATE AFTER FEEDBACK

    My reports work fine now without AJAX (although there is the 100 second time-out with CloudFlare). I was trying to convert them to AJAX in order to avoid grey-clouding a subdomain because I didn't want to expose my IP address. If I was going to grey-cloud a subdomain I would do it on the original code, which would be much simpler than AJAX-ifying my code! My question is "how to fix my AJAX code so that I would have the benefits of avoiding the 100 second timeout, but without the disadvantage of exposing my IP address..."

  • gordon613
    gordon613 almost 7 years
    Thank you @jules for replying. My reports work fine now without AJAX (although there is the 100 second time-out with CloudFlare). I was trying to convert them to AJAX in order to avoid grey-clouding a subdomain because I didn't want to expose my IP address. If I was going to grey-cloud a subdomain I would do it on the original code, which would be much simpler than AJAX-ifying my code! My question was really how to fix my AJAX code so that I would have the benefits of avoiding the 100 second timeout, but without the disadvantage of exposing my IP address... Perhaps it can't be done!