Search in sources :

Example 11 with TOTorrentAnnounceURLSet

use of com.biglybt.core.torrent.TOTorrentAnnounceURLSet in project BiglyBT by BiglySoftware.

the class RelatedContentManager method getKeys.

private byte[][] getKeys(Download download) {
    byte[] tracker_keys = null;
    byte[] ws_keys = null;
    try {
        Torrent torrent = download.getTorrent();
        if (torrent != null) {
            TOTorrent to_torrent = PluginCoreUtils.unwrap(torrent);
            Set<String> tracker_domains = new HashSet<>();
            addURLToDomainKeySet(tracker_domains, to_torrent.getAnnounceURL());
            TOTorrentAnnounceURLGroup group = to_torrent.getAnnounceURLGroup();
            TOTorrentAnnounceURLSet[] sets = group.getAnnounceURLSets();
            for (TOTorrentAnnounceURLSet set : sets) {
                URL[] urls = set.getAnnounceURLs();
                for (URL u : urls) {
                    addURLToDomainKeySet(tracker_domains, u);
                }
            }
            tracker_keys = domainsToArray(tracker_domains, 8);
            Set<String> ws_domains = new HashSet<>();
            List getright = BDecoder.decodeStrings(getURLList(to_torrent, "url-list"));
            List webseeds = BDecoder.decodeStrings(getURLList(to_torrent, "httpseeds"));
            for (List l : new List[] { getright, webseeds }) {
                for (Object o : l) {
                    if (o instanceof String) {
                        try {
                            addURLToDomainKeySet(ws_domains, new URL((String) o));
                        } catch (Throwable e) {
                        }
                    }
                }
            }
            ws_keys = domainsToArray(ws_domains, 3);
        }
    } catch (Throwable e) {
    }
    return (new byte[][] { tracker_keys, ws_keys });
}
Also used : TOTorrent(com.biglybt.core.torrent.TOTorrent) Torrent(com.biglybt.pif.torrent.Torrent) TOTorrentAnnounceURLGroup(com.biglybt.core.torrent.TOTorrentAnnounceURLGroup) URL(java.net.URL) TOTorrent(com.biglybt.core.torrent.TOTorrent) TOTorrentAnnounceURLSet(com.biglybt.core.torrent.TOTorrentAnnounceURLSet)

Example 12 with TOTorrentAnnounceURLSet

use of com.biglybt.core.torrent.TOTorrentAnnounceURLSet in project BiglyBT by BiglySoftware.

the class TOTorrentDeserialiseImpl method construct.

protected void construct(Map meta_data) throws TOTorrentException {
    try {
        String announce_url = null;
        boolean got_announce = false;
        boolean got_announce_list = false;
        boolean bad_announce = false;
        // decode the stuff
        Iterator root_it = meta_data.keySet().iterator();
        while (root_it.hasNext()) {
            String key = (String) root_it.next();
            if (key.equalsIgnoreCase(TK_ANNOUNCE)) {
                got_announce = true;
                announce_url = readStringFromMetaData(meta_data, TK_ANNOUNCE);
                if (announce_url == null || announce_url.trim().length() == 0) {
                    bad_announce = true;
                } else {
                    announce_url = announce_url.replaceAll(" ", "");
                    try {
                        setAnnounceURL(new URL(announce_url));
                    } catch (MalformedURLException e) {
                        if (!announce_url.contains("://")) {
                            announce_url = "http:/" + (announce_url.startsWith("/") ? "" : "/") + announce_url;
                        } else if (announce_url.startsWith("utp:")) {
                            // common typo for udp
                            announce_url = "udp" + announce_url.substring(3);
                        }
                        try {
                            setAnnounceURL(new URL(announce_url));
                        } catch (MalformedURLException f) {
                            Debug.out("Invalid announce url: " + announce_url);
                            bad_announce = true;
                        }
                    }
                }
            } else if (key.equalsIgnoreCase(TK_ANNOUNCE_LIST)) {
                got_announce_list = true;
                List announce_list = null;
                Object ann_list = meta_data.get(TK_ANNOUNCE_LIST);
                if (ann_list instanceof List) {
                    // some malformed torrents have this key as a zero-sized string instead of a zero-sized list
                    announce_list = (List) ann_list;
                }
                if (announce_list != null && announce_list.size() > 0) {
                    announce_url = readStringFromMetaData(meta_data, TK_ANNOUNCE);
                    if (announce_url != null) {
                        announce_url = announce_url.replaceAll(" ", "");
                    }
                    boolean announce_url_found = false;
                    for (int i = 0; i < announce_list.size(); i++) {
                        Object temp = announce_list.get(i);
                        if (temp instanceof byte[]) {
                            List l = new ArrayList();
                            l.add(temp);
                            temp = l;
                        }
                        if (temp instanceof List) {
                            Vector urls = new Vector();
                            List set = (List) temp;
                            while (set.size() > 0) {
                                // seen a case where this is a list with the announce url first and then some other junk
                                Object temp2 = set.remove(0);
                                try {
                                    if (temp2 instanceof List) {
                                        List junk = (List) temp2;
                                        if (junk.size() > 0) {
                                            set.add(junk.get(0));
                                        }
                                        continue;
                                    }
                                    String url_str = readStringFromMetaData((byte[]) temp2);
                                    url_str = url_str.replaceAll(" ", "");
                                    try {
                                        urls.add(new URL(StringInterner.intern(url_str)));
                                        if (url_str.equalsIgnoreCase(announce_url)) {
                                            announce_url_found = true;
                                        }
                                    } catch (MalformedURLException e) {
                                        if (!url_str.contains("://")) {
                                            url_str = "http:/" + (url_str.startsWith("/") ? "" : "/") + url_str;
                                        } else if (url_str.startsWith("utp:")) {
                                            // common typo
                                            url_str = "udp" + url_str.substring(3);
                                        }
                                        try {
                                            urls.add(new URL(StringInterner.intern(url_str)));
                                            if (url_str.equalsIgnoreCase(announce_url)) {
                                                announce_url_found = true;
                                            }
                                        } catch (MalformedURLException f) {
                                            Debug.out("Invalid url: " + url_str, f);
                                        }
                                    }
                                } catch (Throwable e) {
                                    Debug.out("Torrent has invalid url-list entry (" + temp2 + ") - ignoring: meta=" + meta_data, e);
                                }
                            }
                            if (urls.size() > 0) {
                                URL[] url_array = new URL[urls.size()];
                                urls.copyInto(url_array);
                                addTorrentAnnounceURLSet(url_array);
                            }
                        } else {
                            Debug.out("Torrent has invalid url-list entry (" + temp + ") - ignoring: meta=" + meta_data);
                        }
                    }
                    if (!announce_url_found && announce_url != null && announce_url.length() > 0) {
                        try {
                            Vector urls = new Vector();
                            urls.add(new URL(StringInterner.intern(announce_url)));
                            URL[] url_array = new URL[urls.size()];
                            urls.copyInto(url_array);
                            addTorrentAnnounceURLSet(url_array);
                        } catch (Exception e) {
                            Debug.out("Invalid URL '" + announce_url + "' - meta=" + meta_data, e);
                        }
                    }
                }
            } else if (key.equalsIgnoreCase(TK_COMMENT)) {
                setComment((byte[]) meta_data.get(TK_COMMENT));
            } else if (key.equalsIgnoreCase(TK_CREATED_BY)) {
                setCreatedBy((byte[]) meta_data.get(TK_CREATED_BY));
            } else if (key.equalsIgnoreCase(TK_CREATION_DATE)) {
                try {
                    Long creation_date = (Long) meta_data.get(TK_CREATION_DATE);
                    if (creation_date != null) {
                        setCreationDate(creation_date.longValue());
                    }
                } catch (Exception e) {
                    System.out.println("creation_date extraction fails, ignoring");
                }
            } else if (key.equalsIgnoreCase(TK_INFO)) {
            // processed later
            } else {
                Object prop = meta_data.get(key);
                if (prop instanceof byte[]) {
                    setAdditionalByteArrayProperty(key, (byte[]) prop);
                } else if (prop instanceof Long) {
                    setAdditionalLongProperty(key, (Long) prop);
                } else if (prop instanceof List) {
                    setAdditionalListProperty(key, (List) prop);
                } else {
                    setAdditionalMapProperty(key, (Map) prop);
                }
            }
        }
        if (bad_announce) {
            if (got_announce_list) {
                TOTorrentAnnounceURLSet[] sets = getAnnounceURLGroup().getAnnounceURLSets();
                if (sets.length > 0) {
                    setAnnounceURL(sets[0].getAnnounceURLs()[0]);
                } else {
                    throw (new TOTorrentException("ANNOUNCE_URL malformed ('" + announce_url + "' and no usable announce list)", TOTorrentException.RT_DECODE_FAILS));
                }
            } else {
                throw (new TOTorrentException("ANNOUNCE_URL malformed ('" + announce_url + "'", TOTorrentException.RT_DECODE_FAILS));
            }
        }
        if (!(got_announce_list || got_announce)) {
            setAnnounceURL(TorrentUtils.getDecentralisedEmptyURL());
        }
        if (getAnnounceURL() == null) {
            boolean done = false;
            if (got_announce_list) {
                TOTorrentAnnounceURLSet[] sets = getAnnounceURLGroup().getAnnounceURLSets();
                if (sets.length > 0) {
                    setAnnounceURL(sets[0].getAnnounceURLs()[0]);
                    done = true;
                }
            }
            if (!done) {
                setAnnounceURL(TorrentUtils.getDecentralisedEmptyURL());
            }
        }
        Map info = (Map) meta_data.get(TK_INFO);
        if (info == null) {
            throw (new TOTorrentException("Decode fails, 'info' element not found'", TOTorrentException.RT_DECODE_FAILS));
        }
        boolean hasUTF8Keys = info.containsKey(TK_NAME_UTF8);
        setName((byte[]) info.get(TK_NAME));
        long piece_length = ((Long) info.get(TK_PIECE_LENGTH)).longValue();
        if (piece_length <= 0) {
            throw (new TOTorrentException("Decode fails, piece-length is invalid", TOTorrentException.RT_DECODE_FAILS));
        }
        setPieceLength(piece_length);
        setHashFromInfo(info);
        Long simple_file_length = (Long) info.get(TK_LENGTH);
        long total_length = 0;
        String encoding = getAdditionalStringProperty("encoding");
        hasUTF8Keys &= encoding == null || encoding.equals(ENCODING_ACTUALLY_UTF8_KEYS);
        if (simple_file_length != null) {
            setSimpleTorrent(true);
            total_length = simple_file_length.longValue();
            if (hasUTF8Keys) {
                setNameUTF8((byte[]) info.get(TK_NAME_UTF8));
                setAdditionalStringProperty("encoding", ENCODING_ACTUALLY_UTF8_KEYS);
            }
            setFiles(new TOTorrentFileImpl[] { new TOTorrentFileImpl(this, 0, 0, total_length, new byte[][] { getName() }) });
        } else {
            setSimpleTorrent(false);
            List meta_files = (List) info.get(TK_FILES);
            TOTorrentFileImpl[] files = new TOTorrentFileImpl[meta_files.size()];
            if (hasUTF8Keys) {
                for (int i = 0; i < files.length; i++) {
                    Map file_map = (Map) meta_files.get(i);
                    hasUTF8Keys &= file_map.containsKey(TK_PATH_UTF8);
                    if (!hasUTF8Keys) {
                        break;
                    }
                }
                if (hasUTF8Keys) {
                    setNameUTF8((byte[]) info.get(TK_NAME_UTF8));
                    setAdditionalStringProperty("encoding", ENCODING_ACTUALLY_UTF8_KEYS);
                }
            }
            for (int i = 0; i < files.length; i++) {
                Map file_map = (Map) meta_files.get(i);
                long len = ((Long) file_map.get(TK_LENGTH)).longValue();
                List paths = (List) file_map.get(TK_PATH);
                List paths8 = (List) file_map.get(TK_PATH_UTF8);
                byte[][] path_comps = null;
                if (paths != null) {
                    path_comps = new byte[paths.size()][];
                    for (int j = 0; j < paths.size(); j++) {
                        path_comps[j] = (byte[]) paths.get(j);
                    }
                }
                TOTorrentFileImpl file;
                if (hasUTF8Keys) {
                    byte[][] path_comps8 = new byte[paths8.size()][];
                    for (int j = 0; j < paths8.size(); j++) {
                        path_comps8[j] = (byte[]) paths8.get(j);
                    }
                    file = files[i] = new TOTorrentFileImpl(this, i, total_length, len, path_comps, path_comps8);
                } else {
                    file = files[i] = new TOTorrentFileImpl(this, i, total_length, len, path_comps);
                }
                total_length += len;
                // preserve any non-standard attributes
                Iterator file_it = file_map.keySet().iterator();
                while (file_it.hasNext()) {
                    String key = (String) file_it.next();
                    if (key.equals(TK_LENGTH) || key.equals(TK_PATH)) {
                    // standard
                    // we don't skip TK_PATH_UTF8 because some code might assume getAdditionalProperty can get it
                    } else {
                        file.setAdditionalProperty(key, file_map.get(key));
                    }
                }
            }
            setFiles(files);
        }
        byte[] flat_pieces = (byte[]) info.get(TK_PIECES);
        // work out how many pieces we require for the torrent
        int pieces_required = (int) ((total_length + (piece_length - 1)) / piece_length);
        int pieces_supplied = flat_pieces.length / 20;
        if (pieces_supplied < pieces_required) {
            throw (new TOTorrentException("Decode fails, insufficient pieces supplied", TOTorrentException.RT_DECODE_FAILS));
        }
        if (pieces_supplied > pieces_required) {
            Debug.out("Torrent '" + new String(getName()) + "' has too many pieces (required=" + pieces_required + ",supplied=" + pieces_supplied + ") - ignoring excess");
        }
        byte[][] pieces = new byte[pieces_supplied][20];
        for (int i = 0; i < pieces.length; i++) {
            System.arraycopy(flat_pieces, i * 20, pieces[i], 0, 20);
        }
        setPieces(pieces);
        // extract and additional info elements
        Iterator info_it = info.keySet().iterator();
        while (info_it.hasNext()) {
            String key = (String) info_it.next();
            if (key.equals(TK_NAME) || key.equals(TK_LENGTH) || key.equals(TK_FILES) || key.equals(TK_PIECE_LENGTH) || key.equals(TK_PIECES)) {
            // standard attributes
            } else {
                addAdditionalInfoProperty(key, info.get(key));
            }
        }
        try {
            byte[] ho = (byte[]) info.get(TK_HASH_OVERRIDE);
            if (ho != null) {
                setHashOverride(ho);
            } else {
                if (info instanceof LightHashMapEx) {
                    LightHashMapEx info_ex = (LightHashMapEx) info;
                    if (info_ex.getFlag(LightHashMapEx.FL_MAP_ORDER_INCORRECT)) {
                        String name = getUTF8Name();
                        if (name == null) {
                            name = new String(getName());
                        }
                        String message = MessageText.getString("torrent.decode.info.order.bad", new String[] { name });
                        LogAlert alert = new LogAlert(this, LogAlert.UNREPEATABLE, LogAlert.AT_WARNING, message);
                        alert.forceNotify = true;
                        Logger.log(alert);
                    }
                }
            }
        } catch (Throwable e) {
            Debug.printStackTrace(e);
        }
    } catch (Throwable e) {
        if (e instanceof TOTorrentException) {
            throw ((TOTorrentException) e);
        }
        throw (new TOTorrentException("Torrent decode fails '" + Debug.getNestedExceptionMessageAndStack(e) + "'", TOTorrentException.RT_DECODE_FAILS, e));
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) URL(java.net.URL) TOTorrentException(com.biglybt.core.torrent.TOTorrentException) MalformedURLException(java.net.MalformedURLException) LogAlert(com.biglybt.core.logging.LogAlert) TOTorrentException(com.biglybt.core.torrent.TOTorrentException) TOTorrentAnnounceURLSet(com.biglybt.core.torrent.TOTorrentAnnounceURLSet)

Example 13 with TOTorrentAnnounceURLSet

use of com.biglybt.core.torrent.TOTorrentAnnounceURLSet in project BiglyBT by BiglySoftware.

the class TOTorrentXMLSerialiser method writeRoot.

protected void writeRoot() throws TOTorrentException {
    writeLineRaw("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
    writeLineRaw("<tor:TORRENT");
    writeLineRaw("\txmlns:tor=\"http://azureus.sourceforge.net/files\"");
    writeLineRaw("\txmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"");
    writeLineRaw("\txsi:schemaLocation=\"http://azureus.sourceforge.net/files http://azureus.sourceforge.net/files/torrent.xsd\">");
    try {
        indent();
        writeTag("ANNOUNCE_URL", torrent.getAnnounceURL().toString());
        TOTorrentAnnounceURLSet[] sets = torrent.getAnnounceURLGroup().getAnnounceURLSets();
        if (sets.length > 0) {
            writeLineRaw("<ANNOUNCE_LIST>");
            try {
                indent();
                for (int i = 0; i < sets.length; i++) {
                    TOTorrentAnnounceURLSet set = sets[i];
                    URL[] urls = set.getAnnounceURLs();
                    writeLineRaw("<ANNOUNCE_ENTRY>");
                    try {
                        indent();
                        for (int j = 0; j < urls.length; j++) {
                            writeTag("ANNOUNCE_URL", urls[j].toString());
                        }
                    } finally {
                        exdent();
                    }
                    writeLineRaw("</ANNOUNCE_ENTRY>");
                }
            } finally {
                exdent();
            }
            writeLineRaw("</ANNOUNCE_LIST>");
        }
        byte[] comment = torrent.getComment();
        if (comment != null) {
            writeLocalisableTag("COMMENT", comment);
        }
        long creation_date = torrent.getCreationDate();
        if (creation_date != 0) {
            writeTag("CREATION_DATE", creation_date);
        }
        byte[] created_by = torrent.getCreatedBy();
        if (created_by != null) {
            writeLocalisableTag("CREATED_BY", created_by);
        }
        writeTag("TORRENT_HASH", torrent.getHash());
        byte[] hash_override = torrent.getHashOverride();
        if (hash_override != null) {
            writeTag("TORRENT_HASH_OVERRIDE", hash_override);
        }
        writeInfo();
        Map additional_properties = torrent.getAdditionalProperties();
        Iterator it = additional_properties.keySet().iterator();
        while (it.hasNext()) {
            String key = (String) it.next();
            writeGenericMapEntry(key, additional_properties.get(key));
        }
    } finally {
        exdent();
    }
    writeLineRaw("</tor:TORRENT>");
}
Also used : Iterator(java.util.Iterator) TOTorrentAnnounceURLSet(com.biglybt.core.torrent.TOTorrentAnnounceURLSet) Map(java.util.Map) URL(java.net.URL)

Example 14 with TOTorrentAnnounceURLSet

use of com.biglybt.core.torrent.TOTorrentAnnounceURLSet in project BiglyBT by BiglySoftware.

the class TorrentAnnounceURLListImpl method setAlreadyExists.

protected boolean setAlreadyExists(URL[] urls) {
    TOTorrentAnnounceURLGroup group = torrent.getTorrent().getAnnounceURLGroup();
    TOTorrentAnnounceURLSet[] sets = group.getAnnounceURLSets();
    for (int i = 0; i < sets.length; i++) {
        URL[] u = sets[i].getAnnounceURLs();
        if (u.length != urls.length) {
            continue;
        }
        boolean all_found = true;
        for (int j = 0; j < urls.length; j++) {
            URL u1 = urls[j];
            boolean this_found = false;
            for (int k = 0; k < u.length; k++) {
                URL u2 = u[k];
                if (u1.toString().equals(u2.toString())) {
                    this_found = true;
                    break;
                }
            }
            if (!this_found) {
                all_found = false;
                break;
            }
        }
        if (all_found) {
            return (true);
        }
    }
    return (false);
}
Also used : TOTorrentAnnounceURLSet(com.biglybt.core.torrent.TOTorrentAnnounceURLSet) TOTorrentAnnounceURLGroup(com.biglybt.core.torrent.TOTorrentAnnounceURLGroup) URL(java.net.URL)

Example 15 with TOTorrentAnnounceURLSet

use of com.biglybt.core.torrent.TOTorrentAnnounceURLSet in project BiglyBT by BiglySoftware.

the class TorrentAnnounceURLListImpl method getSets.

@Override
public TorrentAnnounceURLListSet[] getSets() {
    TOTorrentAnnounceURLGroup group = torrent.getTorrent().getAnnounceURLGroup();
    TOTorrentAnnounceURLSet[] sets = group.getAnnounceURLSets();
    TorrentAnnounceURLListSet[] res = new TorrentAnnounceURLListSet[sets.length];
    for (int i = 0; i < res.length; i++) {
        res[i] = new TorrentAnnounceURLListSetImpl(this, sets[i]);
    }
    return (res);
}
Also used : TorrentAnnounceURLListSet(com.biglybt.pif.torrent.TorrentAnnounceURLListSet) TOTorrentAnnounceURLSet(com.biglybt.core.torrent.TOTorrentAnnounceURLSet) TOTorrentAnnounceURLGroup(com.biglybt.core.torrent.TOTorrentAnnounceURLGroup)

Aggregations

TOTorrentAnnounceURLSet (com.biglybt.core.torrent.TOTorrentAnnounceURLSet)16 URL (java.net.URL)14 TOTorrent (com.biglybt.core.torrent.TOTorrent)7 TOTorrentAnnounceURLGroup (com.biglybt.core.torrent.TOTorrentAnnounceURLGroup)7 File (java.io.File)3 TOTorrentException (com.biglybt.core.torrent.TOTorrentException)2 TRTrackerBTAnnouncerImpl (com.biglybt.core.tracker.client.impl.bt.TRTrackerBTAnnouncerImpl)2 DiskManagerFileInfo (com.biglybt.core.disk.DiskManagerFileInfo)1 DiskManagerFileInfoSet (com.biglybt.core.disk.DiskManagerFileInfoSet)1 LocaleUtilDecoder (com.biglybt.core.internat.LocaleUtilDecoder)1 LogAlert (com.biglybt.core.logging.LogAlert)1 LogEvent (com.biglybt.core.logging.LogEvent)1 TOTorrentCreator (com.biglybt.core.torrent.TOTorrentCreator)1 TOTorrentFile (com.biglybt.core.torrent.TOTorrentFile)1 TOTorrentProgressListener (com.biglybt.core.torrent.TOTorrentProgressListener)1 TRTrackerAnnouncer (com.biglybt.core.tracker.client.TRTrackerAnnouncer)1 TRTrackerScraperResponse (com.biglybt.core.tracker.client.TRTrackerScraperResponse)1 TRTrackerDHTAnnouncerImpl (com.biglybt.core.tracker.client.impl.dht.TRTrackerDHTAnnouncerImpl)1 DownloadTypeComplete (com.biglybt.pif.download.DownloadTypeComplete)1 DownloadTypeIncomplete (com.biglybt.pif.download.DownloadTypeIncomplete)1