changed camouflaging scheme

This commit is contained in:
eerieaerial 2025-05-14 22:26:08 +04:00
parent 235addc585
commit 98ccedecac
5 changed files with 235 additions and 319 deletions

View file

@ -9,39 +9,34 @@
* On the date above, in accordance with the Business Source License, use
* of this software will be governed by version 2.0 of the Apache License.
*/
/****/
#include "CamoPattern.hpp"
#include <ctime>
#include "RuntimeEnvironment.hpp"
#include "Topology.hpp"
namespace ZeroTier {
// Initialize static members of CamoPattern
bool CamoPattern::isInitialized = false;
CamoLevel CamoPattern::camoLevel;
uint32_t CamoPattern::camoWord;
CamoRelayRule CamoPattern::relayRule;
CamoPatternArray CamoPattern::camoValues;
CamoIndexMap CamoPattern::camoIndices;
std::mutex CamoPattern::camoMutex;
KnownHostsMap CamoPattern::knownHosts;
CamoAutoApplyBits CamoPattern::camoAutoApply;
std::mt19937 CamoPattern::rng(std::random_device{}());
// Implementation of getCamoLevel
CamoLevel CamoPattern::getCamoLevel(const Address host, const RuntimeEnvironment * const RR)
CamoClass CamoPattern::getCamoClass(const Address host, const RuntimeEnvironment * const RR)
{
CamoLevel result = CamoLevel::INAPPLICABLE;
// First check if we already know this host's camo level
CamoClass result = CamoClass::NEVER;
if (isInitialized)
{
char buf[64];
host.toString(buf);
CT("GETTING CAMO LEVEL FOR HOST %s", buf);
CT("GETTING CAMO CLASS FOR HOST %s", buf);
auto it = knownHosts.find(host);
if (it != knownHosts.end()) {
result = it->second;
CT("HOST IS KNOWN, LEVEL: %u", result);
CT("HOST IS KNOWN, CLASS: %u", result);
}
else
{
@ -52,48 +47,40 @@ CamoLevel CamoPattern::getCamoLevel(const Address host, const RuntimeEnvironment
it = knownHosts.find(host);
if (it != knownHosts.end()) {
result = it->second;
CT("HOST IS KNOWN AFTER LOCK WAITING");
CT("HOST IS KNOWN AFTER LOCK WAITING, CLASS: %u", result);
}
else
{
CT("HOST IS NOT KNOWN");
if (!RR->topology->isProhibitedEndpoint(host, InetAddress()))
switch(RR->topology->role(host))
{
switch(RR->topology->role(host))
{
case ZT_PEER_ROLE_PLANET:
CT("HOST IS A PLANET");
result = CamoLevel::PLANET;
break;
case ZT_PEER_ROLE_MOON:
CT("HOST IS A MOON");
result = CamoLevel::MOON;
break;
default:
result = CamoLevel::NODE;
Mutex::Lock _l(RR->node->_networks_m);
Hashtable<uint64_t, SharedPtr<Network>>::Iterator i(RR->node->_networks);
uint64_t * k = (uint64_t *)0;
SharedPtr<Network> *v = (SharedPtr<Network> *)0;
while(i.next(k, v))
case ZT_PEER_ROLE_PLANET:
CT("HOST IS A PLANET");
break;
case ZT_PEER_ROLE_MOON:
CT("HOST IS A MOON");
result = CamoClass::MOON;
break;
default:
result = CamoClass::NODE;
Mutex::Lock _l(RR->node->_networks_m);
Hashtable<uint64_t, SharedPtr<Network>>::Iterator i(RR->node->_networks);
uint64_t * k = (uint64_t *)0;
SharedPtr<Network> *v = (SharedPtr<Network> *)0;
while(i.next(k, v))
{
if (host == ((*v)->controller()))
{
if (host == ((*v)->controller()))
{
CT("HOST IS A CONTROLLER");
result = CamoLevel::CONTROLLER;
break;
}
CT("HOST IS A CONTROLLER");
result = CamoClass::CONTROLLER;
break;
}
if (result == CamoLevel::NODE)
{
CT("HOST IS A SIMPLE NODE");
}
break;
}
}
else
{
CT("HOST IS A ZT GLOBAL ROOT");
}
if (result == CamoClass::NODE)
{
CT("HOST IS A SIMPLE NODE");
}
break;
}
knownHosts[host] = result;
}
@ -102,10 +89,18 @@ CamoLevel CamoPattern::getCamoLevel(const Address host, const RuntimeEnvironment
return result;
}
// Implementation of isCamoRequired
// Implementation of isCamoRequired - determines if camouflage should be applied based on host and rules
bool CamoPattern::isCamoRequired(const Address host, const RuntimeEnvironment * const RR, const bool hadCamo, const bool isRelay)
{
bool result = false;
auto isRequiredByClass = [](const Address host, const RuntimeEnvironment * const RR) -> bool {
CamoClass camoClass = getCamoClass(host, RR);
return camoClass < CamoClass::AUTO_APPLY_COUNT ?
camoAutoApply[camoClass] :
camoClass == CamoClass::ALWAYS;
};
if (isInitialized && isRelay)
{
switch(relayRule)
@ -116,7 +111,7 @@ bool CamoPattern::isCamoRequired(const Address host, const RuntimeEnvironment *
break;
case CamoRelayRule::KNOWNHOSTS:
CT("IS RELAY, APPLYING KNOWNHOSTS RULE");
result = getCamoLevel(host, RR) <= camoLevel;
result = isRequiredByClass(host, RR);
break;
case CamoRelayRule::STRIP:
CT("IS RELAY, APPLYING STRIP RULE");
@ -130,39 +125,24 @@ bool CamoPattern::isCamoRequired(const Address host, const RuntimeEnvironment *
}
else if (isInitialized)
{
result = getCamoLevel(host, RR) <= camoLevel;
result = isRequiredByClass(host, RR);
CT("IS CAMO REQUIRED: %b", result);
}
return result;
}
// Implementation of init
void CamoPattern::init(CamoLevel level, uint32_t word, KnownHostsMap hosts, CamoRelayRule rule)
// Implementation of init - initializes the camouflage system with the specified settings
void CamoPattern::init(CamoAutoApplyBits autoApply, KnownHostsMap hosts, CamoRelayRule rule)
{
std::lock_guard<std::mutex> lock(camoMutex);
if (!isInitialized)
{
camoLevel = level;
camoWord = word;
camoAutoApply = autoApply;
knownHosts = hosts;
relayRule = rule;
CT("CAMO LEVEL: %u, WORD: %08x, KNOWN HOSTS COUNT: %lu, RELAY RULE: %u", level, word, hosts.size(), rule);
std::mt19937 rng(camoWord);
for (size_t i = 0; i < PATTERN_COUNT; i++)
{
uint32_t random = rng();
CT("CAMO INDEX: %lu, VALUE: %08x", i, random);
for (size_t j = 0; j < BYTES_IN_WORD; j++)
{
camoValues[i][j] = (random >> (j * 8)) & 0xff;
}
camoIndices[camoValues[i]] = i;
}
CT("KNOWN HOSTS COUNT: %lu, RELAY RULE: %u", hosts.size(), rule);
isInitialized = true;
}
}
} // namespace ZeroTier