Ejemplo n.º 1
0
 private void UpdateLogFiles(IPBanConfig newConfig)
 {
     // remove existing log files that are no longer in config
     foreach (IPBanLogFileScanner file in logFilesToParse.ToArray())
     {
         if (newConfig.LogFilesToParse.FirstOrDefault(f => f.PathsAndMasks.Contains(file.PathAndMask)) is null)
         {
             file.Dispose();
             logFilesToParse.Remove(file);
         }
     }
     foreach (IPBanLogFileToParse newFile in newConfig.LogFilesToParse)
     {
         string[] pathsAndMasks = newFile.PathsAndMasks;
         for (int i = 0; i < pathsAndMasks.Length; i++)
         {
             string pathAndMask = pathsAndMasks[i];
             if (!string.IsNullOrWhiteSpace(pathAndMask))
             {
                 // if we don't have this log file and the platform matches, add it
                 bool noMatchingLogFile = logFilesToParse.FirstOrDefault(f => f.PathAndMask == pathAndMask) is null;
                 bool platformMatches   = !string.IsNullOrWhiteSpace(newFile.PlatformRegex) &&
                                          Regex.IsMatch(OSUtility.Description, newFile.PlatformRegex.ToString().Trim(), RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
                 if (noMatchingLogFile && platformMatches)
                 {
                     // log files use a timer internally and do not need to be updated regularly
                     IPBanIPAddressLogFileScannerOptions options = new IPBanIPAddressLogFileScannerOptions
                     {
                         Dns                         = service.DnsLookup,
                         LoginHandler                = service,
                         MaxFileSizeBytes            = newFile.MaxFileSize,
                         PathAndMask                 = pathAndMask,
                         PingIntervalMilliseconds    = (service.ManualCycle ? 0 : newFile.PingInterval),
                         RegexFailure                = newFile.FailedLoginRegex,
                         RegexSuccess                = newFile.SuccessfulLoginRegex,
                         RegexFailureTimestampFormat = newFile.FailedLoginRegexTimestampFormat,
                         RegexSuccessTimestampFormat = newFile.SuccessfulLoginRegexTimestampFormat,
                         Source                      = newFile.Source,
                         FailedLoginThreshold        = newFile.FailedLoginThreshold
                     };
                     IPBanLogFileScanner scanner = new IPBanLogFileScanner(options);
                     logFilesToParse.Add(scanner);
                     Logger.Info("Adding log file to parse: {0}", pathAndMask);
                 }
                 else
                 {
                     Logger.Trace("Ignoring log file path {0}, regex: {1}, no matching file: {2}, platform match: {3}",
                                  pathAndMask, newFile.PlatformRegex, noMatchingLogFile, platformMatches);
                 }
             }
         }
     }
 }
Ejemplo n.º 2
0
 private void UpdateLogFiles(IPBanConfig newConfig)
 {
     // remove existing log files that are no longer in config
     foreach (LogFileScanner file in logFilesToParse.ToArray())
     {
         if (newConfig.LogFilesToParse.FirstOrDefault(f => f.PathsAndMasks.Contains(file.PathAndMask)) is null)
         {
             file.Dispose();
             logFilesToParse.Remove(file);
         }
     }
     foreach (IPBanLogFileToParse newFile in newConfig.LogFilesToParse)
     {
         string[] pathsAndMasks = newFile.PathAndMask.Split('\n');
         for (int i = 0; i < pathsAndMasks.Length; i++)
         {
             string pathAndMask = pathsAndMasks[i].Trim();
             if (pathAndMask.Length != 0)
             {
                 // if we don't have this log file and the platform matches, add it
                 if (logFilesToParse.FirstOrDefault(f => f.PathAndMask == pathAndMask) is null &&
                     !string.IsNullOrWhiteSpace(newFile.PlatformRegex) &&
                     Regex.IsMatch(OSUtility.Description, newFile.PlatformRegex.ToString().Trim(), RegexOptions.IgnoreCase | RegexOptions.CultureInvariant))
                 {
                     // log files use a timer internally and do not need to be updated regularly
                     IPBanIPAddressLogFileScannerOptions options = new IPBanIPAddressLogFileScannerOptions
                     {
                         Dns                         = DnsLookup,
                         LoginHandler                = this,
                         MaxFileSizeBytes            = newFile.MaxFileSize,
                         PathAndMask                 = pathAndMask,
                         PingIntervalMilliseconds    = newFile.PingInterval,
                         Recursive                   = newFile.Recursive,
                         RegexFailure                = newFile.FailedLoginRegex,
                         RegexSuccess                = newFile.SuccessfulLoginRegex,
                         RegexFailureTimestampFormat = newFile.FailedLoginRegexTimestampFormat,
                         RegexSuccessTimestampFormat = newFile.SuccessfulLoginRegexTimestampFormat,
                         Source                      = newFile.Source
                     };
                     LogFileScanner scanner = new IPBanIPAddressLogFileScanner(options);
                     logFilesToParse.Add(scanner);
                     Logger.Debug("Adding log file to parse: {0}", pathAndMask);
                 }
                 else
                 {
                     Logger.Debug("Ignoring log file path {0}, regex: {1}", pathAndMask, newFile.PlatformRegex);
                 }
             }
         }
     }
Ejemplo n.º 3
0
        /// <summary>
        /// Create a log file scanner
        /// </summary>
        /// <param name="options">Options</param>
        public IPBanLogFileScanner(IPBanIPAddressLogFileScannerOptions options) : base(options.PathAndMask, options.MaxFileSizeBytes, options.PingIntervalMilliseconds)
        {
            options.ThrowIfNull(nameof(options));
            options.LoginHandler.ThrowIfNull(nameof(options.LoginHandler));
            options.Dns.ThrowIfNull(nameof(options.Dns));
            Source = options.Source;

            this.loginHandler = options.LoginHandler;
            this.dns          = options.Dns;

            this.regexFailure = IPBanConfig.ParseRegex(options.RegexFailure, true);
            this.regexFailureTimestampFormat = options.RegexFailureTimestampFormat;

            this.regexSuccess = IPBanConfig.ParseRegex(options.RegexSuccess, true);
            this.regexSuccessTimestampFormat = options.RegexSuccessTimestampFormat;
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Check if this log file scanner matches all the provided options
        /// </summary>
        /// <param name="options">Options</param>
        /// <returns>True if matches options, false otherwise</returns>
        public bool MatchesOptions(IPBanIPAddressLogFileScannerOptions options)
        {
            if (options is null)
            {
                return(false);
            }

            return(Source == options.Source &&
                   FailedLoginThreshold == options.FailedLoginThreshold &&
                   FailedLogLevel == options.FailedLogLevel &&
                   SuccessfulLogLevel == options.SuccessfulLogLevel &&
                   this.loginHandler == options.LoginHandler &&
                   this.dns == options.Dns &&
                   this.regexFailure?.ToString() == options.RegexFailure?.ToString() &&
                   this.regexFailureTimestampFormat == options.RegexFailureTimestampFormat &&
                   this.regexSuccess?.ToString() == options.RegexSuccess?.ToString() &&
                   this.regexSuccessTimestampFormat == options.RegexSuccessTimestampFormat);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Create a log file scanner
        /// </summary>
        /// <param name="options">Options</param>
        public IPBanLogFileScanner(IPBanIPAddressLogFileScannerOptions options) : base(options.PathAndMask, options.MaxFileSizeBytes, options.PingIntervalMilliseconds)
        {
            options.ThrowIfNull(nameof(options));
            options.LoginHandler.ThrowIfNull(nameof(options.LoginHandler));
            options.Dns.ThrowIfNull(nameof(options.Dns));
            Source = options.Source;
            FailedLoginThreshold = options.FailedLoginThreshold;
            FailedLogLevel       = options.FailedLogLevel;
            SuccessfulLogLevel   = options.SuccessfulLogLevel;

            this.loginHandler = options.LoginHandler;
            this.dns          = options.Dns;

            this.regexFailure = options.RegexFailure;
            this.regexFailureTimestampFormat = options.RegexFailureTimestampFormat;

            this.regexSuccess = options.RegexSuccess;
            this.regexSuccessTimestampFormat = options.RegexSuccessTimestampFormat;
        }