mirror of
https://github.com/Tautulli/Tautulli.git
synced 2025-07-16 02:02:58 -07:00
Refactor log filter
This commit is contained in:
parent
6380de3e6c
commit
55ffd68023
1 changed files with 36 additions and 31 deletions
|
@ -80,7 +80,7 @@ class BlacklistFilter(logging.Filter):
|
||||||
Log filter for blacklisted tokens and passwords
|
Log filter for blacklisted tokens and passwords
|
||||||
"""
|
"""
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
pass
|
super(BlacklistFilter, self).__init__()
|
||||||
|
|
||||||
def filter(self, record):
|
def filter(self, record):
|
||||||
if not plexpy.CONFIG.LOG_BLACKLIST:
|
if not plexpy.CONFIG.LOG_BLACKLIST:
|
||||||
|
@ -98,30 +98,29 @@ class BlacklistFilter(logging.Filter):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
class PublicIPFilter(logging.Filter):
|
class RegexFilter(logging.Filter):
|
||||||
"""
|
"""
|
||||||
Log filter for public IP addresses
|
Base class for regex log filter
|
||||||
"""
|
"""
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
pass
|
super(RegexFilter, self).__init__()
|
||||||
|
|
||||||
|
self.regex = re.compile(r'')
|
||||||
|
|
||||||
def filter(self, record):
|
def filter(self, record):
|
||||||
if not plexpy.CONFIG.LOG_BLACKLIST:
|
if not plexpy.CONFIG.LOG_BLACKLIST:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Currently only checking for ipv4 addresses
|
matches = self.regex.findall(record.msg)
|
||||||
ipv4 = re.findall(r'[0-9]+(?:\.[0-9]+){3}(?!\d*-[a-z0-9]{6})', record.msg)
|
for match in matches:
|
||||||
for ip in ipv4:
|
record.msg = self.replace(record.msg, match)
|
||||||
if helpers.is_public_ip(ip):
|
|
||||||
record.msg = record.msg.replace(ip, ip.partition('.')[0] + '.***.***.***')
|
|
||||||
|
|
||||||
args = []
|
args = []
|
||||||
for arg in record.args:
|
for arg in record.args:
|
||||||
ipv4 = re.findall(r'[0-9]+(?:\.[0-9]+){3}(?!\d*-[a-z0-9]{6})', arg) if isinstance(arg, basestring) else []
|
matches = self.regex.findall(arg) if isinstance(arg, basestring) else []
|
||||||
for ip in ipv4:
|
for match in matches:
|
||||||
if helpers.is_public_ip(ip):
|
arg = self.replace(arg, match)
|
||||||
arg = arg.replace(ip, ip.partition('.')[0] + '.***.***.***')
|
|
||||||
args.append(arg)
|
args.append(arg)
|
||||||
record.args = tuple(args)
|
record.args = tuple(args)
|
||||||
except:
|
except:
|
||||||
|
@ -129,31 +128,37 @@ class PublicIPFilter(logging.Filter):
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
def replace(self, text, match):
|
||||||
|
return text
|
||||||
|
|
||||||
class PlexTokenFilter(logging.Filter):
|
|
||||||
|
class PublicIPFilter(RegexFilter):
|
||||||
|
"""
|
||||||
|
Log filter for public IP addresses
|
||||||
|
"""
|
||||||
|
def __init__(self):
|
||||||
|
super(PublicIPFilter, self).__init__()
|
||||||
|
|
||||||
|
# Currently only checking for ipv4 addresses
|
||||||
|
self.regex = re.compile(r'[0-9]+(?:\.[0-9]+){3}(?!\d*-[a-z0-9]{6})')
|
||||||
|
|
||||||
|
def replace(self, text, ip):
|
||||||
|
if helpers.is_public_ip(ip):
|
||||||
|
return text.replace(ip, ip.partition('.')[0] + '.***.***.***')
|
||||||
|
return text
|
||||||
|
|
||||||
|
|
||||||
|
class PlexTokenFilter(RegexFilter):
|
||||||
"""
|
"""
|
||||||
Log filter for X-Plex-Token
|
Log filter for X-Plex-Token
|
||||||
"""
|
"""
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
pass
|
super(PlexTokenFilter, self).__init__()
|
||||||
|
|
||||||
def filter(self, record):
|
self.regex = re.compile(r'X-Plex-Token(?:=|%3D)([a-zA-Z0-9]+)')
|
||||||
try:
|
|
||||||
tokens = re.findall(r'X-Plex-Token(?:=|%3D)([a-zA-Z0-9]+)', record.msg)
|
|
||||||
for token in tokens:
|
|
||||||
record.msg = record.msg.replace(token, 8 * '*' + token[-2:])
|
|
||||||
|
|
||||||
args = []
|
def replace(self, text, token):
|
||||||
for arg in record.args:
|
return text.replace(token, 8 * '*' + token[-2:])
|
||||||
tokens = re.findall(r'X-Plex-Token(?:=|%3D)([a-zA-Z0-9]+)', arg) if isinstance(arg, basestring) else []
|
|
||||||
for token in tokens:
|
|
||||||
arg = arg.replace(token, 8 * '*' + token[-2:])
|
|
||||||
args.append(arg)
|
|
||||||
record.args = tuple(args)
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
@contextlib.contextmanager
|
@contextlib.contextmanager
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue