Prefer udp over http announces if we have both

This commit is contained in:
zzz
2025-05-02 09:26:44 -04:00
parent ed244e9135
commit 696ebee8cc

View File

@ -28,6 +28,7 @@ import java.net.URISyntaxException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator;
import java.util.Date; import java.util.Date;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
@ -292,7 +293,6 @@ public class TrackerClient implements Runnable {
// followed by the secondary open trackers // followed by the secondary open trackers
// It's painful, but try to make sure if an open tracker is also // It's painful, but try to make sure if an open tracker is also
// the primary tracker, that we don't add it twice. // the primary tracker, that we don't add it twice.
// todo: check for b32 matches as well
String primary = null; String primary = null;
if (meta != null) if (meta != null)
primary = meta.getAnnounce(); primary = meta.getAnnounce();
@ -317,37 +317,31 @@ public class TrackerClient implements Runnable {
// announce list // announce list
// We completely ignore the BEP 12 processing rules // We completely ignore the BEP 12 processing rules
if (meta != null && !meta.isPrivate()) { if (meta != null && !meta.isPrivate()) {
List<String> urls = new ArrayList<String>(16);
List<List<String>> list = meta.getAnnounceList(); List<List<String>> list = meta.getAnnounceList();
if (list != null) { if (list != null) {
for (List<String> llist : list) { for (List<String> llist : list) {
for (String url : llist) { for (String url : llist) {
if (!isNewValidTracker(trackerHashes, url)) urls.add(url);
continue;
trackers.add(new TCTracker(url, trackers.isEmpty()));
if (_log.shouldLog(Log.DEBUG))
_log.debug("Additional announce (list): [" + url + "] for infoHash: " + infoHash);
} }
} }
if (trackers.size() > 2) { }
// shuffle everything but the primary // configured open trackers
TCTracker pri = trackers.remove(0); urls.addAll(_util.getOpenTrackers());
Collections.shuffle(trackers, _util.getContext().random()); if (urls.size() > 1) {
trackers.add(0, pri); Collections.shuffle(trackers, _util.getContext().random());
if (_util.udpEnabled()) {
// sort the list to put udp first so it will trump http
Collections.sort(urls, new URLComparator());
} }
} }
} for (String url : urls) {
// configured open trackers
if (meta == null || !meta.isPrivate()) {
List<String> tlist = _util.getOpenTrackers();
for (int i = 0; i < tlist.size(); i++) {
String url = tlist.get(i);
if (!isNewValidTracker(trackerHashes, url)) if (!isNewValidTracker(trackerHashes, url))
continue; continue;
// opentrackers are primary if we don't have primary // first one is primary if we don't have a primary
trackers.add(new TCTracker(url, trackers.isEmpty())); trackers.add(new TCTracker(url, trackers.isEmpty()));
if (_log.shouldLog(Log.DEBUG)) if (_log.shouldLog(Log.DEBUG))
_log.debug("Additional announce: [" + url + "] for infoHash: " + infoHash); _log.debug("Additional announce: [" + url + "] for infoHash: " + infoHash);
} }
} }
@ -1088,6 +1082,23 @@ public class TrackerClient implements Runnable {
return null; return null;
} }
/**
* UDP before HTTP
*
* @since 0.9.67
*/
private static class URLComparator implements Comparator<String> {
public int compare(String l, String r) {
boolean ul = l.startsWith("udp://");
boolean ur = r.startsWith("udp://");
if (ul && !ur)
return -1;
if (ur && !ul)
return -1;
return 0;
}
}
private static class TCTracker private static class TCTracker
{ {
final String announce; final String announce;