use of net.i2p.router.Banlist in project i2p.i2p by i2p.
the class NewsFetcher method processBlocklistEntries.
/**
* Process blocklist entries
*
* @since 0.9.28
*/
private void processBlocklistEntries(BlocklistEntries ble) {
long oldTime = _context.getProperty(PROP_BLOCKLIST_TIME, 0L);
if (ble.updated <= oldTime) {
if (_log.shouldWarn())
_log.warn("Not processing blocklist " + new Date(ble.updated) + ", already have " + new Date(oldTime));
return;
}
Blocklist bl = _context.blocklist();
Banlist ban = _context.banlist();
DateFormat fmt = DateFormat.getDateInstance(DateFormat.SHORT);
fmt.setTimeZone(SystemVersion.getSystemTimeZone(_context));
String reason = "Blocklist feed " + new Date(ble.updated);
int banned = 0;
for (Iterator<String> iter = ble.entries.iterator(); iter.hasNext(); ) {
String s = iter.next();
if (s.length() == 44) {
byte[] b = Base64.decode(s);
if (b == null || b.length != Hash.HASH_LENGTH) {
iter.remove();
continue;
}
Hash h = Hash.create(b);
if (!ban.isBanlistedForever(h))
ban.banlistRouterForever(h, reason);
} else {
byte[] ip = Addresses.getIP(s);
if (ip == null) {
iter.remove();
continue;
}
if (!bl.isBlocklisted(ip))
bl.add(ip);
}
if (++banned >= BlocklistEntries.MAX_ENTRIES) {
// prevent somebody from destroying the whole network
break;
}
}
for (String s : ble.removes) {
if (s.length() == 44) {
byte[] b = Base64.decode(s);
if (b == null || b.length != Hash.HASH_LENGTH)
continue;
Hash h = Hash.create(b);
if (ban.isBanlistedForever(h))
ban.unbanlistRouter(h);
} else {
byte[] ip = Addresses.getIP(s);
if (ip == null)
continue;
if (bl.isBlocklisted(ip))
bl.remove(ip);
}
}
// Save the blocks. We do not save the unblocks.
File f = new SecureFile(_context.getConfigDir(), BLOCKLIST_DIR);
f.mkdirs();
f = new File(f, BLOCKLIST_FILE);
boolean fail = false;
BufferedWriter out = null;
try {
out = new BufferedWriter(new OutputStreamWriter(new SecureFileOutputStream(f), "UTF-8"));
out.write("# ");
out.write(ble.supdated);
out.newLine();
banned = 0;
for (String s : ble.entries) {
// IPv6
s = s.replace(':', ';');
out.write(reason);
out.write(':');
out.write(s);
out.newLine();
if (++banned >= BlocklistEntries.MAX_ENTRIES)
break;
}
} catch (IOException ioe) {
_log.error("Error writing blocklist", ioe);
fail = true;
} finally {
if (out != null)
try {
out.close();
} catch (IOException ioe) {
}
}
if (!fail) {
f.setLastModified(ble.updated);
String upd = Long.toString(ble.updated);
_context.router().saveConfig(PROP_BLOCKLIST_TIME, upd);
_mgr.notifyVersionAvailable(this, _currentURI, BLOCKLIST, "", HTTP, null, upd, "");
}
if (_log.shouldWarn())
_log.warn("Processed " + ble.entries.size() + " blocks and " + ble.removes.size() + " unblocks from news feed");
}
use of net.i2p.router.Banlist in project i2p.i2p by i2p.
the class BanlistRenderer method renderStatusHTML.
public void renderStatusHTML(Writer out) throws IOException {
StringBuilder buf = new StringBuilder(1024);
// move to the jsp
// buf.append("<h2>Banned Peers</h2>");
Map<Hash, Banlist.Entry> entries = new TreeMap<Hash, Banlist.Entry>(new HashComparator());
entries.putAll(_context.banlist().getEntries());
if (entries.isEmpty()) {
buf.append("<i>").append(_t("none")).append("</i>");
out.write(buf.toString());
return;
}
buf.append("<ul id=\"banlist\">");
for (Map.Entry<Hash, Banlist.Entry> e : entries.entrySet()) {
Hash key = e.getKey();
Banlist.Entry entry = e.getValue();
long expires = entry.expireOn - _context.clock().now();
if (expires <= 0)
continue;
buf.append("<li>").append(_context.commSystem().renderPeerHTML(key));
buf.append(' ');
String expireString = DataHelper.formatDuration2(expires);
if (key.equals(Hash.FAKE_HASH))
buf.append(_t("Permanently banned"));
else if (expires < 5l * 24 * 60 * 60 * 1000)
buf.append(_t("Temporary ban expiring in {0}", expireString));
else
buf.append(_t("Banned until restart or in {0}", expireString));
Set<String> transports = entry.transports;
if ((transports != null) && (!transports.isEmpty()))
buf.append(" on the following transport: ").append(transports);
if (entry.cause != null) {
buf.append("<br>\n");
if (entry.causeCode != null)
buf.append(_t(entry.cause, entry.causeCode));
else
buf.append(_t(entry.cause));
}
if (!key.equals(Hash.FAKE_HASH)) {
buf.append(" <a href=\"configpeer?peer=").append(key.toBase64()).append("#unsh\">[").append(_t("unban now")).append("]</a>");
}
buf.append("</li>\n");
}
buf.append("</ul>\n");
out.write(buf.toString());
out.flush();
}
Aggregations