From b0ffeeb3348cf696aec315c3a7ae5d81b88ba675 Mon Sep 17 00:00:00 2001 From: momo5502 Date: Tue, 30 Aug 2016 17:51:30 +0200 Subject: [PATCH] Obfuscate default nodes --- deps/mongoose | 2 +- deps/protobuf | 2 +- src/Components/Modules/Node.cpp | 46 ++++++++++++++++++++++++--------- src/Utils/Compression.cpp | 12 ++++++--- 4 files changed, 44 insertions(+), 18 deletions(-) diff --git a/deps/mongoose b/deps/mongoose index b3fb21da..844c7787 160000 --- a/deps/mongoose +++ b/deps/mongoose @@ -1 +1 @@ -Subproject commit b3fb21dacccfb08b046b73740eec52cd66e944de +Subproject commit 844c7787f9d86715c80ab56f4f28d1b808df5341 diff --git a/deps/protobuf b/deps/protobuf index 3d9d1a12..e721ce66 160000 --- a/deps/protobuf +++ b/deps/protobuf @@ -1 +1 @@ -Subproject commit 3d9d1a1255583bac550f7bf94f3016e8c238fa5e +Subproject commit e721ce66cfeaa5d8790ecba09c73d1ef399887d2 diff --git a/src/Components/Modules/Node.cpp b/src/Components/Modules/Node.cpp index 2fcd22a5..7c6e8e6f 100644 --- a/src/Components/Modules/Node.cpp +++ b/src/Components/Modules/Node.cpp @@ -8,19 +8,13 @@ namespace Components void Node::LoadNodePreset() { + Proto::Node::List list; FileSystem::File defaultNodes("nodes_default.dat"); - if (!defaultNodes.Exists()) return; + if (!defaultNodes.Exists() || !list.ParseFromString(Utils::Compression::ZLib::Decompress(defaultNodes.GetBuffer()))) return; - auto buffer = defaultNodes.GetBuffer(); - Utils::String::Replace(buffer, "\r", ""); - - auto nodes = Utils::String::Explode(buffer, '\n'); - for (auto node : nodes) + for (int i = 0; i < list.address_size(); ++i) { - if (!node.empty()) - { - Node::AddNode(node); - } + Node::AddNode(list.address(i)); } } @@ -28,7 +22,7 @@ namespace Components { Proto::Node::List list; std::string nodes = Utils::IO::ReadFile("players/nodes.dat"); - if (nodes.empty() || !list.ParseFromString(nodes)) return; + if (nodes.empty() || !list.ParseFromString(Utils::Compression::ZLib::Decompress(nodes))) return; for (int i = 0; i < list.address_size(); ++i) { @@ -60,7 +54,10 @@ namespace Components } CreateDirectoryW(L"players", NULL); - Utils::IO::WriteFile("players/nodes.dat", list.SerializeAsString()); + + + + Utils::IO::WriteFile("players/nodes.dat", Utils::Compression::ZLib::Compress(list.SerializeAsString())); } Node::NodeEntry* Node::FindNode(Network::Address address) @@ -907,6 +904,31 @@ namespace Components auto duration = std::chrono::duration_cast(std::chrono::high_resolution_clock::now() - startTime).count(); Logger::Print("took %llims\n", duration); + printf("Testing ZLib compression..."); + + std::string test = fmt::sprintf("%c", Utils::Cryptography::Rand::GenerateInt()); + + for (int i = 0; i < 21; ++i) + { + std::string compressed = Utils::Compression::ZLib::Compress(test); + std::string decompressed = Utils::Compression::ZLib::Decompress(compressed); + + if (test != decompressed) + { + printf("Error\n"); + printf("Compressing %d bytes and decompressing failed!\n", test.size()); + return false; + } + + auto size = test.size(); + for (unsigned int j = 0; j < size; ++j) + { + test.append(fmt::sprintf("%c", Utils::Cryptography::Rand::GenerateInt())); + } + } + + printf("Success\n"); + return true; } } diff --git a/src/Utils/Compression.cpp b/src/Utils/Compression.cpp index 02c85351..4eec358d 100644 --- a/src/Utils/Compression.cpp +++ b/src/Utils/Compression.cpp @@ -6,8 +6,13 @@ namespace Utils { std::string ZLib::Compress(std::string data) { + Utils::Memory::Allocator allocator; unsigned long length = (data.size() * 2); - char* buffer = Utils::Memory::AllocateArray(length); + + // Make sure the buffer is large enough + if (length < 100) length *= 10; + + char* buffer = allocator.AllocateArray(length); if (compress2(reinterpret_cast(buffer), &length, reinterpret_cast(const_cast(data.data())), data.size(), Z_BEST_COMPRESSION) != Z_OK) { @@ -18,8 +23,6 @@ namespace Utils data.clear(); data.append(buffer, length); - Utils::Memory::Free(buffer); - return data; } @@ -44,6 +47,7 @@ namespace Utils { stream.avail_in = std::min(static_cast(CHUNK), data.size() - (dataPtr - data.data())); stream.next_in = reinterpret_cast(dataPtr); + dataPtr += stream.avail_in; do { @@ -51,7 +55,7 @@ namespace Utils stream.next_out = dest; ret = inflate(&stream, Z_NO_FLUSH); - if (ret == Z_STREAM_ERROR) + if (ret != Z_OK && ret != Z_STREAM_END) { inflateEnd(&stream); return "";