mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-09-13 10:24:58 +02:00
PublicIP: add option --publicip-ipv6
This commit is contained in:
@@ -1776,6 +1776,11 @@
|
||||
"minimum": 0,
|
||||
"default": "disabled (0)"
|
||||
},
|
||||
"ipv6": {
|
||||
"description": "Whether to use IPv6 for public IP detection server",
|
||||
"type": "boolean",
|
||||
"default": false
|
||||
},
|
||||
"key": {
|
||||
"$ref": "#/$defs/key"
|
||||
},
|
||||
|
||||
@@ -22,6 +22,7 @@ typedef struct FFNetworkingState {
|
||||
#endif
|
||||
|
||||
uint32_t timeout;
|
||||
bool ipv6;
|
||||
} FFNetworkingState;
|
||||
|
||||
const char* ffNetworkingSendHttpRequest(FFNetworkingState* state, const char* host, const char* path, const char* headers);
|
||||
|
||||
@@ -14,7 +14,7 @@ static const char* connectAndSend(FFNetworkingState* state)
|
||||
struct addrinfo* addr;
|
||||
|
||||
if(getaddrinfo(state->host.chars, "80", &(struct addrinfo) {
|
||||
.ai_family = AF_INET,
|
||||
.ai_family = state->ipv6 ? AF_INET6 : AF_INET,
|
||||
.ai_socktype = SOCK_STREAM,
|
||||
}, &addr) != 0)
|
||||
{
|
||||
|
||||
@@ -51,7 +51,7 @@ const char* ffNetworkingSendHttpRequest(FFNetworkingState* state, const char* ho
|
||||
struct addrinfo* addr;
|
||||
|
||||
if(getaddrinfo(host, "80", &(struct addrinfo) {
|
||||
.ai_family = AF_INET,
|
||||
.ai_family = state.ipv6 ? AF_INET6 : AF_INET,
|
||||
.ai_socktype = SOCK_STREAM,
|
||||
}, &addr) != 0)
|
||||
return "getaddrinfo() failed";
|
||||
|
||||
@@ -1348,6 +1348,14 @@
|
||||
"type": "str"
|
||||
}
|
||||
},
|
||||
{
|
||||
"long": "publicip-ipv6",
|
||||
"desc": "Whether to use IPv6 for public IP detection server",
|
||||
"arg": {
|
||||
"type": "bool",
|
||||
"default": false
|
||||
}
|
||||
},
|
||||
{
|
||||
"long": "weather-location",
|
||||
"desc": "Set the location to be used",
|
||||
|
||||
@@ -2,21 +2,24 @@
|
||||
#include "common/networking.h"
|
||||
|
||||
#define FF_UNITIALIZED ((const char*)(uintptr_t) -1)
|
||||
static FFNetworkingState state;
|
||||
static const char* status = FF_UNITIALIZED;
|
||||
static FFNetworkingState states[2];
|
||||
static const char* statuses[2] = { FF_UNITIALIZED, FF_UNITIALIZED };
|
||||
|
||||
void ffPreparePublicIp(FFPublicIpOptions* options)
|
||||
{
|
||||
if (status != FF_UNITIALIZED)
|
||||
FFNetworkingState* state = &states[options->ipv6];
|
||||
const char** status = &statuses[options->ipv6];
|
||||
if (*status != FF_UNITIALIZED)
|
||||
{
|
||||
fputs("Error: PublicIp module can only be used once due to internal limitations\n", stderr);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
state.timeout = options->timeout;
|
||||
state->timeout = options->timeout;
|
||||
state->ipv6 = options->ipv6;
|
||||
|
||||
if (options->url.length == 0)
|
||||
status = ffNetworkingSendHttpRequest(&state, "ipinfo.io", "/json", NULL);
|
||||
*status = ffNetworkingSendHttpRequest(state, options->ipv6 ? "v6.ipinfo.io" : "ipinfo.io", "/json", NULL);
|
||||
else
|
||||
{
|
||||
FF_STRBUF_AUTO_DESTROY host = ffStrbufCreateCopy(&options->url);
|
||||
@@ -40,7 +43,7 @@ void ffPreparePublicIp(FFPublicIpOptions* options)
|
||||
host.chars[pathStartIndex] = '\0';
|
||||
}
|
||||
|
||||
status = ffNetworkingSendHttpRequest(&state, host.chars, path.length == 0 ? "/" : path.chars, NULL);
|
||||
*status = ffNetworkingSendHttpRequest(state, host.chars, path.length == 0 ? "/" : path.chars, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,14 +56,16 @@ static inline void wrapYyjsonFree(yyjson_doc** doc)
|
||||
|
||||
const char* ffDetectPublicIp(FFPublicIpOptions* options, FFPublicIpResult* result)
|
||||
{
|
||||
if (status == FF_UNITIALIZED)
|
||||
FFNetworkingState* state = &states[options->ipv6];
|
||||
const char** status = &statuses[options->ipv6];
|
||||
if (*status == FF_UNITIALIZED)
|
||||
ffPreparePublicIp(options);
|
||||
|
||||
if (status != NULL)
|
||||
return status;
|
||||
if (*status != NULL)
|
||||
return *status;
|
||||
|
||||
FF_STRBUF_AUTO_DESTROY response = ffStrbufCreateA(4096);
|
||||
const char* error = ffNetworkingRecvHttpResponse(&state, &response);
|
||||
const char* error = ffNetworkingRecvHttpResponse(state, &response);
|
||||
if (error == NULL)
|
||||
ffStrbufSubstrAfterFirstS(&response, "\r\n\r\n");
|
||||
else
|
||||
|
||||
@@ -11,4 +11,5 @@ typedef struct FFPublicIpOptions
|
||||
|
||||
FFstrbuf url;
|
||||
uint32_t timeout;
|
||||
bool ipv6;
|
||||
} FFPublicIpOptions;
|
||||
|
||||
@@ -59,6 +59,12 @@ bool ffParsePublicIpCommandOptions(FFPublicIpOptions* options, const char* key,
|
||||
return true;
|
||||
}
|
||||
|
||||
if (ffStrEqualsIgnCase(subKey, "ipv6"))
|
||||
{
|
||||
options->ipv6 = ffOptionParseBoolean(value);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -87,6 +93,12 @@ void ffParsePublicIpJsonObject(FFPublicIpOptions* options, yyjson_val* module)
|
||||
continue;
|
||||
}
|
||||
|
||||
if (ffStrEqualsIgnCase(key, "ipv6"))
|
||||
{
|
||||
options->ipv6 = yyjson_get_bool(val);
|
||||
continue;
|
||||
}
|
||||
|
||||
ffPrintError(FF_PUBLICIP_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", key);
|
||||
}
|
||||
}
|
||||
@@ -100,6 +112,12 @@ void ffGeneratePublicIpJsonConfig(FFPublicIpOptions* options, yyjson_mut_doc* do
|
||||
|
||||
if (!ffStrbufEqual(&options->url, &defaultOptions.url))
|
||||
yyjson_mut_obj_add_strbuf(doc, module, "url", &options->url);
|
||||
|
||||
if (defaultOptions.timeout != options->timeout)
|
||||
yyjson_mut_obj_add_uint(doc, module, "timeout", options->timeout);
|
||||
|
||||
if (defaultOptions.ipv6 != options->ipv6)
|
||||
yyjson_mut_obj_add_bool(doc, module, "ipv6", options->ipv6);
|
||||
}
|
||||
|
||||
void ffGeneratePublicIpJsonResult(FFPublicIpOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
|
||||
@@ -148,6 +166,7 @@ void ffInitPublicIpOptions(FFPublicIpOptions* options)
|
||||
|
||||
ffStrbufInit(&options->url);
|
||||
options->timeout = 0;
|
||||
options->ipv6 = false;
|
||||
}
|
||||
|
||||
void ffDestroyPublicIpOptions(FFPublicIpOptions* options)
|
||||
|
||||
Reference in New Issue
Block a user