Expose real tracker state to web UI

This commit is contained in:
ookami125 2026-06-17 02:14:10 -04:00
parent 6be239943a
commit 6ec5b19a73
4 changed files with 249 additions and 20 deletions

View file

@ -574,6 +574,53 @@ static json_t *tracker_hosts(json_t *trackers) {
return hosts;
}
static bool tracker_host(char out[256], const char *url) {
if (!url || strstr(url, "**")) return false;
const char *start = strstr(url, "://");
start = start ? start + 3 : url;
size_t len = strcspn(start, "/");
if (len == 0 || len >= 256) return false;
snprintf(out, 256, "%.*s", (int)len, start);
return true;
}
static json_t *tracker_summary(json_t *torrents) {
json_t *summary = json_array();
if (!summary || !json_is_array(torrents)) return summary;
size_t tindex;
json_t *torrent;
json_array_foreach(torrents, tindex, torrent) {
json_t *trackers = json_object_get(torrent, "trackers");
if (!json_is_array(trackers)) continue;
size_t index;
json_t *tracker;
json_array_foreach(trackers, index, tracker) {
int64_t tier = json_integer_value(json_object_get(tracker, "tier"));
if (tier < 0) continue;
char host[256];
if (!tracker_host(host, json_string_value(
json_object_get(tracker, "url"))))
continue;
bool found = false;
size_t hindex;
json_t *entry;
json_array_foreach(summary, hindex, entry) {
if (strcmp(json_string_or(entry, "host", ""), host) != 0)
continue;
json_int_t count =
json_integer_value(json_object_get(entry, "count"));
json_object_set_new(entry, "count", json_integer(count + 1));
found = true;
break;
}
if (!found)
json_array_append_new(summary, json_pack(
"{s:s,s:i}", "host", host, "count", 1));
}
}
return summary;
}
static json_t *map_peer_list(json_t *torrent) {
json_t *out = json_array();
json_t *peers = json_object_get(torrent, "peer_list");
@ -799,15 +846,11 @@ static json_t *map_torrent(json_t *torrent, bool detail, double dlspeed) {
json_t *files = NULL;
json_t *peers_list = NULL;
json_t *hosts = NULL;
json_t *source_trackers = json_object_get(torrent, "trackers");
hosts = tracker_hosts(source_trackers);
if (detail) {
trackers = json_array();
if (trackers)
json_array_append_new(trackers, json_pack(
"{s:s,s:i,s:s,s:i,s:i,s:i,s:i,s:s}",
"url", "** [DHT] **", "tier", -1, "status", "working",
"seeds", (int)json_u64(torrent, "peers_discovered"),
"peers", (int)json_u64(torrent, "peers"),
"leeches", -1, "downloaded", -1, "message", ""));
trackers = json_is_array(source_trackers)
? json_deep_copy(source_trackers) : json_array();
files = json_array();
if (files)
json_array_append_new(files, json_pack(
@ -815,7 +858,6 @@ static json_t *map_torrent(json_t *torrent, bool detail, double dlspeed) {
"size", (json_int_t)total, "progress", progress,
"priority", 1, "availability", 1.0));
peers_list = map_peer_list(torrent);
hosts = tracker_hosts(trackers);
}
uint64_t discovered = json_u64(torrent, "peers_discovered");
@ -825,11 +867,11 @@ static json_t *map_torrent(json_t *torrent, bool detail, double dlspeed) {
json_t *out = json_object();
if (!out) {
free(name);
json_decref(hosts);
if (detail) {
json_decref(trackers);
json_decref(files);
json_decref(peers_list);
json_decref(hosts);
}
return NULL;
}
@ -1013,7 +1055,12 @@ static void api_meta(int fd) {
json_deep_copy(g_webui.categories));
json_object_set_new(json, "tags", json_deep_copy(g_webui.tags));
pthread_mutex_unlock(&g_webui.meta_lock);
json_object_set_new(json, "trackers", json_array());
json_t *params = json_object();
json_t *torrents = rpc_call_json("torrents", params);
json_decref(params);
json_t *trackers = tracker_summary(torrents);
json_decref(torrents);
json_object_set_new(json, "trackers", trackers ? trackers : json_array());
json_object_set_new(preferences, "save_path",
json_string(getenv("NAUT_WEBUI_SAVE_PATH")
? getenv("NAUT_WEBUI_SAVE_PATH") : "."));