Skip to content

Commit

Permalink
Merge branch 'i2p.i2p.2.4.0-revert-blocklist-changes' into 'master'
Browse files Browse the repository at this point in the history
Revert Blocklist/Banlist Changes

See merge request i2p-hackers/i2p.i2p!149
  • Loading branch information
eyedeekay committed Nov 8, 2023
2 parents 8ce79f3 + 878f7b0 commit d8415f2
Show file tree
Hide file tree
Showing 17 changed files with 77 additions and 129 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ private void processBlocklistEntries(BlocklistEntries ble) {
continue;
}
Hash h = Hash.create(b);
if (!ban.isBanlistedHard(h)) {
if (!ban.isBanlistedForever(h)) {
ban.banlistRouterForever(h, reason);
_context.commSystem().forceDisconnect(h);
}
Expand All @@ -681,7 +681,7 @@ private void processBlocklistEntries(BlocklistEntries ble) {
if (b == null || b.length != Hash.HASH_LENGTH)
continue;
Hash h = Hash.create(b);
if (ban.isBanlistedHard(h))
if (ban.isBanlistedForever(h))
ban.unbanlistRouter(h);
} else {
byte[] ip = Addresses.getIP(s);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,12 @@ else if (expires < 5l*24*60*60*1000)
buf.append(" on the following transport: ").append(transports);
if (entry.cause != null) {
buf.append("<br>\n");
if (entry.causeComment != null)
buf.append(_t(entry.cause, entry.causeComment));
if (entry.causeCode != null)
buf.append(_t(entry.cause, entry.causeCode));
else
buf.append(_t(entry.cause));
}
if (!key.equals(Hash.FAKE_HASH)) {
if (entry.causeCode == 1)
buf.append(" (H)");
// note: CSS hides anchor text
buf.append(" <a href=\"configpeer?peer=").append(key.toBase64())
.append("#unsh\" title=\"").append(unban).append("\">[").append(unban).append("]</a>");
Expand Down
114 changes: 37 additions & 77 deletions router/java/src/net/i2p/router/Banlist.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,8 @@ public static class Entry {
public long expireOn;
/** why they were banlisted */
public String cause;
/** separate comment so cause can contain {0} for translation */
public String causeComment;
/** Code used for classifying the handling of the ban */
public Integer causeCode;
/** separate code so cause can contain {0} for translation */
public String causeCode;
/** what transports they were banlisted for (String), or null for all transports */
public Set<String> transports;
}
Expand All @@ -54,18 +52,15 @@ public static class Entry {
public final static long BANLIST_DURATION_MS = 7*60*1000;
public final static long BANLIST_DURATION_MAX = 30*60*1000;
public final static long BANLIST_DURATION_PARTIAL = 10*60*1000;
public final static long BANLIST_DURATION_HARD = 181l*24*60*60*1000; // will get rounded down to 180d on console
public final static long BANLIST_DURATION_FOREVER = 181l*24*60*60*1000; // will get rounded down to 180d on console
/**
* Buggy i2pd fork
* @since 0.9.52
*/
public final static long BANLIST_DURATION_NO_NETWORK = 30*24*60*60*1000L;
public final static long BANLIST_DURATION_LOCALHOST = 2*60*60*1000;
private final static long BANLIST_CLEANER_START_DELAY = BANLIST_DURATION_PARTIAL;

public final static Integer BANLIST_CODE_SOFT = 0;
public final static Integer BANLIST_CODE_HARD = 1;


public Banlist(RouterContext context) {
_context = context;
_log = context.logManager().getLog(Banlist.class);
Expand Down Expand Up @@ -135,8 +130,8 @@ public boolean banlistRouter(Hash peer) {
/**
* @return true if it WAS previously on the list
*/
public boolean banlistRouter(String reasonComment, Hash peer, String reason) {
return banlistRouter(peer, reason, reasonComment, null, false);
public boolean banlistRouter(String reasonCode, Hash peer, String reason) {
return banlistRouter(peer, reason, reasonCode, null, false);
}

/**
Expand All @@ -156,65 +151,46 @@ public boolean banlistRouterForever(Hash peer, String reason) {
/**
* @return true if it WAS previously on the list
*/
public boolean banlistRouterForever(Hash peer, String reason, String reasonComment) {
return banlistRouter(peer, reason, reasonComment, null, true);
public boolean banlistRouterForever(Hash peer, String reason, String reasonCode) {
return banlistRouter(peer, reason, reasonCode, null, true);
}

/**
* @return true if it WAS previously on the list
*/
public boolean banlistRouter(Hash peer, String reason, String transport, boolean hard) {
return banlistRouter(peer, reason, null, transport, hard);
public boolean banlistRouter(Hash peer, String reason, String transport, boolean forever) {
return banlistRouter(peer, reason, null, transport, forever);
}

/**
* @return true if it WAS previously on the list
*/
private boolean banlistRouter(Hash peer, String reason, String reasonComment, String transport, boolean hard) {
private boolean banlistRouter(Hash peer, String reason, String reasonCode, String transport, boolean forever) {
long expireOn;
Integer reasonCode;
if (hard) {
expireOn = _context.clock().now() + BANLIST_DURATION_HARD;
reasonCode = BANLIST_CODE_HARD;
if (forever) {
expireOn = _context.clock().now() + BANLIST_DURATION_FOREVER;
} else if (transport != null) {
expireOn = _context.clock().now() + BANLIST_DURATION_PARTIAL;
reasonCode = BANLIST_CODE_SOFT;
} else {
long period = BANLIST_DURATION_MS + _context.random().nextLong(BANLIST_DURATION_MS / 4);
if (period > BANLIST_DURATION_MAX)
period = BANLIST_DURATION_MAX;
expireOn = _context.clock().now() + period;
reasonCode = BANLIST_CODE_SOFT;
}
return banlistRouter(peer, reason, reasonComment, reasonCode, transport, expireOn);
}

/**
* @return true if it WAS previously on the list
*/
public boolean banlistRouter(Hash peer, String reason, String reasonComment, String transport, long expireOn) {
Integer reasonCode = BANLIST_CODE_SOFT; // Default
// To maintain legacy behavior, set reasonCode to BANLIST_CODE_HARD
// if expireOn is longer than 2 days.
if (expireOn > _context.clock().now() + 2*24*60*60*1000L)
reasonCode = BANLIST_CODE_HARD;
return banlistRouter(peer, reason, reasonComment, reasonCode, transport, expireOn);
return banlistRouter(peer, reason, reasonCode, transport, expireOn);
}

/**
* So that we may specify an expiration
*
* @param reason may be null
* @param reasonComment may be null
* @param reasonCode Integer handling code.
* BANLIST_CODE_SOFT - 0 - SOFT ban handling
* BANLIST_CODE_HARD - 1 - HARD ban handling (corresponds to legacy 'forever' ban handling)
* @param reasonCode may be null
* @param expireOn absolute time, not a duration
* @param transport may be null
* @return true if it WAS previously on the list
* @since 0.9.18
*/
public boolean banlistRouter(Hash peer, String reason, String reasonComment, Integer reasonCode, String transport, long expireOn) {
public boolean banlistRouter(Hash peer, String reason, String reasonCode, String transport, long expireOn) {
if (peer == null) {
_log.error("ban null?", new Exception());
return false;
Expand All @@ -232,40 +208,33 @@ public boolean banlistRouter(Hash peer, String reason, String reasonComment, Int
Entry e = new Entry();
e.expireOn = expireOn;
e.cause = reason;
e.causeComment = reasonComment;
e.causeCode = reasonCode;
e.transports = null;
if (transport != null) {
e.transports = new ConcurrentHashSet<String>(2);
e.transports.add(transport);
}

Entry old = _entries.get(peer);
if (old != null) {
wasAlready = true;
// take the oldest expiration and cause, combine transports
if (old.expireOn > e.expireOn) {
e.expireOn = old.expireOn;
e.cause = old.cause;
e.causeComment = old.causeComment;
}
// Preserve BANLIST_CODE_HARD over BANLIST_CODE_SOFT
// Otherwise, take the highest banlist handling code.
if ((e.causeCode == 1) || (old.causeCode == 1))
e.causeCode = 1;
else
e.causeCode = Math.max(e.causeCode, old.causeCode);
if (e.transports != null) {
if (old.transports != null)
e.transports.addAll(old.transports);
else {
e.transports = null;
e.cause = reason;
e.causeComment = reasonComment;
Entry old = _entries.get(peer);
if (old != null) {
wasAlready = true;
// take the oldest expiration and cause, combine transports
if (old.expireOn > e.expireOn) {
e.expireOn = old.expireOn;
e.cause = old.cause;
e.causeCode = old.causeCode;
}
if (e.transports != null) {
if (old.transports != null)
e.transports.addAll(old.transports);
else {
e.transports = null;
e.cause = reason;
e.causeCode = reasonCode;
}
}
}
}
_entries.put(peer, e);
_entries.put(peer, e);

if (transport == null) {
// we hate the peer on *any* transport
Expand Down Expand Up @@ -349,19 +318,10 @@ public boolean isBanlisted(Hash peer, String transport) {

return rv;
}

public boolean isBanlistedHard(Hash peer) {
boolean rv = false;


public boolean isBanlistedForever(Hash peer) {
Entry entry = _entries.get(peer);
if (entry == null)
rv = false;
else if (entry.causeCode == BANLIST_CODE_HARD)
rv = true;
else
rv = (entry.expireOn > _context.clock().now() + 2*24*60*60*1000L);

return rv;
return entry != null && entry.expireOn > _context.clock().now() + 2*24*60*60*1000L;
}

/** @deprecated moved to router console */
Expand Down
39 changes: 14 additions & 25 deletions router/java/src/net/i2p/router/Blocklist.java
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,11 @@ public synchronized void startup() {
// but it's important to have this initialized before we read in the netdb.
//job.getTiming().setStartAfter(_context.clock().now() + 30*1000);
_context.jobQueue().addJob(job);
Job cleanupJob = new CleanupJob();
cleanupJob.getTiming().setStartAfter(_context.clock().now() + expireInterval());
_context.jobQueue().addJob(cleanupJob);
if (expireInterval() > 0) {
Job cleanupJob = new CleanupJob();
cleanupJob.getTiming().setStartAfter(_context.clock().now() + expireInterval());
_context.jobQueue().addJob(cleanupJob);
}
}

/**
Expand Down Expand Up @@ -278,21 +280,12 @@ public String getName(){
return "Expire blocklist at user-defined interval of " + expireInterval();
}
public void runJob() {
int jobInterval;

if (expireInterval() > 0) {
clear();
_lastExpired = System.currentTimeMillis();
jobInterval = expireInterval();
if (_log.shouldLog(Log.DEBUG))
_log.debug("Expiring blocklist entrys at" + _lastExpired);
} else {
// Set the next job interval to 15 minutes when expireInterval disabled
jobInterval = 15 * 60 * 1000;
}

clear();
_lastExpired = System.currentTimeMillis();
if (_log.shouldLog(Log.DEBUG))
_log.debug("Expiring blocklist entrys at" + _lastExpired);
// schedule the next one
super.requeue(jobInterval);
super.requeue(expireInterval());
}
}

Expand Down Expand Up @@ -368,9 +361,7 @@ private int process() {

private void banlistRouter(Hash peer, String reason, String comment) {
if (expireInterval() > 0)
_context.banlist().banlistRouter(peer, reason, comment,
_context.banlist().BANLIST_CODE_HARD, null,
_context.clock().now() + expireInterval());
_context.banlist().banlistRouter(peer, reason, comment, null, expireInterval());
else
_context.banlist().banlistRouterForever(peer, reason, comment);
}
Expand Down Expand Up @@ -1281,13 +1272,11 @@ public void runJob() {
* So we also stagger these jobs.
*
*/
private void banlistRouter( Hash peer, String reason, String reasonComment, long duration) {
private void banlistRouter( Hash peer, String reason, String reasonCode, long duration) {
if (duration > 0)
_context.banlist().banlistRouter(peer, reason, reasonComment,
_context.banlist().BANLIST_CODE_HARD, null,
System.currentTimeMillis()+expireInterval());
_context.banlist().banlistRouter(peer, reason, reasonCode, null, System.currentTimeMillis()+expireInterval());
else
_context.banlist().banlistRouterForever(peer, reason, reasonComment);
_context.banlist().banlistRouterForever(peer, reason, reasonCode);
}
private synchronized void banlistRouter(Hash peer, List<byte[]> ips, long duration) {
// This only checks one file for now, pick the best one
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public Job createJob(I2NPMessage receivedMessage, RouterIdentity from, Hash from

DatabaseLookupMessage dlm = (DatabaseLookupMessage)receivedMessage;
boolean isBanned = dlm.getFrom() != null
&& (_context.banlist().isBanlistedHard(dlm.getFrom())
&& (_context.banlist().isBanlistedForever(dlm.getFrom())
|| _context.banlist().isBanlisted(dlm.getFrom()));
if (isBanned) {
_context.statManager().addRateData("netDb.lookupsDroppedDueToPriorBan", 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ public boolean floodConditional(DatabaseEntry ds) {
if (!floodfillEnabled())
return false;
Hash h = ds.getHash();
if (_context.banlist().isBanlistedHard(h))
if (_context.banlist().isBanlistedForever(h))
return false;
if (shouldThrottleFlood(h)) {
_context.statManager().addRateData("netDb.floodThrottled", 1);
Expand Down Expand Up @@ -668,7 +668,7 @@ protected void lookupBeforeDropping(Hash peer, RouterInfo info) {
knownRouters > MAX_DB_BEFORE_SKIPPING_SEARCH ||
_context.jobQueue().getMaxLag() > 500 ||
_context.router().gracefulShutdownInProgress() ||
_context.banlist().isBanlistedHard(peer)) {
_context.banlist().isBanlistedForever(peer)) {
// don't try to overload ourselves (e.g. failing 3000 router refs at
// once, and then firing off 3000 netDb lookup tasks)
// Also don't queue a search if we have plenty of routerinfos
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ private List<Hash> selectFloodfillParticipants(Set<Hash> toIgnore, KBucketSet<Ha
List<Hash> rv = new ArrayList<Hash>(set.size());
for (Hash h : set) {
if ((toIgnore != null && toIgnore.contains(h)) ||
_context.banlist().isBanlistedHard(h))
_context.banlist().isBanlistedForever(h))
continue;
rv.add(h);
}
Expand Down Expand Up @@ -371,7 +371,7 @@ public void add(Hash entry) {
//if (_context.banlist().isBanlisted(entry))
// return;
// ... unless they are really bad
if (_context.banlist().isBanlistedHard(entry))
if (_context.banlist().isBanlistedForever(entry))
return;
RouterInfo info = (RouterInfo) _context.netDb().lookupLocallyWithoutValidation(entry);
//if (info == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ else if (_fromHash.equals(key))
// Check new routerinfo address against blocklist
if (wasNew) {
// TODO should we not flood temporarily banned routers either?
boolean forever = getContext().banlist().isBanlistedHard(key);
boolean forever = getContext().banlist().isBanlistedForever(key);
if (forever) {
wasNew = false; // don't flood
shouldStore = false; // don't call heardAbout()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void runJob() {
invalidPeers++;
continue;
}
if (getContext().banlist().isBanlistedHard(peer)) {
if (getContext().banlist().isBanlistedForever(peer)) {
oldPeers++;
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ void newPeerToTry(Hash peer) {
if (peer.equals(getContext().routerHash()) ||
peer.equals(_key))
return;
if (getContext().banlist().isBanlistedHard(peer)) {
if (getContext().banlist().isBanlistedForever(peer)) {
if (_log.shouldLog(Log.INFO))
_log.info(getJobId() + ": banlisted peer from DSRM " + peer);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,7 @@ public void lookupRouterInfo(Hash key, Job onFindJob, Job onFailedLookupJob, lon
if (ri != null) {
if (onFindJob != null)
_context.jobQueue().addJob(onFindJob);
} else if (_context.banlist().isBanlistedHard(key)) {
} else if (_context.banlist().isBanlistedForever(key)) {
if (onFailedLookupJob != null)
_context.jobQueue().addJob(onFailedLookupJob);
} else if (isNegativeCached(key)) {
Expand Down
Loading

0 comments on commit d8415f2

Please sign in to comment.