Change filter to ignore unicode errors in python3

Also do not try to convert unicode to unicode again for python3 and
python2
pull/128/merge^2
Steven Hiscocks 2013-02-23 21:03:51 +00:00
parent ad7119a3fe
commit 2bb3469644
1 changed files with 16 additions and 11 deletions

View File

@ -35,7 +35,7 @@ from datedetector import DateDetector
from mytime import MyTime from mytime import MyTime
from failregex import FailRegex, Regex, RegexException from failregex import FailRegex, Regex, RegexException
import logging, re, os, fcntl, time import logging, re, os, fcntl, time, sys
# Gets the instance of the logger. # Gets the instance of the logger.
logSys = logging.getLogger("fail2ban.filter") logSys = logging.getLogger("fail2ban.filter")
@ -289,22 +289,24 @@ class Filter(JailThread):
def processLine(self, line): def processLine(self, line):
"""Split the time portion from log msg and return findFailures on them """Split the time portion from log msg and return findFailures on them
""" """
if (sys.version_info >= (3,) and isinstance(line, bytes)) or \
(sys.version_info < (3,) and isinstance(line, str)):
try: try:
# Decode line to UTF-8 # Decode line to UTF-8
l = line.decode('utf-8') line = line.decode('utf-8')
except UnicodeDecodeError: except UnicodeDecodeError:
l = line line = line
timeMatch = self.dateDetector.matchTime(l) timeMatch = self.dateDetector.matchTime(line)
if timeMatch: if timeMatch:
# Lets split into time part and log part of the line # Lets split into time part and log part of the line
timeLine = timeMatch.group() timeLine = timeMatch.group()
# Lets leave the beginning in as well, so if there is no # Lets leave the beginning in as well, so if there is no
# anchore at the beginning of the time regexp, we don't # anchore at the beginning of the time regexp, we don't
# at least allow injection. Should be harmless otherwise # at least allow injection. Should be harmless otherwise
logLine = l[:timeMatch.start()] + l[timeMatch.end():] logLine = line[:timeMatch.start()] + line[timeMatch.end():]
else: else:
timeLine = l timeLine = line
logLine = l logLine = line
return self.findFailure(timeLine, logLine) return self.findFailure(timeLine, logLine)
def processLineAndAdd(self, line): def processLineAndAdd(self, line):
@ -520,6 +522,9 @@ class FileContainer:
self.__tail = tail self.__tail = tail
self.__handler = None self.__handler = None
# Try to open the file. Raises an exception if an error occured. # Try to open the file. Raises an exception if an error occured.
if sys.version_info >= (3,):
handler = open(filename, errors='ignore')
else:
handler = open(filename) handler = open(filename)
stats = os.fstat(handler.fileno()) stats = os.fstat(handler.fileno())
self.__ino = stats.st_ino self.__ino = stats.st_ino