Changeset fb4d85f


Ignore:
Timestamp:
Dec 18, 2011 1:28:26 PM (9 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
61810b7
Parents:
937d2c54
Message:
Location:
router/java/src/net/i2p/router
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • router/java/src/net/i2p/router/Blocklist.java

    r937d2c54 rfb4d85f  
    66 */
    77
     8import java.io.BufferedReader;
    89import java.io.IOException;
    910import java.io.File;
    1011import java.io.FileInputStream;
     12import java.io.InputStreamReader;
    1113import java.io.Writer;
    1214import java.net.InetAddress;
     
    2426
    2527import net.i2p.data.Base64;
    26 import net.i2p.data.DataHelper;
    2728import net.i2p.data.Hash;
    2829import net.i2p.data.RouterAddress;
    2930import net.i2p.data.RouterInfo;
    3031import net.i2p.router.networkdb.kademlia.FloodfillNetworkDatabaseFacade;
     32import net.i2p.util.Addresses;
    3133import net.i2p.util.ConcurrentHashSet;
    3234import net.i2p.util.Log;
     
    4850 *
    4951 * So, this class maintains three separate lists:
     52 *<pre>
    5053 *   1) The list of IP ranges, read in from a file at startup
    5154 *   2) The list of hashes, read in from the same file
    5255 *   3) A list of single IPs, initially empty, added to as needed
     56 *</pre>
    5357 *
    5458 * Read in the IP blocklist from a file, store it in-memory as efficiently
     
    198202        try {
    199203            in = new FileInputStream(BLFile);
    200             StringBuilder buf = new StringBuilder(128);
    201             while (DataHelper.readLine(in, buf) && count < maxSize) {
     204            BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF-8"));
     205            String buf = null;
     206            while ((buf = br.readLine()) != null && count < maxSize) {
    202207                Entry e = parse(buf, true);
    203                 buf.setLength(0);
    204208                if (e == null) {
    205209                    badcount++;
     
    277281    }
    278282
    279     private Entry parse(StringBuilder buf, boolean bitch) {
     283    private Entry parse(String buf, boolean bitch) {
    280284        byte[] ip1;
    281285        byte[] ip2;
     
    284288        if (end1 <= 0)
    285289            return null;  // blank
    286         if (buf.charAt(end1 - 1) == '\r') {  // DataHelper.readLine leaves the \r on there
    287             buf.deleteCharAt(end1 - 1);
    288             end1--;
    289         }
     290        //if (buf.charAt(end1 - 1) == '\r') {  // DataHelper.readLine leaves the \r on there
     291        //    buf.deleteCharAt(end1 - 1);
     292        //    end1--;
     293        //}
    290294        if (end1 <= 0)
    291295            return null;  // blank
     
    388392        try {
    389393            in = new FileInputStream(BLFile);
    390             StringBuilder buf = new StringBuilder(128);
    391             while (DataHelper.readLine(in, buf)) {
     394            BufferedReader br = new BufferedReader(new InputStreamReader(in, "ISO-8859-1"));
     395            while (br.readLine() != null) {
    392396                lines++;
    393                 buf.setLength(0);
    394397            }
    395398        } catch (IOException ioe) {
     
    458461        if (add(toInt(ip)))
    459462            if (_log.shouldLog(Log.WARN))
    460                 _log.warn("Adding IP to blocklist: " + (ip[0]&0xff) + '.' + (ip[1]&0xff) + '.' + (ip[2]&0xff) + '.' + (ip[3]&0xff));
     463                _log.warn("Adding IP to blocklist: " + Addresses.toString(ip));
    461464    }
    462465
     
    644647    }
    645648
    646     /** IP to string */
    647     public static String toStr(byte[] ip) {
    648         return toStr(toInt(ip));
    649     }
    650 
    651649    private static String toStr(long entry) {
    652650        StringBuilder buf = new StringBuilder(32);
     
    682680        // Temporary reason, until the job finishes
    683681        String reason = _x("IP banned by blocklist.txt entry {0}");
    684         _context.shitlist().shitlistRouterForever(peer, reason, toStr(ip));
     682        _context.shitlist().shitlistRouterForever(peer, reason, Addresses.toString(ip));
    685683        if (!  _context.getBooleanPropertyDefaultTrue(PROP_BLOCKLIST_DETAIL))
    686684            return;
     
    744742            try {
    745743                in = new FileInputStream(BLFile);
    746                 StringBuilder buf = new StringBuilder(128);
     744                BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF-8"));
     745                String buf = null;
    747746                // assume the file is unsorted, so go through the whole thing
    748                 while (DataHelper.readLine(in, buf)) {
     747                while ((buf = br.readLine()) != null) {
    749748                    Entry e = parse(buf, false);
    750749                    if (e == null || e.peer != null) {
    751                         buf.setLength(0);
    752750                        continue;
    753751                    }
     
    767765                        return;
    768766                    }
    769                     buf.setLength(0);
    770767                }
    771768            } catch (IOException ioe) {
     
    784781     *  Write directly to the stream so we don't OOM on a huge list.
    785782     *  Go through each list twice since we store out-of-order.
     783     *
     784     *  TODO move to routerconsole, but that would require exposing the _blocklist array.
    786785     */
    787786    public void renderStatusHTML(Writer out) throws IOException {
  • router/java/src/net/i2p/router/networkdb/kademlia/PersistentDataStore.java

    r937d2c54 rfb4d85f  
    99 */
    1010
     11import java.io.BufferedInputStream;
     12import java.io.BufferedOutputStream;
    1113import java.io.File;
    1214import java.io.FileInputStream;
     
    1416import java.io.FilenameFilter;
    1517import java.io.IOException;
     18import java.io.InputStream;
     19import java.io.OutputStream;
    1620import java.util.Iterator;
    1721import java.util.Locale;
     
    6468        ctx.statManager().createRateStat("netDb.writeOut", "How many we wrote", "NetworkDatabase", new long[] { 20*60*1000 });
    6569        ctx.statManager().createRateStat("netDb.writeTime", "How long it took", "NetworkDatabase", new long[] { 20*60*1000 });
     70        //ctx.statManager().createRateStat("netDb.readTime", "How long one took", "NetworkDatabase", new long[] { 20*60*1000 });
    6671        _writer = new Writer();
    6772        I2PThread writer = new I2PThread(_writer, "DBWriter");
     
    199204            boolean exists = (null != _keys.put(key, data));
    200205            if (exists)
    201                 _context.statManager().addRateData("netDb.writeClobber", pending, 0);
    202             _context.statManager().addRateData("netDb.writePending", pending, 0);
     206                _context.statManager().addRateData("netDb.writeClobber", pending);
     207            _context.statManager().addRateData("netDb.writePending", pending);
    203208        }
    204209
     
    254259                        if (_log.shouldLog(Log.INFO))
    255260                            _log.info("Wrote " + lastCount + " entries to disk in " + time);
    256                          _context.statManager().addRateData("netDb.writeOut", lastCount, 0);
    257                          _context.statManager().addRateData("netDb.writeTime", time, 0);
     261                         _context.statManager().addRateData("netDb.writeOut", lastCount);
     262                         _context.statManager().addRateData("netDb.writeTime", time);
    258263                    }
    259264                    if (_quit)
     
    280285        if (_log.shouldLog(Log.INFO))
    281286            _log.info("Writing key " + key);
    282         FileOutputStream fos = null;
     287        OutputStream fos = null;
    283288        File dbFile = null;
    284289        try {
     
    297302                // our filesystem is out of date, lets replace it
    298303                fos = new SecureFileOutputStream(dbFile);
     304                fos = new BufferedOutputStream(fos);
    299305                try {
    300306                    data.writeBytes(fos);
     
    369375                            // Also this allows us to wait until it is really done to call checkReseed() and set _initialized
    370376                            //PersistentDataStore.this._context.jobQueue().addJob(new ReadRouterJob(routerInfoFiles[i], key));
     377                            //long start = System.currentTimeMillis();
    371378                            (new ReadRouterJob(routerInfoFiles[i], key)).runJob();
     379                            //_context.statManager().addRateData("netDb.readTime", System.currentTimeMillis() - start);
    372380                        }
    373381                    }
     
    383391   
    384392    private class ReadRouterJob extends JobImpl {
    385         private File _routerFile;
    386         private Hash _key;
     393        private final File _routerFile;
     394        private final Hash _key;
     395
    387396        public ReadRouterJob(File routerFile, Hash key) {
    388397            super(PersistentDataStore.this._context);
     
    411420            if (!shouldRead()) return;
    412421            try {
    413                 FileInputStream fis = null;
     422                InputStream fis = null;
    414423                boolean corrupt = false;
    415424                try {
    416425                    fis = new FileInputStream(_routerFile);
     426                    fis = new BufferedInputStream(fis);
    417427                    RouterInfo ri = new RouterInfo();
    418428                    ri.readBytes(fis);
Note: See TracChangeset for help on using the changeset viewer.