public void Start()
        {
            if (_listener != null)
            {
                throw new InvalidOperationException("Cannot restart server.");
            }

            _listener = new TcpListener(_options.Address, _port);

            _listener.Start(_options.ListenBacklog);
            var ipEndpoint = (IPEndPoint)_listener.LocalEndpoint;

            _port = ipEndpoint.Port;
            _log.WriteLine("[Server] waiting for connections ({0}:{1})", ipEndpoint.Address, ipEndpoint.Port);
        }
Ejemplo n.º 2
0
        public async Task HttpsRequestAsync(Func <string, Task <string> > httpConversation = null)
        {
            _log.WriteLine("[Client] Disabling SslPolicyErrors: {0}", _options.IgnoreSslPolicyErrors.ToString());

            if (httpConversation == null)
            {
                httpConversation = DefaultHttpConversation;
            }

            using (var certValidationPolicy = new SslStreamCertificatePolicy(_options.IgnoreSslPolicyErrors))
                using (var tcp = new TcpClient())
                {
                    await ConnectToHostAsync(tcp);

                    using (Stream = new SslStream(tcp.GetStream(), false, certValidationPolicy.SslStreamCallback))
                    {
                        X509CertificateCollection clientCerts = null;

                        if (_options.ClientCertificate != null)
                        {
                            clientCerts = new X509CertificateCollection();
                            clientCerts.Add(_options.ClientCertificate);
                        }

                        _log.WriteLine(
                            "[Client] Connected. Authenticating: server={0}; clientCert={1}",
                            _options.ServerName,
                            _options.ClientCertificate != null ? _options.ClientCertificate.Subject : "<null>");

                        try
                        {
                            await Stream.AuthenticateAsClientAsync(
                                _options.ServerName,
                                clientCerts,
                                _options.AllowedProtocols,
                                checkCertificateRevocation : false).ConfigureAwait(false);
                        }
                        catch (Exception ex)
                        {
                            _log.WriteLine("[Client] Exception : {0}", ex);
                            throw ex;
                        }

                        _log.WriteLine("[Client] Authenticated protocol {0}", Stream.SslProtocol);

                        int    bytesRead      = 0;
                        string responseString = null;

                        while (true)
                        {
                            string requestString = await httpConversation(responseString).ConfigureAwait(false);

                            if (requestString == null)
                            {
                                return;
                            }

                            if (requestString.Length > 0)
                            {
                                byte[] requestBuffer = Encoding.UTF8.GetBytes(requestString);

                                _log.WriteLine("[Client] Sending request ({0} Bytes)", requestBuffer.Length);
                                await Stream.WriteAsync(requestBuffer, 0, requestBuffer.Length).ConfigureAwait(false);
                            }

                            _log.WriteLine("[Client] Waiting for reply...");

                            byte[] responseBuffer = new byte[2048];
                            bytesRead = await Stream.ReadAsync(responseBuffer, 0, responseBuffer.Length).ConfigureAwait(false);

                            responseString = Encoding.UTF8.GetString(responseBuffer, 0, bytesRead);
                            _log.WriteLine("[Client] {0} Bytes, Response: <{1}>", bytesRead, responseString);
                        }
                    }
                }
        }