Search in sources :

Example 1 with IpRange

use of com.biglybt.core.ipfilter.IpRange in project BiglyBT by BiglySoftware.

the class IpFilterAutoLoaderImpl method loadOtherFilters.

protected void loadOtherFilters(boolean allowAsyncDownloading, boolean loadOldWhileAsyncDownloading) {
    int p2bVersion = -1;
    try {
        class_mon.enter();
        List new_ipRanges = new ArrayList(1024);
        InputStream fin = null;
        BufferedInputStream bin = null;
        boolean isURL = false;
        try {
            // open the file
            String file = COConfigurationManager.getStringParameter(CFG_AUTOLOAD_FILE);
            Logger.log(new LogEvent(LOGID, "IP Filter file: " + file));
            File filtersFile = new File(file);
            if (filtersFile.exists()) {
                isURL = false;
            } else {
                if (!UrlUtils.isURL(file)) {
                    return;
                }
                isURL = true;
                filtersFile = FileUtil.getUserFile("ipfilter.dl");
                if (filtersFile.exists()) {
                    if (allowAsyncDownloading) {
                        Logger.log(new LogEvent(LOGID, "Downloading " + file + "  async"));
                        downloadFiltersAsync(new URL(file));
                        if (!loadOldWhileAsyncDownloading) {
                            return;
                        }
                    }
                } else {
                    // no old dl, download sync now
                    Logger.log(new LogEvent(LOGID, "sync Downloading " + file));
                    try {
                        ResourceDownloader rd = ResourceDownloaderFactoryImpl.getSingleton().create(new URL(file));
                        fin = rd.download();
                        FileUtil.copyFile(fin, filtersFile);
                        setNextAutoDownload(true);
                    } catch (ResourceDownloaderException e) {
                        return;
                    }
                }
            }
            fin = new FileInputStream(filtersFile);
            bin = new BufferedInputStream(fin, 16384);
            // extract (g)zip'd file and open that
            byte[] headerBytes = new byte[2];
            bin.mark(3);
            bin.read(headerBytes, 0, 2);
            bin.reset();
            if (headerBytes[1] == (byte) 0x8b && headerBytes[0] == 0x1f) {
                GZIPInputStream gzip = new GZIPInputStream(bin);
                filtersFile = FileUtil.getUserFile("ipfilter.ext");
                FileUtil.copyFile(gzip, filtersFile);
                fin = new FileInputStream(filtersFile);
                bin = new BufferedInputStream(fin, 16384);
            } else if (headerBytes[0] == 0x50 && headerBytes[1] == 0x4b) {
                ZipInputStream zip = new ZipInputStream(bin);
                ZipEntry zipEntry = zip.getNextEntry();
                // Skip small files
                while (zipEntry != null && zipEntry.getSize() < 1024 * 1024) {
                    zipEntry = zip.getNextEntry();
                }
                if (zipEntry == null) {
                    return;
                }
                filtersFile = FileUtil.getUserFile("ipfilter.ext");
                FileUtil.copyFile(zip, filtersFile);
                fin = new FileInputStream(filtersFile);
                bin = new BufferedInputStream(fin, 16384);
            }
            bin.mark(8);
            p2bVersion = getP2BFileVersion(bin);
            if (p2bVersion < 1 || p2bVersion > 3) {
                bin.reset();
                loadDATFilters(bin);
                return;
            }
            byte[] descBytes = new byte[255];
            byte[] ipBytes = new byte[4];
            String encoding = p2bVersion == 1 ? "ISO-8859-1" : "UTF-8";
            if (p2bVersion == 1 || p2bVersion == 2) {
                while (true) {
                    String description = readString(bin, descBytes, encoding);
                    int read = bin.read(ipBytes);
                    if (read < 4) {
                        break;
                    }
                    int startIp = ByteFormatter.byteArrayToInt(ipBytes);
                    read = bin.read(ipBytes);
                    if (read < 4) {
                        break;
                    }
                    int endIp = ByteFormatter.byteArrayToInt(ipBytes);
                    IpRangeImpl ipRange = new IpRangeImpl(description, startIp, endIp, true);
                    ipRange.setAddedToRangeList(true);
                    new_ipRanges.add(ipRange);
                }
            } else {
                // version 3
                int read = bin.read(ipBytes);
                if (read < 4) {
                    return;
                }
                int numDescs = ByteFormatter.byteArrayToInt(ipBytes);
                String[] descs = new String[numDescs];
                for (int i = 0; i < numDescs; i++) {
                    descs[i] = readString(bin, descBytes, encoding);
                }
                read = bin.read(ipBytes);
                if (read < 4) {
                    return;
                }
                int numRanges = ByteFormatter.byteArrayToInt(ipBytes);
                for (int i = 0; i < numRanges; i++) {
                    read = bin.read(ipBytes);
                    if (read < 4) {
                        return;
                    }
                    int descIdx = ByteFormatter.byteArrayToInt(ipBytes);
                    read = bin.read(ipBytes);
                    if (read < 4) {
                        return;
                    }
                    int startIp = ByteFormatter.byteArrayToInt(ipBytes);
                    read = bin.read(ipBytes);
                    if (read < 4) {
                        return;
                    }
                    int endIp = ByteFormatter.byteArrayToInt(ipBytes);
                    String description = descIdx < descs.length && descIdx >= 0 ? descs[descIdx] : "";
                    IpRangeImpl ipRange = new IpRangeImpl(description, startIp, endIp, true);
                    ipRange.setAddedToRangeList(true);
                    new_ipRanges.add(ipRange);
                }
            }
        } catch (IOException e) {
            Debug.out(e);
        } finally {
            if (bin != null) {
                try {
                    bin.close();
                } catch (Throwable e) {
                }
            }
            if (fin != null) {
                try {
                    fin.close();
                } catch (Throwable e) {
                }
            }
            Iterator it = new_ipRanges.iterator();
            while (it.hasNext()) {
                ((IpRange) it.next()).checkValid();
            }
            ipFilter.markAsUpToDate();
            if (!isURL) {
                setFileReloadTimer();
            }
        }
    } finally {
        class_mon.exit();
    }
}
Also used : IpRange(com.biglybt.core.ipfilter.IpRange) LogEvent(com.biglybt.core.logging.LogEvent) GZIPInputStream(java.util.zip.GZIPInputStream) ZipInputStream(java.util.zip.ZipInputStream) ResourceDownloaderException(com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderException) ZipEntry(java.util.zip.ZipEntry) ArrayList(java.util.ArrayList) ResourceDownloader(com.biglybt.pif.utils.resourcedownloader.ResourceDownloader) URL(java.net.URL) GZIPInputStream(java.util.zip.GZIPInputStream) ZipInputStream(java.util.zip.ZipInputStream) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with IpRange

