-
Notifications
You must be signed in to change notification settings - Fork 1
/
dht-spider.cpp
301 lines (278 loc) · 7.43 KB
/
dht-spider.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
/*
* Copyright (c) 2022 by Thomas A. Early N7TAE
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <opendht.h>
#include <iostream>
#include <set>
#include <map>
#include <list>
#include <atomic>
#include "dht-values.h"
static const std::string default_bs("xlx757.openquad.net");
static dht::Where w;
SMrefdPeers1 mrefdPeers;
SUrfdPeers1 urfdPeers;
static std::map<std::string, std::set<std::string>> Web;
static std::atomic<bool> ready;
static void Trim(std::string &s)
{
while (! s.empty())
{
if (isspace(s.at(0)))
s.erase(0, 1);
else if (isspace(s.back()))
{
s.resize(s.size()-1);
}
else
break;
}
}
static void FindPeers(dht::DhtRunner &node, const std::string &refcs, const char module, const bool isM17)
{
mrefdPeers.timestamp = 0;
mrefdPeers.sequence = 0;
mrefdPeers.list.clear();
ready = false;
node.get(
dht::InfoHash::get(refcs),
[](const std::shared_ptr<dht::Value> &v)
{
if (v->checkSignature())
{
if (0 == v->user_type.compare(MREFD_PEERS_1))
{
auto rdat = dht::Value::unpack<SMrefdPeers1>(*v);
if (rdat.timestamp > mrefdPeers.timestamp)
{
mrefdPeers = dht::Value::unpack<SMrefdPeers1>(*v);
}
else if (rdat.timestamp == mrefdPeers.timestamp)
{
if (rdat.sequence > mrefdPeers.sequence)
mrefdPeers = dht::Value::unpack<SMrefdPeers1>(*v);
}
}
else if (0 == v->user_type.compare(URFD_PEERS_1))
{
auto rdat = dht::Value::unpack<SUrfdPeers1>(*v);
if (rdat.timestamp > urfdPeers.timestamp)
{
urfdPeers = dht::Value::unpack<SUrfdPeers1>(*v);
}
else if (rdat.timestamp == urfdPeers.timestamp)
{
if (rdat.sequence > urfdPeers.sequence)
urfdPeers = dht::Value::unpack<SUrfdPeers1>(*v);
}
}
}
else
{
std::cout << "Value signature failed!" << std::endl;
}
return true;
},
[](bool success)
{
if (!success)
{
std::cerr << "get() failed!" << std::endl;
}
ready = true;
},
{}, // empty filter
w
);
// wait for node.get() to complete
while (! ready)
{
// a bit of a hack, but it works!
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
// add the webnode to the map
std::set<std::string> peerset;
if (isM17)
{
for (const auto &p : mrefdPeers.list)
{
const auto modules = std::get<toUType(EMrefdPeerFields::Modules)>(p);
if (std::string::npos != modules.find(module)) // add only if the peer is using this module
{
auto ref = std::get<toUType(EMrefdPeerFields::Callsign)>(p);
Trim(ref);
auto rval = peerset.insert(ref);
if (false == rval.second)
std::cout << "WARNING: " << ref << "could not be added to the " << refcs << " mrefdPeers!" << std::endl;
}
}
}
else
{
for (const auto &p : urfdPeers.list)
{
const auto modules = std::get<toUType(EUrfdPeerFields::Modules)>(p);
if (std::string::npos != modules.find(module)) // add only if the peer is using this module
{
auto ref = std::get<toUType(EUrfdPeerFields::Callsign)>(p);
Trim(ref);
auto rval = peerset.insert(ref);
if (false == rval.second)
std::cout << "WARNING: " << ref << "could not be added to the " << refcs << " urfdPeers!" << std::endl;
}
}
}
auto rval = Web.emplace(refcs, peerset);
if (false == rval.second)
{
std::cerr << "ERROR: Could not create Web map item for " << refcs << std::endl;
exit(EXIT_FAILURE);
}
// now, keep looking via this recursive call
for (const auto &pstr : Web[refcs])
{
if (Web.end() == Web.find(pstr)) // if it hasn't already been added...
FindPeers(node, pstr, module, isM17); // then add it!
}
}
static void Usage(std::ostream &ostr, const char *comname)
{
ostr << "usage: " << comname << " [-b bootstrap] [-l] node_name module" << std::endl << std::endl;
ostr << "Options:" << std::endl;
ostr << " -b (bootstrap) argument is any running node on the dht network" << std::endl;
ostr << " -l to only print the list of linked peers" << std::endl;
ostr << " If not specified, " << default_bs << " will be used." << std::endl;
}
int main(int argc, char *argv[])
{
bool onlylist = false;
// parse the command line
std::string bs(default_bs);
while (1)
{
int c = getopt(argc, argv, "b:l");
if (c < 0)
{
if (1 == argc)
{
Usage(std::cout, argv[0]);
exit(EXIT_SUCCESS);
}
break;
}
switch (c)
{
case 'b':
bs.assign(optarg);
break;
case 'l':
onlylist = true;
break;
default:
Usage(std::cerr, argv[0]);
exit(EXIT_FAILURE);
}
}
if (optind + 2 != argc)
{
std::cerr << "Error: " << argv[0] << " needs two arguments!" << std::endl;
Usage(std::cerr, argv[0]);
exit(EXIT_FAILURE);
}
auto p = argv[optind];
while (*p)
{
if (std::islower(*p))
*p = std::toupper(*p);
p++;
}
const std::string key(argv[optind]);
const auto isM17 = 0 == key.compare(0, 4, "M17-");
p = argv[++optind];
if (1 != std::strlen(p) || ! std::isalpha(*p))
{
std::cerr << "Error: second argument must specify a single module!" << std::endl;
Usage(std::cerr, argv[0]);
exit(EXIT_FAILURE);
}
const char module = std::toupper(*p);
// command line parsing done
// log into the dht
std::string name("Spider");
name += std::to_string(getpid());
dht::DhtRunner node;
node.run(17171, dht::crypto::generateIdentity(name), true, 59973);
node.bootstrap(bs, "17171");
if (! onlylist)
{
std::cout << "Running node using name " << name << " and bootstrapping from " << bs << std::endl;
std::cout << "Shared module " << module << " map:" << std::endl;
}
// set up the where condition
w.id(isM17 ? toUType(EMrefdValueID::Peers) : toUType(EUrfdValueID::Peers));
// start the spider
FindPeers(node, key, module, isM17);
// make a list of all the reflectors which were found to be interconnected
// the list will be in alphabetical order because std::map is ordered by each item's key
std::list<std::string> group;
for (const auto &row : Web)
{
group.push_back(row.first);
}
if (onlylist)
{
for (const auto &p : group)
{
std::cout << p << std::endl;
}
}
else
{
// now print the connect matrix
const std::string space(isM17 ? 9 : 8, ' ');
std::cout << space << std::string(2*Web.size()+1, '=') << std::endl;
for (const auto &row : group)
{
std::cout << row << " |";
for (const auto &col : group)
{
if (row == col)
{
std::cout << ' ' << ((Web[row].size()) ? '=' : '?');
}
else
{
std::cout << ' ' << ((Web[row].end() == Web[row].find(col)) ? ' ' : '+');
}
}
std::cout << " | " << row.substr(isM17 ? 4 : 3) << std::endl;
}
std::cout << space << std::string(2*Web.size()+1, '=') << std::endl;
for (int i=0; i<3; i++)
{
std::cout << space;
for (const auto &ref : group)
{
int j = i + (isM17 ? 4 : 3);
std::cout << ' ' << ref.at(j);
}
std::cout << std::endl;
}
}
node.join();
return EXIT_SUCCESS;
}