PublicIP: print location

This commit is contained in:
李通洲
2023-07-15 11:24:12 +08:00
parent a9466654b2
commit 426aae1ab6
2 changed files with 25 additions and 1 deletions
+1
View File
@@ -23,6 +23,7 @@ Features:
* Support detecting brightness of external displays with DDC/CI (guard behind `--allow-slow-operations`) (Brightness)
* Add option `--size-ndigits` and `--size-max-prefix` (#494)
* Add option `--processing-timeout` to the timeout when waiting for child processes.
* Public IP module prints the IP location if `--publicip-url` is not set
# 1.12.2
+24 -1
View File
@@ -10,10 +10,17 @@
static FFNetworkingState state;
static int status = -1;
static inline void wrapYyjsonFree(yyjson_doc** doc)
{
assert(doc);
if (*doc)
yyjson_doc_free(*doc);
}
void ffPreparePublicIp(FFPublicIpOptions* options)
{
if (options->url.length == 0)
status = ffNetworkingSendHttpRequest(&state, "ipinfo.io", "/ip", NULL);
status = ffNetworkingSendHttpRequest(&state, "ipinfo.io", "/json", NULL);
else
{
FF_STRBUF_AUTO_DESTROY host = ffStrbufCreateCopy(&options->url);
@@ -56,6 +63,22 @@ void ffPrintPublicIp(FFPublicIpOptions* options)
if (options->moduleArgs.outputFormat.length == 0)
{
ffPrintLogoAndKey(FF_PUBLICIP_DISPLAY_NAME, 0, &options->moduleArgs.key, &options->moduleArgs.keyColor);
if (options->url.length == 0)
{
yyjson_doc* __attribute__((__cleanup__(wrapYyjsonFree))) doc = yyjson_read_opts(result.chars, result.length, 0, NULL, NULL);
if (doc)
{
yyjson_val* root = yyjson_doc_get_root(doc);
printf("%s (%s, %s)\n",
yyjson_get_str(yyjson_obj_get(root, "ip")),
yyjson_get_str(yyjson_obj_get(root, "city")),
yyjson_get_str(yyjson_obj_get(root, "country"))
);
return;
}
}
ffStrbufPutTo(&result, stdout);
}
else