Repository navigation
Expand file tree
/
Copy pathDownload.cpp
More file actions
635 lines (508 loc) · 22.7 KB
/
Copy pathDownload.cpp
File metadata and controls
635 lines (508 loc) · 22.7 KB
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
//
// Created by Oliver Mrovcak on 04/01/2024.
//
#include "Download.h"
using boost::asio::ip::tcp;
Download::Download(std::string protocol, std::string hostname, std::string savePath, std::string downloadPath, std::string username, std::string password, int priority) {
this->protocol = protocol;
this->hostname = hostname;
this->downloadPath = downloadPath;
this->username = username;
this->password = password;
this->priority = priority;
this->savePath = savePath;
}
Download::Download(std::string protocol, std::string hostname, std::string savePath, std::string downloadPath, std::string username, std::string password, int priority, std::string scheduledTime) {
this->protocol = protocol;
this->hostname = hostname;
this->downloadPath = downloadPath;
this->username = username;
this->password = password;
this->priority = priority;
this->savePath = savePath;
this->scheduledTime = scheduledTime;
}
Download::~Download() {
}
bool Download::isPaused() {
return paused;
}
bool Download::isStarted() {
return started;
}
std::string Download::getUsername() {
return username;
}
std::string Download::getPassword() {
return password;
}
std::string Download::getHostname() {
return hostname;
}
std::string Download::getSavePath() {
return savePath;
}
std::string Download::getDownloadPath() {
return downloadPath;
}
std::string Download::getProtocol() {
return protocol;
}
unsigned long Download::getSize() {
return size;
}
std::string Download::getFilename() {
return "file";
}
unsigned long Download::getCurrentSize() {
return currentSize;
}
void Download::setSize(unsigned long size) {
this->size = size;
}
std::string Download::getScheduledTime() {
return scheduledTime;
}
bool Download::isCancelled() {
return cancelled;
}
bool Download::isCompleted() {
return completed;
}
void Download::setCompleted(bool completed) {
this->completed = completed;
}
void Download::start() {
if (protocol == "FTP") {
ftpDownload();
} else if (protocol == "FTPS") {
ftpsDownload();
} else if (protocol == "HTTP") {
httpDownload();
} else if (protocol == "HTTPS") {
httpsDownload();
} else {
std::cout << "Invalid protocol" << std::endl;
}
}
void Download::pause() {
std::lock_guard<std::mutex> lock(mtx); // Lock the mutex
paused = true;
}
void Download::resume() {
std::unique_lock<std::mutex> lock(mtx);
paused = false; // Clear the paused flag
cv.notify_one(); // Wake up the download thread
}
void Download::cancel() {
std::lock_guard<std::mutex> lock(mtx); // Lock the mutex
cancelled = true;
}
void Download::restart() {
// Restart download
}
void Download::setPriority(int priority) {
this->priority = priority;
}
void Download::ftpDownload() {
try {
boost::asio::io_service io_service;
// Resolve the server address and port
tcp::resolver resolver(io_service);
tcp::resolver::query query(this->hostname, "21");
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
// Create and connect the socket
tcp::socket socket(io_service);
boost::asio::connect(socket, endpoint_iterator);
// Function to read a response line from the server
auto read_response = [&](tcp::socket &sock) {
boost::asio::streambuf response;
boost::asio::read_until(sock, response, "\n");
std::string s((std::istreambuf_iterator<char>(&response)), std::istreambuf_iterator<char>());
printw("%s\n", s.c_str());
return s;
};
// Read the welcome message
read_response(socket);
// Send the USER command
std::string user_cmd = "USER " + this->username + "\r\n";
boost::asio::write(socket, boost::asio::buffer(user_cmd));
read_response(socket);
// Send the PASS command
std::string pass_cmd = "PASS " + this->password + "\r\n";
boost::asio::write(socket, boost::asio::buffer(pass_cmd));
read_response(socket);
// Send the PASV command to enter passive mode
std::string pasv_cmd = "PASV\r\n";
boost::asio::write(socket, boost::asio::buffer(pasv_cmd));
auto pasv_response = read_response(socket);
std::string size_cmd = "SIZE " + this->downloadPath + "\r\n";
boost::asio::write(socket, boost::asio::buffer(size_cmd));
auto size_response = read_response(socket);
// Parse the response to get the size
std::regex size_regex("\\s(\\d+)");
std::smatch matches;
if (std::regex_search(size_response, matches, size_regex) && matches.size() == 2) {
unsigned long file_size = std::stoul(matches[1].str());
setSize(file_size);
} else {
std::cerr << "Failed to get the file size." << std::endl;
}
// Parse the PASV response for IP address and port
std::string data_ip;
unsigned short data_port;
if (parse_pasv_response(pasv_response, data_ip, data_port)) {
std::cout << "Data connection details - IP: " << data_ip << " Port: " << data_port << std::endl;
// Connect a new socket for the data transfer
tcp::endpoint data_endpoint(boost::asio::ip::address::from_string(data_ip), data_port);
tcp::socket data_socket(io_service);
data_socket.connect(data_endpoint);
// Send the RETR command
std::string retr_cmd = "RETR " + this->downloadPath + "\r\n";
boost::asio::write(socket, boost::asio::buffer(retr_cmd));
read_response(socket);
size_t lastSlashPos = this->downloadPath.find_last_of("/\\");
std::string filename = lastSlashPos != std::string::npos ? this->downloadPath.substr(lastSlashPos + 1) : this->downloadPath;
std::string fullSavePath = this->savePath + (this->savePath.back() == '/' ? "" : "/") + filename;
// Open a file to write the downloaded data
std::ofstream outfile;
if (std::ifstream(fullSavePath)) {
std::cerr << "File already exists. Downloading copy" << std::endl;
outfile.open(getNewFileName(fullSavePath) , std::ios::binary);
} else {
outfile.open(fullSavePath, std::ios::binary);
}
// Read and write the file data from the data socket
boost::asio::streambuf response;
boost::system::error_code ec;
while (boost::asio::read(data_socket, response, boost::asio::transfer_at_least(1), ec)) {
{
std::unique_lock<std::mutex> lock(mtx);
cv.wait(lock, [this] { return !paused; });
}
{
std::lock_guard<std::mutex> lock(mtx);
started = true;
if (cancelled) {
return;
}
if (paused) {
std::cout << "Stopping download" << std::endl;
break;
}
}
size_t data_length = response.size(); // Get the length of the received data
currentSize += data_length; // Update the current download size
outfile << &response;
}
setCompleted(true);
if (ec && ec != boost::asio::error::eof) {
printw("Error during read: %s\n", ec.message().c_str());
}
outfile.close();
data_socket.close();
} else {
std::cerr << "Failed to parse the PASV response." << std::endl;
}
}
catch (std::exception& e) {
printw("Exception: %s\n", e.what());
}
}
void Download::ftpsDownload() {
boost::asio::io_service io_service;
// Create an SSL context and specify the use of TLS
boost::asio::ssl::context ctx(boost::asio::ssl::context::sslv23); // TLS v1.2
ctx.set_default_verify_paths(); // Use the default paths for finding CA certificates
ctx.set_verify_mode(boost::asio::ssl::verify_peer); // Verify the server certificate
// Create an SSL stream that encapsulates a TCP socket
boost::asio::ssl::stream<boost::asio::ip::tcp::socket> ssl_socket(io_service, ctx);
// Resolve the server address and port (FTP typically uses port 21)
boost::asio::ip::tcp::resolver resolver(io_service);
auto endpoints = resolver.resolve({this->hostname, "ftps"});
// Establish a connection to the server
boost::asio::connect(ssl_socket.lowest_layer(), endpoints);
ssl_socket.lowest_layer().set_option(boost::asio::ip::tcp::no_delay(true));
// Perform SSL handshake as a client
ssl_socket.handshake(boost::asio::ssl::stream_base::client);
// Function to read a response line from the server
auto read_response = [&](boost::asio::ssl::stream<boost::asio::ip::tcp::socket> &ssl_sock) {
boost::asio::streambuf response;
boost::asio::read_until(ssl_sock, response, "\n");
std::string s((std::istreambuf_iterator<char>(&response)), std::istreambuf_iterator<char>());
std::cout << s << std::endl; // Print server response for debugging
return s;
};
// Send the AUTH TLS command to switch to secure communication
std::string auth_cmd = "AUTH TLS\r\n";
boost::asio::write(ssl_socket, boost::asio::buffer(auth_cmd));
read_response(ssl_socket);
// Send the USER command
std::string user_cmd = "USER " + this->username + "\r\n";
boost::asio::write(ssl_socket, boost::asio::buffer(user_cmd));
read_response(ssl_socket);
// Send the PASS command
std::string pass_cmd = "PASS " + this->password + "\r\n";
boost::asio::write(ssl_socket, boost::asio::buffer(pass_cmd));
read_response(ssl_socket);
// Send the PASV command to enter passive mode
std::string pasv_cmd = "PASV\r\n";
boost::asio::write(ssl_socket, boost::asio::buffer(pasv_cmd));
auto pasv_response = read_response(ssl_socket);
// Parse the PASV response for IP address and port
std::string data_ip;
unsigned short data_port;
if (!parse_pasv_response(pasv_response, data_ip, data_port)) {
std::cerr << "Failed to parse the PASV response." << std::endl;
return;
}
// Connect a new socket for the data transfer
tcp::endpoint data_endpoint(boost::asio::ip::address::from_string(data_ip), data_port);
boost::asio::ip::tcp::socket data_socket(io_service);
data_socket.connect(data_endpoint);
// Send the RETR command to initiate the transfer
std::string retr_cmd = "RETR " + this->downloadPath + "\r\n";
boost::asio::write(ssl_socket, boost::asio::buffer(retr_cmd));
read_response(ssl_socket);
// Determine the filename from the download path
size_t lastSlashPos = this->downloadPath.find_last_of("/\\");
std::string filename = lastSlashPos != std::string::npos ? this->downloadPath.substr(lastSlashPos + 1) : this->downloadPath;
std::string fullSavePath = this->savePath + (this->savePath.back() == '/' ? "" : "/") + filename;
// Open a file to write the downloaded data
std::ofstream outfile(getNewFileName(fullSavePath), std::ios::binary);
// Read and write the file data from the data socket
boost::asio::streambuf response;
boost::system::error_code ec;
while (boost::asio::read(data_socket, response, boost::asio::transfer_at_least(1), ec)) {
{
std::unique_lock<std::mutex> lock(mtx);
cv.wait(lock, [this] { return !paused; });
}
{
std::lock_guard<std::mutex> lock(mtx);
started = true;
if (cancelled) {
return;
}
if (paused) {
std::cout << "Stopping download" << std::endl;
break;
}
}
size_t data_length = response.size();
currentSize += data_length;
outfile << &response;
}
// Close the file and sockets
outfile.close();
data_socket.close();
ssl_socket.shutdown(); // Shut down SSL on the connection
ssl_socket.lowest_layer().close(); // Close the underlying socket
if (ec != boost::asio::error::eof) {
std::cerr << "Error during read: " << ec.message() << std::endl;
} else {
setCompleted(true);
std::cout << "Download completed successfully." << std::endl;
}
}
void Download::httpDownload() {
boost::asio::io_service io_service;
// Resolve the server address and port
tcp::resolver resolver(io_service);
tcp::resolver::query query(this->hostname, "80");
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
// Create and connect the socket
tcp::socket socket(io_service);
boost::asio::connect(socket, endpoint_iterator);
// Form the request message
std::ostringstream request_stream;
request_stream << "GET " << this->downloadPath << " HTTP/1.0\r\n";
request_stream << "Host: " << this->hostname << "\r\n";
request_stream << "Accept: */*\r\n";
request_stream << "Connection: close\r\n\r\n";
// Send the request
std::string request = request_stream.str();
boost::asio::write(socket, boost::asio::buffer(request));
// Read the response status line
boost::asio::streambuf response;
boost::asio::read_until(socket, response, "\r\n");
// Check that response is OK
std::istream response_stream(&response);
std::string http_version;
unsigned int status_code;
std::string status_message;
response_stream >> http_version >> status_code;
std::getline(response_stream, status_message);
if (!response_stream || http_version.substr(0, 5) != "HTTP/")
{
std::cerr << "Invalid response\n";
return;
}
if (status_code != 200)
{
std::cerr << "Response returned with status code " << status_code << "\n";
return;
}
std::cout << "200"<< std::endl;
// Read the response headers, which are terminated by a blank line
boost::asio::read_until(socket, response, "\r\n\r\n");
// Process the response headers
std::string header;
while (std::getline(response_stream, header) && header != "\r")
{
// Headers processing...
}
// Open a file to write the downloaded data
size_t lastSlashPos = this->downloadPath.find_last_of("/\\");
std::string filename = lastSlashPos != std::string::npos ? this->downloadPath.substr(lastSlashPos + 1) : this->downloadPath;
std::string fullSavePath = this->savePath + (this->savePath.back() == '/' ? "" : "/") + filename;
std::ofstream outfile(fullSavePath, std::ios::binary);
// Write whatever content we already have to output
if (response.size() > 0) {
outfile << &response;
}
// Continue reading until EOF, writing data to output as we go
boost::system::error_code ec;
while (boost::asio::read(socket, response, boost::asio::transfer_at_least(1), ec)) {
{
std::unique_lock<std::mutex> lock(mtx);
cv.wait(lock, [this] { return !paused; }); // Wait while the download is paused
}
{
std::lock_guard<std::mutex> lock(mtx);
started = true;
if (cancelled) {
return;
}
if (paused) {
std::cout << "Stopping download" << std::endl;
break;
}
}
size_t data_length = response.size(); // Get the length of the received data
currentSize += data_length; // Update the current download size
outfile << &response;
}
std::cout << size << std::endl;
std::cout << currentSize << std::endl;
std::cout << fullSavePath << std::endl;
std::cout << downloadPath << std::endl;
setCompleted(true);
if (ec != boost::asio::error::eof) {
std::cerr << "Error during read: " << ec.message() << "\n";
}
outfile.close();
}
void Download::httpsDownload() {
boost::asio::io_service io_service;
// Create an SSL context and specify the use of TLS
boost::asio::ssl::context ctx(boost::asio::ssl::context::tls);
ctx.set_default_verify_paths(); // Use the default paths for finding CA certificates
ctx.set_verify_mode(boost::asio::ssl::verify_peer); // Verify the server certificate
// Create an SSL stream that encapsulates a TCP socket
boost::asio::ssl::stream<boost::asio::ip::tcp::socket> ssl_socket(io_service, ctx);
// Resolve the server address and port (HTTPS typically uses port 443)
boost::asio::ip::tcp::resolver resolver(io_service);
auto endpoints = resolver.resolve({this->hostname, "443"});
// Establish a connection to the server
boost::asio::connect(ssl_socket.lowest_layer(), endpoints);
ssl_socket.lowest_layer().set_option(boost::asio::ip::tcp::no_delay(true));
// Perform SSL handshake
ssl_socket.handshake(boost::asio::ssl::stream_base::client);
// Form the HTTPS request message
std::ostringstream request_stream;
request_stream << "GET " << this->downloadPath << " HTTP/1.1\r\n";
request_stream << "Host: " << this->hostname << "\r\n";
request_stream << "Accept: */*\r\n";
request_stream << "Connection: close\r\n\r\n";
std::string request = request_stream.str();
// Send the request
boost::asio::write(ssl_socket, boost::asio::buffer(request));
// Declare a buffer to hold the response
boost::asio::streambuf response;
// Read the response status line
boost::asio::read_until(ssl_socket, response, "\r\n");
// Check the response status
std::istream response_stream(&response);
std::string http_version;
unsigned int status_code;
std::string status_message;
response_stream >> http_version >> status_code;
std::getline(response_stream, status_message);
if (!response_stream || http_version.substr(0, 5) != "HTTP/") {
std::cerr << "Invalid response" << std::endl;
return;
}
if (status_code != 200) {
std::cerr << "Response returned with status code " << status_code << std::endl;
return;
}
// Read and discard the response headers
boost::asio::read_until(ssl_socket, response, "\r\n\r\n");
std::string header;
while (std::getline(response_stream, header) && header != "\r") {
std::regex content_length_regex("Content-Length:\\s*(\\d+)");
std::smatch matches;
if (std::regex_search(header, matches, content_length_regex) && matches.size() > 1) {
size = std::stoull(matches[1].str());
std::cout << "File size: " << size << " bytes" << std::endl;
}
}
// Calculate the full save path
std::string filename = downloadPath.substr(downloadPath.find_last_of("/") + 1);
std::string fullSavePath = this->savePath + (this->savePath.back() == '/' ? "" : "/") + filename;
if (std::ifstream(fullSavePath)) {
std::cerr << "File already exists. Downloading copy." << std::endl;
fullSavePath = getNewFileName(fullSavePath); // Use the getNewFileName function to get a new file name
}
std::ofstream outfile(fullSavePath, std::ios::binary);
std::cout << "Saving to: " << fullSavePath << std::endl;
// Read until EOF, writing data to the output file
boost::system::error_code ec;
while (boost::asio::read(ssl_socket, response, boost::asio::transfer_at_least(1), ec)) {
{
std::unique_lock<std::mutex> lock(mtx);
cv.wait(lock, [this] { return !paused; });
}
{
std::lock_guard<std::mutex> lock(mtx);
started = true;
if (cancelled) {
return;
}
if (paused) {
std::cout << "Stopping download" << std::endl;
break;
}
}
outfile << &response;
}
if (ec != boost::asio::error::eof) {
std::cerr << "Error during read: " << ec.message() << std::endl;
}
setCompleted(true);
outfile.close();
}
bool Download::parse_pasv_response(const std::string &response, std::string &ip_address, unsigned short &port) {
std::regex pasv_regex("\\((\\d+),(\\d+),(\\d+),(\\d+),(\\d+),(\\d+)\\)");
std::smatch matches;
if (std::regex_search(response, matches, pasv_regex) && matches.size() == 7) {
ip_address = matches[1].str() + "." + matches[2].str() + "." + matches[3].str() + "." + matches[4].str();
int p1 = std::stoi(matches[5].str());
int p2 = std::stoi(matches[6].str());
port = static_cast<unsigned short>((p1 * 256) + p2);
return true;
}
return false;
}
std::string Download::getNewFileName(const std::string& originalPath) {
std::string basePath = originalPath.substr(0, originalPath.find_last_of("."));
std::string extension = originalPath.substr(originalPath.find_last_of("."));
int counter = 1; // Start numbering from 1
std::string newPath = basePath + "(" + std::to_string(counter) + ")" + extension;
// Keep incrementing the counter until the file doesn't exist
while (std::ifstream(newPath)) {
newPath = basePath + "(" + std::to_string(++counter) + ")" + extension;
}
return newPath;
}