绕过 CloudFlare 的 100 秒超时
Bypassing CloudFlare's time-out of 100 seconds
我正在尝试 AJAX-ify 我的报告,以绕过 CloudFlare 对通过其站点 运行 的请求施加的 100 秒超时。
见
我做了以下事情:
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;
});
}
和以下 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%>
该代码在不到 100 秒的时间内运行良好。但是没有工作超过 100 秒。
有什么想法吗?
反馈后更新
我的报告现在可以在没有 AJAX 的情况下正常工作(尽管 CloudFlare 有 100 秒超时)。我试图将它们转换为 AJAX 以避免子域出现灰色云,因为我不想公开我的 IP 地址。如果我要灰化一个子域,我会在原始代码上进行,这比 AJAX- 化我的代码要简单得多!我的问题是 "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..."
由于 cloudflare 不缓存 html 或 xhr,您可以对子域进行 greycloud,但在服务器上将其用作别名。例如...
在 CloudFlare dns 中:
- www 123.123.123.123 = 橙色(受保护)
- ajax 123.123.123.123 = 灰色(仅限 dns)
在您的网站控制面板中添加 ajax.mydomain.com 作为别名。
最后,在您的代码中使用绕过 cloudflare 访问您的服务器的 fqdn。
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;
});
}
这确实暴露了您的 IP 地址。但是应该几乎没有性能损失,具体取决于返回的内容。
以防其他人遇到同样的问题,我将发布我是如何最终实现这一目标的。我放弃了尝试使用 AJAX 到 运行 报告,而是通过线程 运行 报告,而是使用 AJAX 到 "poll" 来检查是否报告已创建。我所做的基本上是这样的。
请注意,我从代码中删除了很多内容,例如安全例程和错误检查例程,只是给出基本框架。
我创建了一个名为 ThreadMyReport
的 java class
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;
}
}
我生成报告的所有原始代码都在 ReportMyReport.loadAndSaveMyReport 中找到。报告完成后,它会在服务器上保存一个 fileName fileID 的文件。
然后我启动了一个线程去 运行 报告
ThreadMyReport a = new ThreadMyReport(theDate);
Thread theThread=new Thread(a);
theThread.start();
fileID=a.getFileID();
然后我添加了一个 java 脚本例程,每秒通过 AJAX 检查文件是否已创建,如果已创建则显示报告。
<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>
AJAX 代码如下所示:
<%
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%>
我正在尝试 AJAX-ify 我的报告,以绕过 CloudFlare 对通过其站点 运行 的请求施加的 100 秒超时。
见
我做了以下事情:
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;
});
}
和以下 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%>
该代码在不到 100 秒的时间内运行良好。但是没有工作超过 100 秒。
有什么想法吗?
反馈后更新
我的报告现在可以在没有 AJAX 的情况下正常工作(尽管 CloudFlare 有 100 秒超时)。我试图将它们转换为 AJAX 以避免子域出现灰色云,因为我不想公开我的 IP 地址。如果我要灰化一个子域,我会在原始代码上进行,这比 AJAX- 化我的代码要简单得多!我的问题是 "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..."
由于 cloudflare 不缓存 html 或 xhr,您可以对子域进行 greycloud,但在服务器上将其用作别名。例如...
在 CloudFlare dns 中:
- www 123.123.123.123 = 橙色(受保护)
- ajax 123.123.123.123 = 灰色(仅限 dns)
在您的网站控制面板中添加 ajax.mydomain.com 作为别名。
最后,在您的代码中使用绕过 cloudflare 访问您的服务器的 fqdn。
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;
});
}
这确实暴露了您的 IP 地址。但是应该几乎没有性能损失,具体取决于返回的内容。
以防其他人遇到同样的问题,我将发布我是如何最终实现这一目标的。我放弃了尝试使用 AJAX 到 运行 报告,而是通过线程 运行 报告,而是使用 AJAX 到 "poll" 来检查是否报告已创建。我所做的基本上是这样的。
请注意,我从代码中删除了很多内容,例如安全例程和错误检查例程,只是给出基本框架。
我创建了一个名为 ThreadMyReport
的 java classpublic 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;
}
}
我生成报告的所有原始代码都在 ReportMyReport.loadAndSaveMyReport 中找到。报告完成后,它会在服务器上保存一个 fileName fileID 的文件。
然后我启动了一个线程去 运行 报告
ThreadMyReport a = new ThreadMyReport(theDate);
Thread theThread=new Thread(a);
theThread.start();
fileID=a.getFileID();
然后我添加了一个 java 脚本例程,每秒通过 AJAX 检查文件是否已创建,如果已创建则显示报告。
<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>
AJAX 代码如下所示:
<%
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%>