use of com.biglybt.core.ipfilter.IpRange in project BiglyBT by BiglySoftware.

the class IPAddressRangeManager method isInRange.

protected Object isInRange(long address_long) {
    try {
        this_mon.enter();
        checkRebuild();
        if (mergedRanges.length == 0) {
            return (null);
        }
        // assisted binary chop
        int bottom = 0;
        int top = mergedRanges.length - 1;
        int current = -1;
        while (top >= 0 && bottom < mergedRanges.length && bottom <= top) {
            current = (bottom + top) / 2;
            IpRange e = mergedRanges[current];
            long this_start = e.getStartIpLong();
            long this_end = e.getMergedEndLong();
            if (address_long == this_start) {
                break;
            } else if (address_long > this_start) {
                if (address_long <= this_end) {
                    break;
                }
                // lies to the right of this entry
                bottom = current + 1;
            } else if (address_long == this_end) {
                break;
            } else {
                if (address_long >= this_start) {
                    break;
                }
                top = current - 1;
            }
        }
        if (top >= 0 && bottom < mergedRanges.length && bottom <= top) {
            IpRange e = mergedRanges[current];
            if (address_long <= e.getEndIpLong()) {
                return (e);
            }
            IpRange[] merged = e.getMergedEntries();
            if (merged == null) {
                Debug.out("IPAddressRangeManager: inconsistent merged details - no entries");
                return (null);
            }
            for (int i = 0; i < merged.length; i++) {
                IpRange me = merged[i];
                if (me.getStartIpLong() <= address_long && me.getEndIpLong() >= address_long) {
                    return (me);
                }
            }
            Debug.out("IPAddressRangeManager: inconsistent merged details - entry not found");
        }
        return (null);
    } finally {
        this_mon.exit();
    }
}
Also used : IpRange(com.biglybt.core.ipfilter.IpRange)

