Search in sources :

Example 1 with Banlist

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");
}
Also used : SecureFile(net.i2p.util.SecureFile) IOException(java.io.IOException) Hash(net.i2p.data.Hash) Date(java.util.Date) RFC822Date(net.i2p.util.RFC822Date) Banlist(net.i2p.router.Banlist) BufferedWriter(java.io.BufferedWriter) Blocklist(net.i2p.router.Blocklist) DateFormat(java.text.DateFormat) OutputStreamWriter(java.io.OutputStreamWriter) SecureFileOutputStream(net.i2p.util.SecureFileOutputStream) SU3File(net.i2p.crypto.SU3File) SecureFile(net.i2p.util.SecureFile) File(java.io.File)

Example 2 with Banlist

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();
}
Also used : Hash(net.i2p.data.Hash) TreeMap(java.util.TreeMap) Banlist(net.i2p.router.Banlist) TreeMap(java.util.TreeMap) Map(java.util.Map)

Aggregations

Hash (net.i2p.data.Hash)2 Banlist (net.i2p.router.Banlist)2 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 IOException (java.io.IOException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 DateFormat (java.text.DateFormat)1 Date (java.util.Date)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 SU3File (net.i2p.crypto.SU3File)1 Blocklist (net.i2p.router.Blocklist)1 RFC822Date (net.i2p.util.RFC822Date)1 SecureFile (net.i2p.util.SecureFile)1 SecureFileOutputStream (net.i2p.util.SecureFileOutputStream)1