コード例 #1
0
        /// <summary>
        /// Process a line, checking for ip addresses
        /// </summary>
        /// <param name="line">Line to process</param>
        /// <returns>True</returns>
        protected override bool OnProcessLine(string line)
        {
            IPBanLog.Debug("Parsing log file line {0}...", line);
            IPAddressLogInfo info = IPBanService.GetIPAddressInfoFromRegex(dns, regex, line);

            if (info.FoundMatch)
            {
                info.Source = info.Source ?? Source;
                IPBanLog.Debug("Log file found match, ip: {0}, user: {1}, source: {2}, count: {3}", info.IPAddress, info.UserName, info.Source, info.Count);
                failedLogin.AddFailedLogin(info);
            }
            else
            {
                IPBanLog.Debug("No match for line {0}", line);
            }

            return(true);
        }
コード例 #2
0
        private bool PingFile(WatchedFile file, FileStream fs)
        {
            const int maxCountBeforeNewline = 1024;
            int       b;
            long      lastNewlinePos = -1;

            byte[] bytes;
            long   end = Math.Min(file.LastLength, fs.Length);
            int    countBeforeNewline = 0;

            fs.Position = file.LastPosition;

            IPBanLog.Info("Processing watched file {0}, len = {1}, pos = {2}", file.FileName, file.LastLength, file.LastPosition);

            while (fs.Position < end && countBeforeNewline++ != maxCountBeforeNewline)
            {
                // read until last \n is found
                b = fs.ReadByte();
                if (b == '\n')
                {
                    lastNewlinePos     = fs.Position - 1;
                    countBeforeNewline = 0;
                }
            }

            if (countBeforeNewline == maxCountBeforeNewline)
            {
                throw new InvalidOperationException("Log file " + this.fileMask + " may not be a plain text new line delimited file");
            }

            if (lastNewlinePos > -1)
            {
                // set file position ready for the next read right after the newline
                fs.Position = file.LastPosition;
                bytes       = new BinaryReader(fs).ReadBytes((int)(lastNewlinePos - fs.Position));

                // set position for next ping
                file.LastPosition = lastNewlinePos + 1;

                // read text and run regex to find ip addresses to ban
                string   subString = Encoding.UTF8.GetString(bytes);
                string[] lines     = subString.Split('\n');
                bool     foundOne  = false;

                // find ip and user name from all lines
                foreach (string line in lines)
                {
                    string trimmedLine = line.Trim();
                    IPBanLog.Debug("Parsing log file line {0}...", trimmedLine);
                    IPAddressLogInfo info = IPBanService.GetIPAddressInfoFromRegex(dns, Regex, trimmedLine);
                    if (info.FoundMatch)
                    {
                        info.Source = info.Source ?? Source;
                        IPBanLog.Debug("Log file found match, ip: {0}, user: {1}, source: {2}, count: {3}", info.IPAddress, info.UserName, info.Source, info.Count);
                        failedLogin.AddFailedLogin(info);
                        foundOne = true;
                    }
                    else
                    {
                        IPBanLog.Debug("No match for line {0}", line);
                    }
                }

                if (foundOne)
                {
                    // signal that we have found ip addresses
                    ipEvent.Set();
                }
            }

            return(maxFileSize > 0 && fs.Length > maxFileSize);
        }