Changeset fd2819c


Ignore:
Timestamp:
Mar 28, 2019 4:44:55 PM (15 months ago)
Author:
zab2 <zab2@…>
Branches:
master
Children:
84ea533
Parents:
fd6cb07
Message:

Use Hash objects instead of Strings representing b32s

Location:
apps/i2ptunnel/java/src/net/i2p/i2ptunnel/access
Files:
5 edited

Legend:

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

    rfd6cb07 rfd2819c  
    1919import net.i2p.util.SecureFileOutputStream;
    2020import net.i2p.data.Destination;
     21import net.i2p.data.Hash;
    2122import net.i2p.i2ptunnel.I2PTunnelTask;
    2223import net.i2p.client.streaming.IncomingConnectionFilter;
     
    3435     * Trackers for known destinations defined in access lists
    3536     */
    36     private final Map<String, DestTracker> knownDests = new HashMap<String, DestTracker>();
     37    private final Map<Hash, DestTracker> knownDests = new HashMap<Hash, DestTracker>();
    3738    /**
    3839     * Trackers for unknown destinations not defined in access lists
    3940     */
    40     private final Map<String, DestTracker> unknownDests = new HashMap<String, DestTracker>();
     41    private final Map<Hash, DestTracker> unknownDests = new HashMap<Hash, DestTracker>();
    4142
    4243    AccessFilter(I2PAppContext context, FilterDefinition definition, I2PTunnelTask task)
     
    5455    @Override
    5556    public boolean allowDestination(Destination d) {
    56         String b32 = d.toBase32();
     57        Hash hash = d.getHash();
    5758        long now = context.clock().now();
    5859        DestTracker tracker = null;
    5960        synchronized(knownDests) {
    60             tracker = knownDests.get(b32);
     61            tracker = knownDests.get(hash);
    6162        }
    6263        if (tracker == null) {
    6364            synchronized(unknownDests) {
    64                 tracker = unknownDests.get(b32);
     65                tracker = unknownDests.get(hash);
    6566                if (tracker == null) {
    66                     tracker = new DestTracker(b32, definition.getDefaultThreshold());
    67                     unknownDests.put(b32, tracker);
     67                    tracker = new DestTracker(hash, definition.getDefaultThreshold());
     68                    unknownDests.put(hash, tracker);
    6869                }
    6970            }
     
    9091                    if (!tracker.getCounter().isBreached(threshold))
    9192                        continue;
    92                     breached.add(tracker.getB32());
     93                    breached.add(tracker.getHash().toBase32());
    9394                }
    9495            }
     
    133134
    134135        synchronized(unknownDests) {
    135             for (Iterator<Map.Entry<String,DestTracker>> iter = unknownDests.entrySet().iterator();
     136            for (Iterator<Map.Entry<Hash,DestTracker>> iter = unknownDests.entrySet().iterator();
    136137                    iter.hasNext();) {
    137                 Map.Entry<String,DestTracker> entry = iter.next();
     138                Map.Entry<Hash,DestTracker> entry = iter.next();
    138139                if (entry.getValue().purge(olderThan))
    139140                    iter.remove();
  • apps/i2ptunnel/java/src/net/i2p/i2ptunnel/access/DestTracker.java

    rfd6cb07 rfd2819c  
    11package net.i2p.i2ptunnel.access;
    22
     3import net.i2p.data.Hash;
    34
    45class DestTracker {
    56   
    6     private final String b32;
     7    private final Hash hash;
    78    private final Threshold threshold;
    89    private final AccessCounter counter;
    910
    10     DestTracker(String b32, Threshold threshold) {
    11         this.b32 = b32;
     11    DestTracker(Hash hash, Threshold threshold) {
     12        this.hash = hash;
    1213        this.threshold = threshold;
    1314        this.counter = new AccessCounter();
    1415    }
    1516
    16     String getB32() {
    17         return b32;
     17    Hash getHash() {
     18        return hash;
    1819    }
    1920
  • apps/i2ptunnel/java/src/net/i2p/i2ptunnel/access/ExplicitFilterDefinitionElement.java

    rfd6cb07 rfd2819c  
    33import java.util.Map;
    44
     5import net.i2p.data.Hash;
     6
    57class ExplicitFilterDefinitionElement extends FilterDefinitionElement {
    68
    7     private final String b32;
     9    private final Hash hash;
    810
    9     ExplicitFilterDefinitionElement(String b32, Threshold threshold) {
     11    ExplicitFilterDefinitionElement(String b32, Threshold threshold) throws InvalidDefinitionException {
    1012        super(threshold);
    11         this.b32 = b32;
     13        this.hash = fromBase32(b32);
    1214    }
    1315
    1416    @Override
    15     public void update(Map<String, DestTracker> map) {
    16         if (map.containsKey(b32))
     17    public void update(Map<Hash, DestTracker> map) {
     18        if (map.containsKey(hash))
    1719            return;
    18         map.put(b32, new DestTracker(b32, threshold));
     20        map.put(hash, new DestTracker(hash, threshold));
    1921    }
    2022}
  • apps/i2ptunnel/java/src/net/i2p/i2ptunnel/access/FileFilterDefinitionElement.java

    rfd6cb07 rfd2819c  
    77import java.io.FileReader;
    88import java.io.IOException;
     9
     10import net.i2p.data.Hash;
    911
    1012class FileFilterDefinitionElement extends FilterDefinitionElement {
     
    1820
    1921    @Override
    20     public void update(Map<String, DestTracker> map) throws IOException {
     22    public void update(Map<Hash, DestTracker> map) throws IOException {
    2123        if (!(file.exists() && file.isFile()))
    2224            return;
     
    2628            String b32;
    2729            while((b32 = reader.readLine()) != null) {
    28                 if (map.containsKey(b32))
     30                Hash hash = fromBase32(b32);
     31                if (map.containsKey(hash))
    2932                    continue;
    30                 map.put(b32, new DestTracker(b32, threshold));
     33                map.put(hash, new DestTracker(hash, threshold));
    3134            }
     35        } catch (InvalidDefinitionException bad32) {
     36            throw new IOException("invalid access list entry", bad32);
    3237        } finally {
    3338            if (reader != null) {
  • apps/i2ptunnel/java/src/net/i2p/i2ptunnel/access/FilterDefinitionElement.java

    rfd6cb07 rfd2819c  
    33import java.util.Map;
    44import java.io.IOException;
     5
     6import net.i2p.data.Hash;
     7import net.i2p.data.Base32;
    58
    69abstract class FilterDefinitionElement {
     
    1215    }
    1316
    14     abstract void update(Map<String, DestTracker> map) throws IOException;
     17    abstract void update(Map<Hash, DestTracker> map) throws IOException;
    1518
    1619    Threshold getThreshold() {
    1720        return threshold;
    1821    }
     22
     23    protected static Hash fromBase32(String b32) throws InvalidDefinitionException {
     24        if (!b32.endsWith("b32.i2p"))
     25            throw new InvalidDefinitionException("Invalid b32 " + b32);
     26        b32 = b32.substring(0, b32.length() - 7);
     27        return new Hash(Base32.decode(b32));
     28    }
    1929}
Note: See TracChangeset for help on using the changeset viewer.