Changeset fc9ad32


Ignore:
Timestamp:
Mar 29, 2019 3:36:03 PM (17 months ago)
Author:
zab2 <zab2@…>
Branches:
master
Children:
c0c9582
Parents:
2304e9b
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • apps/i2ptunnel/java/src/net/i2p/i2ptunnel/access/DefinitionParser.java

    r2304e9b rfc9ad32  
    1414 */
    1515class DefinitionParser {
     16
     17    private static enum Rule { DEFAULT, EXPLICIT, FILE, RECORDER }
    1618
    1719    /**
     
    6567     *
    6668     * <pre>
    67      * default 15/5
    68      * default allow
    69      * default deny
     69     * 15/5 default
     70     * allow default
     71     * deny default
    7072     * </pre>
    7173     *
     
    9294     *
    9395     * <pre>
    94      * recorder 30/5 /path/aggressive.txt
    95      * recorder 60/5 /path/very_aggressive.txt
     96     * 30/5 recorder /path/aggressive.txt
     97     * 60/5 recorder /path/very_aggressive.txt
    9698     * </pre>
    9799     * <p>
     
    104106     * <pre>
    105107     * # by default there are no limits
    106      * default allow
     108     * allow default
    107109     * # but record overly aggressive destinations
    108      * recorder 30/5 /path/throttled.txt
     110     * 30/5 recorder /path/throttled.txt
    109111     * # and any that end up in that file will get throttled in the future
    110112     * 15/5 file /path/throttled.txt
     
    122124     * </p>
    123125     * <pre>
    124      * default 15/5
     126     * 15/5 default
    125127     * allow file /path/friends.txt
    126128     * deny file /path/enemies.txt
    127      * recorder 60/5 /path/suspicious.txt
     129     * 60/5 recorder /path/suspicious.txt
    128130     * </pre>
    129131     *
     
    136138
    137139        for (String line : definition) {
    138             String [] split = DataHelper.split(line," ");
     140            String [] split = DataHelper.split(line,"[ \t]");
    139141            split[0] = split[0].toLowerCase();
    140             if ("default".equals(split[0]))
    141                 builder.setDefaultThreshold(parseThreshold(line.substring(7).trim()));
    142             else if ("recorder".equals(split[0]))
    143                 builder.addRecorder(parseRecorder(line.substring(8).trim()));
    144             else
    145                 builder.addElement(parseElement(line));
     142           
     143            Threshold threshold = parseThreshold(split[0]);
     144            Rule rule = parseRule(split[1]);
     145
     146            File file;
     147            switch(rule) {
     148            case DEFAULT:
     149                builder.setDefaultThreshold(threshold);
     150                break;
     151            case EXPLICIT:
     152                builder.addElement(new ExplicitFilterDefinitionElement(split[2], threshold));
     153                break;
     154            case FILE:
     155                file = parseFileName(line, split);
     156                builder.addElement(new FileFilterDefinitionElement(file, threshold));
     157                break;
     158            case RECORDER:
     159                file = parseFileName(line, split);
     160                builder.addRecorder(new Recorder(file, threshold));
     161            }
    146162        }
    147163
     
    172188    }
    173189
    174     private static Recorder parseRecorder(String line) throws InvalidDefinitionException {
    175         String thresholdString = extractThreshold(line);
    176 
    177         Threshold threshold = parseThreshold(thresholdString);
    178 
    179         String line2 = line.substring(thresholdString.length()).trim();
    180         if (line2.length() == 0)
    181             throw new InvalidDefinitionException("Invalid definition "+ line);
    182         File file = new File(line2);
    183         return new Recorder(file, threshold);
    184     }
    185 
    186     private static String extractThreshold(String line) {
    187         StringBuilder thresholdBuilder = new StringBuilder();
    188         int i = 0;
    189         while(i < line.length()) {
    190             char c = line.charAt(i);
    191             if (c == ' ' || c == '\t')
    192                 break;
    193             i++;
    194             thresholdBuilder.append(c);
    195         }
    196         return thresholdBuilder.toString();
    197     }
    198 
    199     private static FilterDefinitionElement parseElement(String line) throws InvalidDefinitionException {
    200         String thresholdString = extractThreshold(line);
    201         Threshold threshold = parseThreshold(thresholdString);
    202         String line2 = line.substring(thresholdString.length()).trim();
    203         String[] split = DataHelper.split(line2," ");
    204         if (split.length < 2)
    205             throw new InvalidDefinitionException("invalid definition "+line);
    206         if ("explicit".equals(split[0]))
    207             return new ExplicitFilterDefinitionElement(split[1], threshold);
    208         if ("file".equals(split[0])) {
    209             String line3 = line2.substring(4).trim();
    210             File file = new File(line3);
    211             return new FileFilterDefinitionElement(file, threshold);
    212         }
    213         throw new InvalidDefinitionException("invalid definition "+line);
    214     }
     190    private static Rule parseRule(String s) throws InvalidDefinitionException {
     191        if ("default".equals(s))
     192            return Rule.DEFAULT;
     193        if ("explicit".equals(s))
     194            return Rule.EXPLICIT;
     195        if ("file".equals(s))
     196            return Rule.FILE;
     197        if ("recorder".equals(s))
     198            return Rule.RECORDER;
     199
     200        throw new InvalidDefinitionException("unknown rule "+s);
     201    }
     202
     203    private static File parseFileName(String s, String[] split) throws InvalidDefinitionException {
     204        if (split.length < 3)
     205            throw new InvalidDefinitionException("invalid definition "+s);
     206        int beginIndex = s.indexOf(split[1]);
     207        if (beginIndex < 0)
     208            throw new IllegalStateException("shouldn't have gotten here "+s);
     209        return new File(s.substring(beginIndex + split[1].length()).trim());
     210    }
     211
    215212
    216213    private static class DefinitionBuilder {
Note: See TracChangeset for help on using the changeset viewer.