Search in sources :

Example 1 with Blocklist

use of net.i2p.router.Blocklist 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)

Aggregations

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 SU3File (net.i2p.crypto.SU3File)1 Hash (net.i2p.data.Hash)1 Banlist (net.i2p.router.Banlist)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