mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-09-13 02:42:09 +02:00
DNS: add option --dns-show-type
This commit is contained in:
@@ -705,6 +705,7 @@
|
||||
"disk",
|
||||
"diskio",
|
||||
"de",
|
||||
"dns",
|
||||
"editor",
|
||||
"font",
|
||||
"gamepad",
|
||||
@@ -1434,6 +1435,41 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"title": "DNS",
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"properties": {
|
||||
"type": {
|
||||
"const": "dns",
|
||||
"description": "Print DNS servers"
|
||||
},
|
||||
"showType": {
|
||||
"enum": [
|
||||
"ipv4",
|
||||
"ipv6",
|
||||
"both"
|
||||
],
|
||||
"default": "both",
|
||||
"description": "Specify the type of DNS servers should be detected"
|
||||
},
|
||||
"key": {
|
||||
"$ref": "#/$defs/key"
|
||||
},
|
||||
"keyColor": {
|
||||
"$ref": "#/$defs/keyColor"
|
||||
},
|
||||
"outputColor": {
|
||||
"$ref": "#/$defs/outputColor"
|
||||
},
|
||||
"keyWidth": {
|
||||
"$ref": "#/$defs/keyWidth"
|
||||
},
|
||||
"format": {
|
||||
"$ref": "#/$defs/format"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"title": "Gamepad",
|
||||
"type": "object",
|
||||
|
||||
@@ -1367,6 +1367,19 @@
|
||||
"default": false
|
||||
}
|
||||
},
|
||||
{
|
||||
"long": "dns-show-type",
|
||||
"desc": "Specify the type of DNS servers should be detected",
|
||||
"arg": {
|
||||
"type": "enum",
|
||||
"enum": {
|
||||
"ipv4": "Show IPv4 addresses only",
|
||||
"ipv6": "Show IPv6 addresses only",
|
||||
"both": "Show both IPv4 and IPv6 addresses"
|
||||
},
|
||||
"default": "both"
|
||||
}
|
||||
},
|
||||
{
|
||||
"long": "netio-name-prefix",
|
||||
"desc": "Show interfaces with given name prefix only",
|
||||
|
||||
@@ -2,4 +2,4 @@
|
||||
|
||||
#include "fastfetch.h"
|
||||
|
||||
const char* ffDetectDNS(FFlist* results /* list of FFstrbuf */);
|
||||
const char* ffDetectDNS(FFDNSOptions* options, FFlist* results /* list of FFstrbuf */);
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
#include "util/mallocHelper.h"
|
||||
#include "util/stringUtils.h"
|
||||
|
||||
const char* ffDetectDNS(FFlist* results)
|
||||
const char* ffDetectDNS(FFDNSOptions* options, FFlist* results)
|
||||
{
|
||||
FF_AUTO_CLOSE_FILE FILE* file = fopen(FASTFETCH_TARGET_DIR_ROOT "/etc/resolv.conf", "r");
|
||||
if (!file)
|
||||
@@ -15,12 +15,20 @@ const char* ffDetectDNS(FFlist* results)
|
||||
|
||||
while (getline(&line, &len, file) != -1)
|
||||
{
|
||||
if (ffStrStartsWith(line, "nameserver "))
|
||||
if (ffStrStartsWith(line, "nameserver"))
|
||||
{
|
||||
const char* nameserver = line + strlen("nameserver");
|
||||
while (*nameserver == ' ' || *nameserver == '\t')
|
||||
nameserver++;
|
||||
if (*nameserver == '\0') continue;
|
||||
|
||||
if ((ffStrContainsC(nameserver, ':') && !(options->showType & FF_DNS_TYPE_IPV6_BIT)) ||
|
||||
(ffStrContainsC(nameserver, '.') && !(options->showType & FF_DNS_TYPE_IPV4_BIT)))
|
||||
continue;
|
||||
|
||||
FFstrbuf* item = (FFstrbuf*) ffListAdd(results);
|
||||
ffStrbufInitS(item, line + strlen("nameserver "));
|
||||
ffStrbufInitS(item, nameserver);
|
||||
ffStrbufTrimRightSpace(item);
|
||||
ffStrbufTrimLeft(item, ' ');
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
|
||||
@@ -1,30 +1,67 @@
|
||||
#include "detection/dns/dns.h"
|
||||
#include "common/netif/netif.h"
|
||||
#include "util/mallocHelper.h"
|
||||
|
||||
#include <ws2tcpip.h>
|
||||
#include <iphlpapi.h>
|
||||
|
||||
const char* ffDetectDNS(FFlist* results)
|
||||
const char* ffDetectDNS(FFDNSOptions* options, FFlist* results)
|
||||
{
|
||||
FF_AUTO_FREE FIXED_INFO* fixedInfo = malloc(sizeof(FIXED_INFO));
|
||||
ULONG size = sizeof(fixedInfo);
|
||||
while (true)
|
||||
IP_ADAPTER_ADDRESSES* FF_AUTO_FREE adapter_addresses = NULL;
|
||||
|
||||
// Multiple attempts in case interfaces change while
|
||||
// we are in the middle of querying them.
|
||||
DWORD adapter_addresses_buffer_size = 0;
|
||||
for (int attempts = 0;; ++attempts)
|
||||
{
|
||||
DWORD res = GetNetworkParams(fixedInfo, &size);
|
||||
if (res == ERROR_BUFFER_OVERFLOW)
|
||||
if (adapter_addresses_buffer_size)
|
||||
{
|
||||
fixedInfo = realloc(fixedInfo, size);
|
||||
continue;
|
||||
adapter_addresses = (IP_ADAPTER_ADDRESSES*)realloc(adapter_addresses, adapter_addresses_buffer_size);
|
||||
assert(adapter_addresses);
|
||||
}
|
||||
else if (res != ERROR_SUCCESS)
|
||||
return "GetNetworkParams() failed";
|
||||
break;
|
||||
|
||||
DWORD error = GetAdaptersAddresses(
|
||||
options->showType & FF_DNS_TYPE_IPV4_BIT
|
||||
? options->showType & FF_DNS_TYPE_IPV6_BIT ? AF_UNSPEC : AF_INET
|
||||
: AF_INET6,
|
||||
GAA_FLAG_SKIP_UNICAST | GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_FRIENDLY_NAME,
|
||||
NULL,
|
||||
adapter_addresses,
|
||||
&adapter_addresses_buffer_size);
|
||||
|
||||
if (error == ERROR_SUCCESS)
|
||||
break;
|
||||
else if (ERROR_BUFFER_OVERFLOW == error && attempts < 4)
|
||||
continue;
|
||||
else
|
||||
return "GetAdaptersAddresses() failed";
|
||||
}
|
||||
|
||||
for (IP_ADDR_STRING* dns = &fixedInfo->DnsServerList; dns; dns = dns->Next)
|
||||
uint32_t defaultRouteIfIndex = ffNetifGetDefaultRouteIfIndex();
|
||||
// Iterate through all of the adapters
|
||||
for (IP_ADAPTER_ADDRESSES* adapter = adapter_addresses; adapter; adapter = adapter->Next)
|
||||
{
|
||||
FFstrbuf* item = (FFstrbuf*) ffListAdd(results);
|
||||
ffStrbufInitS(item, dns->IpAddress.String);
|
||||
ffStrbufTrimRightSpace(item);
|
||||
if (adapter->IfIndex != defaultRouteIfIndex) continue;
|
||||
if (adapter->OperStatus != IfOperStatusUp) continue;
|
||||
|
||||
for (IP_ADAPTER_DNS_SERVER_ADDRESS_XP * ifa = adapter->FirstDnsServerAddress; ifa; ifa = ifa->Next)
|
||||
{
|
||||
FFstrbuf* item = (FFstrbuf*) ffListAdd(results);
|
||||
if (ifa->Address.lpSockaddr->sa_family == AF_INET)
|
||||
{
|
||||
SOCKADDR_IN* ipv4 = (SOCKADDR_IN*) ifa->Address.lpSockaddr;
|
||||
ffStrbufInitA(item, INET_ADDRSTRLEN);
|
||||
inet_ntop(AF_INET, &ipv4->sin_addr, item->chars, item->allocated);
|
||||
}
|
||||
else if (ifa->Address.lpSockaddr->sa_family == AF_INET6)
|
||||
{
|
||||
SOCKADDR_IN6* ipv6 = (SOCKADDR_IN6*) ifa->Address.lpSockaddr;
|
||||
ffStrbufInitA(item, INET6_ADDRSTRLEN);
|
||||
inet_ntop(AF_INET6, &ipv6->sin6_addr, item->chars, item->allocated);
|
||||
}
|
||||
ffStrbufRecalculateLength(item);
|
||||
}
|
||||
break;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
+47
-2
@@ -10,7 +10,7 @@ void ffPrintDNS(FFDNSOptions* options)
|
||||
{
|
||||
FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFstrbuf));
|
||||
|
||||
const char* error = ffDetectDNS(&result);
|
||||
const char* error = ffDetectDNS(options, &result);
|
||||
|
||||
if (error)
|
||||
{
|
||||
@@ -66,6 +66,17 @@ bool ffParseDNSCommandOptions(FFDNSOptions* options, const char* key, const char
|
||||
if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs))
|
||||
return true;
|
||||
|
||||
if (ffStrEqualsIgnCase(subKey, "show-type"))
|
||||
{
|
||||
options->showType = (FFDNSShowType) ffOptionParseEnum(key, value, (FFKeyValuePair[]) {
|
||||
{ "both", FF_DNS_TYPE_BOTH },
|
||||
{ "ipv4", FF_DNS_TYPE_IPV4_BIT },
|
||||
{ "ipv6", FF_DNS_TYPE_IPV6_BIT },
|
||||
{},
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -82,6 +93,22 @@ void ffParseDNSJsonObject(FFDNSOptions* options, yyjson_val* module)
|
||||
if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs))
|
||||
continue;
|
||||
|
||||
if (ffStrEqualsIgnCase(key, "showType"))
|
||||
{
|
||||
int value;
|
||||
const char* error = ffJsonConfigParseEnum(val, &value, (FFKeyValuePair[]) {
|
||||
{ "both", FF_DNS_TYPE_BOTH },
|
||||
{ "ipv4", FF_DNS_TYPE_IPV4_BIT },
|
||||
{ "ipv6", FF_DNS_TYPE_IPV6_BIT },
|
||||
{},
|
||||
});
|
||||
if (error)
|
||||
ffPrintError(FF_DNS_MODULE_NAME, 0, NULL, FF_PRINT_TYPE_NO_CUSTOM_KEY, "Invalid %s value: %s", key, error);
|
||||
else
|
||||
options->showType = (FFDNSShowType) value;
|
||||
continue;
|
||||
}
|
||||
|
||||
ffPrintError(FF_DNS_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", key);
|
||||
}
|
||||
}
|
||||
@@ -92,13 +119,29 @@ void ffGenerateDNSJsonConfig(FFDNSOptions* options, yyjson_mut_doc* doc, yyjson_
|
||||
ffInitDNSOptions(&defaultOptions);
|
||||
|
||||
ffJsonConfigGenerateModuleArgsConfig(doc, module, &defaultOptions.moduleArgs, &options->moduleArgs);
|
||||
|
||||
if (defaultOptions.showType != options->showType)
|
||||
{
|
||||
switch (options->showType)
|
||||
{
|
||||
case FF_DNS_TYPE_IPV4_BIT:
|
||||
yyjson_mut_obj_add_str(doc, module, "showType", "ipv4");
|
||||
break;
|
||||
case FF_DNS_TYPE_IPV6_BIT:
|
||||
yyjson_mut_obj_add_str(doc, module, "showType", "ipv6");
|
||||
break;
|
||||
case FF_DNS_TYPE_BOTH:
|
||||
yyjson_mut_obj_add_str(doc, module, "showType", "both");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ffGenerateDNSJsonResult(FF_MAYBE_UNUSED FFDNSOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
|
||||
{
|
||||
FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFstrbuf));
|
||||
|
||||
const char* error = ffDetectDNS(&result);
|
||||
const char* error = ffDetectDNS(options, &result);
|
||||
|
||||
if (error)
|
||||
{
|
||||
@@ -141,6 +184,8 @@ void ffInitDNSOptions(FFDNSOptions* options)
|
||||
ffGenerateDNSJsonConfig
|
||||
);
|
||||
ffOptionInitModuleArg(&options->moduleArgs);
|
||||
|
||||
options->showType = FF_DNS_TYPE_BOTH;
|
||||
}
|
||||
|
||||
void ffDestroyDNSOptions(FFDNSOptions* options)
|
||||
|
||||
@@ -4,8 +4,16 @@
|
||||
|
||||
#include "common/option.h"
|
||||
|
||||
typedef enum FFDNSShowType {
|
||||
FF_DNS_TYPE_IPV4_BIT = 1,
|
||||
FF_DNS_TYPE_IPV6_BIT = 2,
|
||||
FF_DNS_TYPE_BOTH = FF_DNS_TYPE_IPV4_BIT | FF_DNS_TYPE_IPV6_BIT,
|
||||
} FFDNSShowType;
|
||||
|
||||
typedef struct FFDNSOptions
|
||||
{
|
||||
FFModuleBaseInfo moduleInfo;
|
||||
FFModuleArgs moduleArgs;
|
||||
|
||||
FFDNSShowType showType;
|
||||
} FFDNSOptions;
|
||||
|
||||
Reference in New Issue
Block a user