2021-10-17 12:42:15 +08:00
|
|
|
#include "fastfetch.h"
|
2022-07-25 13:15:01 +02:00
|
|
|
#include "common/printing.h"
|
2022-10-08 22:48:13 +08:00
|
|
|
#include "detection/localip/localip.h"
|
2021-10-17 12:42:15 +08:00
|
|
|
|
2022-01-10 20:20:18 +01:00
|
|
|
#define FF_LOCALIP_MODULE_NAME "Local IP"
|
2022-10-08 22:48:13 +08:00
|
|
|
#define FF_LOCALIP_NUM_FORMAT_ARGS 2
|
2021-10-17 12:42:15 +08:00
|
|
|
|
2023-02-02 17:50:22 +08:00
|
|
|
static int sortIpsV4First(const FFLocalIpResult* left, const FFLocalIpResult* right)
|
|
|
|
|
{
|
2023-02-02 22:25:31 +08:00
|
|
|
int name = ffStrbufComp(&left->name, &right->name);
|
|
|
|
|
if (name != 0)
|
|
|
|
|
return name;
|
|
|
|
|
|
2023-02-02 17:50:22 +08:00
|
|
|
if (left->ipv6 != right->ipv6)
|
|
|
|
|
return left->ipv6 == false ? -1 : 1;
|
|
|
|
|
|
|
|
|
|
return ffStrbufComp(&left->addr, &right->addr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int sortIpsV6First(const FFLocalIpResult* left, const FFLocalIpResult* right)
|
|
|
|
|
{
|
2023-02-02 22:25:31 +08:00
|
|
|
int name = ffStrbufComp(&left->name, &right->name);
|
|
|
|
|
if (name != 0)
|
|
|
|
|
return name;
|
|
|
|
|
|
2023-02-02 17:50:22 +08:00
|
|
|
if (left->ipv6 != right->ipv6)
|
|
|
|
|
return left->ipv6 == true ? -1 : 1;
|
|
|
|
|
|
|
|
|
|
return ffStrbufComp(&left->addr, &right->addr);
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-02 22:25:31 +08:00
|
|
|
static void formatKey(const FFinstance* instance, const FFLocalIpResult* ip, FFstrbuf* key)
|
|
|
|
|
{
|
|
|
|
|
if(instance->config.localIP.key.length == 0)
|
|
|
|
|
{
|
|
|
|
|
if(ip->name.length)
|
|
|
|
|
ffStrbufSetF(key, FF_LOCALIP_MODULE_NAME " (%*s)", ip->name.length, ip->name.chars);
|
|
|
|
|
else
|
|
|
|
|
ffStrbufSetS(key, FF_LOCALIP_MODULE_NAME);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ffStrbufClear(key);
|
|
|
|
|
ffParseFormatString(key, &instance->config.localIP.key, 1, (FFformatarg[]){
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &ip->name}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void printIpsOneline(FFlist* ips)
|
|
|
|
|
{
|
|
|
|
|
uint32_t index = 0;
|
|
|
|
|
FF_LIST_FOR_EACH(FFLocalIpResult, ip, *ips)
|
|
|
|
|
{
|
|
|
|
|
if (index++ > 0)
|
|
|
|
|
putchar(' ');
|
|
|
|
|
ffStrbufWriteTo(&ip->addr, stdout);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void printIpsWithKey(FFinstance* instance, FFlist* ips)
|
|
|
|
|
{
|
|
|
|
|
FFLocalIpResult* ip = (FFLocalIpResult*) ffListGet(ips, 0);
|
|
|
|
|
if (instance->config.localIpCompactType == FF_LOCALIP_COMPACT_TYPE_MULTILINE)
|
|
|
|
|
{
|
|
|
|
|
FF_STRBUF_AUTO_DESTROY key;
|
|
|
|
|
ffStrbufInit(&key);
|
|
|
|
|
formatKey(instance, ip, &key);
|
|
|
|
|
ffPrintLogoAndKey(instance, key.chars, 0, NULL);
|
|
|
|
|
}
|
|
|
|
|
printIpsOneline(ips);
|
|
|
|
|
if (instance->config.localIpCompactType == FF_LOCALIP_COMPACT_TYPE_ONELINE)
|
|
|
|
|
printf(" (%s) ", ip->name.chars);
|
|
|
|
|
else
|
|
|
|
|
putchar('\n');
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-08 22:48:13 +08:00
|
|
|
void ffPrintLocalIp(FFinstance* instance)
|
2021-10-17 12:42:15 +08:00
|
|
|
{
|
2023-02-02 17:50:22 +08:00
|
|
|
FF_LIST_AUTO_DESTROY results;
|
2022-10-08 22:48:13 +08:00
|
|
|
ffListInit(&results, sizeof(FFLocalIpResult));
|
2021-10-17 12:42:15 +08:00
|
|
|
|
2022-10-08 22:48:13 +08:00
|
|
|
const char* error = ffDetectLocalIps(instance, &results);
|
2021-10-17 12:42:15 +08:00
|
|
|
|
2022-10-08 22:48:13 +08:00
|
|
|
if(error)
|
2022-06-17 18:15:36 +02:00
|
|
|
{
|
2022-10-08 22:48:13 +08:00
|
|
|
ffPrintError(instance, FF_LOCALIP_MODULE_NAME, 0, &instance->config.localIP, "%s", error);
|
2023-02-02 17:50:22 +08:00
|
|
|
return;
|
2021-10-17 12:42:15 +08:00
|
|
|
}
|
|
|
|
|
|
2022-10-08 22:48:13 +08:00
|
|
|
if(results.length == 0)
|
|
|
|
|
{
|
|
|
|
|
ffPrintError(instance, FF_LOCALIP_MODULE_NAME, 0, &instance->config.localIP, "Failed to detect any IPs");
|
2023-02-02 17:50:22 +08:00
|
|
|
return;
|
2021-10-17 12:42:15 +08:00
|
|
|
}
|
|
|
|
|
|
2023-02-02 22:25:31 +08:00
|
|
|
ffListSort(&results, instance->config.localIpV6First ? (void*) sortIpsV6First : (void*) sortIpsV4First);
|
|
|
|
|
|
2023-02-02 17:50:22 +08:00
|
|
|
if (instance->config.localIpCompactType != FF_LOCALIP_COMPACT_TYPE_NONE)
|
2022-10-08 22:48:13 +08:00
|
|
|
{
|
2023-02-02 22:25:31 +08:00
|
|
|
if (instance->config.localIpCompactType == FF_LOCALIP_COMPACT_TYPE_ONELINE)
|
|
|
|
|
ffPrintLogoAndKey(instance, FF_LOCALIP_MODULE_NAME, 0, &instance->config.localIP.key);
|
2021-10-17 12:42:15 +08:00
|
|
|
|
2023-02-02 22:25:31 +08:00
|
|
|
FF_LIST_AUTO_DESTROY ips;
|
|
|
|
|
ffListInit(&ips, sizeof(FFLocalIpResult)); // weak references
|
2021-10-17 12:42:15 +08:00
|
|
|
|
2023-02-02 17:50:22 +08:00
|
|
|
FF_LIST_FOR_EACH(FFLocalIpResult, ip, results)
|
2022-10-08 22:48:13 +08:00
|
|
|
{
|
2023-02-02 22:25:31 +08:00
|
|
|
if (ips.length > 0 && !ffStrbufEqual(&ip->name, &ip[-1].name))
|
|
|
|
|
{
|
|
|
|
|
printIpsWithKey(instance, &ips);
|
|
|
|
|
ips.length = 0;
|
|
|
|
|
}
|
|
|
|
|
*(FFLocalIpResult*)ffListAdd(&ips) = *ip;
|
2021-10-17 12:42:15 +08:00
|
|
|
}
|
2023-02-02 22:25:31 +08:00
|
|
|
printIpsWithKey(instance, &ips);
|
2021-10-17 12:42:15 +08:00
|
|
|
}
|
2023-02-02 17:50:22 +08:00
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
FF_STRBUF_AUTO_DESTROY key;
|
|
|
|
|
ffStrbufInit(&key);
|
2021-10-17 12:42:15 +08:00
|
|
|
|
2023-02-02 17:50:22 +08:00
|
|
|
FF_LIST_FOR_EACH(FFLocalIpResult, ip, results)
|
|
|
|
|
{
|
2023-02-02 22:25:31 +08:00
|
|
|
formatKey(instance, ip, &key);
|
2023-02-02 17:50:22 +08:00
|
|
|
if(instance->config.localIP.outputFormat.length == 0)
|
|
|
|
|
{
|
|
|
|
|
ffPrintLogoAndKey(instance, key.chars, 0, NULL);
|
|
|
|
|
ffStrbufPutTo(&ip->addr, stdout);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ffPrintFormatString(instance, key.chars, 0, NULL, &instance->config.localIP.outputFormat, FF_LOCALIP_NUM_FORMAT_ARGS, (FFformatarg[]){
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &ip->addr},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_STRING, ip->ipv6 ? "IPv6" : "IPv4"}
|
|
|
|
|
});
|
|
|
|
|
}
|
2022-10-08 22:48:13 +08:00
|
|
|
|
2023-02-02 17:50:22 +08:00
|
|
|
ffStrbufDestroy(&ip->name);
|
|
|
|
|
ffStrbufDestroy(&ip->addr);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-10-17 12:42:15 +08:00
|
|
|
}
|