/// <summary> /// Create a log file scanner /// </summary> /// <param name="failedLogin">Interface for handling failed logins</param> /// <param name="dns">Interface for dns lookup</param> /// <param name="source">The source, i.e. SSH or SMTP, etc.</param> /// <param name="pathAndMask">File path and mask (i.e. /var/log/auth*.log)</param> /// <param name="recursive">Whether to parse all sub directories of path and mask recursively</param> /// <param name="regex">Regex to parse file lines to pull out ipaddress and username</param> /// <param name="maxFileSize">Max size of file before it is deleted or 0 for unlimited</param> /// <param name="pingIntervalMilliseconds"></param> public IPBanLogFileScanner(IFailedLogin failedLogin, IDnsLookup dns, string source, string pathAndMask, bool recursive, string regex, long maxFileSize = 0, int pingIntervalMilliseconds = 10000) { failedLogin.ThrowIfNull(nameof(failedLogin)); dns.ThrowIfNull(nameof(dns)); Source = source; this.failedLogin = failedLogin; this.dns = dns; this.maxFileSize = maxFileSize; PathAndMask = pathAndMask; Regex = IPBanConfig.ParseRegex(regex); directoryToWatch = Path.GetDirectoryName(pathAndMask); fileMask = Path.GetFileName(pathAndMask); pingTimer = new System.Timers.Timer(pingIntervalMilliseconds); pingTimer.Elapsed += PingTimerElapsed; pingTimer.Start(); // add initial files SearchOption option = (recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly); string dir = Path.GetDirectoryName(pathAndMask); if (Directory.Exists(dir)) { foreach (string existingFileName in Directory.GetFiles(dir, Path.GetFileName(pathAndMask), option)) { // start at end of existing files AddPingFile(existingFileName, new FileInfo(existingFileName).Length); } } }
/// <summary> /// Create a log file scanner /// </summary> /// <param name="failedLogin">Interface for handling failed logins</param> /// <param name="dns">Interface for dns lookup</param> /// <param name="source">The source, i.e. SSH or SMTP, etc.</param> /// <param name="pathAndMask">File path and mask (i.e. /var/log/auth*.log)</param> /// <param name="recursive">Whether to parse all sub directories of path and mask recursively</param> /// <param name="regex">Regex to parse file lines to pull out ipaddress and username</param> /// <param name="maxFileSizeBytes">Max size of file (in bytes) before it is deleted or 0 for unlimited</param> /// <param name="pingIntervalMilliseconds">Ping interval in milliseconds, less than 1 for manual ping required</param> public IPBanIPAddressLogFileScanner ( IFailedLogin failedLogin, IDnsLookup dns, string source, string pathAndMask, bool recursive, string regex, long maxFileSizeBytes = 0, int pingIntervalMilliseconds = 0 ) : base(pathAndMask, recursive, maxFileSizeBytes, pingIntervalMilliseconds) { failedLogin.ThrowIfNull(nameof(failedLogin)); dns.ThrowIfNull(nameof(dns)); Source = source; this.failedLogin = failedLogin; this.dns = dns; this.regex = IPBanConfig.ParseRegex(regex); }
/// <summary> /// Create a log file scanner /// </summary> /// <param name="loginHandler">Interface for handling logins</param> /// <param name="dns">Interface for dns lookup</param> /// <param name="source">The source, i.e. SSH or SMTP, etc.</param> /// <param name="pathAndMask">File path and mask (i.e. /var/log/auth*.log)</param> /// <param name="recursive">Whether to parse all sub directories of path and mask recursively</param> /// <param name="regexFailure">Regex to parse file lines to pull out failed login ipaddress and username</param> /// <param name="regexSuccess">Regex to parse file lines to pull out successful login ipaddress and username</param> /// <param name="maxFileSizeBytes">Max size of file (in bytes) before it is deleted or 0 for unlimited</param> /// <param name="pingIntervalMilliseconds">Ping interval in milliseconds, less than 1 for manual ping required</param> public IPBanIPAddressLogFileScanner ( IIPAddressEventHandler loginHandler, IDnsLookup dns, string source, string pathAndMask, bool recursive, string regexFailure, string regexSuccess, long maxFileSizeBytes = 0, int pingIntervalMilliseconds = 0 ) : base(pathAndMask, recursive, maxFileSizeBytes, pingIntervalMilliseconds) { loginHandler.ThrowIfNull(nameof(loginHandler)); dns.ThrowIfNull(nameof(dns)); Source = source; this.loginHandler = loginHandler; this.dns = dns; this.regexFailure = IPBanConfig.ParseRegex(regexFailure); this.regexSuccess = IPBanConfig.ParseRegex(regexSuccess); }