Compare commits

...

5 Commits

7 changed files with 252 additions and 48 deletions

View File

@ -0,0 +1,64 @@
/*
* Copyright (C) 2021 Christian Pierre MOMON <christian@momon.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
package fr.devinsy.logar.app;
import org.apache.commons.lang3.StringUtils;
/**
* The Enum DryOption.
*/
public enum ExtractOption
{
IP,
DATETIME,
USERAGENT;
public static ExtractOption of(final String source)
{
ExtractOption result;
if (source == null)
{
result = null;
}
else
{
String token = source.toLowerCase().trim();
if (StringUtils.equals(token, "ip"))
{
result = IP;
}
else if (StringUtils.equals(token, "dateime"))
{
result = DATETIME;
}
else if (StringUtils.equals(token, "useragent"))
{
result = USERAGENT;
}
else
{
result = null;
}
}
//
return result;
}
}

View File

@ -393,11 +393,22 @@ public final class Logar
*/
public static void checkLogFiles(final File source)
{
Files files = FilesUtils.search(source, LOGFILE_PATTERN).sortByName();
for (File file : files)
if (source == null)
{
checkLogFile(file);
System.out.println("Undefined source.");
}
else if (!source.exists())
{
System.out.println("Missing source to check.");
}
else
{
Files files = FilesUtils.search(source, LOGFILE_PATTERN).sortByName();
for (File file : files)
{
checkLogFile(file);
}
}
}
@ -410,11 +421,22 @@ public final class Logar
*/
public static void checkSort(final File source) throws IOException
{
Files files = FilesUtils.search(source, LOGFILE_PATTERN).sortByName();
for (File file : files)
if (source == null)
{
checkSortFile(file);
System.out.println("Undefined source.");
}
else if (!source.exists())
{
System.out.println("Missing source to check.");
}
else
{
Files files = FilesUtils.search(source, LOGFILE_PATTERN).sortByName();
for (File file : files)
{
checkSortFile(file);
}
}
}
@ -473,6 +495,87 @@ public final class Logar
}
}
/**
* Extract user agent.
*
* @param source
* the source
* @throws IOException
* Signals that an I/O exception has occurred.
*/
public static void extract(final File source, final ExtractOption option) throws IOException
{
if (source == null)
{
System.out.println("Undefined source.");
}
else if (option == null)
{
System.out.println("Undefined option.");
}
else if (!source.exists())
{
System.out.println("Missing source to sort.");
}
else
{
Files files = FilesUtils.search(source, LOGFILE_PATTERN).removeHidden().keep(".*access.*").sortByName();
// System.out.println(files.size());
System.err.println(files.size());
for (File file : files)
{
System.err.println("== Extract userAgent for [" + file.getName() + "]");
try
{
LineIterator iterator = new LineIterator(file);
while (iterator.hasNext())
{
String line = iterator.next();
// System.out.println(line);
try
{
Log log = LogParser.parseAccessLog(line);
String extract;
switch (option)
{
case IP:
extract = log.getIp();
break;
case DATETIME:
extract = log.getDatetimeValue();
break;
case USERAGENT:
extract = log.getUserAgent();
break;
default:
extract = null;
}
System.out.println(extract);
}
catch (IllegalArgumentException exception)
{
System.out.println("Bad format line: " + line);
exception.printStackTrace();
}
catch (DateTimeParseException exception)
{
System.out.println("Bad datetime format: " + line);
}
}
}
catch (IOException exception)
{
System.err.println("Error with file [" + file.getAbsolutePath() + "]");
exception.printStackTrace();
}
}
}
}
/**
* Sort.
*
@ -483,12 +586,23 @@ public final class Logar
*/
public static void sort(final File source) throws IOException
{
Files files = FilesUtils.searchEndingWith(source, LOGFILE_PATTERN).removeHidden().sortByName();
for (File file : files)
if (source == null)
{
System.out.println("== Sort for [" + file.getName() + "]");
LogFile.sortLogFile(file);
System.out.println("Undefined source.");
}
else if (!source.exists())
{
System.out.println("Missing source to sort.");
}
else
{
Files files = FilesUtils.searchEndingWith(source, LOGFILE_PATTERN).removeHidden().sortByName();
for (File file : files)
{
System.out.println("== Sort for [" + file.getName() + "]");
LogFile.sortLogFile(file);
}
}
}

View File

@ -19,6 +19,7 @@
package fr.devinsy.logar.app.anonymizer;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -236,13 +237,17 @@ public final class Ipv4Generator
{
result = null;
}
else if (StringUtils.equalsAny(ip, "0.0.0.0", "127.0.0.1"))
{
result = ip;
}
else
{
result = random(ip.length());
if (result.equals(ip))
while (StringUtils.equals(result, ip))
{
random(ip);
result = random(ip);
}
}

View File

@ -19,6 +19,7 @@
package fr.devinsy.logar.app.anonymizer;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -61,6 +62,18 @@ public final class Ipv6Generator
{
buffer.append(c);
}
else if (index == 0)
{
buffer.append('b');
}
else if (index == 1)
{
buffer.append('a');
}
else if (index == 2)
{
buffer.append('d');
}
else
{
buffer.append(RandomStringUtils.random(1, "0123456789abcdef"));
@ -68,9 +81,9 @@ public final class Ipv6Generator
}
result = buffer.toString();
if (result.equals(ip))
while (StringUtils.equals(result, ip))
{
random(ip);
result = random(ip);
}
}

View File

@ -88,7 +88,7 @@ public final class LogParser
}
catch (DateTimeParseException exception)
{
throw new IllegalArgumentException("Bad line format (date): " + line);
throw new IllegalArgumentException("Bad line format (date): " + line, exception);
}
//
@ -144,7 +144,7 @@ public final class LogParser
}
catch (DateTimeParseException exception)
{
throw new IllegalArgumentException("Bad line format (date): " + line);
throw new IllegalArgumentException("Bad line format (date): " + line, exception);
}
//

View File

@ -26,6 +26,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import fr.devinsy.logar.app.DryOption;
import fr.devinsy.logar.app.ExtractOption;
import fr.devinsy.logar.app.Logar;
import fr.devinsy.strings.StringList;
@ -223,6 +224,13 @@ public final class LogarCLI
Logar.checkSort(source);
}
else if (isMatching(args, "extract", "\\s*(ip|datetime|useragent)\\s*", "\\s*\\S+\\s*"))
{
ExtractOption token = ExtractOption.of(args[1]);
File source = new File(args[2]);
Logar.extract(source, token);
}
else if (isMatching(args, "sort", "\\s*\\S+\\s*"))
{
File source = new File(args[1]);

View File

@ -48,7 +48,7 @@ public class FilesUtils
Files result;
result = new Files();
if (source != null)
if ((source != null) && (source.exists()))
{
if (source.isFile())
{
@ -74,34 +74,6 @@ public class FilesUtils
return result;
}
/**
* List recursively.
*
* @param source
* the source
* @param extensions
* the extensions
* @return the files
*/
public static Files searchEndingWith(final File source, final String... extensions)
{
Files result;
result = new Files();
Files full = listRecursively(source);
for (File file : full)
{
if (StringUtils.endsWithAny(file.getName(), extensions))
{
result.add(file);
}
}
//
return result;
}
/**
* Search recursively.
*
@ -131,4 +103,32 @@ public class FilesUtils
//
return result;
}
/**
* List recursively.
*
* @param source
* the source
* @param extensions
* the extensions
* @return the files
*/
public static Files searchEndingWith(final File source, final String... extensions)
{
Files result;
result = new Files();
Files full = listRecursively(source);
for (File file : full)
{
if (StringUtils.endsWithAny(file.getName(), extensions))
{
result.add(file);
}
}
//
return result;
}
}