diff --git a/Dockerfile b/Dockerfile index de7d803..2674e4f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,12 +1,15 @@ # Keep Alpine's musl ABI and rTorrent version; export only runtime dependencies. FROM alpine:3.24 AS runtime RUN apk add --no-cache rtorrent ca-certificates tzdata \ - build-base lddtreepax python3 + build-base curl-dev lddtreepax python3 COPY install/rtorrent-entrypoint.c /build/rtorrent-entrypoint.c RUN cc -Os -Wall -Wextra -Werror -o /usr/local/bin/rtorrent-entrypoint /build/rtorrent-entrypoint.c \ && strip /usr/local/bin/rtorrent-entrypoint +COPY install/rtorrent-public-ip.c /build/rtorrent-public-ip.c +RUN cc -Os -Wall -Wextra -Werror -o /usr/local/bin/rtorrent-public-ip /build/rtorrent-public-ip.c -lcurl \ + && strip /usr/local/bin/rtorrent-public-ip COPY build/runtime-root.py /build/runtime-root.py -RUN python3 /build/runtime-root.py /runtime /usr/bin/rtorrent /usr/local/bin/rtorrent-entrypoint +RUN python3 /build/runtime-root.py /runtime /usr/bin/rtorrent /usr/local/bin/rtorrent-entrypoint /usr/local/bin/rtorrent-public-ip COPY tests/rtorrent-smoke.c /build/rtorrent-smoke.c RUN cc -static -Os -Wall -Wextra -Werror -o /build/rtorrent-smoke /build/rtorrent-smoke.c diff --git a/README.md b/README.md index ceecff0..31b5a4d 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,9 @@ The default Dockerfile exports the Alpine 3.24 rTorrent binary, musl libraries, CA certificates and timezone/terminal data into a `scratch` image. It contains no shell, package manager, BusyBox, or build tools. The native launcher removes the stale session lock and execs rTorrent; it also supplies the TCP health check. -CI starts rTorrent and verifies an SCGI XML-RPC request before publication. +CI starts rTorrent, runs the native public-IP helper through a scheduled +`execute.capture` hook, and verifies the resulting address over SCGI before +publication. Use the default Compose file with the existing `.env` and mounted data: @@ -53,3 +55,32 @@ the default Compose file now use distroless `:latest`. Root UID, HOME=/root, SCGI port 5001, download paths and the session volume remain supported. Mounted `rtorrent.rc` execute hooks must use programs available in the image; shell commands and arbitrary external utilities are unavailable. + + +## Public-IP reporting without a shell + +The image includes `/usr/local/bin/rtorrent-public-ip`, a native libcurl helper. +It accepts `URL [PROXY]`, limits connections to 5 seconds and the complete lookup +to 10 seconds, validates an IPv4 response, and prints it without a trailing newline. +Failed requests and invalid responses return an error instead of an address. +It does not require bash or the curl command-line executable. + +Replace the old `get_public_ip_address` definition and `ip_tick` schedule in the +host file mounted at `/root/.rtorrent.rc` with the following (adjust the proxy): + +```ini +method.insert = get_public_ip_address, simple|private, "execute.capture=/usr/local/bin/rtorrent-public-ip,https://ipv4.icanhazip.com,http://10.10.12.60:8118" +schedule2 = ip_tick, 0, 1800, "network.local_address.set=(get_public_ip_address)" +``` + +Remove or leave commented the old bash-based definition; do not define both. +Pull and recreate the services from your Compose project after publication: + +```sh +docker compose pull rtorrent +docker compose up -d --force-recreate rtorrent floodui +``` + +The image cannot replace a bind-mounted configuration. Updating the image alone +will not fix an existing `execute.capture=bash,...` hook. This migration preserves +public-IP reporting through the configured proxy while keeping the runtime shell-free. diff --git a/install/rtorrent-public-ip.c b/install/rtorrent-public-ip.c new file mode 100644 index 0000000..5443e3b --- /dev/null +++ b/install/rtorrent-public-ip.c @@ -0,0 +1,65 @@ +#include +#include +#include +#include +#include + +struct response { + char text[128]; + size_t length; +}; + +static size_t receive(void *data, size_t size, size_t count, void *context) { + struct response *response = context; + if (size && count > (sizeof(response->text) - 1 - response->length) / size) + return 0; + size_t length = size * count; + memcpy(response->text + response->length, data, length); + response->length += length; + return length; +} + +int main(int argc, char **argv) { + if (argc < 2 || argc > 3) { + fprintf(stderr, "Usage: rtorrent-public-ip URL [PROXY]\n"); + return 2; + } + if (curl_global_init(CURL_GLOBAL_DEFAULT) != CURLE_OK) return 1; + CURL *curl = curl_easy_init(); + if (!curl) { curl_global_cleanup(); return 1; } + struct response response = {{0}, 0}; + char error[CURL_ERROR_SIZE] = {0}; + curl_easy_setopt(curl, CURLOPT_URL, argv[1]); + if (argc == 3) { + curl_easy_setopt(curl, CURLOPT_PROXY, argv[2]); + curl_easy_setopt(curl, CURLOPT_NOPROXY, ""); + } + curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 5L); + curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10L); + curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L); + curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, receive); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response); + curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error); + CURLcode result = curl_easy_perform(curl); + curl_easy_cleanup(curl); + curl_global_cleanup(); + if (result != CURLE_OK) { + fprintf(stderr, "Public IP lookup failed: %s\n", error[0] ? error : curl_easy_strerror(result)); + return 1; + } + /* Reject embedded NULs; trim the newline returned by IP services. */ + if (memchr(response.text, '\0', response.length)) return 1; + while (response.length && isspace((unsigned char)response.text[response.length - 1])) + --response.length; + response.text[response.length] = '\0'; + char *ip = response.text; + while (isspace((unsigned char)*ip)) ++ip; + struct in_addr address; + if (inet_pton(AF_INET, ip, &address) != 1) { + fprintf(stderr, "Public IP lookup did not return an IPv4 address\n"); + return 1; + } + fputs(ip, stdout); + return 0; +} diff --git a/tests/rtorrent-smoke.c b/tests/rtorrent-smoke.c index ef6c660..c6e3871 100644 --- a/tests/rtorrent-smoke.c +++ b/tests/rtorrent-smoke.c @@ -17,6 +17,7 @@ static void pause_tick(void) { } int main(void) { + alarm(60); /* Fail the build if a scheduled execute hook stalls rTorrent. */ const char *forbidden[] = {"/bin/sh", "/bin/bash", "/bin/busybox", "/sbin/apk", NULL}; for (int i = 0; forbidden[i]; ++i) { if (access(forbidden[i], F_OK) == 0) { @@ -24,12 +25,19 @@ int main(void) { return 1; } } + /* Exercise the production execute.capture hook without external networking. */ + FILE *fixture = fopen("/tmp/rtorrent-public-ip.txt", "w"); + if (!fixture) return 1; + fputs("203.0.113.42\n", fixture); + fclose(fixture); const char *config = "/tmp/rtorrent-smoke.rc"; const char *lock = "/home/nfs_download/rsession/rtorrent.lock"; FILE *f = fopen(config, "w"); if (!f) return 1; fputs("system.daemon.set = true\nnetwork.bind_address.set = 0.0.0.0\n" - "network.scgi.open_port = 127.0.0.1:5001\n", f); + "network.scgi.open_port = 127.0.0.1:5001\n" + "method.insert = get_public_ip_address, simple|private, \"execute.capture=/usr/local/bin/rtorrent-public-ip,file:///tmp/rtorrent-public-ip.txt\"\n" + "schedule2 = ip_tick, 0, 1800, \"network.local_address.set=(get_public_ip_address)\"\n", f); fclose(f); f = fopen(lock, "w"); if (!f) return 1; @@ -61,9 +69,11 @@ int main(void) { pause_tick(); } if (fd < 0 || access(lock, F_OK) == 0) goto cleanup; + /* Allow the immediate scheduled public-IP hook to execute. */ + for (int i = 0; i < 20; ++i) pause_tick(); struct timeval timeout = {5, 0}; setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)); - const char *body = "system.client_version"; + const char *body = "network.local_address"; char headers[128], request[512], response[8192]; int hlen = snprintf(headers, sizeof(headers), "CONTENT_LENGTH%c%zu%cSCGI%c1%c", 0, strlen(body), 0, 0, 0); int prefix = snprintf(request, sizeof(request), "%d:", hlen); @@ -78,7 +88,8 @@ int main(void) { while (used < sizeof(response) - 1 && (n = recv(fd, response + used, sizeof(response) - 1 - used, 0)) > 0) used += (size_t)n; response[used] = '\0'; - if (!strstr(response, "") || strstr(response, "")) { + if (!strstr(response, "") || strstr(response, "") || + !strstr(response, "203.0.113.42")) { fprintf(stderr, "SCGI test failed: %s\n", response); goto cleanup; } @@ -89,7 +100,7 @@ int main(void) { _exit(127); } if (waitpid(health, &status, 0) < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) goto cleanup; - puts("Distroless rTorrent: startup, stale lock cleanup, SCGI and healthcheck passed"); + puts("Distroless rTorrent: startup, public-IP execute hook, SCGI and healthcheck passed"); result = 0; cleanup: if (fd >= 0) close(fd); @@ -101,6 +112,7 @@ cleanup: } if (child > 0) { kill(child, SIGKILL); waitpid(child, &status, 0); } } + unlink("/tmp/rtorrent-public-ip.txt"); unlink(config); unlink(lock); return result;