Exemple #1
0
        private void ExtensionsManager_OnBeforeNewConnection(WebSocket socket, string ip)
        {
            DateTime currentDate = DateTime.UtcNow;

            if (BanCandidatesManager.RegisterViolation(ip))
            {
                BannedClientsManager.RegisterBan(ip, currentDate);
                throw new ConnectionCloseException(WebSocketCloseStatus.PolicyViolation, "Too many connections.");
            }
            else if (BannedClientsManager.IsClientBanned(ip, currentDate))
            {
                throw new ConnectionCloseException(WebSocketCloseStatus.PolicyViolation, "This client is banned.");
            }
        }
Exemple #2
0
        private void ExtensionsManager_OnConnectionValidated(BaseWebSocketConnection connection)
        {
            var currentDate = DateTime.UtcNow;

            if (BanCandidatesManager.RegisterViolation(connection.ClientPubKey?.ToString()))
            {
                BannedClientsManager.RegisterBan(connection.Ip, currentDate);
                BannedClientsManager.RegisterBan(connection.ClientPubKey?.ToString(), currentDate);
                throw new ConnectionCloseException(WebSocketCloseStatus.PolicyViolation, "Too many connections.");
            }
            else if (BannedClientsManager.IsClientBanned(connection.Ip, currentDate) ||
                     BannedClientsManager.IsClientBanned(connection.ClientPubKey?.ToString(), currentDate))
            {
                throw new ConnectionCloseException(WebSocketCloseStatus.PolicyViolation, "This client is banned.");
            }
        }
Exemple #3
0
        private void ExtensionsManager_OnHandleMessageFailed(BaseWebSocketConnection connection, MessageEnvelope envelope, Exception exception)
        {
            var currentDate = DateTime.UtcNow;

            if (exception is BaseClientException && //bad requests, forbidden, too many requests etc.
                BanCandidatesManager.RegisterViolation(connection.Ip, connection.ClientPubKey?.ToString()))    //riched max allowed violation count
            {
                BannedClientsManager.RegisterBan(connection.Ip, currentDate);
                BannedClientsManager.RegisterBan(connection.ClientPubKey?.ToString(), currentDate);
                throw new ConnectionCloseException(WebSocketCloseStatus.PolicyViolation, "Too many invalid messages.");
            }
            else if (BannedClientsManager.IsClientBanned(connection.Ip, currentDate) ||
                     BannedClientsManager.IsClientBanned(connection.ClientPubKey?.ToString(), currentDate))
            {
                throw new ConnectionCloseException(WebSocketCloseStatus.PolicyViolation, "This client is banned.");
            }
        }