Restore public IP reporting with a shell-free native helper
Build and Push Docker Image / build (push) Successful in 52s

This commit is contained in:
Ketan Patel committed 2026-10-04 19:15:40 -04:00
1 parent b43f776d54
commit 18c861f52c
4 files changed
+118 -7

No files matched your search

+65
View File
@@ -0,0 +1,65 @@
#include <arpa/inet.h>
#include <ctype.h>
#include <curl/curl.h>
#include <stdio.h>
#include <string.h>
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;
}