Merge branch 'main' of ssh://anscenter.ddns.net:2222/anscenter/ANSCORE

# Conflicts:
#	core/anslicensing/hwid.cpp
This commit is contained in:
2026-08-21 10:31:45 +10:00
7 changed files with 720 additions and 226 deletions
+83 -6
View File
@@ -21,9 +21,10 @@
#include <net/if.h>
#ifdef __linux__
# include <sys/ioctl.h>
# include <sys/stat.h> // stat() — physical-NIC test in linuxIsPhysicalNic
# include <netinet/in.h>
# include <unistd.h>
# include <string.h>
# include <string.h> // strncmp/strlen (same test)
// Match Linux to FreeBSD
# define AF_LINK AF_PACKET
@@ -82,6 +83,47 @@ void GetCPUIDPropertyList(std::list<std::string>* propList)
propList->push_back(buf);
}
#ifdef __linux__
// A PHYSICAL network interface, i.e. one backed by a real hardware device.
//
// Why this exists: the hardware id is built from up to 6 enumerated MACs, and
// on Linux getifaddrs() also returns VIRTUAL interfaces — docker0 and the
// veth* pairs a container creates, tun*/wg* from a VPN, br-*/virbr* bridges.
// Container veths get RANDOM MACs and come and go with the containers, so an
// unfiltered enumeration can hand back a different set on every call: the id
// changes and an activated licence silently stops matching its machine. Boxes
// running Docker (the Coder feature) hit this routinely.
//
// The test mirrors what the Windows branch already does via WMI ("PNPDeviceId
// LIKE %PCI%" — physical adapters only). /sys/class/net/<name>/device is a
// symlink to the backing device and exists ONLY for hardware-backed
// interfaces; virtual ones have no such link. That is the kernel's own answer,
// so it stays correct as new virtual-interface flavours appear — unlike a name
// denylist, which is kept only as a cheap second line of defence.
static bool linuxIsPhysicalNic(const char* ifname)
{
if (ifname == NULL || ifname[0] == '\0') return false;
// Names that are virtual by construction. Not exhaustive on purpose — the
// /sys check below is the real test; this only catches oddities where /sys
// is unavailable (unusual containers, minimal namespaces).
static const char* kVirtualPrefixes[] = {
"lo", "docker", "veth", "br-", "virbr", "tun", "tap", "wg",
"vmnet", "vboxnet", "zt", "ppp", "dummy", "kube", "cni", "flannel",
"lxc", "lxd", "nerdctl", "podman", "cali", "weave"
};
for (const char* prefix : kVirtualPrefixes) {
if (strncmp(ifname, prefix, strlen(prefix)) == 0) return false;
}
std::string path = std::string("/sys/class/net/") + ifname + "/device";
struct stat st;
// stat() (not lstat) so the symlink is followed — we want "the target
// exists", i.e. a real backing device.
return stat(path.c_str(), &st) == 0;
}
#endif // __linux__
void GetMACPropertyList(std::list<std::string>* propList, int maxCount)
{
// Head of the interface address linked list
@@ -99,6 +141,14 @@ void GetMACPropertyList(std::list<std::string>* propList, int maxCount)
return;
}
#ifdef __linux__
// Collected separately so physical NICs can be preferred wholesale. Taking
// them inline would let a veth that merely appears EARLIER in the list
// consume one of the maxCount slots and push a real NIC out.
std::list<std::string> physical;
std::list<std::string> fallback;
#endif
while (current != NULL && maxCount > 0) {
const sockaddr_in* interfaceAddress = castToIP4(current->ifa_addr);
@@ -127,8 +177,18 @@ void GetMACPropertyList(std::list<std::string>* propList, int maxCount)
{
char buf[18];
sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x", MAC[0], MAC[1], MAC[2], MAC[3], MAC[4], MAC[5]);
#ifdef __linux__
// Bucket now, choose after the walk (see the lists above).
// maxCount is NOT decremented here: the cap applies to the
// SELECTED set, not to whatever happened to be enumerated.
if (linuxIsPhysicalNic(current->ifa_name))
physical.push_back(buf);
else
fallback.push_back(buf);
#else
propList->push_back(buf);
maxCount--;
#endif
}
}
@@ -137,6 +197,21 @@ void GetMACPropertyList(std::list<std::string>* propList, int maxCount)
freeifaddrs(ifap);
ifap = NULL;
#ifdef __linux__
// Physical NICs only — that is the whole point. FAIL-SAFE: if the filter
// found none (no /sys, an exotic NIC driver, a network namespace with only
// virtual links) fall back to the unfiltered set rather than returning
// nothing. An empty list would drop ALL MAC entropy from the id, and with
// the system-UUID and disk-serial slots already constant on Linux that
// would leave near-identical ids across different machines — far worse
// than an id that occasionally drifts.
std::list<std::string>& chosen = physical.empty() ? fallback : physical;
for (std::list<std::string>::const_iterator it = chosen.begin();
it != chosen.end() && maxCount > 0; ++it, --maxCount) {
propList->push_back(*it);
}
#endif
}
void GetHDDPropertyList(std::list<std::string>* propList, int maxCount)
@@ -220,11 +295,13 @@ const char* HardwareId::GetCurrentHardwareId()
WmiHelper wmi;
#endif
// 3 version bits + 9 x 16-bit hashes = 147 bits are written below, but only
// the first 16 bytes are base32-encoded into the HWID (the HDD fields are
// truncated by the 25-char format). The stream buffer must still hold every
// write: 125 bits (16 bytes) overflowed the allocation by 3 bytes.
BitStream3 bits(160);
// 147 bits are actually written below (3 version + 9 x 16-bit hashes). The
// previous size of 125 allocated only 16 bytes and the last two hash slots
// overflowed the heap block by 3 bytes (asymptomatic on the Windows heap,
// SIGSEGV under macOS malloc — found when ANSGateway enabled this code on
// POSIX). Only the first 16 bytes feed the base32 encode, so sizing the
// stream correctly changes no id on any platform.
BitStream3 bits(147);
list<string> propList;
unsigned char version = 1;
unsigned short hash1, hash2, hash3, hash4;