Compare commits
No commits in common. "7b13dafbdc0b5c1cea759da4e97bcff46b32aaf7" and "192001bbd4070fe3a45703cd55847a479bb16081" have entirely different histories.
7b13dafbdc
...
192001bbd4
|
@ -1,64 +0,0 @@
|
||||||
/*
|
|
||||||
* 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;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -393,22 +393,11 @@ public final class Logar
|
||||||
*/
|
*/
|
||||||
public static void checkLogFiles(final File source)
|
public static void checkLogFiles(final File source)
|
||||||
{
|
{
|
||||||
if (source == null)
|
Files files = FilesUtils.search(source, LOGFILE_PATTERN).sortByName();
|
||||||
{
|
|
||||||
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)
|
for (File file : files)
|
||||||
{
|
{
|
||||||
checkLogFile(file);
|
checkLogFile(file);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -421,22 +410,11 @@ public final class Logar
|
||||||
*/
|
*/
|
||||||
public static void checkSort(final File source) throws IOException
|
public static void checkSort(final File source) throws IOException
|
||||||
{
|
{
|
||||||
if (source == null)
|
Files files = FilesUtils.search(source, LOGFILE_PATTERN).sortByName();
|
||||||
{
|
|
||||||
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)
|
for (File file : files)
|
||||||
{
|
{
|
||||||
checkSortFile(file);
|
checkSortFile(file);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -495,87 +473,6 @@ 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.
|
||||||
*
|
*
|
||||||
|
@ -586,23 +483,12 @@ public final class Logar
|
||||||
*/
|
*/
|
||||||
public static void sort(final File source) throws IOException
|
public static void sort(final File source) throws IOException
|
||||||
{
|
{
|
||||||
if (source == null)
|
Files files = FilesUtils.searchEndingWith(source, LOGFILE_PATTERN).removeHidden().sortByName();
|
||||||
{
|
|
||||||
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)
|
for (File file : files)
|
||||||
{
|
{
|
||||||
System.out.println("== Sort for [" + file.getName() + "]");
|
System.out.println("== Sort for [" + file.getName() + "]");
|
||||||
LogFile.sortLogFile(file);
|
LogFile.sortLogFile(file);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -19,7 +19,6 @@
|
||||||
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;
|
||||||
|
|
||||||
|
@ -237,17 +236,13 @@ 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());
|
||||||
|
|
||||||
while (StringUtils.equals(result, ip))
|
if (result.equals(ip))
|
||||||
{
|
{
|
||||||
result = random(ip);
|
random(ip);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -19,7 +19,6 @@
|
||||||
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;
|
||||||
|
|
||||||
|
@ -62,18 +61,6 @@ 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"));
|
||||||
|
@ -81,9 +68,9 @@ public final class Ipv6Generator
|
||||||
}
|
}
|
||||||
result = buffer.toString();
|
result = buffer.toString();
|
||||||
|
|
||||||
while (StringUtils.equals(result, ip))
|
if (result.equals(ip))
|
||||||
{
|
{
|
||||||
result = random(ip);
|
random(ip);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -88,7 +88,7 @@ public final class LogParser
|
||||||
}
|
}
|
||||||
catch (DateTimeParseException exception)
|
catch (DateTimeParseException exception)
|
||||||
{
|
{
|
||||||
throw new IllegalArgumentException("Bad line format (date): " + line, exception);
|
throw new IllegalArgumentException("Bad line format (date): " + line);
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
|
@ -144,7 +144,7 @@ public final class LogParser
|
||||||
}
|
}
|
||||||
catch (DateTimeParseException exception)
|
catch (DateTimeParseException exception)
|
||||||
{
|
{
|
||||||
throw new IllegalArgumentException("Bad line format (date): " + line, exception);
|
throw new IllegalArgumentException("Bad line format (date): " + line);
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
|
@ -26,7 +26,6 @@ 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;
|
||||||
|
|
||||||
|
@ -224,13 +223,6 @@ 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) && (source.exists()))
|
if (source != null)
|
||||||
{
|
{
|
||||||
if (source.isFile())
|
if (source.isFile())
|
||||||
{
|
{
|
||||||
|
@ -74,6 +74,34 @@ 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.
|
||||||
*
|
*
|
||||||
|
@ -103,32 +131,4 @@ 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
Block a user