mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2025-02-20 11:43:27 +08:00
PublicIp: improve performance
This commit is contained in:
parent
e7c9b80d85
commit
e289db184c
@ -359,6 +359,7 @@ static void exitSignalHandler(int signal)
|
||||
void ffStart(FFinstance* instance)
|
||||
{
|
||||
ffPrepareCPUUsage();
|
||||
ffPreparePublicIp(instance);
|
||||
|
||||
if(instance->config.multithreading)
|
||||
startDetectionThreads(instance);
|
||||
|
@ -6,22 +6,23 @@
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
|
||||
void ffNetworkingGetHttp(const char* host, const char* path, uint32_t timeout, FFstrbuf* buffer)
|
||||
int ffNetworkingSendHttpRequest(const char* host, const char* path, uint32_t timeout)
|
||||
{
|
||||
struct addrinfo hints = {0};
|
||||
hints.ai_family = AF_INET;
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
struct addrinfo hints = {
|
||||
.ai_family = AF_INET,
|
||||
.ai_socktype = SOCK_STREAM,
|
||||
};
|
||||
|
||||
struct addrinfo* addr;
|
||||
|
||||
if(getaddrinfo(host, "80", &hints, &addr) != 0)
|
||||
return;
|
||||
return -1;
|
||||
|
||||
int sock = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
|
||||
if(sock == -1)
|
||||
int sockfd = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
|
||||
if(sockfd == -1)
|
||||
{
|
||||
freeaddrinfo(addr);
|
||||
return;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(timeout > 0)
|
||||
@ -29,14 +30,14 @@ void ffNetworkingGetHttp(const char* host, const char* path, uint32_t timeout, F
|
||||
struct timeval timev;
|
||||
timev.tv_sec = 0;
|
||||
timev.tv_usec = (__typeof__(timev.tv_usec)) (timeout * 1000); //milliseconds to microseconds
|
||||
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timev, sizeof(timev));
|
||||
setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &timev, sizeof(timev));
|
||||
}
|
||||
|
||||
if(connect(sock, addr->ai_addr, addr->ai_addrlen) == -1)
|
||||
if(connect(sockfd, addr->ai_addr, addr->ai_addrlen) == -1)
|
||||
{
|
||||
close(sock);
|
||||
close(sockfd);
|
||||
freeaddrinfo(addr);
|
||||
return;
|
||||
return -1;
|
||||
}
|
||||
|
||||
freeaddrinfo(addr);
|
||||
@ -49,14 +50,19 @@ void ffNetworkingGetHttp(const char* host, const char* path, uint32_t timeout, F
|
||||
ffStrbufAppendS(&command, host);
|
||||
ffStrbufAppendS(&command, "\r\n\r\n");
|
||||
|
||||
if(send(sock, command.chars, command.length, 0) == -1)
|
||||
if(send(sockfd, command.chars, command.length, 0) == -1)
|
||||
{
|
||||
ffStrbufDestroy(&command);
|
||||
close(sock);
|
||||
return;
|
||||
close(sockfd);
|
||||
return -1;
|
||||
}
|
||||
ffStrbufDestroy(&command);
|
||||
return sockfd;
|
||||
}
|
||||
|
||||
ssize_t received = recv(sock, buffer->chars + buffer->length, ffStrbufGetFree(buffer), 0);
|
||||
void ffNetworkingRecvHttpResponse(int sockfd, FFstrbuf* buffer)
|
||||
{
|
||||
ssize_t received = recv(sockfd, buffer->chars + buffer->length, ffStrbufGetFree(buffer), 0);
|
||||
|
||||
if(received > 0)
|
||||
{
|
||||
@ -64,6 +70,12 @@ void ffNetworkingGetHttp(const char* host, const char* path, uint32_t timeout, F
|
||||
buffer->chars[buffer->length] = '\0';
|
||||
}
|
||||
|
||||
ffStrbufDestroy(&command);
|
||||
close(sock);
|
||||
close(sockfd);
|
||||
}
|
||||
|
||||
void ffNetworkingGetHttp(const char* host, const char* path, uint32_t timeout, FFstrbuf* buffer)
|
||||
{
|
||||
int sockfd = ffNetworkingSendHttpRequest(host, path, timeout);
|
||||
if(sockfd > 0)
|
||||
ffNetworkingRecvHttpResponse(sockfd, buffer);
|
||||
}
|
||||
|
@ -5,6 +5,8 @@
|
||||
|
||||
#include "util/FFstrbuf.h"
|
||||
|
||||
int ffNetworkingSendHttpRequest(const char* host, const char* path, uint32_t timeout);
|
||||
void ffNetworkingRecvHttpResponse(int sock, FFstrbuf* buffer);
|
||||
void ffNetworkingGetHttp(const char* host, const char* path, uint32_t timeout, FFstrbuf* buffer);
|
||||
|
||||
#endif
|
||||
|
@ -236,6 +236,7 @@ void ffLogoBuiltinListAutocompletion();
|
||||
|
||||
void ffPrintDateTimeFormat(FFinstance* instance, const char* moduleName, const FFModuleArgs* moduleArgs);
|
||||
void ffPrepareCPUUsage();
|
||||
void ffPreparePublicIp(FFinstance* instance);
|
||||
|
||||
//Printing
|
||||
|
||||
|
@ -5,16 +5,32 @@
|
||||
#define FF_PUBLICIP_MODULE_NAME "Public IP"
|
||||
#define FF_PUBLICIP_NUM_FORMAT_ARGS 1
|
||||
|
||||
static int sockfd;
|
||||
|
||||
void ffPreparePublicIp(FFinstance* instance)
|
||||
{
|
||||
sockfd = ffNetworkingSendHttpRequest("ipinfo.io", "/ip", instance->config.publicIpTimeout);
|
||||
}
|
||||
|
||||
void ffPrintPublicIp(FFinstance* instance)
|
||||
{
|
||||
if(sockfd == 0)
|
||||
ffPreparePublicIp(instance);
|
||||
|
||||
if(sockfd < 0)
|
||||
{
|
||||
ffPrintError(instance, FF_PUBLICIP_MODULE_NAME, 0, &instance->config.publicIP, "Failed to connect to an IP detection server");
|
||||
return;
|
||||
}
|
||||
|
||||
FFstrbuf result;
|
||||
ffStrbufInitA(&result, 4096);
|
||||
ffNetworkingGetHttp("ipinfo.io", "/ip", instance->config.publicIpTimeout, &result);
|
||||
ffNetworkingRecvHttpResponse(sockfd, &result);
|
||||
ffStrbufSubstrAfterFirstS(&result, "\r\n\r\n");
|
||||
|
||||
if(result.length == 0)
|
||||
{
|
||||
ffPrintError(instance, FF_PUBLICIP_MODULE_NAME, 0, &instance->config.publicIP, "Failed to connect to an IP detection server");
|
||||
ffPrintError(instance, FF_PUBLICIP_MODULE_NAME, 0, &instance->config.publicIP, "Failed to receive the server response");
|
||||
ffStrbufDestroy(&result);
|
||||
return;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user