PublicIP: add option --publicip-ipv6

This commit is contained in:
李通洲
2024-05-09 10:13:04 +08:00
parent 7eb145fb7f
commit acc77ebddd
8 changed files with 51 additions and 12 deletions
+5
View File
@@ -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"
},
+1
View File
@@ -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);
+1 -1
View File
@@ -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)
{
+1 -1
View File
@@ -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";
+8
View File
@@ -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",
+15 -10
View File
@@ -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
+1
View File
@@ -11,4 +11,5 @@ typedef struct FFPublicIpOptions
FFstrbuf url;
uint32_t timeout;
bool ipv6;
} FFPublicIpOptions;
+19
View File
@@ -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)