2022-01-10 20:20:18 +01:00
|
|
|
#include "fastfetch.h"
|
2022-07-25 13:15:01 +02:00
|
|
|
#include "common/printing.h"
|
2022-07-25 14:48:59 +02:00
|
|
|
#include "common/networking.h"
|
2022-01-10 20:20:18 +01:00
|
|
|
|
|
|
|
|
#define FF_PUBLICIP_MODULE_NAME "Public IP"
|
|
|
|
|
#define FF_PUBLICIP_NUM_FORMAT_ARGS 1
|
|
|
|
|
|
2022-09-28 03:10:33 +08:00
|
|
|
static int sockfd;
|
|
|
|
|
|
|
|
|
|
void ffPreparePublicIp(FFinstance* instance)
|
|
|
|
|
{
|
2022-09-29 14:22:40 +08:00
|
|
|
if(instance->config.publicIpUrl.length == 0)
|
2022-09-29 17:05:01 +08:00
|
|
|
sockfd = ffNetworkingSendHttpRequest("ipinfo.io", "/ip", NULL, instance->config.publicIpTimeout);
|
2022-09-29 14:22:40 +08:00
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
FFstrbuf host;
|
|
|
|
|
ffStrbufInitCopy(&host, &instance->config.publicIpUrl);
|
|
|
|
|
ffStrbufSubstrAfterFirstS(&host, "://");
|
|
|
|
|
uint32_t pathStartIndex = ffStrbufFirstIndexC(&host, '/');
|
|
|
|
|
|
|
|
|
|
FFstrbuf path;
|
|
|
|
|
ffStrbufInit(&path);
|
|
|
|
|
if(pathStartIndex != host.length)
|
|
|
|
|
{
|
|
|
|
|
ffStrbufAppendNS(&path, pathStartIndex, host.chars + (host.length - pathStartIndex));
|
|
|
|
|
host.length = pathStartIndex;
|
|
|
|
|
host.chars[pathStartIndex] = '\0';
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-29 17:05:01 +08:00
|
|
|
sockfd = ffNetworkingSendHttpRequest(host.chars, path.length == 0 ? "/" : path.chars, NULL, instance->config.publicIpTimeout);
|
2022-09-29 14:22:40 +08:00
|
|
|
|
|
|
|
|
ffStrbufDestroy(&path);
|
|
|
|
|
ffStrbufDestroy(&host);
|
|
|
|
|
}
|
2022-09-28 03:10:33 +08:00
|
|
|
}
|
|
|
|
|
|
2022-01-10 20:20:18 +01:00
|
|
|
void ffPrintPublicIp(FFinstance* instance)
|
|
|
|
|
{
|
2022-09-28 03:10:33 +08:00
|
|
|
if(sockfd == 0)
|
|
|
|
|
ffPreparePublicIp(instance);
|
|
|
|
|
|
|
|
|
|
if(sockfd < 0)
|
|
|
|
|
{
|
|
|
|
|
ffPrintError(instance, FF_PUBLICIP_MODULE_NAME, 0, &instance->config.publicIP, "Failed to connect to an IP detection server");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-10 20:20:18 +01:00
|
|
|
FFstrbuf result;
|
|
|
|
|
ffStrbufInitA(&result, 4096);
|
2022-09-30 17:43:23 +08:00
|
|
|
bool success = ffNetworkingRecvHttpResponse(sockfd, &result);
|
|
|
|
|
if(success) ffStrbufSubstrAfterFirstS(&result, "\r\n\r\n");
|
2022-01-10 20:20:18 +01:00
|
|
|
|
2022-09-30 17:43:23 +08:00
|
|
|
if(!success || result.length == 0)
|
2022-01-10 20:20:18 +01:00
|
|
|
{
|
2022-09-28 03:10:33 +08:00
|
|
|
ffPrintError(instance, FF_PUBLICIP_MODULE_NAME, 0, &instance->config.publicIP, "Failed to receive the server response");
|
2022-09-22 21:28:20 +08:00
|
|
|
ffStrbufDestroy(&result);
|
2022-01-10 20:20:18 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-17 18:15:36 +02:00
|
|
|
if(instance->config.publicIP.outputFormat.length == 0)
|
2022-01-10 20:20:18 +01:00
|
|
|
{
|
2022-06-17 18:15:36 +02:00
|
|
|
ffPrintLogoAndKey(instance, FF_PUBLICIP_MODULE_NAME, 0, &instance->config.publicIP.key);
|
2022-01-10 20:20:18 +01:00
|
|
|
ffStrbufPutTo(&result, stdout);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-06-17 18:15:36 +02:00
|
|
|
ffPrintFormat(instance, FF_PUBLICIP_MODULE_NAME, 0, &instance->config.publicIP, FF_PUBLICIP_NUM_FORMAT_ARGS, (FFformatarg[]) {
|
2022-01-10 20:20:18 +01:00
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &result}
|
|
|
|
|
});
|
|
|
|
|
}
|
2022-09-22 21:28:20 +08:00
|
|
|
|
|
|
|
|
ffStrbufDestroy(&result);
|
2022-01-10 20:20:18 +01:00
|
|
|
}
|