Restore public IP reporting with a shell-free native helper
Build and Push Docker Image / build (push) Successful in 52s
Build and Push Docker Image / build (push) Successful in 52s
This commit is contained in:
1 parent
b43f776d54
commit
18c861f52c
4 files changed
+118
-7
No files matched your search
+16
-4
@@ -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 = "<?xml version=\"1.0\"?><methodCall><methodName>system.client_version</methodName><params/></methodCall>";
|
||||
const char *body = "<?xml version=\"1.0\"?><methodCall><methodName>network.local_address</methodName><params/></methodCall>";
|
||||
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, "<methodResponse>") || strstr(response, "<fault>")) {
|
||||
if (!strstr(response, "<methodResponse>") || strstr(response, "<fault>") ||
|
||||
!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;
|
||||
|
||||
Reference in new issue
Block a user