use of com.biglybt.core.tracker.AllTrackersManager.AllTrackersTracker in project BiglyBT by BiglySoftware.
the class AllTrackersManagerImpl method unregister.
private void unregister(String name) {
AllTrackersTrackerImpl existing_tracker = host_map.remove(name);
if (existing_tracker != null) {
for (AllTrackersListener listener : listeners) {
List<AllTrackersTracker> trackers = new ArrayList<>();
trackers.add(existing_tracker);
try {
listener.trackerEventOccurred(new AllTrackersEventImpl(AllTrackersEvent.ET_TRACKER_REMOVED, trackers));
} catch (Throwable e) {
Debug.out(e);
}
}
}
}
use of com.biglybt.core.tracker.AllTrackersManager.AllTrackersTracker in project BiglyBT by BiglySoftware.
the class AllTrackersManagerImpl method register.
private AllTrackersTrackerImpl register(TOTorrent torrent_maybe_null, String name) {
AllTrackersTrackerImpl existing_tracker = host_map.get(name);
if (existing_tracker == null) {
AllTrackersTrackerImpl new_tracker = new AllTrackersTrackerImpl(name);
existing_tracker = host_map.putIfAbsent(name, new_tracker);
if (existing_tracker == null) {
for (AllTrackersListener listener : listeners) {
List<AllTrackersTracker> trackers = new ArrayList<>();
trackers.add(new_tracker);
try {
listener.trackerEventOccurred(new AllTrackersEventImpl(AllTrackersEvent.ET_TRACKER_ADDED, trackers));
} catch (Throwable e) {
Debug.out(e);
}
}
new_tracker.setRegistered(torrent_maybe_null);
return (new_tracker);
}
}
existing_tracker.setRegistered(torrent_maybe_null);
return (existing_tracker);
}
use of com.biglybt.core.tracker.AllTrackersManager.AllTrackersTracker in project BiglyBT by BiglySoftware.
the class TRTrackerBTAnnouncerImpl method constructRequest.
private TRTrackerAnnouncerRequestImpl constructRequest(String evt, URL _url) throws Exception {
String url = _url.toString();
StringBuffer request = new StringBuffer(url);
// if url already has a query component then just append our parameters on the end
if (url.indexOf('?') != -1) {
request.append('&');
} else {
request.append('?');
}
// the client-id stuff RELIES on info_hash being the FIRST parameter added by
// us to the URL, so don't change it!
request.append(info_hash);
request.append(tracker_peer_id_str);
int tcp_port = announce_data_provider.getTCPListeningPortNumber();
AllTrackersTracker att = all_trackers.getTracker(_url);
boolean disable_crypto_port = false;
if (att != null) {
Map<String, Object> options = att.getOptions();
if (options != null) {
Number cp = (Number) options.get(AllTrackersTracker.OPT_CRYPTO_PORT);
if (cp != null && cp.intValue() == 2) {
disable_crypto_port = true;
}
}
}
String port_details;
if (announce_data_provider.getCryptoLevel() == NetworkManager.CRYPTO_OVERRIDE_REQUIRED) {
port_details = TRTrackerUtils.getPortsForURLFullCrypto(tcp_port);
} else {
port_details = TRTrackerUtils.getPortsForURL(tcp_port, disable_crypto_port);
}
try {
if (_url.getHost().endsWith(".amazonaws.com")) {
port_details = port_details.replaceAll("&port=0", "&port=" + tcp_port);
}
} catch (Throwable e) {
}
request.append(port_details);
long total_sent = announce_data_provider.getTotalSent();
long total_received = announce_data_provider.getTotalReceived();
request.append("&uploaded=").append(total_sent);
request.append("&downloaded=").append(total_received);
if (Constants.DOWNLOAD_SOURCES_PRETEND_COMPLETE) {
request.append("&left=0");
} else {
request.append("&left=").append(announce_data_provider.getRemaining());
}
// 3017: added at request of tracker admins who want to be able to monitor swarm poisoning
request.append("&corrupt=").append(announce_data_provider.getFailedHashCheck());
// TrackerID extension
if (tracker_id.length() > 0) {
request.append("&trackerid=").append(tracker_id);
}
if (evt.length() != 0) {
request.append("&event=").append(evt);
}
boolean stopped = evt.equals("stopped");
if (stopped) {
request.append("&numwant=0");
if (stopped_for_queue) {
request.append("&azq=1");
}
} else {
// calculate how many peers we should ask for
int numwant = Math.min(calculateNumWant(), userMaxNumwant);
request.append("&numwant=").append(numwant);
// no_peer_id has been made obsolete by 'compact'
}
// actually, leave this in, ask PARG why!
request.append("&no_peer_id=1");
String tracker_network = AENetworkClassifier.categoriseAddress(_url.getHost());
// latest space saving measure, a compact return type where peers are returned
// as 6 byte entries in a single byte[] (4 bytes ip, 2 byte port)
// leave this as always supplied, ask PARG why
request.append("&compact=1");
// any explicit override takes precedence over any implicit override added
// when hosting torrents
String explicit_ips = COConfigurationManager.getStringParameter("Override Ip", "");
String ip = null;
// make sure this tracker network is enabled
boolean network_ok = false;
boolean normal_network_ok = false;
if (peer_networks == null) {
network_ok = true;
normal_network_ok = true;
} else {
for (int i = 0; i < peer_networks.length; i++) {
if (peer_networks[i] == AENetworkClassifier.AT_PUBLIC) {
normal_network_ok = true;
}
if (peer_networks[i] == tracker_network) {
network_ok = true;
}
}
}
if (!network_ok) {
throw (new Exception("Network not enabled for url '" + _url + "'"));
}
String normal_explicit = null;
if (explicit_ips.length() > 0) {
// gotta select an appropriate override based on network type
StringTokenizer tok = new StringTokenizer(explicit_ips, ";");
while (tok.hasMoreTokens()) {
String this_address = tok.nextToken().trim();
if (this_address.length() > 0) {
String cat = AENetworkClassifier.categoriseAddress(this_address);
if (cat == AENetworkClassifier.AT_PUBLIC) {
normal_explicit = this_address;
}
if (tracker_network == cat) {
ip = this_address;
break;
}
}
}
}
if (ip == null) {
if (normal_network_ok && normal_explicit != null) {
ip = normal_explicit;
} else {
if (ip_override != null && !TorrentUtils.isDecentralised(ip_override)) {
ip = ip_override;
}
}
}
if (ip != null) {
if (tracker_network == AENetworkClassifier.AT_PUBLIC) {
try {
ip = PRHelpers.DNSToIPAddress(ip);
} catch (UnknownHostException e) {
if (Logger.isEnabled())
Logger.log(new LogEvent(torrent, LOGID, LogEvent.LT_ERROR, "IP Override host resolution of '" + ip + "' fails, using unresolved address"));
}
}
request.append("&ip=").append(ip);
}
if (COConfigurationManager.getBooleanParameter("Tracker Key Enable Client", true)) {
request.append("&key=").append(helper.getTrackerKey());
}
String ext = announce_data_provider.getExtensions();
if (ext != null) {
while (ext.startsWith("&")) {
ext = ext.substring(1);
}
request.append("&");
request.append(ext);
}
request.append("&azver=" + TRTrackerAnnouncer.AZ_TRACKER_VERSION_CURRENT);
if (az_tracker && !stopped) {
int up = announce_data_provider.getUploadSpeedKBSec(evt.equals("started"));
if (up > 0) {
request.append("&azup=").append(up);
}
String as = NetworkAdmin.getSingleton().getCurrentASN().getAS();
if (as.length() > 0) {
request.append("&azas=").append(URLEncoder.encode(as, "UTF8"));
}
DHTNetworkPosition best_position = DHTNetworkPositionManager.getBestLocalPosition();
if (best_position != null) {
try {
byte[] bytes = DHTNetworkPositionManager.serialisePosition(best_position);
request.append("&aznp=").append(Base32.encode(bytes));
} catch (Throwable e) {
Debug.printStackTrace(e);
}
}
}
if (tracker_network == AENetworkClassifier.AT_I2P) {
String temp = request.toString();
int pos = temp.indexOf('?');
String head = temp.substring(0, pos);
String tail = temp.substring(pos + 1);
String[] bits = tail.split("&");
Map<String, String> map = new HashMap<>();
for (String bit : bits) {
String[] arg = bit.split("=");
map.put(arg[0], arg[1]);
}
tail = "";
for (String str : new String[] { "info_hash", "peer_id", "port", "ip", "uploaded", "downloaded", "left", "compact", "event", "numwant" }) {
String val = map.get(str);
if (val != null) {
tail += (tail.length() == 0 ? "" : "&") + str + "=" + map.get(str);
}
}
request = new StringBuffer(head + "?" + tail);
}
return (new TRTrackerAnnouncerRequestImpl(helper.getSessionID(), torrent_hash_target, stopped, new URL(request.toString()), total_sent, total_received));
}
use of com.biglybt.core.tracker.AllTrackersManager.AllTrackersTracker in project BiglyBT by BiglySoftware.
the class AllTrackersManagerImpl method processUpdates.
private void processUpdates(boolean for_close) {
synchronized (process_lock) {
if (pending_torrents != null && CoreFactory.isCoreRunning()) {
for (TOTorrent torrent : pending_torrents) {
torrent.addListener(AllTrackersManagerImpl.this);
}
got_running = true;
pending_torrents = null;
}
Set<AllTrackersTracker> updates = new HashSet<>();
while (!update_queue.isEmpty()) {
Object[] entry = update_queue.remove();
try {
Object e0 = entry[0];
if (e0 instanceof TOTorrent) {
TOTorrent torrent = (TOTorrent) e0;
if (pending_torrents == null) {
torrent.addListener(AllTrackersManagerImpl.this);
} else {
pending_torrents.add(torrent);
}
continue;
} else if (e0 instanceof String) {
String cmd = (String) e0;
if (cmd.equals("logging_changed")) {
updateLogging();
} else {
Debug.out("eh?");
}
continue;
}
AllTrackersTrackerImpl tracker = (AllTrackersTrackerImpl) e0;
if (host_map.containsKey(tracker.getTrackerName())) {
Object obj = entry[1];
boolean updated = false;
if (obj instanceof String) {
String cmd = (String) obj;
if (cmd.equals("reset_stats")) {
tracker.resetReportedStatsSupport();
updated = true;
} else {
Debug.out("eh?");
}
} else if (obj instanceof TRTrackerAnnouncerResponse) {
TRTrackerAnnouncerResponse a_resp = (TRTrackerAnnouncerResponse) obj;
if (tracker.setOK(a_resp.getStatus() == TRTrackerAnnouncerResponse.ST_ONLINE)) {
updated = true;
}
if (tracker.setStatusString(a_resp.getStatusString())) {
updated = true;
}
if (updated) {
tracker.log(a_resp);
}
} else if (obj instanceof TRTrackerScraperResponse) {
if (tracker.hasStatus()) {
continue;
}
TRTrackerScraperResponse s_resp = (TRTrackerScraperResponse) obj;
if (tracker.setOK(s_resp.getStatus() == TRTrackerScraperResponse.ST_ONLINE)) {
updated = true;
}
if (tracker.setStatusString(s_resp.getStatusString())) {
updated = true;
}
} else if (obj instanceof TRTrackerAnnouncerRequest) {
TRTrackerAnnouncerRequest req = (TRTrackerAnnouncerRequest) obj;
tracker.updateSession(req);
tracker.log(req, false);
updated = true;
}
if (updated) {
updates.add(tracker);
}
}
} catch (Throwable e) {
Debug.out(e);
}
}
if (for_close) {
Set<String> active_privates = new HashSet<>();
for (TRTrackerAnnouncerRequest req : active_requests.keySet()) {
String key = ingestURL(req.getURL());
if (key != null) {
AllTrackersTrackerImpl existing_tracker = host_map.get(key);
if (existing_tracker != null) {
existing_tracker.log(req, true);
if (existing_tracker.getPrivatePercentage() > 80) {
active_privates.add(existing_tracker.getShortKey());
}
}
}
}
if (!active_privates.isEmpty()) {
COConfigurationManager.setParameter(CONFIG_PRIVATE_ACTIVE_AT_CLOSE, new ArrayList<String>(active_privates));
}
} else {
if (!updates.isEmpty()) {
List<AllTrackersTracker> trackers = new ArrayList<>(updates);
for (AllTrackersListener listener : listeners) {
try {
listener.trackerEventOccurred(new AllTrackersEventImpl(AllTrackersEvent.ET_TRACKER_UPDATED, trackers));
} catch (Throwable e) {
Debug.out(e);
}
}
}
}
}
}
Aggregations