forked from I2P_Developers/i2p.i2p
Use Hash objects instead of Strings representing b32s
This commit is contained in:
@@ -18,6 +18,7 @@ import net.i2p.util.SimpleTimer2;
|
||||
import net.i2p.util.Log;
|
||||
import net.i2p.util.SecureFileOutputStream;
|
||||
import net.i2p.data.Destination;
|
||||
import net.i2p.data.Hash;
|
||||
import net.i2p.i2ptunnel.I2PTunnelTask;
|
||||
import net.i2p.client.streaming.IncomingConnectionFilter;
|
||||
|
||||
@@ -33,11 +34,11 @@ class AccessFilter implements IncomingConnectionFilter {
|
||||
/**
|
||||
* Trackers for known destinations defined in access lists
|
||||
*/
|
||||
private final Map<String, DestTracker> knownDests = new HashMap<String, DestTracker>();
|
||||
private final Map<Hash, DestTracker> knownDests = new HashMap<Hash, DestTracker>();
|
||||
/**
|
||||
* Trackers for unknown destinations not defined in access lists
|
||||
*/
|
||||
private final Map<String, DestTracker> unknownDests = new HashMap<String, DestTracker>();
|
||||
private final Map<Hash, DestTracker> unknownDests = new HashMap<Hash, DestTracker>();
|
||||
|
||||
AccessFilter(I2PAppContext context, FilterDefinition definition, I2PTunnelTask task)
|
||||
throws IOException {
|
||||
@@ -53,18 +54,18 @@ class AccessFilter implements IncomingConnectionFilter {
|
||||
|
||||
@Override
|
||||
public boolean allowDestination(Destination d) {
|
||||
String b32 = d.toBase32();
|
||||
Hash hash = d.getHash();
|
||||
long now = context.clock().now();
|
||||
DestTracker tracker = null;
|
||||
synchronized(knownDests) {
|
||||
tracker = knownDests.get(b32);
|
||||
tracker = knownDests.get(hash);
|
||||
}
|
||||
if (tracker == null) {
|
||||
synchronized(unknownDests) {
|
||||
tracker = unknownDests.get(b32);
|
||||
tracker = unknownDests.get(hash);
|
||||
if (tracker == null) {
|
||||
tracker = new DestTracker(b32, definition.getDefaultThreshold());
|
||||
unknownDests.put(b32, tracker);
|
||||
tracker = new DestTracker(hash, definition.getDefaultThreshold());
|
||||
unknownDests.put(hash, tracker);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -89,7 +90,7 @@ class AccessFilter implements IncomingConnectionFilter {
|
||||
for (DestTracker tracker : unknownDests.values()) {
|
||||
if (!tracker.getCounter().isBreached(threshold))
|
||||
continue;
|
||||
breached.add(tracker.getB32());
|
||||
breached.add(tracker.getHash().toBase32());
|
||||
}
|
||||
}
|
||||
if (breached.isEmpty())
|
||||
@@ -132,9 +133,9 @@ class AccessFilter implements IncomingConnectionFilter {
|
||||
}
|
||||
|
||||
synchronized(unknownDests) {
|
||||
for (Iterator<Map.Entry<String,DestTracker>> iter = unknownDests.entrySet().iterator();
|
||||
for (Iterator<Map.Entry<Hash,DestTracker>> iter = unknownDests.entrySet().iterator();
|
||||
iter.hasNext();) {
|
||||
Map.Entry<String,DestTracker> entry = iter.next();
|
||||
Map.Entry<Hash,DestTracker> entry = iter.next();
|
||||
if (entry.getValue().purge(olderThan))
|
||||
iter.remove();
|
||||
}
|
||||
|
@@ -1,20 +1,21 @@
|
||||
package net.i2p.i2ptunnel.access;
|
||||
|
||||
import net.i2p.data.Hash;
|
||||
|
||||
class DestTracker {
|
||||
|
||||
private final String b32;
|
||||
private final Hash hash;
|
||||
private final Threshold threshold;
|
||||
private final AccessCounter counter;
|
||||
|
||||
DestTracker(String b32, Threshold threshold) {
|
||||
this.b32 = b32;
|
||||
DestTracker(Hash hash, Threshold threshold) {
|
||||
this.hash = hash;
|
||||
this.threshold = threshold;
|
||||
this.counter = new AccessCounter();
|
||||
}
|
||||
|
||||
String getB32() {
|
||||
return b32;
|
||||
Hash getHash() {
|
||||
return hash;
|
||||
}
|
||||
|
||||
AccessCounter getCounter() {
|
||||
|
@@ -2,19 +2,21 @@ package net.i2p.i2ptunnel.access;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import net.i2p.data.Hash;
|
||||
|
||||
class ExplicitFilterDefinitionElement extends FilterDefinitionElement {
|
||||
|
||||
private final String b32;
|
||||
private final Hash hash;
|
||||
|
||||
ExplicitFilterDefinitionElement(String b32, Threshold threshold) {
|
||||
ExplicitFilterDefinitionElement(String b32, Threshold threshold) throws InvalidDefinitionException {
|
||||
super(threshold);
|
||||
this.b32 = b32;
|
||||
this.hash = fromBase32(b32);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(Map<String, DestTracker> map) {
|
||||
if (map.containsKey(b32))
|
||||
public void update(Map<Hash, DestTracker> map) {
|
||||
if (map.containsKey(hash))
|
||||
return;
|
||||
map.put(b32, new DestTracker(b32, threshold));
|
||||
map.put(hash, new DestTracker(hash, threshold));
|
||||
}
|
||||
}
|
||||
|
@@ -7,6 +7,8 @@ import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
import net.i2p.data.Hash;
|
||||
|
||||
class FileFilterDefinitionElement extends FilterDefinitionElement {
|
||||
|
||||
private final File file;
|
||||
@@ -17,7 +19,7 @@ class FileFilterDefinitionElement extends FilterDefinitionElement {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(Map<String, DestTracker> map) throws IOException {
|
||||
public void update(Map<Hash, DestTracker> map) throws IOException {
|
||||
if (!(file.exists() && file.isFile()))
|
||||
return;
|
||||
BufferedReader reader = null;
|
||||
@@ -25,10 +27,13 @@ class FileFilterDefinitionElement extends FilterDefinitionElement {
|
||||
reader = new BufferedReader(new FileReader(file));
|
||||
String b32;
|
||||
while((b32 = reader.readLine()) != null) {
|
||||
if (map.containsKey(b32))
|
||||
Hash hash = fromBase32(b32);
|
||||
if (map.containsKey(hash))
|
||||
continue;
|
||||
map.put(b32, new DestTracker(b32, threshold));
|
||||
map.put(hash, new DestTracker(hash, threshold));
|
||||
}
|
||||
} catch (InvalidDefinitionException bad32) {
|
||||
throw new IOException("invalid access list entry", bad32);
|
||||
} finally {
|
||||
if (reader != null) {
|
||||
try { reader.close(); } catch (IOException ignored) {}
|
||||
|
@@ -3,6 +3,9 @@ package net.i2p.i2ptunnel.access;
|
||||
import java.util.Map;
|
||||
import java.io.IOException;
|
||||
|
||||
import net.i2p.data.Hash;
|
||||
import net.i2p.data.Base32;
|
||||
|
||||
abstract class FilterDefinitionElement {
|
||||
|
||||
protected final Threshold threshold;
|
||||
@@ -11,9 +14,16 @@ abstract class FilterDefinitionElement {
|
||||
this.threshold = threshold;
|
||||
}
|
||||
|
||||
abstract void update(Map<String, DestTracker> map) throws IOException;
|
||||
abstract void update(Map<Hash, DestTracker> map) throws IOException;
|
||||
|
||||
Threshold getThreshold() {
|
||||
return threshold;
|
||||
}
|
||||
|
||||
protected static Hash fromBase32(String b32) throws InvalidDefinitionException {
|
||||
if (!b32.endsWith("b32.i2p"))
|
||||
throw new InvalidDefinitionException("Invalid b32 " + b32);
|
||||
b32 = b32.substring(0, b32.length() - 7);
|
||||
return new Hash(Base32.decode(b32));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user