/
commands.cpp
442 lines (401 loc) · 14.4 KB
/
commands.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
#include "commands.h"
#include "folderdb.h"
#include "nodedb.h"
#include "settings.h"
#include "util/humanreadable.h"
#include "server.h"
#include "net/netsock.h"
#include "net/netaddr.h"
#include "net/net.h"
#include "serialize.h"
#include "compression.h"
#include "filetime.h"
#include "util/pathtools.h"
#include "util/vt100.h"
#include <iostream>
#include <memory>
#include <algorithm>
#include <cassert>
using namespace std;
namespace cmd
{
void help()
{
std::cout << "Usage: tbak <command> [<arg(s)>]\n"
"Commands:\n"
"folder show : Show the list of tracked folders\n"
"folder status <path> : Query the nodes for this folder's status\n"
"folder add-source <path> : Start tracking a local folder\n"
"folder add-archive <path> : Start tracking a remote folder\n"
"folder remove-source <path> : Stop tracking a source folder\n"
"folder remove-archive <path> : Stop tracking an archive folder\n"
"folder push <path> : Send the folder to other nodes's archive\n"
"folder restore <path> : Download missing files from other node's archives\n"
"node showkey : Show our node's public key\n"
"node show : Show the list of remote nodes\n"
"node add <URL> [<key>] : Add a remote node by hostname, optionally with the provided public key\n"
"node remove <URL> : Remove a remote node\n"
"node start : Start running as a server node\n"
<< std::flush;
}
void folderShow()
{
FolderDB fdb(folderDBPath());
printf("%*s %*s %*s\n",48,"Path",8,"Type",12,"Space used");
for (const Source& f : fdb.getSources())
{
std::string path = f.getPath(), type = "Source",
size = humanReadableSize(f.getSize());
printf("%*s ",48, path.c_str());
printf("%*s ",8, type.c_str());
printf("%*s \n",12, size.c_str());
}
for (const Archive& f : fdb.getArchives())
{
std::string path = f.getPathHash().toBase64(), type = "Archive",
size = humanReadableSize(f.getActualSize());
printf("%*s ",48, path.c_str());
printf("%*s ",8, type.c_str());
printf("%*s \n",12, size.c_str());
}
}
void folderRemoveSource(const string &path)
{
FolderDB fdb(folderDBPath());
fdb.removeSource(normalizePath(path));
}
void folderRemoveArchive(const string &pathHashStr)
{
FolderDB fdb(folderDBPath());
fdb.removeArchive(pathHashStr);
}
void folderAddSource(const string &path)
{
FolderDB fdb(folderDBPath());
fdb.addSource(normalizePath(path));
}
bool folderAddArchive(const string &path)
{
FolderDB fdb(folderDBPath());
NodeDB ndb(nodeDBPath());
string folderpath{normalizePath(path)};
PathHash pathHash{folderpath};
const vector<Node>& nodes = ndb.getNodes();
for (const Node& node : nodes)
{
Server server(serverConfigPath(), ndb, fdb);
NetSock sock(NetAddr{node.getUri()});
if (!Net::sendAuth(sock, server))
{
cerr << "Couldn't authenticate with that node"<<endl;
return false;
}
NetPacket reply = sock.secureRequest({NetPacket::FolderStats, ::serialize(pathHash)}, server, node.getPk());
if (reply.type != NetPacket::FolderStats)
{
cout << "Node "<<node.getUri()<<" doesn't have this archive"<<endl;
continue;
}
cout << "Found archive on node "<<node.getUri()<<endl;
fdb.addArchive(pathHash);
break;
}
return true;
}
bool folderPush(const string &path)
{
FolderDB fdb(folderDBPath());
NodeDB ndb(nodeDBPath());
string sourcePath{normalizePath(path)};
PathHash sourcePathHash{sourcePath};
// Make a list of local files
const Source* src = fdb.getSource(sourcePath);
if (!src)
{
cout <<"Source folder "<<sourcePath<<" not found"<<endl;
return false;
}
cout << "Building list of local files..."<<flush;
vector<SourceFile> lEntries = src->getSourceFiles();
sort(begin(lEntries), end(lEntries));
cout << vt100::CLEARLINE << "Found "<<lEntries.size()<<" local files"<<endl;
// Push to all the nodes
Server server(serverConfigPath(), ndb, fdb);
const vector<Node>& nodes = ndb.getNodes();
for (const Node& node : nodes)
{
NetSock sock(NetAddr{node.getUri()});
if (!Net::sendAuth(sock, server))
{
cout << "Couldn't authenticate with node "<<node.getUri()<<endl;
continue;
}
cout << "Pushing to node "<<node.getUri()<<endl;
// Try to get the content list of the folder, create it if necessary
vector<FileTime> rEntries;
try {
rEntries = node.fetchFolderList(sock, server, sourcePathHash);
} catch (const runtime_error& e) {
cout<<"Node "<<node.getUri()<<" doesn't have this folder, creating it"<<endl;
if (!node.createFolder(sock, server, sourcePathHash))
{
cout << "Couldn't create the folder on node "+node.getUri()+'\n';
continue;
}
try {
rEntries = node.fetchFolderList(sock, server, sourcePathHash);
} catch (const runtime_error& e) {
cout << "Error: "<<e.what()<<endl;
continue;
}
}
// Both lists are sorted by hash, we iterate over both at the same time
// This allows us to find the files to upload and delete in one pass
cout << "Building diff..."<<flush;
vector<SourceFile> updiff; // Files we need to upload
vector<FileTime> deldiff; // Files we need to delete
if (rEntries.size() > lEntries.size())
deldiff.reserve(rEntries.size() - lEntries.size());
else if (lEntries.size() > rEntries.size())
updiff.reserve(lEntries.size() - rEntries.size());
sort(begin(rEntries), end(rEntries));
{
auto lit = lEntries.begin(), lend = lEntries.end();
auto rit = rEntries.begin(), rend = rEntries.end();
while (lit != lend && rit != rend)
{
// If we don't have this remote file
if (rit->hash < lit->getPathHash())
{
deldiff.push_back(*rit);
++rit;
}
// If the remote doesn't have this file
else if (lit->getPathHash() < rit->hash)
{
updiff.push_back(*lit);
++lit;
}
// If we both have this file
else
{
if (rit->mtime != lit->getAttrs().mtime)
updiff.push_back(*lit);
++lit;
++rit;
}
}
updiff.insert(updiff.end(), lit, lend);
deldiff.insert(deldiff.end(), rit, rend);
}
cout <<vt100::CLEARLINE<<"Need to upload "<<updiff.size()
<<" files and delete "<<deldiff.size()<<" remote files"<<endl;
for (const SourceFile& file : updiff)
{
cout << "Uploading "<<file.getPath()<<" ("
<<humanReadableSize(file.getRawSize())<<')'<<endl;
if (!node.uploadFile(sock, server, sourcePathHash, file))
{
cout << "Failed to upload "<<file.getPath()<<endl;
continue;
}
}
for (const FileTime& file : deldiff)
{
cout << "Deleting remote file "<<file.hash.toBase64()<<endl;
if (!node.deleteFile(sock, server, sourcePathHash, file.hash))
{
cout << "Failed to delete "<<file.hash.toBase64()<<endl;
continue;
}
}
}
return true;
}
void folderStatus(const string &path)
{
FolderDB fdb(folderDBPath());
NodeDB ndb(nodeDBPath());
printf("%*s %*s\n",24,"URI",12,"Size");
string folderPath{normalizePath(path)};
PathHash folderPathHash{folderPath};
const vector<Node>& nodes = ndb.getNodes();
for (const Node& node : nodes)
{
Server server(serverConfigPath(), ndb, fdb);
NetSock sock;
if (!sock.connect(node.getUri()))
{
printf("%*s (couldn't connect to node)\n",24,node.getUri().c_str());
continue;
}
if (!Net::sendAuth(sock, server))
{
printf("%*s (couldn't authenticate with node)\n",24,node.getUri().c_str());
continue;
}
NetPacket request{NetPacket::FolderStats, ::serialize(folderPathHash)};
Crypto::encryptPacket(request, server, node.getPk());
sock.send(request);
NetPacket reply = sock.recvPacket();
Crypto::decryptPacket(reply, server, node.getPk());
if (reply.type != NetPacket::FolderStats)
{
printf("%*s ???\n",24,node.getUri().c_str());
continue;
}
auto it = reply.data.cbegin();
uint64_t size = ::deserializeConsume<uint64_t>(it);
printf("%*s %*s\n",24,node.getUri().c_str(),
12, humanReadableSize(size).c_str());
}
}
bool folderRestore(const string &path)
{
FolderDB fdb(folderDBPath());
NodeDB ndb(nodeDBPath());
string sourcePath{normalizePath(path)};
PathHash sourcePathHash{sourcePath};
// Make a list of local files
Source* src = fdb.getSource(sourcePath);
if (!src)
{
cout <<"Source folder "<<sourcePath<<" not found"<<endl;
return false;
}
cout << "Building list of local files..."<<flush;
vector<SourceFile> lEntries = src->getSourceFiles();
sort(begin(lEntries), end(lEntries));
cout << vt100::CLEARLINE << "Found "<<lEntries.size()<<" local files"<<endl;
// Push to all the nodes
Server server(serverConfigPath(), ndb, fdb);
const vector<Node>& nodes = ndb.getNodes();
for (const Node& node : nodes)
{
NetSock sock(NetAddr{node.getUri()});
if (!Net::sendAuth(sock, server))
{
cout << "Couldn't authenticate with node "<<node.getUri()<<endl;
continue;
}
cout << "Restoring from node "<<node.getUri()<<endl;
// Try to get the content list of the folder, create it if necessary
vector<FileTime> rEntries;
try {
rEntries = node.fetchFolderList(sock, server, sourcePathHash);
} catch (const runtime_error& e) {
cout<<"Node "<<node.getUri()<<" doesn't have this folder, skipping it"<<endl;
continue;
}
// Both lists are sorted by hash, we iterate over both at the same time
// This allows us to find the files to download in one pass
cout << "Building diff..."<<flush;
vector<FileTime> downdiff; // Files we need to download
if (rEntries.size() > lEntries.size())
downdiff.reserve(rEntries.size() - lEntries.size());
sort(begin(rEntries), end(rEntries));
{
auto lit = lEntries.begin(), lend = lEntries.end();
auto rit = rEntries.begin(), rend = rEntries.end();
while (lit != lend && rit != rend)
{
// If we don't have this remote file
if (rit->hash < lit->getPathHash())
{
downdiff.push_back(*rit);
++rit;
}
// If the remote doesn't have this file
else if (lit->getPathHash() < rit->hash)
{
++lit;
}
// If we both have this file
else
{
if (rit->mtime > lit->getAttrs().mtime)
downdiff.push_back(*rit);
++lit;
++rit;
}
}
downdiff.insert(downdiff.end(), rit, rend);
}
cout <<vt100::CLEARLINE<<"Need to download "<<downdiff.size()<<" files"<<endl;
for (const FileTime& file : downdiff)
{
cout << "Downloading encrypted file metadata... "<<flush;
uint64_t fileSize;
string filePath;
vector<char> meta;
{
meta = node.downloadFileMetadata(sock, server, sourcePathHash, file.hash);
auto it = meta.cend()-sizeof(uint64_t);
fileSize = ::deserializeConsume<uint64_t>(it);
meta.erase(meta.cend()-sizeof(uint64_t), meta.cend());
Crypto::decrypt(meta, server, server.getPublicKey());
it = meta.cbegin();
filePath = ArchiveFile::deserializePath(it);
}
cout << vt100::CLEARLINE << "Downloading file "<<filePath
<<" ("<<humanReadableSize(fileSize)<<")..."<<endl;
vector<char> data = node.downloadFile(sock, server, sourcePathHash, file.hash);
uint64_t mtime;
{
auto it = data.cbegin();
mtime = ::deserializeConsume<uint64_t>(it);
size_t msize = ::dataToVUint(it);
data.erase(data.begin(), it+msize);
}
Crypto::decrypt(data, server, server.getPublicKey());
data = Compression::inflate(data);
src->restoreFile(meta, mtime, data);
}
}
return true;
}
void nodeShow()
{
NodeDB ndb(nodeDBPath());
printf("%*s %s\n",65,"Public key","URI");
for (const Node& n : ndb.getNodes())
{
printf("%*s ", 65, n.getPkString().c_str());
printf("%s\n", n.getUri().c_str());
}
}
void nodeAdd(const string &uri)
{
NodeDB ndb(nodeDBPath());
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());
ndb.removeNode(uri);
}
bool nodeStart()
{
FolderDB fdb(folderDBPath());
NodeDB ndb(nodeDBPath());
Server server(serverConfigPath(), ndb, fdb);
int r = server.exec();
cout << "Server exiting with status "<<r<<endl;
fdb.save();
ndb.save();
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;
}
}