webui: replace nautctl web server with a loadable plugin

Drop the web UI that was compiled into nautctl and serve the
torrent-ui front end (../torrent-ui/public) from a native plugin
(plugins/webui) loaded via `nautd --plugin`. The plugin talks to the
engine only through the host call_rpc ABI and adapts the daemon's RPC
surface to the qBittorrent-style contract the UI expects (snapshot/SSE,
torrent detail tabs, add/delete, cookie auth).

Also folds in the daemon refactor that owns per-torrent worker threads
and the swarm engine (naut_swarm) used by the plugin's data source.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
ookami125 2026-06-17 00:21:48 -04:00
parent 50a357968a
commit 8dde48c05a
27 changed files with 2706 additions and 403 deletions

View file

@ -35,7 +35,9 @@ typedef struct naut_metainfo {
const uint8_t *piece_hashes;
naut_file *files; size_t num_files;
char **trackers; size_t num_trackers; /* announce + announce-list, flattened */
char **trackers; size_t num_trackers;
/* Parallel to trackers. Equal values belong to one BEP-12 tier. */
uint32_t *tracker_tiers;
/* internals kept alive so piece_hashes/name stay valid */
void *_owned;

View file

@ -25,6 +25,7 @@ typedef naut_err (*naut_plugin_rpc_fn)(void *context,
char **response_json);
typedef void (*naut_plugin_event_fn)(void *context,
const naut_event *event);
typedef naut_err (*naut_plugin_shutdown_fn)(void);
typedef struct naut_host_api {
uint32_t abi_version;
@ -41,6 +42,8 @@ typedef struct naut_host_api {
void *context);
void (*emit_event)(void *host_context, const naut_event *event);
void (*log)(void *host_context, int level, const char *message);
naut_err (*call_rpc)(void *host_context, const char *method,
const char *request_json, char **response_json);
} naut_host_api;
/* Every plugin exports this exact symbol. */

View file

@ -67,6 +67,11 @@ typedef void (*naut_file_complete_cb)(void *ctx, uint32_t file_index, const char
void naut_download_set_file_cb(naut_download *d, naut_file_complete_cb cb, void *ctx);
bool naut_download_file_complete(const naut_download *d, uint32_t file_index);
/* Optional owner-thread notification after a piece verifies and is persisted. */
typedef void (*naut_piece_complete_cb)(void *ctx, uint32_t piece_index);
void naut_download_set_piece_cb(naut_download *d, naut_piece_complete_cb cb,
void *ctx);
/* Hand out the next block to request. false => nothing left to hand out right
* now (all blocks have been requested). */
bool naut_download_next_request(naut_download *d,

View file

@ -8,7 +8,8 @@
#include <jansson.h>
#define NAUT_RPC_VERSION 1
#define NAUT_RPC_MAX_PAYLOAD (1u << 20)
/* Generous enough to carry a base64-encoded .torrent upload in add_torrent. */
#define NAUT_RPC_MAX_PAYLOAD (8u << 20)
typedef enum {
NAUT_RPC_REQUEST = 1,

View file

@ -33,11 +33,12 @@ naut_err naut_storage_write(naut_storage *s, int64_t offset, const void *buf, si
naut_err naut_storage_read(naut_storage *s, int64_t offset, void *buf, size_t len);
naut_err naut_storage_sync(naut_storage *s);
/* Move one completed file out to `dest` (rename, or copy+unlink across file
* systems). The caller must guarantee the file is complete every piece
* overlapping it verified so no further writes target it. After this the slot
* is "externalized": subsequent I/O to its region returns NAUT_ERR_RANGE. This
* is the storage half of the "move files as they finish" feature. */
/* Move one file to `dest` (rename, or copy+unlink across file systems) and keep
* tracking it there: the slot's path is updated and its fd reopened, so the
* engine can still read/write/seed the file at its new location. Typically
* called the moment a file completes (the storage half of "move files as they
* finish"), but safe at any time. The owning process therefore never loses
* track of a moved file. Returns NAUT_ERR_IO if the move or reopen fails. */
naut_err naut_storage_relocate(naut_storage *s, size_t file_index, const char *dest);
int64_t naut_storage_total(const naut_storage *s);

52
include/naut/swarm.h Normal file
View file

@ -0,0 +1,52 @@
/* swarm.h - reusable multi-peer download driver.
*
* naut_swarm is a small CLI wrapper around this API. Long-running applications
* such as nautd own the worker thread and use callbacks for progress, control
* commands, cancellation, and event delivery.
*/
#ifndef NAUT_SWARM_H
#define NAUT_SWARM_H
#include "naut/common.h"
#include "naut/event.h"
#include "naut/storage.h"
typedef struct {
uint64_t total_bytes;
uint64_t bytes_done;
uint32_t total_pieces;
uint32_t pieces_done;
uint32_t peers_total; /* discovered endpoints */
uint32_t peers_connecting;
uint32_t peers_active;
uint32_t peers_failed;
double elapsed_seconds;
} naut_swarm_stats;
typedef void (*naut_swarm_progress_cb)(void *context,
const naut_swarm_stats *stats);
/* Called on the swarm owner thread. The callback may safely operate on storage,
* including relocating completed files. */
typedef void (*naut_swarm_control_cb)(void *context, naut_storage *storage);
typedef bool (*naut_swarm_stop_cb)(void *context);
typedef struct {
const char *source; /* .torrent path or magnet URI */
const char *output_dir;
const char *const *peers; /* optional explicit ip:port endpoints */
size_t num_peers;
uint64_t torrent_id;
naut_event_bus *events; /* optional */
bool keep_alive; /* retain completed storage until stopped */
naut_swarm_progress_cb on_progress;
naut_swarm_control_cb on_control;
naut_swarm_stop_cb should_stop;
void *context;
} naut_swarm_config;
/* Blocks until the torrent completes, is cancelled, or fails. */
naut_err naut_swarm_run(const naut_swarm_config *config);
#endif /* NAUT_SWARM_H */