Пример #1
0
        private async Task <bool> StartTls(ClientMetadata client)
        {
            try
            {
                await client.SslStream.AuthenticateAsServerAsync(
                    _SslCertificate,
                    MutuallyAuthenticate,
                    SslProtocols.Tls12,
                    !AcceptInvalidCertificates);

                if (!client.SslStream.IsEncrypted)
                {
                    Logger?.Invoke(_Header + "Client " + client.IpPort + " not encrypted, disconnecting");
                    client.Dispose();
                    return(false);
                }

                if (!client.SslStream.IsAuthenticated)
                {
                    Logger?.Invoke(_Header + "Client " + client.IpPort + " not SSL/TLS authenticated, disconnecting");
                    client.Dispose();
                    return(false);
                }

                if (MutuallyAuthenticate && !client.SslStream.IsMutuallyAuthenticated)
                {
                    Logger?.Invoke(_Header + "Client " + client.IpPort + " failed mutual authentication, disconnecting");
                    client.Dispose();
                    return(false);
                }
            }
            catch (Exception e)
            {
                Logger?.Invoke(_Header + "Client " + client.IpPort + " SSL/TLS exception: " + Environment.NewLine + e.ToString());
                client.Dispose();
                return(false);
            }

            return(true);
        }
Пример #2
0
        /// <summary>
        /// Disconnects the specified client.
        /// </summary>
        /// <param name="ipPort">IP:port of the client.</param>
        public void DisconnectClient(string ipPort)
        {
            if (String.IsNullOrEmpty(ipPort))
            {
                throw new ArgumentNullException(nameof(ipPort));
            }

            lock (_ClientsLock)
            {
                if (_Clients.ContainsKey(ipPort))
                {
                    ClientMetadata client = _Clients[ipPort];
                    client.Dispose();
                    _Clients.Remove(ipPort);
                }

                Logger?.Invoke(_Header + "Removed: " + ipPort);
            }

            ClientDisconnected?.Invoke(this, new ClientDisconnectedEventArgs(ipPort, DisconnectReason.Kicked));
        }
Пример #3
0
        private async void AcceptConnections()
        {
            while (!_Token.IsCancellationRequested)
            {
                ClientMetadata client = null;

                try
                {
                    System.Net.Sockets.TcpClient tcpClient = await _Listener.AcceptTcpClientAsync();

                    string clientIp = tcpClient.Client.RemoteEndPoint.ToString();

                    client = new ClientMetadata(tcpClient);

                    if (_Ssl)
                    {
                        if (AcceptInvalidCertificates)
                        {
                            client.SslStream = new SslStream(client.NetworkStream, false, new RemoteCertificateValidationCallback(AcceptCertificate));
                        }
                        else
                        {
                            client.SslStream = new SslStream(client.NetworkStream, false);
                        }

                        bool success = await StartTls(client);

                        if (!success)
                        {
                            client.Dispose();
                            continue;
                        }
                    }

                    lock (_Clients)
                    {
                        _Clients.Add(clientIp, client);
                    }

                    Logger?.Invoke("Starting connection monitor for: " + clientIp);
                    Task unawaited = Task.Run(() => ClientConnectionMonitor(client), client.Token);
                    ClientConnected?.Invoke(this, new ClientConnectedEventArgs(clientIp));
                }
                catch (OperationCanceledException)
                {
                    return;
                }
                catch (ObjectDisposedException)
                {
                    if (client != null)
                    {
                        client.Dispose();
                    }
                    continue;
                }
                catch (Exception e)
                {
                    if (client != null)
                    {
                        client.Dispose();
                    }
                    Logger?.Invoke(_Header + "Exception while awaiting connections: " + e.ToString());
                    continue;
                }
            }

            _IsListening = false;
        }