Naut/apps/nautctl/main.c
ookami125 8dde48c05a 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>
2026-06-17 00:21:48 -04:00

152 lines
4.7 KiB
C

#include "naut/rpc.h"
#include <jansson.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define DEFAULT_SOCKET "/tmp/nautd.sock"
static void usage(const char *program) {
fprintf(stderr,
"usage: %s [--socket PATH] COMMAND [ARGS]\n"
"\n"
"commands:\n"
" add SOURCE OUTPUT [IP:PORT ...]\n"
" list\n"
" show TORRENT_ID\n"
" remove TORRENT_ID\n"
" script PATH | unscript\n"
" status | events | shutdown\n"
" METHOD [PARAMS_JSON] (raw RPC)\n", program);
}
static json_t *parse_params(const char *text) {
if (!text) return json_object();
json_error_t error;
return json_loads(text, JSON_REJECT_DUPLICATES | JSON_DECODE_ANY, &error);
}
static int print_json(json_t *json) {
if (!json) return 1;
if (json_dumpf(json, stdout, JSON_INDENT(2) | JSON_SORT_KEYS) != 0)
return 1;
putchar('\n');
return 0;
}
static int stream_events(const char *socket_path) {
int fd = naut_rpc_connect_unix(socket_path);
if (fd < 0) return 1;
json_t *request = json_pack("{s:s,s:o}", "method", "subscribe",
"params", json_object());
if (!request ||
naut_rpc_send_json(fd, NAUT_RPC_REQUEST, request) != NAUT_OK) {
json_decref(request);
close(fd);
return 1;
}
json_decref(request);
for (;;) {
naut_rpc_frame_type type;
json_t *payload = NULL;
if (naut_rpc_recv_json(fd, &type, &payload) != NAUT_OK) {
close(fd);
return 1;
}
print_json(payload);
fflush(stdout);
json_decref(payload);
}
}
static bool parse_id(const char *text, json_int_t *id) {
if (!text || !*text || *text == '-') return false;
char *end = NULL;
unsigned long long value = strtoull(text, &end, 10);
if (!end || *end || value > (unsigned long long)INT64_MAX) return false;
*id = (json_int_t)value;
return true;
}
int main(int argc, char **argv) {
const char *socket_path = getenv("NAUT_SOCKET");
if (!socket_path || !*socket_path) socket_path = DEFAULT_SOCKET;
int arg = 1;
if (arg < argc && strcmp(argv[arg], "--socket") == 0) {
if (arg + 1 >= argc) {
usage(argv[0]);
return 2;
}
socket_path = argv[arg + 1];
arg += 2;
}
if (arg >= argc) {
usage(argv[0]);
return 2;
}
signal(SIGPIPE, SIG_IGN);
const char *method = argv[arg++];
if (strcmp(method, "events") == 0)
return stream_events(socket_path);
json_t *params = NULL;
if (strcmp(method, "add") == 0) {
if (arg + 1 >= argc) { usage(argv[0]); return 2; }
method = "add_torrent";
params = json_pack("{s:s,s:s}", "source", argv[arg],
"output", argv[arg + 1]);
arg += 2;
json_t *peers = json_array();
if (!params || !peers) {
json_decref(params);
json_decref(peers);
return 1;
}
while (arg < argc)
json_array_append_new(peers, json_string(argv[arg++]));
json_object_set_new(params, "peers", peers);
} else if (strcmp(method, "list") == 0) {
if (arg != argc) { usage(argv[0]); return 2; }
method = "torrents";
params = json_object();
} else if (strcmp(method, "show") == 0 ||
strcmp(method, "remove") == 0) {
json_int_t id;
if (arg + 1 != argc || !parse_id(argv[arg], &id)) {
usage(argv[0]);
return 2;
}
method = strcmp(method, "show") == 0 ? "torrent" :
"remove_torrent";
params = json_pack("{s:I}", "torrent_id", id);
} else if (strcmp(method, "script") == 0) {
if (arg + 1 != argc) { usage(argv[0]); return 2; }
method = "load_script";
params = json_pack("{s:s}", "path", argv[arg]);
} else if (strcmp(method, "unscript") == 0) {
if (arg != argc) { usage(argv[0]); return 2; }
method = "unload_script";
params = json_object();
} else {
if (arg + 1 < argc) { usage(argv[0]); return 2; }
params = parse_params(arg < argc ? argv[arg] : NULL);
}
if (!params) {
fprintf(stderr, "nautctl: invalid JSON parameters\n");
return 2;
}
json_t *reply = NULL;
naut_err error = naut_rpc_call(socket_path, method, params, &reply);
json_decref(params);
if (error != NAUT_OK) {
fprintf(stderr, "nautctl: RPC failed: %s\n", naut_strerror(error));
return 1;
}
int result = print_json(reply);
bool ok = json_is_true(json_object_get(reply, "ok"));
json_decref(reply);
return result || !ok;
}