Update load/save code, set TTL to 1 hour and remove node storage.

This commit is contained in:
/dev/urandom 2016-08-27 21:20:08 +02:00
parent d3d8884f93
commit 624173f7cf
No known key found for this signature in database
GPG Key ID: 41322B973E0F295E
2 changed files with 7 additions and 125 deletions

View File

@ -19,8 +19,9 @@ namespace Components
Singleton = this; Singleton = this;
Logger::Print("Initializing BitMessage...\n"); Logger::Print("Initializing BitMessage...\n");
this->LoadObjectsAndKeys(); this->BMClient = new BitMRC(BITMESSAGE_OBJECT_STORAGE_FILENAME, BITMESSAGE_KEYS_FILENAME);
this->LoadNodes(); this->BMClient->init();
this->BMClient->defaultTTL = 1 * 60 * 60; // 1 hour
if (this->BMClient->PrivAddresses.empty()) if (this->BMClient->PrivAddresses.empty())
{ {
@ -29,7 +30,7 @@ namespace Components
// Generate a random address ready to use // Generate a random address ready to use
throw std::runtime_error("Failed to prepare source address for exception handling"); throw std::runtime_error("Failed to prepare source address for exception handling");
} }
this->SaveObjectsAndKeys(); this->BMClient->save();
} }
this->BMClient->start(); this->BMClient->start();
@ -124,9 +125,6 @@ namespace Components
Command::Add("bm_save", [](Command::Params) { Command::Add("bm_save", [](Command::Params) {
Singleton->BMClient->save(BITMESSAGE_OBJECT_STORAGE_FILENAME); Singleton->BMClient->save(BITMESSAGE_OBJECT_STORAGE_FILENAME);
}); });
Command::Add("bm_save_nodes", [](Command::Params) {
Singleton->SaveNodes();
});
Command::Add("bm_address_public", [](Command::Params params) { Command::Add("bm_address_public", [](Command::Params params) {
if (params.Length() < 2) return; if (params.Length() < 2) return;
@ -173,113 +171,6 @@ namespace Components
BMClient->addAddr(myAddress); BMClient->addAddr(myAddress);
return true; return true;
} }
void BitMessage::LoadObjectsAndKeys()
{
Logger::Print("Loading BM objects and keys...\n");
this->BMClient->load(BITMESSAGE_OBJECT_STORAGE_FILENAME);
}
void BitMessage::SaveObjectsAndKeys()
{
Logger::Print("Saving BM objects and keys...\n");
this->BMClient->save(BITMESSAGE_OBJECT_STORAGE_FILENAME);
}
void BitMessage::SaveNodes()
{
Logger::Print("Saving BM nodes...\n");
Proto::Node::List list;
std::vector<std::string> nodeList; // keep track of dupes
std::shared_lock<std::shared_timed_mutex> mlock(Singleton->BMClient->mutex_nodes);
for (auto& node : Singleton->BMClient->Nodes)
{
std::string nodeStr = String::VA("%s:%s", node->Ip.c_str(), node->Port.c_str());
switch (node->state) {
case 0: // Not connected
break;
case 1: // Connecting
break;
case 2: // Connected
if (std::find(nodeList.begin(), nodeList.end(), nodeStr) == nodeList.end())
{
// This allows us to keep track of duplicates
nodeList.push_back(nodeStr);
DWORD ip = 0;
unsigned short port = 0;
sscanf_s(nodeStr.c_str(), "%d.%d.%d.%d:%hu", &ip, &ip + 1, &ip + 2, &ip + 3, &port);
Network::Address address;
address.SetIP(ip);
address.SetPort(port);
address.Serialize(list.add_address());
Logger::Print("Saved node: %s\n", address.GetCString());
}
break;
case 3: // Reconnecting
break;
}
}
mlock.unlock();
if (list.address_size() <= 0)
return;
// Create directory if it doesn't exist yet
auto dirName = const_cast<char*>(BITMESSAGE_NODE_STORAGE_FILENAME.c_str());
PathRemoveFileSpecA(dirName);
CreateDirectoryA(dirName, NULL);
// Now write the actual file
Utils::IO::WriteFile(BITMESSAGE_NODE_STORAGE_FILENAME, list.SerializeAsString());
}
void BitMessage::LoadNodes()
{
Logger::Print("Loading BM nodes...\n");
// Load nodes stored in file
Proto::Node::List list;
std::string nodes = IO::ReadFile(BITMESSAGE_NODE_STORAGE_FILENAME);
if (!nodes.empty() && list.ParseFromString(nodes))
{
for (int i = 0; i < list.address_size(); ++i)
{
Network::Address address = list.address(i);
this->BMClient->connectNode(new NodeConnection(
String::VA("%d.%d.%d.%d", address.GetIP().bytes[0], address.GetIP().bytes[1], address.GetIP().bytes[2], address.GetIP().bytes[3]), // urgh.
String::VA("%d", address.GetPort()), this->BMClient));
}
}
// Load default nodes if not connected to yet
for (auto addr : bitmessageKnownNodes) {
auto portStr = std::string(String::VA("%d", addr.second));
if (!this->HasNode(addr.first, portStr))
{
this->BMClient->connectNode(new NodeConnection(addr.first, portStr, this->BMClient));
}
}
}
bool BitMessage::HasNode(std::string ip, std::string port)
{
for (auto& node : this->BMClient->Nodes)
{
if (node->Ip == ip && node->Port == port)
{
return true;
}
}
return false;
}
} }
#endif #endif

View File

@ -2,8 +2,8 @@
#ifndef DISABLE_BITMESSAGE #ifndef DISABLE_BITMESSAGE
#define BITMESSAGE_NODE_STORAGE_FILENAME std::string("players/bmnodes.dat") #define BITMESSAGE_KEYS_FILENAME std::string("players/bmkey.dat")
#define BITMESSAGE_OBJECT_STORAGE_FILENAME std::string("save") #define BITMESSAGE_OBJECT_STORAGE_FILENAME std::string("players/bmstore.dat")
static const std::map<std::string, unsigned short> bitmessageKnownNodes = { static const std::map<std::string, unsigned short> bitmessageKnownNodes = {
// https://github.com/Bitmessage/PyBitmessage/blob/4622d952e47a7dbb3a90aa79f4d20163aa14b041/src/defaultKnownNodes.py#L15-L23 // https://github.com/Bitmessage/PyBitmessage/blob/4622d952e47a7dbb3a90aa79f4d20163aa14b041/src/defaultKnownNodes.py#L15-L23
@ -37,18 +37,9 @@ namespace Components
#endif #endif
static BitMessage* Singleton; static BitMessage* Singleton;
BitMRC* BMClient = new BitMRC(); BitMRC* BMClient;
void SaveNodes();
bool HasNode(std::string ip, std::string port);
void SaveObjectsAndKeys();
private: private:
void LoadNodes();
void LoadObjectsAndKeys();
bool InitAddr(); bool InitAddr();
}; };
} }