mirror of https://github.com/infosecn1nja/C3.git
Send the snapshot over ApiBridge only if it changed
parent
08c3087aa7
commit
686bbdeb9c
|
@ -168,18 +168,23 @@ void MWR::C3::Core::GateRelay::RunApiBrige(std::string_view apiBrigdeIp, std::ui
|
||||||
Log({ "API bridge connection established on " + std::string{apiBrigdeIp} +':' + std::to_string(apiBrigdePort), MWR::C3::LogMessage::Severity::Information });
|
Log({ "API bridge connection established on " + std::string{apiBrigdeIp} +':' + std::to_string(apiBrigdePort), MWR::C3::LogMessage::Severity::Information });
|
||||||
reconnectWait = 0s;
|
reconnectWait = 0s;
|
||||||
// Enter main loop.
|
// Enter main loop.
|
||||||
|
auto sp = m_Profiler->GetSnapshotProxy();
|
||||||
while (m_IsAlive && connection.IsSending())
|
while (m_IsAlive && connection.IsSending())
|
||||||
{
|
{
|
||||||
// Read socket.
|
// Read socket.
|
||||||
std::this_thread::sleep_for(300ms);
|
std::this_thread::sleep_for(300ms);
|
||||||
try
|
auto newSnapshot = sp.GetSnapshotIfChanged();
|
||||||
|
if (newSnapshot)
|
||||||
{
|
{
|
||||||
connection.Send(Crypto::Encrypt(ByteView{ json{ { "messageType", "GetProfile" }, { "messageData", m_Profiler->Get().m_Gateway.CreateProfileSnapshot() }}.dump() }, m_SessionKeys.second));
|
try
|
||||||
}
|
{
|
||||||
catch (std::exception& exception)
|
connection.Send(Crypto::Encrypt(ByteView{ json{ { "messageType", "GetProfile" }, { "messageData", *newSnapshot }}.dump() }, m_SessionKeys.second));
|
||||||
{
|
}
|
||||||
Log({ "Caught an exception while sending Profile. "s + exception.what(), MWR::C3::LogMessage::Severity::Error });
|
catch (std::exception& exception)
|
||||||
break;
|
{
|
||||||
|
Log({ "Caught an exception while sending Profile. "s + exception.what(), MWR::C3::LogMessage::Severity::Error });
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -172,18 +172,16 @@ MWR::ByteVector MWR::C3::Core::Profiler::Translate(std::string const& type, json
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
void MWR::C3::Core::Profiler::DumpSnapshots()
|
void MWR::C3::Core::Profiler::DumpSnapshots()
|
||||||
{
|
{
|
||||||
std::optional<std::size_t> oldHash;
|
auto sp = GetSnapshotProxy();
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
const auto snapshotTmpPath = std::filesystem::path(m_SnapshotPath).replace_extension(".tmp");
|
const auto newSnapshot = sp.GetSnapshotIfChanged();
|
||||||
const auto snapshot = Get().m_Gateway.CreateProfileSnapshot().dump(4);
|
if (newSnapshot)
|
||||||
const auto snapshotHash = std::hash<std::string>{}(snapshot);
|
|
||||||
if (!oldHash || *oldHash != snapshotHash)
|
|
||||||
{
|
{
|
||||||
oldHash = snapshotHash;
|
const auto snapshotTmpPath = std::filesystem::path(m_SnapshotPath).replace_extension(".tmp");
|
||||||
{
|
{
|
||||||
std::ofstream snapshotTmp{ snapshotTmpPath };
|
std::ofstream snapshotTmp{ snapshotTmpPath };
|
||||||
snapshotTmp << snapshot << std::endl;
|
snapshotTmp << newSnapshot->dump(4) << std::endl;
|
||||||
}
|
}
|
||||||
std::filesystem::rename(snapshotTmpPath, m_SnapshotPath);
|
std::filesystem::rename(snapshotTmpPath, m_SnapshotPath);
|
||||||
}
|
}
|
||||||
|
|
|
@ -536,6 +536,42 @@ namespace MWR::C3::Core
|
||||||
/// @returns binder id
|
/// @returns binder id
|
||||||
uint32_t GetBinderTo(uint32_t);
|
uint32_t GetBinderTo(uint32_t);
|
||||||
|
|
||||||
|
/// Helper to wrap calls to CreateProfileShnapshot
|
||||||
|
class SnapshotProxy
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
/// Create a snapshot proxy
|
||||||
|
/// @param profiler to wrap CreateProfileShnapshot calls
|
||||||
|
SnapshotProxy(Profiler& profiler) :
|
||||||
|
m_Profiler(profiler)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create a snapshot
|
||||||
|
/// @return std::nullopt if snaphot hasn't change since the last call
|
||||||
|
std::optional<json> GetSnapshotIfChanged()
|
||||||
|
{
|
||||||
|
auto currentSnapshot = m_Profiler.Get().m_Gateway.CreateProfileSnapshot();
|
||||||
|
auto currentHash = std::hash<json>{}(currentSnapshot);
|
||||||
|
if (previousHash && currentHash == *previousHash)
|
||||||
|
return {};
|
||||||
|
|
||||||
|
previousHash = currentHash;
|
||||||
|
return currentSnapshot;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
/// Proxied profiler
|
||||||
|
Profiler& m_Profiler;
|
||||||
|
|
||||||
|
/// helper state variable
|
||||||
|
std::optional<size_t> previousHash;
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Create Snapshot proxy for this profiler
|
||||||
|
/// @returns snapshot proxy for this profiler
|
||||||
|
SnapshotProxy GetSnapshotProxy() { return SnapshotProxy(*this); }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
std::optional<Gateway> m_Gateway; ///< The "virtual gateway object".
|
std::optional<Gateway> m_Gateway; ///< The "virtual gateway object".
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue