I2P Address: [http://git.idk.i2p]

Skip to content
Snippets Groups Projects
Commit fc9ad328 authored by zab2's avatar zab2
Browse files

Make the config format consistent (threshold rule [target])

parent 2304e9b5
No related branches found
No related tags found
No related merge requests found
...@@ -14,6 +14,8 @@ import net.i2p.data.DataHelper; ...@@ -14,6 +14,8 @@ import net.i2p.data.DataHelper;
*/ */
class DefinitionParser { class DefinitionParser {
private static enum Rule { DEFAULT, EXPLICIT, FILE, RECORDER }
/** /**
* <p> * <p>
* Processes an array of String objects containing the human-readable definition of * Processes an array of String objects containing the human-readable definition of
...@@ -64,9 +66,9 @@ class DefinitionParser { ...@@ -64,9 +66,9 @@ class DefinitionParser {
* threshold use the keyword "default". The following are examples of default thresholds: * threshold use the keyword "default". The following are examples of default thresholds:
* *
* <pre> * <pre>
* default 15/5 * 15/5 default
* default allow * allow default
* default deny * deny default
* </pre> * </pre>
* *
* Explicit thresholds are applied to a remote destination listed in the definition itself. * Explicit thresholds are applied to a remote destination listed in the definition itself.
...@@ -91,8 +93,8 @@ class DefinitionParser { ...@@ -91,8 +93,8 @@ class DefinitionParser {
* breaches a certain threshold, that destination gets recorded in a given file. Examples: * breaches a certain threshold, that destination gets recorded in a given file. Examples:
* *
* <pre> * <pre>
* recorder 30/5 /path/aggressive.txt * 30/5 recorder /path/aggressive.txt
* recorder 60/5 /path/very_aggressive.txt * 60/5 recorder /path/very_aggressive.txt
* </pre> * </pre>
* <p> * <p>
* It is possible to use a recorder to record aggressive destinations to a given file, * It is possible to use a recorder to record aggressive destinations to a given file,
...@@ -103,9 +105,9 @@ class DefinitionParser { ...@@ -103,9 +105,9 @@ class DefinitionParser {
* </p> * </p>
* <pre> * <pre>
* # by default there are no limits * # by default there are no limits
* default allow * allow default
* # but record overly aggressive destinations * # but record overly aggressive destinations
* recorder 30/5 /path/throttled.txt * 30/5 recorder /path/throttled.txt
* # and any that end up in that file will get throttled in the future * # and any that end up in that file will get throttled in the future
* 15/5 file /path/throttled.txt * 15/5 file /path/throttled.txt
* </pre> * </pre>
...@@ -121,10 +123,10 @@ class DefinitionParser { ...@@ -121,10 +123,10 @@ class DefinitionParser {
* "suspicious.txt": * "suspicious.txt":
* </p> * </p>
* <pre> * <pre>
* default 15/5 * 15/5 default
* allow file /path/friends.txt * allow file /path/friends.txt
* deny file /path/enemies.txt * deny file /path/enemies.txt
* recorder 60/5 /path/suspicious.txt * 60/5 recorder /path/suspicious.txt
* </pre> * </pre>
* *
* @return a FilterDefinition POJO representation for internal use * @return a FilterDefinition POJO representation for internal use
...@@ -135,14 +137,28 @@ class DefinitionParser { ...@@ -135,14 +137,28 @@ class DefinitionParser {
DefinitionBuilder builder = new DefinitionBuilder(); DefinitionBuilder builder = new DefinitionBuilder();
for (String line : definition) { for (String line : definition) {
String [] split = DataHelper.split(line," "); String [] split = DataHelper.split(line,"[ \t]");
split[0] = split[0].toLowerCase(); split[0] = split[0].toLowerCase();
if ("default".equals(split[0]))
builder.setDefaultThreshold(parseThreshold(line.substring(7).trim())); Threshold threshold = parseThreshold(split[0]);
else if ("recorder".equals(split[0])) Rule rule = parseRule(split[1]);
builder.addRecorder(parseRecorder(line.substring(8).trim()));
else File file;
builder.addElement(parseElement(line)); switch(rule) {
case DEFAULT:
builder.setDefaultThreshold(threshold);
break;
case EXPLICIT:
builder.addElement(new ExplicitFilterDefinitionElement(split[2], threshold));
break;
case FILE:
file = parseFileName(line, split);
builder.addElement(new FileFilterDefinitionElement(file, threshold));
break;
case RECORDER:
file = parseFileName(line, split);
builder.addRecorder(new Recorder(file, threshold));
}
} }
return builder.build(); return builder.build();
...@@ -171,47 +187,28 @@ class DefinitionParser { ...@@ -171,47 +187,28 @@ class DefinitionParser {
} }
} }
private static Recorder parseRecorder(String line) throws InvalidDefinitionException { private static Rule parseRule(String s) throws InvalidDefinitionException {
String thresholdString = extractThreshold(line); if ("default".equals(s))
return Rule.DEFAULT;
Threshold threshold = parseThreshold(thresholdString); if ("explicit".equals(s))
return Rule.EXPLICIT;
String line2 = line.substring(thresholdString.length()).trim(); if ("file".equals(s))
if (line2.length() == 0) return Rule.FILE;
throw new InvalidDefinitionException("Invalid definition "+ line); if ("recorder".equals(s))
File file = new File(line2); return Rule.RECORDER;
return new Recorder(file, threshold);
throw new InvalidDefinitionException("unknown rule "+s);
} }
private static String extractThreshold(String line) { private static File parseFileName(String s, String[] split) throws InvalidDefinitionException {
StringBuilder thresholdBuilder = new StringBuilder(); if (split.length < 3)
int i = 0; throw new InvalidDefinitionException("invalid definition "+s);
while(i < line.length()) { int beginIndex = s.indexOf(split[1]);
char c = line.charAt(i); if (beginIndex < 0)
if (c == ' ' || c == '\t') throw new IllegalStateException("shouldn't have gotten here "+s);
break; return new File(s.substring(beginIndex + split[1].length()).trim());
i++;
thresholdBuilder.append(c);
}
return thresholdBuilder.toString();
} }
private static FilterDefinitionElement parseElement(String line) throws InvalidDefinitionException {
String thresholdString = extractThreshold(line);
Threshold threshold = parseThreshold(thresholdString);
String line2 = line.substring(thresholdString.length()).trim();
String[] split = DataHelper.split(line2," ");
if (split.length < 2)
throw new InvalidDefinitionException("invalid definition "+line);
if ("explicit".equals(split[0]))
return new ExplicitFilterDefinitionElement(split[1], threshold);
if ("file".equals(split[0])) {
String line3 = line2.substring(4).trim();
File file = new File(line3);
return new FileFilterDefinitionElement(file, threshold);
}
throw new InvalidDefinitionException("invalid definition "+line);
}
private static class DefinitionBuilder { private static class DefinitionBuilder {
private Threshold threshold; private Threshold threshold;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment