mirror of https://github.com/fail2ban/fail2ban
Change filter to ignore unicode errors in python3
Also do not try to convert unicode to unicode again for python3 and python2pull/128/merge^2
parent
ad7119a3fe
commit
2bb3469644
|
@ -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
|
||||||
"""
|
"""
|
||||||
try:
|
if (sys.version_info >= (3,) and isinstance(line, bytes)) or \
|
||||||
# Decode line to UTF-8
|
(sys.version_info < (3,) and isinstance(line, str)):
|
||||||
l = line.decode('utf-8')
|
try:
|
||||||
except UnicodeDecodeError:
|
# Decode line to UTF-8
|
||||||
l = line
|
line = line.decode('utf-8')
|
||||||
timeMatch = self.dateDetector.matchTime(l)
|
except UnicodeDecodeError:
|
||||||
|
line = line
|
||||||
|
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,7 +522,10 @@ 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.
|
||||||
handler = open(filename)
|
if sys.version_info >= (3,):
|
||||||
|
handler = open(filename, errors='ignore')
|
||||||
|
else:
|
||||||
|
handler = open(filename)
|
||||||
stats = os.fstat(handler.fileno())
|
stats = os.fstat(handler.fileno())
|
||||||
self.__ino = stats.st_ino
|
self.__ino = stats.st_ino
|
||||||
try:
|
try:
|
||||||
|
|
Loading…
Reference in New Issue