From 541e0d92549505603e1c5f80b873c503a39f0661 Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Thu, 22 Apr 2021 18:32:56 +0200 Subject: [PATCH] Added error log anonymization feature. --- src/fr/devinsy/logar/app/Logar.java | 2 +- .../logar/app/anonymizer/Anonymizer.java | 75 ++++++++++++------- src/fr/devinsy/logar/app/log/LogUtils.java | 5 +- 3 files changed, 48 insertions(+), 34 deletions(-) diff --git a/src/fr/devinsy/logar/app/Logar.java b/src/fr/devinsy/logar/app/Logar.java index f0d4208..1c37dd0 100644 --- a/src/fr/devinsy/logar/app/Logar.java +++ b/src/fr/devinsy/logar/app/Logar.java @@ -102,7 +102,7 @@ public final class Logar { if (file.getName().contains("access")) { - anonymizer.anonymizeAccessFile(file); + anonymizer.anonymize(file); } else if (file.getName().contains("error")) { diff --git a/src/fr/devinsy/logar/app/anonymizer/Anonymizer.java b/src/fr/devinsy/logar/app/anonymizer/Anonymizer.java index 530e36f..b5bda8c 100644 --- a/src/fr/devinsy/logar/app/anonymizer/Anonymizer.java +++ b/src/fr/devinsy/logar/app/anonymizer/Anonymizer.java @@ -23,6 +23,7 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintWriter; import java.time.format.DateTimeParseException; +import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.zip.GZIPOutputStream; @@ -59,32 +60,6 @@ public final class Anonymizer this.map = new AnonMap(); } - /** - * Anonymize. - * - * @param log - * the log - * @return the log - */ - public Log anonymizeAccess(final Log log) - { - Log result; - - String anonIp = this.map.anonymizeIp(log.getIp()); - String anonUser = this.map.anonymizeUser(log.getUser()); - - String line = log.getLine().replace(log.getIp(), anonIp); - if (!log.getUser().equals("-")) - { - line = line.replace(log.getUser(), anonUser); - } - - result = new Log(line, log.getDatetime(), anonIp, anonUser); - - // - return result; - } - /** * Anonymize. * @@ -93,7 +68,7 @@ public final class Anonymizer * @param target * the target */ - public void anonymizeAccessFile(final File source) + public void anonymize(final File source) { if (source == null) { @@ -188,13 +163,13 @@ public final class Anonymizer } /** - * Anonymize error. + * Anonymize. * * @param log * the log * @return the log */ - public Log anonymizeError(final Log log) + public Log anonymizeAccess(final Log log) { Log result; @@ -213,6 +188,48 @@ public final class Anonymizer return result; } + /** + * Anonymize error. + * + * @param log + * the log + * @return the log + */ + public Log anonymizeError(final Log log) + { + Log result; + + // Search and anonymized Ipv4 addresses. + Matcher matcher = IPV4_PATTERN.matcher(log.getLine()); + String anonLine = log.getLine(); + while (matcher.find()) + { + String left = anonLine.substring(0, matcher.start()); + String ipv4 = matcher.group(); + String right = anonLine.substring(matcher.end()); + + String anonIpv4 = this.map.get(ipv4); + anonLine = left + anonIpv4 + right; + } + + // Search and anonymized Ipv4 addresses. + matcher = IPV6_PATTERN.matcher(anonLine); + while (matcher.find()) + { + String left = anonLine.substring(0, matcher.start()); + String ipv6 = matcher.group(); + String right = anonLine.substring(matcher.end()); + + String anonIpv6 = this.map.get(ipv6); + anonLine = left + anonIpv6 + right; + } + + result = new Log(anonLine, log.getDatetime()); + + // + return result; + } + /** * Gets the map table. * diff --git a/src/fr/devinsy/logar/app/log/LogUtils.java b/src/fr/devinsy/logar/app/log/LogUtils.java index dbc3229..15437a4 100644 --- a/src/fr/devinsy/logar/app/log/LogUtils.java +++ b/src/fr/devinsy/logar/app/log/LogUtils.java @@ -49,10 +49,7 @@ public final class LogUtils "^(?[a-zA-F0-9\\\\:\\\\.]+) - (?\\S+) \\[(?