-
Notifications
You must be signed in to change notification settings - Fork 1
/
trackerObj.cpp
225 lines (194 loc) · 8.16 KB
/
trackerObj.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
#include "trackerObj.h"
#include "Decoder.h"
#include "Utility.h"
#include "loguru.h"
namespace Bittorrent
{
using namespace utility;
trackerObj::trackerObj()
: trackerAddress{ "" },
lastPeerRequest{ std::chrono::high_resolution_clock::time_point::min()},
peerRequestInterval{ 1800 }, seeders(std::numeric_limits<int>::min()),
leechers(std::numeric_limits<int>::min()), errMessage{""},
isWorking{false}, peerListUpdated{ std::make_shared<sigPeer>() }
{
}
//create required url for GET request
void trackerObj::update(TorrentStatus::currentStatus currentState,
std::vector<byte> clientID, int httpPort, int udpPort,
std::string urlEncodedInfoHash, std::vector<byte> infoHash,
long long uploaded, long long downloaded, long long remaining)
{
//switch case to get enumerator string
std::string stringEvent;
int intEvent = 0;
switch (currentState)
{
case TorrentStatus::currentStatus::completed:
stringEvent = "completed";
intEvent = 1;
break;
case TorrentStatus::currentStatus::started:
stringEvent = "started";
intEvent = 2;
break;
case TorrentStatus::currentStatus::stopped:
stringEvent = "stopped";
intEvent = 3;
break;
}
//URL encode clientID
std::string clientIDString(clientID.begin(), clientID.end());
auto urlEncodedClientID = urlEncode(clientIDString);
//handle tracker url without announce
std::string sep = "";
if (trackerAddress.find("announce") == std::string::npos)
{
sep = "/";
}
//this is for http requests
//compact will be 1 but client will also support non-compact response
std::string url = trackerAddress + sep + "?info_hash=" + urlEncodedInfoHash +
"&peer_id=" + urlEncodedClientID + "&port=" + std::to_string(httpPort)
+ "&uploaded=" + std::to_string(uploaded) + "&downloaded=" +
std::to_string(downloaded) + "&left=" + std::to_string(remaining) +
"&event=" + stringEvent + "&compact=1";
//only requesting new peers if request interval has passed
if (currentState == TorrentStatus::currentStatus::started &&
std::chrono::high_resolution_clock::now() <
(lastPeerRequest + peerRequestInterval))
{
return;
}
//parse url and check if valid
trackerUrl parsedUrl(url);
parsedUrl.parse();
if (parsedUrl.isInvalidURL)
{
LOG_F(ERROR, "Tracker (%s) is invalid!",
trackerAddress.c_str());
return;
}
//temp vector for printing peers
//std::vector<peer> tempPeerList;
//request url using appropriate protocol
if (parsedUrl.protocol == trackerUrl::protocolType::http)
{
if (seeders == std::numeric_limits<int>::min() &&
leechers == std::numeric_limits<int>::min())
{
HTTPClient httpAnnounce(parsedUrl, httpPort, 1);
isWorking = !httpAnnounce.isFail;
errMessage = httpAnnounce.errMessage;
if (isWorking)
{
seeders = httpAnnounce.seeders;
leechers = httpAnnounce.leechers;
peerRequestInterval = httpAnnounce.peerRequestInterval;
//call signal to fire peerListUpdated event
//not thread safe?
(*peerListUpdated)(httpAnnounce.peerList);
//replace peer vector with new vector
peerList = httpAnnounce.peerList;
}
}
else
{
//scrape
HTTPClient httpGen(parsedUrl, httpPort, 0);
isWorking = !httpGen.isFail;
errMessage = httpGen.errMessage;
//announce and update if seeders/leechers values change
if (httpGen.seeders != seeders ||
httpGen.leechers != leechers)
{
LOG_F(INFO,
"Tracker (%s:%s) info changed since last scrape. "
"Switching to HTTP announce request.",
parsedUrl.hostname.c_str(), parsedUrl.port.c_str());
//reset tracker url target (scrape changes it)
httpGen.target = parsedUrl.target;
httpGen.dataTransmission(1);
isWorking = !httpGen.isFail;
errMessage = httpGen.errMessage;
if (errMessage.empty())
{
seeders = httpGen.seeders;
leechers = httpGen.leechers;
peerRequestInterval = httpGen.peerRequestInterval;
//call signal to fire peerListUpdated event
//not thread safe?
(*peerListUpdated)(httpGen.peerList);
//replace peer vector with new vector
peerList = httpGen.peerList;
}
}
}
//handle peers, seeders, leechers, interval
//need some event handling to scrape/announce after interval time has passed
}
else if (parsedUrl.protocol == trackerUrl::protocolType::udp)
{
//announce if first time, otherwise scrape
if (seeders == std::numeric_limits<int>::min() &&
leechers == std::numeric_limits<int>::min())
{
UDPClient udpAnnounce(parsedUrl, clientID, infoHash, uploaded,
downloaded, remaining, intEvent, udpPort, 1);
isWorking = !udpAnnounce.isFail;
if (isWorking)
{
seeders = udpAnnounce.seeders;
leechers = udpAnnounce.leechers;
peerRequestInterval = udpAnnounce.peerRequestInterval;
//call signal to fire peerListUpdated event
//not thread safe?
(*peerListUpdated)(udpAnnounce.peerList);
//replace peer vector with new vector
peerList = udpAnnounce.peerList;
}
}
else
{
//scrape
UDPClient udpGen(parsedUrl, clientID, infoHash, uploaded,
downloaded, remaining, intEvent, udpPort, 0);
isWorking = !udpGen.isFail;
errMessage = udpGen.errMessage;
//announce and update if seeders/leechers values change
if (udpGen.seeders != seeders || udpGen.leechers != leechers)
{
LOG_F(INFO,
"Tracker (%s:%s) info changed since last scrape. "
"Switching to UDP announce request.",
parsedUrl.hostname.c_str(), parsedUrl.port.c_str());
udpGen.dataTransmission(1);
isWorking = !udpGen.isFail;
errMessage = udpGen.errMessage;
if (isWorking)
{
seeders = udpGen.seeders;
leechers = udpGen.leechers;
peerRequestInterval = udpGen.peerRequestInterval;
//call signal to fire peerListUpdated event
//not thread safe?
(*peerListUpdated)(udpGen.peerList);
//replace peer vector with new vector
peerList = udpGen.peerList;
}
}
}
//handle peers, seeders, leechers, interval
//need some event handling to scrape/announce after interval time has passed
}
else
{
return;
}
}
void trackerObj::resetLastRequest()
{
lastPeerRequest =
{std::chrono::high_resolution_clock::time_point::min()};
}
}