.NET WebRequest到CloudFlare会在闲置1小时后导致SSL异常

ult*_*ool 6 .net c# ssl httpwebrequest cloudflare

我一直在使用我用来将图像上传到CloudFlare后面的网站的工具出现问题.此工具首先正常工作,并继续运行,除非请求之间有一个> 1小时的暂停.暂停后,下次连接尝试时会发生异常.

A first chance exception of type 'System.Net.WebException' occurred in System.dll
System.Net.WebException: The request was aborted: Could not create SSL/TLS secure channel.
at System.Net.HttpWebRequest.GetResponse()
Run Code Online (Sandbox Code Playgroud)

我尝试使用调试器来深入研究,但没有InnerException,并且在建立任何连接之前,似乎实际问题源自SChannel.这可以使用以下小程序轻松复制:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Initial connection attempt, this should succeed:");
        RunCFRequest();
        Console.WriteLine("Wait 70 minutes for next connection attempt...");
        Thread.Sleep(70*60*1000);
        Console.WriteLine("Second connection attempt, this one should reproduce the failure:");
        try
        {
            RunCFRequest();
        }
        catch (Exception exc)
        {
            Console.WriteLine(exc.ToString());
        }
        Console.WriteLine("Performing another connection attempt after failure to verify we continue working:");
        RunCFRequest();
        Console.WriteLine("Demo complete. Press any key to exit.");
        Console.ReadKey();
    }

    private static void RunCFRequest()
    {
        Console.WriteLine("Attempting connection at " + DateTime.Now);
        var request = (HttpWebRequest) WebRequest.Create("https://up1.ca");
        using (var response = request.GetResponse())
        {
            using (var responseStream = response.GetResponseStream())
            {
                using (var streamReader = new StreamReader(responseStream))
                {
                    string recvd = streamReader.ReadToEnd();
                    Console.WriteLine("Successfully read stream, got " + recvd.Length + " bytes of data");
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这个简单的代码有什么问题吗?我试图进行数据包捕获以解决为什么会发生这种情况.

可通过https://up1.ca/#4MMkdD_u8v5pLAsSvrCtHw以pcapng格式获取Capture .

捕获包含3个TCP流,可以使用以下wireshark过滤器访问它们:

  • tcp.stream eq 0 =初始连接,成功
  • tcp.stream eq 1 = 70分钟后的第二次连接,由于上述异常而失败
  • tcp.stream eq 2 =在处理并忽略该异常后的另一次尝试,这成功了

基于捕获,我最好的猜测是它与CloudFlare如何恢复SSL会话有关.HttpWebRequest或Microsoft SChannel本身是否存在任何已知问题以及SSL恢复,或者此问题是否特定于CloudFlare?我已经成功地在CloudFlare后面的多个站点上复制了这个问题,但是在直接使用我自己的服务器时我没有遇到过这个问题.我没有SSL恢复.

任何帮助甚至狂野的理论都值得赞赏.我不知道从哪里开始,如果有人能看一下捕捉,我会很感激,如果需要,我会向CF报告.