private async Task <Connection> CheckConnectionAttempt(AuthenticationRequest authenticationRequest, PluginConfiguration config) { Connection connection = null; if (FailedAuthenticationAudit.Exists(a => Equals(a.Ip, authenticationRequest.RemoteAddress.ToString()))) { connection = FailedAuthenticationAudit.FirstOrDefault(c => Equals(c.Ip, authenticationRequest.RemoteAddress.ToString())); var connectionLoginAttemptThreshold = config.ConnectionAttemptsBeforeBan != 0 ? config.ConnectionAttemptsBeforeBan : 3; //If this connection has tried and failed, and is not Banned - but has waited over thirty seconds to try again - reset the attempt count and clear FailedAuthDateTimes List. if (DateTime.UtcNow > connection?.FailedAuthDateTimes.LastOrDefault().AddSeconds(30)) { connection.FailedAuthDateTimes.Clear(); connection.LoginAttempts = 0; } //Log the attempt if (connection?.LoginAttempts < connectionLoginAttemptThreshold) { connection.LoginAttempts += 1; connection.FailedAuthDateTimes.Add(DateTime.UtcNow); return(connection); } //Tried to many times in a row, and too quickly -Ban the IP - could be a brute force attack. if (connection?.FailedAuthDateTimes.FirstOrDefault() > DateTime.UtcNow.AddSeconds(-30)) { connection.IsBanned = true; return(connection); } } else { ReverseLookupData targetData = null; if (Plugin.Instance.Configuration.EnableGeoIp && !IsLocalNetworkIp(authenticationRequest.RemoteAddress)) { targetData = await Target.GetLocation(authenticationRequest.RemoteAddress.ToString()); } // ReSharper disable once ComplexConditionExpression connection = new Connection { FlagIconUrl = targetData is null ? string.Empty : targetData.countryFlag, Isp = targetData is null ? string.Empty : targetData.isp, Ip = authenticationRequest.RemoteAddress.ToString(), DeviceName = authenticationRequest.DeviceName, UserAccountName = authenticationRequest.Username, Proxy = targetData?.proxy ?? false, ServiceProvider = targetData is null ? string.Empty : targetData.isp, Longitude = targetData?.lon ?? 0, Latitude = targetData?.lat ?? 0, LoginAttempts = 1, IsBanned = false, Region = targetData?.regionName ?? string.Empty, FailedAuthDateTimes = new List <DateTime> { DateTime.UtcNow } }; FailedAuthenticationAudit.Add(connection); } return(connection); }