Example 3 with IpRange

use of com.biglybt.core.ipfilter.IpRange in project BiglyBT by BiglySoftware.

the class IPAddressRangeManager method rebuild.

protected void rebuild() {
    if (Logger.isEnabled())
        Logger.log(new LogEvent(LOGID, "IPAddressRangeManager: rebuilding " + entries.size() + " entries starts"));
    IpRange[] ents = new IpRange[entries.size()];
    entries.toArray(ents);
    for (int i = 0; i < ents.length; i++) {
        ents[i].resetMergeInfo();
    }
    // sort based on start address
    Arrays.sort(ents, new Comparator<IpRange>() {

        @Override
        public int compare(IpRange e1, IpRange e2) {
            long diff = e1.getStartIpLong() - e2.getStartIpLong();
            if (diff == 0) {
                diff = e2.getEndIpLong() - e1.getEndIpLong();
            }
            return signum(diff);
        }
    });
    // now merge overlapping ranges
    List me = new ArrayList(ents.length);
    for (int i = 0; i < ents.length; i++) {
        IpRange entry = ents[i];
        if (entry.getMerged()) {
            continue;
        }
        me.add(entry);
        int pos = i + 1;
        while (pos < ents.length) {
            long end_pos = entry.getMergedEndLong();
            IpRange e2 = ents[pos++];
            if (!e2.getMerged()) {
                if (end_pos >= e2.getStartIpLong()) {
                    e2.setMerged();
                    if (e2.getEndIpLong() > end_pos) {
                        entry.setMergedEnd(e2.getEndIpLong());
                        entry.addMergedEntry(e2);
                    }
                } else {
                    break;
                }
            }
        }
    }
    /*
		for (int i=0;i<ents.length;i++){

			entry	e = ents[i];

			System.out.println( Long.toHexString(e.getStart()) + " - " + Long.toHexString(e.getEnd()) + ": " + e.getMerged() + "/" + Long.toHexString(e.getMergedEnd()));
		}
		*/
    mergedRanges = new IpRange[me.size()];
    me.toArray(mergedRanges);
    total_span = 0;
    for (int i = 0; i < mergedRanges.length; i++) {
        IpRange e = mergedRanges[i];
        // span is inclusive
        long span = (e.getMergedEndLong() - e.getStartIpLong()) + 1;
        total_span += span;
    }
    if (Logger.isEnabled())
        Logger.log(new LogEvent(LOGID, "IPAddressRangeManager: rebuilding " + entries.size() + " entries ends"));
}
Also used : IpRange(com.biglybt.core.ipfilter.IpRange) LogEvent(com.biglybt.core.logging.LogEvent)

Example 4 with IpRange

use of com.biglybt.core.ipfilter.IpRange in project BiglyBT by BiglySoftware.

the class IpFilterAutoLoaderImpl method loadDATFilters.

/**
 * Load dat filter as specified at http://wiki.phoenixlabs.org/wiki/DAT_Format
 * @param fin
 * @throws Exception
 *
 * @since 3.0.1.5
 */
