Skip to content

Commit c72c6c4

Browse files
committed
PublicIp: add option to set public IP server URL
1 parent e289db1 commit c72c6c4

File tree

7 files changed

+35
-1
lines changed

7 files changed

+35
-1
lines changed

completions/bash

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ __fastfetch_completion()
217217
"--set"
218218
"--set-keyless"
219219
"--player-name"
220+
"--public-ip-url"
220221
"--public-ip-timeout"
221222
"--os-key"
222223
"--os-format"

src/common/init.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ static void defaultConfig(FFinstance* instance)
243243
ffStrbufInit(&instance->config.localIpNamePrefix);
244244

245245
instance->config.publicIpTimeout = 0;
246+
ffStrbufInit(&instance->config.publicIpUrl);
246247

247248
ffStrbufInitA(&instance->config.osFile, 0);
248249

src/data/config_user.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,12 @@
151151
# Default is "-"
152152
#--separator-string -
153153

154+
# Public IP URL option:
155+
# Sets the URL of public IP detection server to be used.
156+
# Only HTTP protocol is supported, and the value should not contain "http://" prefix.
157+
# Default is "ipinfo.io/ip".
158+
#--public-ip-url "ipinfo.io/ip"
159+
154160
# Public IP timeout option:
155161
# Sets the time to wait for the public ip server to respond.
156162
# Must be a positive integer.

src/data/help.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ Module specific options:
103103
--localip-show-loop <?value>: Show loop back addresses (127.0.0.1) in local ip module. Default is false
104104
--localip-name-prefix <str>: Show ips with given name prefix only. Default is empty
105105
--public-ip-timeout: Time in milliseconds to wait for the public ip server to respond. Default is disabled (0)
106+
--public-ip-url: The URL of public IP detection server to be used.
106107
--player-name: The name of the player to use
107108
--gl <value>: Sets the opengl context creation library to use. Must be auto, egl, glx or osmesa. Default is auto
108109

src/fastfetch.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,6 +1291,8 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con
12911291
optionParseString(key, value, &instance->config.osFile);
12921292
else if(strcasecmp(key, "--player-name") == 0)
12931293
optionParseString(key, value, &instance->config.playerName);
1294+
else if(strcasecmp(key, "--public-ip-url") == 0)
1295+
optionParseString(key, value, &instance->config.publicIpUrl);
12941296
else if(strcasecmp(key, "--public-ip-timeout") == 0)
12951297
instance->config.publicIpTimeout = optionParseUInt32(key, value);
12961298
else if(strcasecmp(key, "--gl") == 0)

src/fastfetch.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ typedef struct FFconfig
174174
bool localIpShowIpV6;
175175
FFstrbuf localIpNamePrefix;
176176

177+
FFstrbuf publicIpUrl;
177178
uint32_t publicIpTimeout;
178179

179180
FFstrbuf osFile;

src/modules/publicip.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,29 @@ static int sockfd;
99

1010
void ffPreparePublicIp(FFinstance* instance)
1111
{
12-
sockfd = ffNetworkingSendHttpRequest("ipinfo.io", "/ip", instance->config.publicIpTimeout);
12+
if(instance->config.publicIpUrl.length == 0)
13+
sockfd = ffNetworkingSendHttpRequest("ipinfo.io", "/ip", instance->config.publicIpTimeout);
14+
else
15+
{
16+
FFstrbuf host;
17+
ffStrbufInitCopy(&host, &instance->config.publicIpUrl);
18+
ffStrbufSubstrAfterFirstS(&host, "://");
19+
uint32_t pathStartIndex = ffStrbufFirstIndexC(&host, '/');
20+
21+
FFstrbuf path;
22+
ffStrbufInit(&path);
23+
if(pathStartIndex != host.length)
24+
{
25+
ffStrbufAppendNS(&path, pathStartIndex, host.chars + (host.length - pathStartIndex));
26+
host.length = pathStartIndex;
27+
host.chars[pathStartIndex] = '\0';
28+
}
29+
30+
sockfd = ffNetworkingSendHttpRequest(host.chars, path.length == 0 ? "/" : path.chars, instance->config.publicIpTimeout);
31+
32+
ffStrbufDestroy(&path);
33+
ffStrbufDestroy(&host);
34+
}
1335
}
1436

1537
void ffPrintPublicIp(FFinstance* instance)

0 commit comments

Comments
 (0)