From fb1407d332f8a31bd2edf21122b5dc9105b1ed59 Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Wed, 12 May 2021 15:04:31 +0200 Subject: [PATCH] Added UserAgent anonymize. --- .../logar/app/anonymizer/Anonymizer.java | 66 +++++++++++++- .../app/anonymizer/UserAgentGenerator.java | 86 +++++++++++++++++++ 2 files changed, 151 insertions(+), 1 deletion(-) create mode 100644 src/fr/devinsy/logar/app/anonymizer/UserAgentGenerator.java diff --git a/src/fr/devinsy/logar/app/anonymizer/Anonymizer.java b/src/fr/devinsy/logar/app/anonymizer/Anonymizer.java index 76e497d..52c08d2 100644 --- a/src/fr/devinsy/logar/app/anonymizer/Anonymizer.java +++ b/src/fr/devinsy/logar/app/anonymizer/Anonymizer.java @@ -119,6 +119,16 @@ public final class Anonymizer // anon.getUser(), anon.getDatetime()); // logger.info("anon={}", anon); + // System.out.println("\\\\\\" + line); + // System.out.println("///" + anon.getLine()); + + // String[] tokens = log.getUserAgent().split("[ + // ()\\[\\]/]"); + // for (String token : tokens) + // { + // out.println(token); + // } + // Make a check. if (StringUtils.equals(line, anon.getLine())) { @@ -158,7 +168,7 @@ public final class Anonymizer } /** - * Anonymize. + * Anonymize access. * * @param log * the log @@ -168,6 +178,42 @@ public final class Anonymizer { Log result; + result = new Log(log); + + // + String anonIp = this.map.anonymizeIp(log.getIp()); + result.setIp(anonIp); + + // + String refererAnon = log.getReferer().replace(log.getIp(), result.getIp()); + result.setReferer(refererAnon); + + // + String anonUser = this.map.anonymizeUser(log.getUser()); + result.setUser(anonUser); + + // + String userAgentAnon = UserAgentGenerator.anonymize(log.getUserAgent()); + result.setUserAgent(userAgentAnon); + + // + result.concateAccessLog(); + + // + return result; + } + + /** + * Anonymize. + * + * @param log + * the log + * @return the log + */ + public Log anonymizeAccessSimple(final Log log) + { + Log result; + // String anonIp = this.map.anonymizeIp(log.getIp()); String line = log.getLine().replace(log.getIp(), anonIp); @@ -228,6 +274,24 @@ public final class Anonymizer return result; } + /** + * Anonymize user agent. + * + * @param log + * the log + * @return the log + */ + public String anonymizeUserAgent(final String userAgent) + { + String result; + + // + result = StringUtils.replaceChars(userAgent, "()_-/@.0123456789", ""); + + // + return result; + } + /** * Gets the map table. * diff --git a/src/fr/devinsy/logar/app/anonymizer/UserAgentGenerator.java b/src/fr/devinsy/logar/app/anonymizer/UserAgentGenerator.java new file mode 100644 index 0000000..08b5ffc --- /dev/null +++ b/src/fr/devinsy/logar/app/anonymizer/UserAgentGenerator.java @@ -0,0 +1,86 @@ +/* + * Copyright (C) 2021 Christian Pierre MOMON + * + * This file is part of Logar, simple tool to manage http log files. + * + * Logar is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * Logar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with Logar. If not, see . + */ +package fr.devinsy.logar.app.anonymizer; + +import org.apache.commons.lang3.RegExUtils; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * The Class UserAgentGenerator. + */ +public final class UserAgentGenerator +{ + private static Logger logger = LoggerFactory.getLogger(UserAgentGenerator.class); + + /** + * Instantiates a new user agent generator. + */ + private UserAgentGenerator() + { + } + + /** + * Shrink. + * + * @param source + * the source + * @return the string + */ + public static String anonymize(final String source) + { + String result; + + if (source == null) + { + result = null; + } + else if (source.equals("-")) + { + result = source; + } + else + { + // StringBuffer buffer = new StringBuffer(source.length()); + // String targets = "./[]_-0123456789"; + // for (int index = 0; index < source.length(); index++) + // { + // char letter = source.charAt(index); + // if (!StringUtils.containsAny(targets, letter)) + // { + // buffer.append(letter); + // } + // } + // + result = source; + result = RegExUtils.replaceAll(result, "https?://[^\\s\\)\\],;]+", ""); + result = RegExUtils.replaceAll(result, "\\(\\)", ""); + result = RegExUtils.replaceAll(result, "build [0-9a-zA-Z\\.-]+", "build"); + result = RegExUtils.replaceAll(result, "\\d{4}/\\d{2}/\\d{2}", ""); + result = RegExUtils.replaceAll(result, "(/|:)[~0-9a-zA-Z\\.-]+", ""); + // result = StringUtils.replaceChars(result, ".[]_-0123456789", ""); + + result = StringUtils.rightPad(result, source.length()); + } + + // + return result; + } +}