private void loadDATFilters(InputStream fin) {
    try {
        class_mon.enter();
        List new_ipRanges = new ArrayList(1024);
        InputStreamReader streamReader = null;
        BufferedReader reader = null;
        try {
            Pattern pattern = Pattern.compile("^(.*):([0-9\\.]+)[^0-9]+([0-9\\.]+).*");
            int parseMode = -1;
            // open the file
            // TODO: test charset fallback (should fallback to ascii)
            streamReader = new InputStreamReader(fin, "utf8");
            reader = new BufferedReader(streamReader);
            int numConsecutiveUnknowns = 0;
            while (numConsecutiveUnknowns < 1000) {
                String line = reader.readLine();
                // System.out.println("line=" + line);
                if (line == null) {
                    break;
                }
                line = line.trim();
                if (line.startsWith("#") || line.length() == 0) {
                    continue;
                }
                String description = "";
                String startIp = null;
                String endIp = null;
                int level = 0;
                if (parseMode <= 0 || parseMode == 1) {
                    Matcher matcher = pattern.matcher(line);
                    if (matcher.find()) {
                        if (parseMode != 1) {
                            parseMode = 1;
                        }
                        description = matcher.group(1);
                        startIp = matcher.group(2);
                        endIp = matcher.group(3);
                    } else {
                        Logger.log(new LogEvent(LOGID, LogEvent.LT_WARNING, "unrecognized line while reading ip filter: " + line));
                    }
                }
                if (parseMode != 1) {
                    if (parseMode != 2) {
                        parseMode = 2;
                    }
                    // spec says:
                    // 1.1.1.1, 1.1.1.2, 100, moo
                    // but I've seen dash format, such as
                    // 1.1.1.1 - 1.1.1.2, 100, moo
                    // so 	 for both
                    String[] sections = line.split(" *[-,] *", 4);
                    if (sections.length >= 2) {
                        if (sections[0].indexOf('.') < 0 || sections[1].indexOf('.') < 0 || sections[0].length() > 15 || sections[1].length() > 15 || sections[0].length() < 7 || sections[1].length() < 7) {
                            numConsecutiveUnknowns++;
                            continue;
                        }
                    }
                    if (sections.length >= 4) {
                        // simple format:
                        // startip, endip, level, desc
                        startIp = sections[0];
                        endIp = sections[1];
                        description = sections[3];
                        try {
                            level = Integer.parseInt(sections[2]);
                        } catch (NumberFormatException e) {
                            description = sections[2] + " " + description;
                        }
                        for (int i = 4; i < sections.length; i++) {
                            description += " " + sections[i];
                        }
                        numConsecutiveUnknowns = 0;
                    } else if (sections.length == 3) {
                        startIp = sections[0];
                        endIp = sections[1];
                        description = sections[2];
                        numConsecutiveUnknowns = 0;
                    } else if (sections.length == 2) {
                        startIp = sections[0];
                        endIp = sections[1];
                        numConsecutiveUnknowns = 0;
                    } else {
                        numConsecutiveUnknowns++;
                        continue;
                    }
                    if (level >= 128) {
                        continue;
                    }
                }
                if (startIp == null || endIp == null) {
                    continue;
                }
                IpRangeImpl ipRange = new IpRangeImpl(description, startIp, endIp, true);
                // System.out.println(parseMode + ":" + description + ";" + ipRange.getStartIp());
                ipRange.setAddedToRangeList(true);
                new_ipRanges.add(ipRange);
            }
        } catch (IOException e) {
            Debug.out(e);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (Throwable e) {
                }
            }
            if (streamReader != null) {
                try {
                    streamReader.close();
                } catch (Throwable e) {
                }
            }
            Iterator it = new_ipRanges.iterator();
            while (it.hasNext()) {
                ((IpRange) it.next()).checkValid();
            }
            ipFilter.markAsUpToDate();
        }
    } finally {
        class_mon.exit();
    }
}
Also used : IpRange(com.biglybt.core.ipfilter.IpRange) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) LogEvent(com.biglybt.core.logging.LogEvent) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

IpRange (com.biglybt.core.ipfilter.IpRange)4 LogEvent (com.biglybt.core.logging.LogEvent)3 ArrayList (java.util.ArrayList)2 Iterator (java.util.Iterator)2 List (java.util.List)2 ResourceDownloader (com.biglybt.pif.utils.resourcedownloader.ResourceDownloader)1 ResourceDownloaderException (com.biglybt.pif.utils.resourcedownloader.ResourceDownloaderException)1 URL (java.net.URL)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 ZipEntry (java.util.zip.ZipEntry)1 ZipInputStream (java.util.zip.ZipInputStream)1