Skip to content

Commit

Permalink
Can now add a node offline
Browse files Browse the repository at this point in the history
  • Loading branch information
tux3 committed Oct 16, 2015
1 parent ae730fc commit 896b49d
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 2 deletions.
18 changes: 17 additions & 1 deletion commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ void help()
"folder update : Update the files database manually\n"
"folder sync : Update and synchronize this folder with other nodes\n"
"folder restore : Overwrite this source folder with the node's archives\n"
"node showkey : Show our node's public key\n"
"node show : Show the list of storage nodes\n"
"node add : Add a storage node\n"
"node add : Add a storage node by IP, optionally with a given public key\n"
"node remove : Remove a storage node\n"
"node start : Start running as a server\n"
<< std::flush;
Expand Down Expand Up @@ -675,6 +676,12 @@ void nodeAdd(const string &uri)
ndb.addNode(uri);
}

void nodeAdd(const string &uri, const string &pk)
{
NodeDB ndb(nodeDBPath());
ndb.addNode(uri, pk);
}

void nodeRemove(const string &uri)
{
NodeDB ndb(nodeDBPath());
Expand All @@ -693,4 +700,13 @@ bool nodeStart()
return r==0;
}

void nodeShowkey()
{
FolderDB fdb(folderDBPath());
NodeDB ndb(nodeDBPath());
Server server(serverConfigPath(), ndb, fdb);
auto pk = server.getPublicKey();
cout << "Our public key is " << Crypto::keyToString(pk)<<endl;
}

}
2 changes: 2 additions & 0 deletions commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ void folderStatus(const std::string& path);
bool folderSync(const std::string& path);
bool folderRestore(const std::string& path);
void nodeShow();
void nodeShowkey();
void nodeAdd(const std::string& uri);
void nodeAdd(const std::string& uri, const std::string& pk);
void nodeRemove(const std::string& uri);
bool nodeStart();

Expand Down
10 changes: 10 additions & 0 deletions crypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ void Crypto::genkeys(PublicKey &pk, SecretKey &sk)
crypto_box_keypair(&pk[0],&sk[0]);
}

PublicKey Crypto::stringToKey(std::string str)
{
if (str.size() != crypto_box_PUBLICKEYBYTES*2)
throw std::runtime_error("Crypto::stringToKey: Not a valid public key");

PublicKey pk;
sodium_hex2bin(pk.data(), sizeof(pk), str.data(), str.size(), nullptr, nullptr, nullptr);
return pk;
}

std::string Crypto::keyToString(PublicKey key)
{
static const char* const lut = "0123456789ABCDEF";
Expand Down
1 change: 1 addition & 0 deletions crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Crypto

static void genkeys(PublicKey& pk, SecretKey& sk);
static std::string keyToString(PublicKey key);
static PublicKey stringToKey(std::string str);
static std::vector<unsigned char> hash(std::string str);
static std::string toBase64(std::vector<unsigned char> data);
static std::string toBase64(unsigned char* data, size_t length);
Expand Down
9 changes: 8 additions & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ int main(int argc, char* argv[])
{
nodeShow();
}
else if (subcommand == "showkey")
{
nodeShowkey();
}
else if (subcommand == "start")
{
if (!nodeStart())
Expand All @@ -135,7 +139,10 @@ int main(int argc, char* argv[])
}
else if (subcommand == "add")
{
nodeAdd(argv[3]);
if (argc >= 5)
nodeAdd(argv[3], argv[4]);
else
nodeAdd(argv[3]);
}
else if (subcommand == "remove")
{
Expand Down
9 changes: 9 additions & 0 deletions nodedb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ void NodeDB::addNode(const std::string& uri)
nodes.push_back(Node(uri, pk));
}

void NodeDB::addNode(const string &uri, const string &pkstr)
{
PublicKey pk = Crypto::stringToKey(pkstr);

cout << "Adding node with pk "<<Crypto::keyToString(pk)<<endl;

nodes.push_back(Node(uri, pk));
}

bool NodeDB::removeNode(const std::string& uri)
{
for (unsigned i=0; i<nodes.size(); ++i)
Expand Down
1 change: 1 addition & 0 deletions nodedb.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class NodeDB

const std::vector<Node>& getNodes() const;
void addNode(const std::string& uri);
void addNode(const std::string& uri, const std::string& pk);
bool removeNode(const std::string& uri);

private:
Expand Down

0 comments on commit 896b49d

Please sign in to comment.