Compare commits
5 Commits
192001bbd4
...
7b13dafbdc
Author | SHA1 | Date |
---|---|---|
Christian P. MOMON | 7b13dafbdc | |
Christian P. MOMON | c5f35ee817 | |
Christian P. MOMON | bb25cfa96d | |
Christian P. MOMON | 25e0fb5f64 | |
Christian P. MOMON | f77179c3f1 |
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -392,6 +392,16 @@ public final class Logar
|
||||||
* the source
|
* the source
|
||||||
*/
|
*/
|
||||||
public static void checkLogFiles(final File source)
|
public static void checkLogFiles(final File source)
|
||||||
|
{
|
||||||
|
if (source == null)
|
||||||
|
{
|
||||||
|
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();
|
Files files = FilesUtils.search(source, LOGFILE_PATTERN).sortByName();
|
||||||
|
|
||||||
|
@ -400,6 +410,7 @@ public final class Logar
|
||||||
checkLogFile(file);
|
checkLogFile(file);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check sort.
|
* Check sort.
|
||||||
|
@ -409,6 +420,16 @@ public final class Logar
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
public static void checkSort(final File source) throws IOException
|
public static void checkSort(final File source) throws IOException
|
||||||
|
{
|
||||||
|
if (source == null)
|
||||||
|
{
|
||||||
|
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();
|
Files files = FilesUtils.search(source, LOGFILE_PATTERN).sortByName();
|
||||||
|
|
||||||
|
@ -417,6 +438,7 @@ public final class Logar
|
||||||
checkSortFile(file);
|
checkSortFile(file);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check sort file.
|
* Check sort 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.
|
* Sort.
|
||||||
*
|
*
|
||||||
|
@ -482,6 +585,16 @@ public final class Logar
|
||||||
* Signals that an I/O exception has occurred.
|
* Signals that an I/O exception has occurred.
|
||||||
*/
|
*/
|
||||||
public static void sort(final File source) throws IOException
|
public static void sort(final File source) throws IOException
|
||||||
|
{
|
||||||
|
if (source == null)
|
||||||
|
{
|
||||||
|
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();
|
Files files = FilesUtils.searchEndingWith(source, LOGFILE_PATTERN).removeHidden().sortByName();
|
||||||
|
|
||||||
|
@ -491,6 +604,7 @@ public final class Logar
|
||||||
LogFile.sortLogFile(file);
|
LogFile.sortLogFile(file);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check concate.
|
* Check concate.
|
||||||
|
|
|
@ -19,6 +19,7 @@
|
||||||
package fr.devinsy.logar.app.anonymizer;
|
package fr.devinsy.logar.app.anonymizer;
|
||||||
|
|
||||||
import org.apache.commons.lang3.RandomUtils;
|
import org.apache.commons.lang3.RandomUtils;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
@ -236,13 +237,17 @@ public final class Ipv4Generator
|
||||||
{
|
{
|
||||||
result = null;
|
result = null;
|
||||||
}
|
}
|
||||||
|
else if (StringUtils.equalsAny(ip, "0.0.0.0", "127.0.0.1"))
|
||||||
|
{
|
||||||
|
result = ip;
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
result = random(ip.length());
|
result = random(ip.length());
|
||||||
|
|
||||||
if (result.equals(ip))
|
while (StringUtils.equals(result, ip))
|
||||||
{
|
{
|
||||||
random(ip);
|
result = random(ip);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -19,6 +19,7 @@
|
||||||
package fr.devinsy.logar.app.anonymizer;
|
package fr.devinsy.logar.app.anonymizer;
|
||||||
|
|
||||||
import org.apache.commons.lang3.RandomStringUtils;
|
import org.apache.commons.lang3.RandomStringUtils;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
@ -61,6 +62,18 @@ public final class Ipv6Generator
|
||||||
{
|
{
|
||||||
buffer.append(c);
|
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
|
else
|
||||||
{
|
{
|
||||||
buffer.append(RandomStringUtils.random(1, "0123456789abcdef"));
|
buffer.append(RandomStringUtils.random(1, "0123456789abcdef"));
|
||||||
|
@ -68,9 +81,9 @@ public final class Ipv6Generator
|
||||||
}
|
}
|
||||||
result = buffer.toString();
|
result = buffer.toString();
|
||||||
|
|
||||||
if (result.equals(ip))
|
while (StringUtils.equals(result, ip))
|
||||||
{
|
{
|
||||||
random(ip);
|
result = random(ip);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -88,7 +88,7 @@ public final class LogParser
|
||||||
}
|
}
|
||||||
catch (DateTimeParseException exception)
|
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)
|
catch (DateTimeParseException exception)
|
||||||
{
|
{
|
||||||
throw new IllegalArgumentException("Bad line format (date): " + line);
|
throw new IllegalArgumentException("Bad line format (date): " + line, exception);
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
|
@ -26,6 +26,7 @@ import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import fr.devinsy.logar.app.DryOption;
|
import fr.devinsy.logar.app.DryOption;
|
||||||
|
import fr.devinsy.logar.app.ExtractOption;
|
||||||
import fr.devinsy.logar.app.Logar;
|
import fr.devinsy.logar.app.Logar;
|
||||||
import fr.devinsy.strings.StringList;
|
import fr.devinsy.strings.StringList;
|
||||||
|
|
||||||
|
@ -223,6 +224,13 @@ public final class LogarCLI
|
||||||
|
|
||||||
Logar.checkSort(source);
|
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*"))
|
else if (isMatching(args, "sort", "\\s*\\S+\\s*"))
|
||||||
{
|
{
|
||||||
File source = new File(args[1]);
|
File source = new File(args[1]);
|
||||||
|
|
|
@ -48,7 +48,7 @@ public class FilesUtils
|
||||||
Files result;
|
Files result;
|
||||||
|
|
||||||
result = new Files();
|
result = new Files();
|
||||||
if (source != null)
|
if ((source != null) && (source.exists()))
|
||||||
{
|
{
|
||||||
if (source.isFile())
|
if (source.isFile())
|
||||||
{
|
{
|
||||||
|
@ -74,34 +74,6 @@ public class FilesUtils
|
||||||
return result;
|
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.
|
* Search recursively.
|
||||||
*
|
*
|
||||||
|
@ -131,4 +103,32 @@ public class FilesUtils
|
||||||
//
|
//
|
||||||
return result;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue