From c72c6c4a2745bbae8ff1b89a5ee5f3cc3f9c4920 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Thu, 29 Sep 2022 14:22:40 +0800 Subject: [PATCH] PublicIp: add option to set public IP server URL --- completions/bash | 1 + src/common/init.c | 1 + src/data/config_user.txt | 6 ++++++ src/data/help.txt | 1 + src/fastfetch.c | 2 ++ src/fastfetch.h | 1 + src/modules/publicip.c | 24 +++++++++++++++++++++++- 7 files changed, 35 insertions(+), 1 deletion(-) diff --git a/completions/bash b/completions/bash index 5c645d89..ffbaacf6 100644 --- a/completions/bash +++ b/completions/bash @@ -217,6 +217,7 @@ __fastfetch_completion() "--set" "--set-keyless" "--player-name" + "--public-ip-url" "--public-ip-timeout" "--os-key" "--os-format" diff --git a/src/common/init.c b/src/common/init.c index 8b8d2f43..0332088f 100644 --- a/src/common/init.c +++ b/src/common/init.c @@ -243,6 +243,7 @@ static void defaultConfig(FFinstance* instance) ffStrbufInit(&instance->config.localIpNamePrefix); instance->config.publicIpTimeout = 0; + ffStrbufInit(&instance->config.publicIpUrl); ffStrbufInitA(&instance->config.osFile, 0); diff --git a/src/data/config_user.txt b/src/data/config_user.txt index d745e0ba..770eff8c 100644 --- a/src/data/config_user.txt +++ b/src/data/config_user.txt @@ -151,6 +151,12 @@ # Default is "-" #--separator-string - +# Public IP URL option: +# Sets the URL of public IP detection server to be used. +# Only HTTP protocol is supported, and the value should not contain "http://" prefix. +# Default is "ipinfo.io/ip". +#--public-ip-url "ipinfo.io/ip" + # Public IP timeout option: # Sets the time to wait for the public ip server to respond. # Must be a positive integer. diff --git a/src/data/help.txt b/src/data/help.txt index 59bf40e3..89ab59c9 100644 --- a/src/data/help.txt +++ b/src/data/help.txt @@ -103,6 +103,7 @@ Module specific options: --localip-show-loop : Show loop back addresses (127.0.0.1) in local ip module. Default is false --localip-name-prefix : Show ips with given name prefix only. Default is empty --public-ip-timeout: Time in milliseconds to wait for the public ip server to respond. Default is disabled (0) + --public-ip-url: The URL of public IP detection server to be used. --player-name: The name of the player to use --gl : Sets the opengl context creation library to use. Must be auto, egl, glx or osmesa. Default is auto diff --git a/src/fastfetch.c b/src/fastfetch.c index 9dc7cdef..c14973c8 100644 --- a/src/fastfetch.c +++ b/src/fastfetch.c @@ -1291,6 +1291,8 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con optionParseString(key, value, &instance->config.osFile); else if(strcasecmp(key, "--player-name") == 0) optionParseString(key, value, &instance->config.playerName); + else if(strcasecmp(key, "--public-ip-url") == 0) + optionParseString(key, value, &instance->config.publicIpUrl); else if(strcasecmp(key, "--public-ip-timeout") == 0) instance->config.publicIpTimeout = optionParseUInt32(key, value); else if(strcasecmp(key, "--gl") == 0) diff --git a/src/fastfetch.h b/src/fastfetch.h index 9d6f9b17..65cfed98 100644 --- a/src/fastfetch.h +++ b/src/fastfetch.h @@ -174,6 +174,7 @@ typedef struct FFconfig bool localIpShowIpV6; FFstrbuf localIpNamePrefix; + FFstrbuf publicIpUrl; uint32_t publicIpTimeout; FFstrbuf osFile; diff --git a/src/modules/publicip.c b/src/modules/publicip.c index ebec9a90..7af4672b 100644 --- a/src/modules/publicip.c +++ b/src/modules/publicip.c @@ -9,7 +9,29 @@ static int sockfd; void ffPreparePublicIp(FFinstance* instance) { - sockfd = ffNetworkingSendHttpRequest("ipinfo.io", "/ip", instance->config.publicIpTimeout); + if(instance->config.publicIpUrl.length == 0) + sockfd = ffNetworkingSendHttpRequest("ipinfo.io", "/ip", instance->config.publicIpTimeout); + 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'; + } + + sockfd = ffNetworkingSendHttpRequest(host.chars, path.length == 0 ? "/" : path.chars, instance->config.publicIpTimeout); + + ffStrbufDestroy(&path); + ffStrbufDestroy(&host); + } } void ffPrintPublicIp(FFinstance* instance)