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.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
@ -292,7 +293,6 @@ public class TrackerClient implements Runnable {
// followed by the secondary open trackers
// It's painful, but try to make sure if an open tracker is also
// the primary tracker, that we don't add it twice.
// todo: check for b32 matches as well
String primary = null;
if (meta != null)
primary = meta.getAnnounce();
@ -317,37 +317,31 @@ public class TrackerClient implements Runnable {
// announce list
// We completely ignore the BEP 12 processing rules
if (meta != null && !meta.isPrivate()) {
List<String> urls = new ArrayList<String>(16);
List<List<String>> list = meta.getAnnounceList();
if (list != null) {
for (List<String> llist : list) {
for (String url : llist) {
if (!isNewValidTracker(trackerHashes, url))
continue;
trackers.add(new TCTracker(url, trackers.isEmpty()));
if (_log.shouldLog(Log.DEBUG))
_log.debug("Additional announce (list): [" + url + "] for infoHash: " + infoHash);
urls.add(url);
}
}
if (trackers.size() > 2) {
// shuffle everything but the primary
TCTracker pri = trackers.remove(0);
Collections.shuffle(trackers, _util.getContext().random());
trackers.add(0, pri);
}
// configured open trackers
urls.addAll(_util.getOpenTrackers());
if (urls.size() > 1) {
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());
}
}
}
// 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);
for (String url : urls) {
if (!isNewValidTracker(trackerHashes, url))
continue;
// opentrackers are primary if we don't have primary
trackers.add(new TCTracker(url, trackers.isEmpty()));
if (_log.shouldLog(Log.DEBUG))
_log.debug("Additional announce: [" + url + "] for infoHash: " + infoHash);
// first one is primary if we don't have a primary
trackers.add(new TCTracker(url, trackers.isEmpty()));
if (_log.shouldLog(Log.DEBUG))
_log.debug("Additional announce: [" + url + "] for infoHash: " + infoHash);
}
}
@ -1088,6 +1082,23 @@ public class TrackerClient implements Runnable {
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
{
final String announce;