Exemple #1
0
        /// <summary>
        /// Authenticate client.
        /// </summary>
        /// <param name="remoteEndpoint">Uri connect.</param>
        /// <param name="sslStream">Sslstreamn argument.</param>
        /// <returns>Returned true if success.</returns>
        private static bool AuthenticateAsClient(Uri remoteEndpoint, SslStream sslStream)
        {
            try
            {
                X509Certificate certificate = new X509Certificate("certificate.pfx");
                sslStream.AuthenticateAsClient(remoteEndpoint.Host, new X509CertificateCollection(new[] { certificate }), SslProtocols.Tls, false);
            }
            catch (AuthenticationException e)
            {
                SMLogger.LogError(e.Message);
                if (e.InnerException != null)
                {
                    SMLogger.LogError(string.Format("Inner exception: {0}", e.InnerException.Message));
                }

                SMLogger.LogError("Authentication failed - closing the connection.");
                return(false);
            }

            return(true);
        }
Exemple #2
0
        /// <summary>
        /// Get files via http request.
        /// </summary>
        /// <param name="content">Downloaded file.</param>
        /// <param name="type">Type of file.</param>
        /// <param name="requestUri">Full name of file.</param>
        /// <param name="uri">The address site.</param>
        private void PrivateGetFile(byte[] content, string type, Uri requestUri, string uri)
        {
            byte[] headers = this.GetHeaders(content);
            if (headers == null)
            {
                SMLogger.LogError("HTTP response: Invalid");
                return;
            }

            this.httpMonitor.LogResponse(headers, content.Length);

            int status = this.GetStatus(headers);

            SMLogger.LogInfo(string.Format("HTTP response: {0}, length: {1}  name:{2}", status, content.LongLength, uri));

            if (status == 200 || status == 101)
            {
                string url       = requestUri.Scheme + "://" + requestUri.Authority;
                string directory = string.Empty;
                string localDir  = string.Empty;
                string file      = requestUri.LocalPath;
                string localFile = Path.GetFileName(uri);

                for (int i = 0; i < requestUri.Segments.Length - 1; i++)
                {
                    directory += requestUri.Segments[i];
                    localDir  += requestUri.Segments[i].Replace('/', '\\');
                }

                if (!string.IsNullOrEmpty(localDir))
                {
                    if (localDir[0] == '\\')
                    {
                        localDir = '.' + localDir;
                    }

                    Directory.CreateDirectory(localDir);
                    localFile = localDir + '\\' + localFile;
                }

                int contentOffset = headers.Length;
                using (var fs = new FileStream(localFile, FileMode.Create))
                {
                    fs.Write(content, contentOffset, content.Length - contentOffset);
                }

                if (type == ContentTypes.TextHtml)
                {
                    string strContent = Encoding.UTF8.GetString(content, contentOffset, content.Length - contentOffset)
                                        .Replace("http2frame_start\r\n", "")
                                        .Replace("http2frame_end", "");

                    XHtmlDocument document = XHtmlDocument.Parse(strContent);

                    string path = url + directory;
                    foreach (var image in document.Images)
                    {
                        this.AddNameToDownloadList(string.Format("{0}/{1}", path.ToLower(), image.ToLower()));
                    }

                    foreach (var link in document.Links)
                    {
                        this.AddNameToDownloadList(string.Format("{0}/{1}", path.ToLower(), link.ToLower()));
                    }

                    foreach (var script in document.Scripts)
                    {
                        this.AddNameToDownloadList(string.Format("{0}/{1}", path.ToLower(), script.ToLower()));
                    }
                }
            }             // if status 200
        }