2003-10-23 01:00:57 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2003, Arvid Norberg
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <iterator>
|
|
|
|
|
2007-03-02 02:16:59 +01:00
|
|
|
#include "libtorrent/config.hpp"
|
|
|
|
|
2014-11-19 10:23:08 +01:00
|
|
|
#ifdef TORRENT_WINDOWS
|
2015-01-07 17:47:37 +01:00
|
|
|
#include <direct.h> // for _mkdir and _getcwd
|
2014-11-29 02:53:22 +01:00
|
|
|
#include <sys/types.h> // for _stat
|
|
|
|
#include <sys/stat.h>
|
2014-11-19 10:23:08 +01:00
|
|
|
#endif
|
|
|
|
|
2004-01-25 19:18:36 +01:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(push, 1)
|
|
|
|
#endif
|
|
|
|
|
2004-01-17 21:04:19 +01:00
|
|
|
#include <boost/bind.hpp>
|
2011-12-23 06:36:13 +01:00
|
|
|
#include <boost/unordered_set.hpp>
|
2003-11-02 22:06:50 +01:00
|
|
|
|
2004-01-25 19:18:36 +01:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif
|
|
|
|
|
2007-12-11 21:02:55 +01:00
|
|
|
#include "libtorrent/extensions/ut_metadata.hpp"
|
2006-11-26 18:44:51 +01:00
|
|
|
#include "libtorrent/extensions/ut_pex.hpp"
|
2007-12-18 07:04:54 +01:00
|
|
|
#include "libtorrent/extensions/smart_ban.hpp"
|
2006-11-14 01:08:16 +01:00
|
|
|
|
2015-09-18 06:23:45 +02:00
|
|
|
#include "libtorrent/torrent_info.hpp"
|
|
|
|
#include "libtorrent/announce_entry.hpp"
|
2003-10-23 01:00:57 +02:00
|
|
|
#include "libtorrent/entry.hpp"
|
|
|
|
#include "libtorrent/bencode.hpp"
|
|
|
|
#include "libtorrent/session.hpp"
|
2003-12-22 08:14:35 +01:00
|
|
|
#include "libtorrent/identify_client.hpp"
|
2004-02-01 01:55:28 +01:00
|
|
|
#include "libtorrent/alert_types.hpp"
|
2006-10-11 16:02:21 +02:00
|
|
|
#include "libtorrent/ip_filter.hpp"
|
2007-12-03 07:03:16 +01:00
|
|
|
#include "libtorrent/magnet_uri.hpp"
|
2008-05-28 08:50:11 +02:00
|
|
|
#include "libtorrent/bitfield.hpp"
|
2009-11-26 06:45:43 +01:00
|
|
|
#include "libtorrent/peer_info.hpp"
|
2015-03-12 06:20:12 +01:00
|
|
|
#include "libtorrent/bdecode.hpp"
|
2014-07-06 21:18:00 +02:00
|
|
|
#include "libtorrent/add_torrent_params.hpp"
|
2010-08-23 10:48:02 +02:00
|
|
|
#include "libtorrent/time.hpp"
|
2012-02-21 06:56:22 +01:00
|
|
|
#include "libtorrent/create_torrent.hpp"
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2014-07-08 17:35:14 +02:00
|
|
|
#include "torrent_view.hpp"
|
2014-07-09 00:37:13 +02:00
|
|
|
#include "session_view.hpp"
|
2014-07-08 17:35:14 +02:00
|
|
|
#include "print.hpp"
|
|
|
|
|
2005-08-15 00:04:58 +02:00
|
|
|
using boost::bind;
|
2014-07-06 21:18:00 +02:00
|
|
|
using libtorrent::total_milliseconds;
|
|
|
|
|
2015-03-15 01:32:55 +01:00
|
|
|
void sleep_ms(int milliseconds)
|
|
|
|
{
|
|
|
|
#if defined TORRENT_WINDOWS || defined TORRENT_CYGWIN
|
|
|
|
Sleep(milliseconds);
|
|
|
|
#elif defined TORRENT_BEOS
|
|
|
|
snooze_until(system_time() + boost::int64_t(milliseconds) * 1000, B_SYSTEM_TIMEBASE);
|
|
|
|
#else
|
|
|
|
usleep(milliseconds * 1000);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
#ifdef _WIN32
|
2003-11-09 19:17:09 +01:00
|
|
|
|
2003-11-02 22:06:50 +01:00
|
|
|
#include <windows.h>
|
2003-10-30 00:28:09 +01:00
|
|
|
#include <conio.h>
|
|
|
|
|
2011-03-31 05:19:05 +02:00
|
|
|
bool sleep_and_input(int* c, int sleep)
|
2003-10-30 00:28:09 +01:00
|
|
|
{
|
2011-06-06 06:24:12 +02:00
|
|
|
for (int i = 0; i < 2; ++i)
|
2003-10-30 00:28:09 +01:00
|
|
|
{
|
2009-05-13 18:02:43 +02:00
|
|
|
if (_kbhit())
|
|
|
|
{
|
|
|
|
*c = _getch();
|
|
|
|
return true;
|
|
|
|
}
|
2011-06-06 06:24:12 +02:00
|
|
|
Sleep(sleep / 2);
|
2003-10-30 00:28:09 +01:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2003-10-31 12:17:45 +01:00
|
|
|
#else
|
|
|
|
|
2014-08-16 19:33:25 +02:00
|
|
|
#include <stdio.h> // for snprintf
|
|
|
|
#include <stdlib.h> // for atoi
|
2003-10-31 12:17:45 +01:00
|
|
|
|
|
|
|
#include <termios.h>
|
|
|
|
#include <string.h>
|
2007-07-23 01:24:27 +02:00
|
|
|
#include <sys/ioctl.h>
|
2014-07-06 21:18:00 +02:00
|
|
|
#include <signal.h>
|
2005-08-15 00:04:58 +02:00
|
|
|
|
2003-10-31 12:17:45 +01:00
|
|
|
struct set_keypress
|
|
|
|
{
|
|
|
|
set_keypress()
|
|
|
|
{
|
2005-08-25 15:11:39 +02:00
|
|
|
termios new_settings;
|
|
|
|
tcgetattr(0,&stored_settings);
|
|
|
|
new_settings = stored_settings;
|
2003-10-31 12:17:45 +01:00
|
|
|
// Disable canonical mode, and set buffer size to 1 byte
|
2015-05-26 21:10:49 +02:00
|
|
|
// and disable echo
|
|
|
|
new_settings.c_lflag &= ~(ICANON | ECHO);
|
2005-08-25 15:11:39 +02:00
|
|
|
new_settings.c_cc[VTIME] = 0;
|
|
|
|
new_settings.c_cc[VMIN] = 1;
|
|
|
|
tcsetattr(0,TCSANOW,&new_settings);
|
2003-10-31 12:17:45 +01:00
|
|
|
}
|
2005-08-25 15:11:39 +02:00
|
|
|
~set_keypress() { tcsetattr(0,TCSANOW,&stored_settings); }
|
|
|
|
termios stored_settings;
|
2003-10-31 12:17:45 +01:00
|
|
|
};
|
|
|
|
|
2011-03-31 05:19:05 +02:00
|
|
|
bool sleep_and_input(int* c, int sleep)
|
2003-10-31 12:17:45 +01:00
|
|
|
{
|
2015-03-12 05:34:54 +01:00
|
|
|
libtorrent::time_point start = libtorrent::clock_type::now();
|
2010-08-23 10:48:02 +02:00
|
|
|
int ret = 0;
|
|
|
|
retry:
|
2003-10-31 12:17:45 +01:00
|
|
|
fd_set set;
|
|
|
|
FD_ZERO(&set);
|
|
|
|
FD_SET(0, &set);
|
2011-06-06 06:24:12 +02:00
|
|
|
timeval tv = {sleep/ 1000, (sleep % 1000) * 1000 };
|
2010-08-23 10:48:02 +02:00
|
|
|
ret = select(1, &set, 0, 0, &tv);
|
|
|
|
if (ret > 0)
|
2010-01-05 14:00:04 +01:00
|
|
|
{
|
2011-03-31 05:19:05 +02:00
|
|
|
*c = getc(stdin);
|
2010-01-05 14:00:04 +01:00
|
|
|
return true;
|
|
|
|
}
|
2010-08-23 10:48:02 +02:00
|
|
|
if (errno == EINTR)
|
|
|
|
{
|
2015-03-12 05:34:54 +01:00
|
|
|
if (total_milliseconds(libtorrent::clock_type::now() - start) < sleep)
|
2010-08-23 10:48:02 +02:00
|
|
|
goto retry;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret < 0 && errno != 0 && errno != ETIMEDOUT)
|
2011-06-06 06:24:12 +02:00
|
|
|
{
|
2010-08-23 10:48:02 +02:00
|
|
|
fprintf(stderr, "select failed: %s\n", strerror(errno));
|
2015-03-15 01:32:55 +01:00
|
|
|
sleep_ms(500);
|
2011-06-06 06:24:12 +02:00
|
|
|
}
|
2010-08-23 10:48:02 +02:00
|
|
|
|
2003-10-31 12:17:45 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2003-10-30 00:28:09 +01:00
|
|
|
#endif
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2008-11-29 09:38:40 +01:00
|
|
|
bool print_trackers = false;
|
2008-04-05 06:53:22 +02:00
|
|
|
bool print_peers = false;
|
|
|
|
bool print_log = false;
|
|
|
|
bool print_downloads = false;
|
|
|
|
bool print_file_progress = false;
|
2009-01-13 18:49:14 +01:00
|
|
|
bool show_pad_files = false;
|
2008-09-20 19:42:25 +02:00
|
|
|
bool show_dht_status = false;
|
2008-04-05 06:53:22 +02:00
|
|
|
bool sequential_download = false;
|
|
|
|
|
|
|
|
bool print_ip = true;
|
|
|
|
bool print_timers = false;
|
|
|
|
bool print_block = false;
|
|
|
|
bool print_peer_rate = false;
|
|
|
|
bool print_fails = false;
|
|
|
|
bool print_send_bufs = true;
|
2014-07-06 21:18:00 +02:00
|
|
|
bool print_disk_stats = false;
|
2009-05-13 03:02:06 +02:00
|
|
|
|
2011-07-04 07:33:06 +02:00
|
|
|
// the number of times we've asked to save resume data
|
|
|
|
// without having received a response (successful or failure)
|
|
|
|
int num_outstanding_resume_data = 0;
|
|
|
|
|
2015-01-17 18:02:58 +01:00
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
|
|
|
std::vector<libtorrent::dht_lookup> dht_active_requests;
|
|
|
|
std::vector<libtorrent::dht_routing_bucket> dht_routing_table;
|
|
|
|
#endif
|
|
|
|
|
2014-07-08 17:35:14 +02:00
|
|
|
torrent_view view;
|
2014-07-09 00:37:13 +02:00
|
|
|
session_view ses_view;
|
2014-07-08 17:35:14 +02:00
|
|
|
|
2014-07-09 00:37:13 +02:00
|
|
|
int load_file(std::string const& filename, std::vector<char>& v
|
|
|
|
, libtorrent::error_code& ec, int limit = 8000000)
|
2013-07-19 23:41:33 +02:00
|
|
|
{
|
|
|
|
ec.clear();
|
|
|
|
FILE* f = fopen(filename.c_str(), "rb");
|
|
|
|
if (f == NULL)
|
|
|
|
{
|
2015-11-24 06:50:51 +01:00
|
|
|
ec.assign(errno, boost::system::system_category());
|
2013-07-19 23:41:33 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int r = fseek(f, 0, SEEK_END);
|
|
|
|
if (r != 0)
|
|
|
|
{
|
2015-11-24 06:50:51 +01:00
|
|
|
ec.assign(errno, boost::system::system_category());
|
2013-07-19 23:41:33 +02:00
|
|
|
fclose(f);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
long s = ftell(f);
|
|
|
|
if (s < 0)
|
|
|
|
{
|
2015-11-24 06:50:51 +01:00
|
|
|
ec.assign(errno, boost::system::system_category());
|
2013-07-19 23:41:33 +02:00
|
|
|
fclose(f);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s > limit)
|
|
|
|
{
|
|
|
|
fclose(f);
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = fseek(f, 0, SEEK_SET);
|
|
|
|
if (r != 0)
|
|
|
|
{
|
2015-11-24 06:50:51 +01:00
|
|
|
ec.assign(errno, boost::system::system_category());
|
2013-07-19 23:41:33 +02:00
|
|
|
fclose(f);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
v.resize(s);
|
|
|
|
if (s == 0)
|
|
|
|
{
|
|
|
|
fclose(f);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = fread(&v[0], 1, v.size(), f);
|
|
|
|
if (r < 0)
|
|
|
|
{
|
2015-11-24 06:50:51 +01:00
|
|
|
ec.assign(errno, boost::system::system_category());
|
2013-07-19 23:41:33 +02:00
|
|
|
fclose(f);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
if (r != s) return -3;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-17 04:41:01 +01:00
|
|
|
bool is_absolute_path(std::string const& f)
|
|
|
|
{
|
|
|
|
if (f.empty()) return false;
|
|
|
|
#if defined(TORRENT_WINDOWS) || defined(TORRENT_OS2)
|
|
|
|
int i = 0;
|
|
|
|
// match the xx:\ or xx:/ form
|
|
|
|
while (f[i] && strchr("abcdefghijklmnopqrstuvxyz", f[i])) ++i;
|
|
|
|
if (i < int(f.size()-1) && f[i] == ':' && (f[i+1] == '\\' || f[i+1] == '/'))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// match the \\ form
|
|
|
|
if (int(f.size()) >= 2 && f[0] == '\\' && f[1] == '\\')
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
#else
|
|
|
|
if (f[0] == '/') return true;
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-01-02 12:11:09 +01:00
|
|
|
std::string leaf_path(std::string f)
|
|
|
|
{
|
|
|
|
if (f.empty()) return "";
|
|
|
|
char const* first = f.c_str();
|
|
|
|
char const* sep = strrchr(first, '/');
|
|
|
|
#if defined(TORRENT_WINDOWS) || defined(TORRENT_OS2)
|
|
|
|
char const* altsep = strrchr(first, '\\');
|
|
|
|
if (sep == 0 || altsep > sep) sep = altsep;
|
|
|
|
#endif
|
|
|
|
if (sep == 0) return f;
|
|
|
|
|
|
|
|
if (sep - first == int(f.size()) - 1)
|
|
|
|
{
|
|
|
|
// if the last character is a / (or \)
|
|
|
|
// ignore it
|
|
|
|
int len = 0;
|
|
|
|
while (sep > first)
|
|
|
|
{
|
|
|
|
--sep;
|
|
|
|
if (*sep == '/'
|
|
|
|
#if defined(TORRENT_WINDOWS) || defined(TORRENT_OS2)
|
|
|
|
|| *sep == '\\'
|
|
|
|
#endif
|
|
|
|
)
|
|
|
|
return std::string(sep + 1, len);
|
|
|
|
++len;
|
|
|
|
}
|
|
|
|
return std::string(first, len);
|
|
|
|
}
|
|
|
|
return std::string(sep + 1);
|
|
|
|
}
|
|
|
|
|
2014-11-17 04:41:01 +01:00
|
|
|
std::string path_append(std::string const& lhs, std::string const& rhs)
|
|
|
|
{
|
|
|
|
if (lhs.empty() || lhs == ".") return rhs;
|
|
|
|
if (rhs.empty() || rhs == ".") return lhs;
|
|
|
|
|
|
|
|
#if defined(TORRENT_WINDOWS) || defined(TORRENT_OS2)
|
|
|
|
#define TORRENT_SEPARATOR "\\"
|
|
|
|
bool need_sep = lhs[lhs.size()-1] != '\\' && lhs[lhs.size()-1] != '/';
|
|
|
|
#else
|
|
|
|
#define TORRENT_SEPARATOR "/"
|
|
|
|
bool need_sep = lhs[lhs.size()-1] != '/';
|
|
|
|
#endif
|
|
|
|
return lhs + (need_sep?TORRENT_SEPARATOR:"") + rhs;
|
|
|
|
}
|
|
|
|
|
2013-07-20 00:47:17 +02:00
|
|
|
bool is_hex(char const *in, int len)
|
|
|
|
{
|
|
|
|
for (char const* end = in + len; in < end; ++in)
|
|
|
|
{
|
2013-07-20 01:16:57 +02:00
|
|
|
if (*in >= '0' && *in <= '9') continue;
|
|
|
|
if (*in >= 'A' && *in <= 'F') continue;
|
|
|
|
if (*in >= 'a' && *in <= 'f') continue;
|
2013-07-20 00:47:17 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-07-20 03:02:15 +02:00
|
|
|
std::string print_endpoint(libtorrent::tcp::endpoint const& ep)
|
|
|
|
{
|
|
|
|
using namespace libtorrent;
|
|
|
|
error_code ec;
|
|
|
|
char buf[200];
|
|
|
|
address const& addr = ep.address();
|
|
|
|
#if TORRENT_USE_IPV6
|
|
|
|
if (addr.is_v6())
|
|
|
|
snprintf(buf, sizeof(buf), "[%s]:%d", addr.to_string(ec).c_str(), ep.port());
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
snprintf(buf, sizeof(buf), "%s:%d", addr.to_string(ec).c_str(), ep.port());
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2010-11-06 19:04:07 +01:00
|
|
|
struct torrent_entry
|
|
|
|
{
|
|
|
|
torrent_entry(libtorrent::torrent_handle h) : handle(h) {}
|
|
|
|
libtorrent::torrent_handle handle;
|
|
|
|
libtorrent::torrent_status status;
|
|
|
|
};
|
|
|
|
|
2011-02-01 10:48:28 +01:00
|
|
|
// maps filenames to torrent_handles
|
2014-07-06 21:18:00 +02:00
|
|
|
typedef std::map<std::string, libtorrent::torrent_handle> handles_t;
|
|
|
|
typedef std::map<libtorrent::sha1_hash, std::string> files_t;
|
|
|
|
|
|
|
|
files_t hash_to_filename;
|
2010-11-06 19:04:07 +01:00
|
|
|
|
2011-02-01 10:48:28 +01:00
|
|
|
using libtorrent::torrent_status;
|
2010-11-06 17:57:15 +01:00
|
|
|
|
2011-02-01 10:48:28 +01:00
|
|
|
bool yes(libtorrent::torrent_status const&)
|
|
|
|
{ return true; }
|
|
|
|
|
2011-12-23 06:36:13 +01:00
|
|
|
FILE* g_log_file = 0;
|
|
|
|
|
2008-05-28 08:50:11 +02:00
|
|
|
std::string const& piece_bar(libtorrent::bitfield const& p, int width)
|
2007-07-09 02:00:35 +02:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
const int table_size = 18;
|
2015-06-11 03:10:22 +02:00
|
|
|
|
2007-07-09 02:00:35 +02:00
|
|
|
double piece_per_char = p.size() / double(width);
|
|
|
|
static std::string bar;
|
|
|
|
bar.clear();
|
|
|
|
bar.reserve(width * 6);
|
|
|
|
bar += "[";
|
2008-08-29 02:42:57 +02:00
|
|
|
if (p.size() == 0)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < width; ++i) bar += ' ';
|
|
|
|
bar += "]";
|
|
|
|
return bar;
|
|
|
|
}
|
2007-07-09 02:00:35 +02:00
|
|
|
|
|
|
|
// the [piece, piece + pieces_per_char) range is the pieces that are represented by each character
|
|
|
|
double piece = 0;
|
|
|
|
for (int i = 0; i < width; ++i, piece += piece_per_char)
|
|
|
|
{
|
|
|
|
int num_pieces = 0;
|
|
|
|
int num_have = 0;
|
2007-10-01 19:17:49 +02:00
|
|
|
int end = (std::max)(int(piece + piece_per_char), int(piece) + 1);
|
2007-07-09 02:00:35 +02:00
|
|
|
for (int k = int(piece); k < end; ++k, ++num_pieces)
|
|
|
|
if (p[k]) ++num_have;
|
2015-04-29 07:48:09 +02:00
|
|
|
int color = int(std::ceil(num_have / float((std::max)(num_pieces, 1)) * (table_size - 1)));
|
2014-07-06 21:18:00 +02:00
|
|
|
char buf[10];
|
|
|
|
snprintf(buf, 10, "48;5;%d", 232 + color);
|
|
|
|
bar += esc(buf);
|
2007-07-09 02:00:35 +02:00
|
|
|
bar += " ";
|
|
|
|
}
|
|
|
|
bar += esc("0");
|
|
|
|
bar += "]";
|
|
|
|
return bar;
|
|
|
|
}
|
|
|
|
|
2007-01-10 16:02:25 +01:00
|
|
|
int peer_index(libtorrent::tcp::endpoint addr, std::vector<libtorrent::peer_info> const& peers)
|
2005-08-15 00:04:58 +02:00
|
|
|
{
|
|
|
|
using namespace libtorrent;
|
|
|
|
std::vector<peer_info>::const_iterator i = std::find_if(peers.begin()
|
2010-07-22 18:49:40 +02:00
|
|
|
, peers.end(), boost::bind(&peer_info::ip, _1) == addr);
|
2007-01-10 16:02:25 +01:00
|
|
|
if (i == peers.end()) return -1;
|
2005-08-15 00:04:58 +02:00
|
|
|
|
2007-01-10 16:02:25 +01:00
|
|
|
return i - peers.begin();
|
2005-08-15 00:04:58 +02:00
|
|
|
}
|
|
|
|
|
2015-02-08 04:24:56 +01:00
|
|
|
// returns the number of lines printed
|
|
|
|
int print_peer_info(std::string& out
|
|
|
|
, std::vector<libtorrent::peer_info> const& peers, int max_lines)
|
2004-07-24 13:54:17 +02:00
|
|
|
{
|
|
|
|
using namespace libtorrent;
|
2015-02-08 04:24:56 +01:00
|
|
|
int pos = 0;
|
2014-07-06 21:18:00 +02:00
|
|
|
if (print_ip) out += "IP ";
|
|
|
|
out += "progress down (total | peak ) up (total | peak ) sent-req tmo bsy rcv flags dn up source ";
|
2009-05-13 03:02:06 +02:00
|
|
|
if (print_fails) out += "fail hshf ";
|
2014-07-06 21:18:00 +02:00
|
|
|
if (print_send_bufs) out += "rq sndb rcvb q-bytes ";
|
2009-05-13 03:02:06 +02:00
|
|
|
if (print_timers) out += "inactive wait timeout q-time ";
|
2014-11-24 05:35:52 +01:00
|
|
|
out += " v disk ^ rtt ";
|
2009-05-13 03:02:06 +02:00
|
|
|
if (print_block) out += "block-progress ";
|
2007-05-24 22:23:14 +02:00
|
|
|
#ifndef TORRENT_DISABLE_RESOLVE_COUNTRIES
|
2009-05-13 03:02:06 +02:00
|
|
|
out += "country ";
|
2007-05-24 22:23:14 +02:00
|
|
|
#endif
|
2010-02-09 04:04:41 +01:00
|
|
|
if (print_peer_rate) out += "peer-rate est.rec.rate ";
|
2015-02-08 04:24:56 +01:00
|
|
|
out += "client \x1b[K\n";
|
|
|
|
++pos;
|
2004-07-24 13:54:17 +02:00
|
|
|
|
2009-05-15 18:21:36 +02:00
|
|
|
char str[500];
|
2004-07-24 13:54:17 +02:00
|
|
|
for (std::vector<peer_info>::const_iterator i = peers.begin();
|
2005-05-25 12:01:01 +02:00
|
|
|
i != peers.end(); ++i)
|
2004-07-24 13:54:17 +02:00
|
|
|
{
|
2014-10-05 21:32:29 +02:00
|
|
|
if (i->flags & (peer_info::handshake | peer_info::connecting))
|
2007-03-15 23:03:56 +01:00
|
|
|
continue;
|
|
|
|
|
2008-04-05 06:53:22 +02:00
|
|
|
if (print_ip)
|
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
snprintf(str, sizeof(str), "%-30s ", (::print_endpoint(i->ip) +
|
2013-10-27 20:56:37 +01:00
|
|
|
(i->flags & peer_info::utp_socket ? " [uTP]" : "") +
|
|
|
|
(i->flags & peer_info::i2p_socket ? " [i2p]" : "")
|
2014-07-06 21:18:00 +02:00
|
|
|
).c_str());
|
2009-05-13 03:02:06 +02:00
|
|
|
out += str;
|
2008-04-05 06:53:22 +02:00
|
|
|
}
|
|
|
|
|
2011-06-06 06:24:12 +02:00
|
|
|
char temp[10];
|
|
|
|
snprintf(temp, sizeof(temp), "%d/%d"
|
|
|
|
, i->download_queue_length
|
|
|
|
, i->target_dl_queue_length);
|
|
|
|
temp[7] = 0;
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
char peer_progress[10];
|
|
|
|
snprintf(peer_progress, sizeof(peer_progress), "%.1f%%", i->progress_ppm / 10000.f);
|
2009-05-15 18:21:36 +02:00
|
|
|
snprintf(str, sizeof(str)
|
2014-07-06 21:18:00 +02:00
|
|
|
, "%s %s%s (%s|%s) %s%s (%s|%s) %s%7s %4d%4d%4d %s%s%s%s%s%s%s%s%s%s%s%s%s %s%s%s %s%s%s %s%s%s%s%s%s "
|
|
|
|
, progress_bar(i->progress_ppm / 1000, 15, col_green, '#', '-', peer_progress).c_str()
|
2009-05-13 03:02:06 +02:00
|
|
|
, esc("32"), add_suffix(i->down_speed, "/s").c_str()
|
|
|
|
, add_suffix(i->total_download).c_str(), add_suffix(i->download_rate_peak, "/s").c_str()
|
|
|
|
, esc("31"), add_suffix(i->up_speed, "/s").c_str(), add_suffix(i->total_upload).c_str()
|
2009-05-15 18:21:36 +02:00
|
|
|
, add_suffix(i->upload_rate_peak, "/s").c_str(), esc("0")
|
|
|
|
|
2011-06-06 06:24:12 +02:00
|
|
|
, temp // sent requests and target number of outstanding reqs.
|
|
|
|
, i->timed_out_requests
|
|
|
|
, i->busy_requests
|
2009-05-15 18:21:36 +02:00
|
|
|
, i->upload_queue_length
|
2009-05-13 03:02:06 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
, color("I", (i->flags & peer_info::interesting)?col_white:col_blue).c_str()
|
|
|
|
, color("C", (i->flags & peer_info::choked)?col_white:col_blue).c_str()
|
|
|
|
, color("i", (i->flags & peer_info::remote_interested)?col_white:col_blue).c_str()
|
|
|
|
, color("c", (i->flags & peer_info::remote_choked)?col_white:col_blue).c_str()
|
|
|
|
, color("x", (i->flags & peer_info::supports_extensions)?col_white:col_blue).c_str()
|
|
|
|
, color("o", (i->flags & peer_info::local_connection)?col_white:col_blue).c_str()
|
|
|
|
, color("p", (i->flags & peer_info::on_parole)?col_white:col_blue).c_str()
|
|
|
|
, color("O", (i->flags & peer_info::optimistic_unchoke)?col_white:col_blue).c_str()
|
|
|
|
, color("S", (i->flags & peer_info::snubbed)?col_white:col_blue).c_str()
|
|
|
|
, color("U", (i->flags & peer_info::upload_only)?col_white:col_blue).c_str()
|
|
|
|
, color("e", (i->flags & peer_info::endgame_mode)?col_white:col_blue).c_str()
|
|
|
|
, color("E", (i->flags & peer_info::rc4_encrypted)?col_white:(i->flags & peer_info::plaintext_encrypted)?col_cyan:col_blue).c_str()
|
|
|
|
, color("h", (i->flags & peer_info::holepunched)?col_white:col_blue).c_str()
|
|
|
|
|
|
|
|
, color("d", (i->read_state & peer_info::bw_disk)?col_white:col_blue).c_str()
|
|
|
|
, color("l", (i->read_state & peer_info::bw_limit)?col_white:col_blue).c_str()
|
|
|
|
, color("n", (i->read_state & peer_info::bw_network)?col_white:col_blue).c_str()
|
|
|
|
, color("d", (i->write_state & peer_info::bw_disk)?col_white:col_blue).c_str()
|
|
|
|
, color("l", (i->write_state & peer_info::bw_limit)?col_white:col_blue).c_str()
|
|
|
|
, color("n", (i->write_state & peer_info::bw_network)?col_white:col_blue).c_str()
|
|
|
|
|
|
|
|
, color("t", (i->source & peer_info::tracker)?col_white:col_blue).c_str()
|
|
|
|
, color("p", (i->source & peer_info::pex)?col_white:col_blue).c_str()
|
|
|
|
, color("d", (i->source & peer_info::dht)?col_white:col_blue).c_str()
|
|
|
|
, color("l", (i->source & peer_info::lsd)?col_white:col_blue).c_str()
|
|
|
|
, color("r", (i->source & peer_info::resume_data)?col_white:col_blue).c_str()
|
|
|
|
, color("i", (i->source & peer_info::incoming)?col_white:col_blue).c_str());
|
2009-05-13 03:02:06 +02:00
|
|
|
out += str;
|
|
|
|
|
2008-04-05 06:53:22 +02:00
|
|
|
if (print_fails)
|
2004-07-24 13:54:17 +02:00
|
|
|
{
|
2009-05-13 03:02:06 +02:00
|
|
|
snprintf(str, sizeof(str), "%3d %3d "
|
|
|
|
, i->failcount, i->num_hashfails);
|
|
|
|
out += str;
|
2004-07-24 13:54:17 +02:00
|
|
|
}
|
2008-04-05 06:53:22 +02:00
|
|
|
if (print_send_bufs)
|
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
snprintf(str, sizeof(str), "%2d %6d %6d%5dkB "
|
|
|
|
, i->requests_in_buffer, i->used_send_buffer
|
|
|
|
, i->used_receive_buffer
|
|
|
|
, i->queue_bytes / 1000);
|
2009-05-13 03:02:06 +02:00
|
|
|
out += str;
|
2008-04-05 06:53:22 +02:00
|
|
|
}
|
|
|
|
if (print_timers)
|
2005-05-30 19:43:03 +02:00
|
|
|
{
|
2014-12-08 18:06:04 +01:00
|
|
|
char req_timeout[20] = "-";
|
|
|
|
// timeout is only meaningful if there is at least one outstanding
|
|
|
|
// request to the peer
|
|
|
|
if (i->download_queue_length > 0)
|
|
|
|
snprintf(req_timeout, sizeof(req_timeout), "%d", i->request_timeout);
|
|
|
|
|
|
|
|
snprintf(str, sizeof(str), "%8d %4d %7s %6d "
|
2014-07-06 21:18:00 +02:00
|
|
|
, int(total_seconds(i->last_active))
|
|
|
|
, int(total_seconds(i->last_request))
|
2014-12-08 18:06:04 +01:00
|
|
|
, req_timeout
|
2014-07-06 21:18:00 +02:00
|
|
|
, int(total_seconds(i->download_queue_time)));
|
2009-05-13 03:02:06 +02:00
|
|
|
out += str;
|
2008-04-05 06:53:22 +02:00
|
|
|
}
|
2014-11-24 05:35:52 +01:00
|
|
|
snprintf(str, sizeof(str), "%s|%s %5d "
|
2009-05-13 03:02:06 +02:00
|
|
|
, add_suffix(i->pending_disk_bytes).c_str()
|
2014-07-06 21:18:00 +02:00
|
|
|
, add_suffix(i->pending_disk_read_bytes).c_str()
|
2009-05-13 03:02:06 +02:00
|
|
|
, i->rtt);
|
|
|
|
out += str;
|
2008-04-05 06:53:22 +02:00
|
|
|
|
|
|
|
if (print_block)
|
|
|
|
{
|
|
|
|
if (i->downloading_piece_index >= 0)
|
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
char buf[50];
|
|
|
|
snprintf(buf, sizeof(buf), "%d:%d", i->downloading_piece_index, i->downloading_block_index);
|
2009-05-13 03:02:06 +02:00
|
|
|
out += progress_bar(
|
2014-07-06 21:18:00 +02:00
|
|
|
i->downloading_progress * 1000 / i->downloading_total, 14, col_green, '-', '#', buf);
|
2008-04-05 06:53:22 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-07-19 06:59:27 +02:00
|
|
|
out += progress_bar(0, 14);
|
2008-04-05 06:53:22 +02:00
|
|
|
}
|
2005-05-30 19:43:03 +02:00
|
|
|
}
|
2004-07-24 13:54:17 +02:00
|
|
|
|
2007-05-24 22:23:14 +02:00
|
|
|
#ifndef TORRENT_DISABLE_RESOLVE_COUNTRIES
|
2007-01-29 08:39:33 +01:00
|
|
|
if (i->country[0] == 0)
|
|
|
|
{
|
2009-05-13 03:02:06 +02:00
|
|
|
out += " ..";
|
2007-01-29 08:39:33 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-05-13 03:02:06 +02:00
|
|
|
snprintf(str, sizeof(str), " %c%c", i->country[0], i->country[1]);
|
|
|
|
out += str;
|
2007-01-29 08:39:33 +01:00
|
|
|
}
|
2007-05-24 22:23:14 +02:00
|
|
|
#endif
|
2009-05-13 03:02:06 +02:00
|
|
|
if (print_peer_rate)
|
|
|
|
{
|
2010-02-09 04:04:41 +01:00
|
|
|
bool unchoked = (i->flags & peer_info::choked) == 0;
|
|
|
|
|
2010-02-13 17:29:17 +01:00
|
|
|
snprintf(str, sizeof(str), " %s %s"
|
2010-02-09 04:04:41 +01:00
|
|
|
, add_suffix(i->remote_dl_rate, "/s").c_str()
|
|
|
|
, unchoked ? add_suffix(i->estimated_reciprocation_rate, "/s").c_str() : " ");
|
2009-05-13 03:02:06 +02:00
|
|
|
out += str;
|
|
|
|
}
|
|
|
|
out += " ";
|
2007-06-07 02:05:18 +02:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
if (i->flags & peer_info::handshake)
|
|
|
|
{
|
2009-05-13 03:02:06 +02:00
|
|
|
out += esc("31");
|
|
|
|
out += " waiting for handshake";
|
|
|
|
out += esc("0");
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
|
|
|
else if (i->flags & peer_info::connecting)
|
2005-11-02 17:28:39 +01:00
|
|
|
{
|
2009-05-13 03:02:06 +02:00
|
|
|
out += esc("31");
|
|
|
|
out += " connecting to peer";
|
|
|
|
out += esc("0");
|
2005-11-02 17:28:39 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-05-13 03:02:06 +02:00
|
|
|
out += " ";
|
|
|
|
out += i->client;
|
2005-11-02 17:28:39 +01:00
|
|
|
}
|
2015-02-08 04:24:56 +01:00
|
|
|
out += "\x1b[K\n";
|
|
|
|
++pos;
|
|
|
|
if (pos >= max_lines) break;
|
2004-07-24 13:54:17 +02:00
|
|
|
}
|
2015-02-08 04:24:56 +01:00
|
|
|
return pos;
|
2004-07-24 13:54:17 +02:00
|
|
|
}
|
|
|
|
|
2010-01-12 02:56:48 +01:00
|
|
|
int listen_port = 6881;
|
2010-01-18 06:29:35 +01:00
|
|
|
int allocation_mode = libtorrent::storage_mode_sparse;
|
2010-01-12 02:56:48 +01:00
|
|
|
std::string save_path(".");
|
|
|
|
int torrent_upload_limit = 0;
|
|
|
|
int torrent_download_limit = 0;
|
|
|
|
std::string monitor_dir;
|
|
|
|
std::string bind_to_interface = "";
|
|
|
|
int poll_interval = 5;
|
|
|
|
int max_connections_per_torrent = 50;
|
2011-08-07 08:34:27 +02:00
|
|
|
bool seed_mode = false;
|
2010-01-12 02:56:48 +01:00
|
|
|
|
2010-09-05 18:01:36 +02:00
|
|
|
bool share_mode = false;
|
2011-07-10 22:17:32 +02:00
|
|
|
bool disable_storage = false;
|
2010-09-05 18:01:36 +02:00
|
|
|
|
2014-07-08 17:35:14 +02:00
|
|
|
bool quit = false;
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
void signal_handler(int signo)
|
|
|
|
{
|
|
|
|
// make the main loop terminate
|
2014-07-08 17:35:14 +02:00
|
|
|
quit = true;
|
2014-07-06 21:18:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void load_torrent(libtorrent::sha1_hash const& ih, std::vector<char>& buf, libtorrent::error_code& ec)
|
|
|
|
{
|
|
|
|
files_t::iterator i = hash_to_filename.find(ih);
|
|
|
|
if (i == hash_to_filename.end())
|
|
|
|
{
|
|
|
|
// for magnet links and torrents downloaded via
|
|
|
|
// URL, the metadata is saved in the resume file
|
|
|
|
// TODO: pick up metadata from the resume file
|
|
|
|
ec.assign(boost::system::errc::no_such_file_or_directory, boost::system::generic_category());
|
|
|
|
return;
|
|
|
|
}
|
2015-07-11 09:39:00 +02:00
|
|
|
load_file(i->second, buf, ec);
|
2014-07-06 21:18:00 +02:00
|
|
|
}
|
|
|
|
|
2012-01-14 17:04:25 +01:00
|
|
|
// if non-empty, a peer that will be added to all torrents
|
|
|
|
std::string peer;
|
|
|
|
|
2005-10-19 02:50:38 +02:00
|
|
|
using boost::bind;
|
|
|
|
|
2015-01-01 19:03:56 +01:00
|
|
|
std::string path_to_url(std::string f)
|
|
|
|
{
|
|
|
|
std::string ret = "file://"
|
|
|
|
#ifdef TORRENT_WINDOWS
|
|
|
|
"/"
|
|
|
|
#endif
|
|
|
|
;
|
|
|
|
static char const hex_chars[] = "0123456789abcdef";
|
|
|
|
static const char unreserved[] =
|
|
|
|
"/-_!.~*()ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
|
|
|
"0123456789";
|
|
|
|
|
2015-01-01 22:42:55 +01:00
|
|
|
// make sure the path is an absolute path
|
2015-01-02 12:11:09 +01:00
|
|
|
if (!is_absolute_path(f))
|
2015-01-01 22:42:55 +01:00
|
|
|
{
|
|
|
|
char cwd[TORRENT_MAX_PATH];
|
2015-01-07 17:47:37 +01:00
|
|
|
#if defined TORRENT_WINDOWS && !defined TORRENT_MINGW
|
|
|
|
_getcwd(cwd, sizeof(cwd));
|
|
|
|
#else
|
2015-01-01 22:42:55 +01:00
|
|
|
getcwd(cwd, sizeof(cwd));
|
2015-01-07 17:47:37 +01:00
|
|
|
#endif
|
2015-01-01 22:42:55 +01:00
|
|
|
f = path_append(cwd, f);
|
|
|
|
}
|
|
|
|
|
2015-04-26 02:51:44 +02:00
|
|
|
for (int i = 0; i < int(f.size()); ++i)
|
2015-01-01 19:03:56 +01:00
|
|
|
{
|
|
|
|
#ifdef TORRENT_WINDOWS
|
|
|
|
if (f[i] == '\\') ret.push_back('/');
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
if (std::strchr(unreserved, f[i]) != NULL) ret.push_back(f[i]);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret.push_back('%');
|
|
|
|
ret.push_back(hex_chars[f[i] >> 4]);
|
|
|
|
ret.push_back(hex_chars[f[i] & 0xf]);
|
|
|
|
}
|
|
|
|
}
|
2015-01-01 22:42:55 +01:00
|
|
|
return ret;
|
2015-01-01 19:03:56 +01:00
|
|
|
}
|
|
|
|
|
2005-10-19 02:50:38 +02:00
|
|
|
// monitored_dir is true if this torrent is added because
|
|
|
|
// it was found in the directory that is monitored. If it
|
|
|
|
// is, it should be remembered so that it can be removed
|
|
|
|
// if it's no longer in that directory.
|
2005-10-16 11:15:46 +02:00
|
|
|
void add_torrent(libtorrent::session& ses
|
2011-10-12 12:27:17 +02:00
|
|
|
, handles_t& files
|
2011-02-09 05:54:22 +01:00
|
|
|
, std::set<libtorrent::torrent_handle>& non_files
|
2014-07-06 21:18:00 +02:00
|
|
|
, std::string torrent
|
2010-01-18 06:29:35 +01:00
|
|
|
, int allocation_mode
|
2009-10-26 02:29:39 +01:00
|
|
|
, std::string const& save_path
|
2008-01-02 19:22:34 +01:00
|
|
|
, bool monitored_dir
|
2008-01-11 00:12:58 +01:00
|
|
|
, int torrent_upload_limit
|
2008-06-29 07:34:22 +02:00
|
|
|
, int torrent_download_limit)
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
|
|
|
using namespace libtorrent;
|
2014-07-06 21:18:00 +02:00
|
|
|
static int counter = 0;
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
printf("[%d] %s\n", counter++, torrent.c_str());
|
2005-10-16 11:15:46 +02:00
|
|
|
|
2008-04-24 05:28:48 +02:00
|
|
|
add_torrent_params p;
|
2011-11-08 06:36:22 +01:00
|
|
|
if (seed_mode) p.flags |= add_torrent_params::flag_seed_mode;
|
2011-07-10 22:17:32 +02:00
|
|
|
if (disable_storage) p.storage = disabled_storage_constructor;
|
2011-11-08 06:36:22 +01:00
|
|
|
if (share_mode) p.flags |= add_torrent_params::flag_share_mode;
|
2008-07-01 01:14:31 +02:00
|
|
|
|
2014-11-17 04:41:01 +01:00
|
|
|
std::string filename = path_append(save_path, path_append(".resume"
|
2015-01-02 12:11:09 +01:00
|
|
|
, leaf_path(torrent) + ".resume"));
|
2008-07-01 01:14:31 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
error_code ec;
|
2015-07-11 09:39:00 +02:00
|
|
|
load_file(filename, p.resume_data, ec);
|
2008-07-01 01:14:31 +02:00
|
|
|
|
2015-01-01 19:03:56 +01:00
|
|
|
p.url = path_to_url(torrent);
|
2008-04-24 05:28:48 +02:00
|
|
|
p.save_path = save_path;
|
2010-01-18 06:29:35 +01:00
|
|
|
p.storage_mode = (storage_mode_t)allocation_mode;
|
2011-11-08 06:36:22 +01:00
|
|
|
p.flags |= add_torrent_params::flag_paused;
|
|
|
|
p.flags &= ~add_torrent_params::flag_duplicate_is_error;
|
|
|
|
p.flags |= add_torrent_params::flag_auto_managed;
|
2011-12-24 05:30:27 +01:00
|
|
|
p.userdata = (void*)strdup(torrent.c_str());
|
|
|
|
ses.async_add_torrent(p);
|
2014-07-06 21:18:00 +02:00
|
|
|
files.insert(std::pair<const std::string, torrent_handle>(torrent, torrent_handle()));
|
2005-10-19 02:50:38 +02:00
|
|
|
}
|
|
|
|
|
2014-11-17 04:41:01 +01:00
|
|
|
std::vector<std::string> list_dir(std::string path
|
2014-11-24 05:25:05 +01:00
|
|
|
, bool (*filter_fun)(std::string const&)
|
|
|
|
, libtorrent::error_code& ec)
|
2014-11-17 04:41:01 +01:00
|
|
|
{
|
|
|
|
std::vector<std::string> ret;
|
|
|
|
#ifdef TORRENT_WINDOWS
|
2014-11-19 10:23:08 +01:00
|
|
|
if (!path.empty() && path[path.size()-1] != '\\') path += "\\*";
|
|
|
|
else path += "*";
|
2014-11-17 04:41:01 +01:00
|
|
|
|
2015-01-08 19:19:35 +01:00
|
|
|
WIN32_FIND_DATAA fd;
|
|
|
|
HANDLE handle = FindFirstFileA(path.c_str(), &fd);
|
2014-11-17 04:41:01 +01:00
|
|
|
if (handle == INVALID_HANDLE_VALUE)
|
2014-11-24 05:25:05 +01:00
|
|
|
{
|
2014-11-28 05:53:21 +01:00
|
|
|
ec.assign(GetLastError(), boost::system::system_category());
|
2014-11-17 04:41:01 +01:00
|
|
|
return ret;
|
2014-11-24 05:25:05 +01:00
|
|
|
}
|
2014-11-17 04:41:01 +01:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2015-01-08 19:19:35 +01:00
|
|
|
std::string p = fd.cFileName;
|
2014-11-17 04:41:01 +01:00
|
|
|
if (filter_fun(p))
|
|
|
|
ret.push_back(p);
|
2015-07-11 09:39:00 +02:00
|
|
|
|
2015-01-08 19:19:35 +01:00
|
|
|
} while (FindNextFileA(handle, &fd));
|
2014-11-17 04:41:01 +01:00
|
|
|
FindClose(handle);
|
|
|
|
#else
|
|
|
|
|
|
|
|
if (!path.empty() && path[path.size()-1] == '/')
|
|
|
|
path.resize(path.size()-1);
|
|
|
|
|
|
|
|
DIR* handle = opendir(path.c_str());
|
2014-11-24 05:25:05 +01:00
|
|
|
if (handle == 0)
|
|
|
|
{
|
2015-11-24 06:50:51 +01:00
|
|
|
ec.assign(errno, boost::system::system_category());
|
2014-11-24 05:25:05 +01:00
|
|
|
return ret;
|
|
|
|
}
|
2014-11-17 04:41:01 +01:00
|
|
|
|
|
|
|
struct dirent de;
|
|
|
|
dirent* dummy;
|
|
|
|
while (readdir_r(handle, &de, &dummy) == 0)
|
|
|
|
{
|
|
|
|
if (dummy == 0) break;
|
|
|
|
|
|
|
|
std::string p = de.d_name;
|
|
|
|
if (filter_fun(p))
|
|
|
|
ret.push_back(p);
|
|
|
|
}
|
2014-11-24 05:25:05 +01:00
|
|
|
closedir(handle);
|
2014-11-17 04:41:01 +01:00
|
|
|
#endif
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool filter_fun(std::string const& p)
|
|
|
|
{
|
|
|
|
for (int i = p.size() - 1; i >= 0; --i)
|
|
|
|
{
|
|
|
|
if (p[i] == '/') break;
|
|
|
|
#ifdef TORRENT_WINDOWS
|
|
|
|
if (p[i] == '\\') break;
|
|
|
|
#endif
|
|
|
|
if (p[i] != '.') continue;
|
|
|
|
return p.compare(i, 8, ".torrent") == 0;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-10-26 02:29:39 +01:00
|
|
|
void scan_dir(std::string const& dir_path
|
2005-10-19 02:50:38 +02:00
|
|
|
, libtorrent::session& ses
|
2015-06-07 19:52:19 +02:00
|
|
|
, handles_t& files
|
2011-02-09 05:54:22 +01:00
|
|
|
, std::set<libtorrent::torrent_handle>& non_files
|
2010-01-18 06:29:35 +01:00
|
|
|
, int allocation_mode
|
2009-10-26 02:29:39 +01:00
|
|
|
, std::string const& save_path
|
2008-01-11 00:12:58 +01:00
|
|
|
, int torrent_upload_limit
|
|
|
|
, int torrent_download_limit)
|
2005-10-19 02:50:38 +02:00
|
|
|
{
|
|
|
|
std::set<std::string> valid;
|
|
|
|
|
|
|
|
using namespace libtorrent;
|
|
|
|
|
2009-10-26 02:29:39 +01:00
|
|
|
error_code ec;
|
2014-11-24 05:25:05 +01:00
|
|
|
std::vector<std::string> ents = list_dir(dir_path, filter_fun, ec);
|
|
|
|
if (ec)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "failed to list directory: (%s : %d) %s\n"
|
|
|
|
, ec.category().name(), ec.value(), ec.message().c_str());
|
|
|
|
return;
|
|
|
|
}
|
2014-01-19 05:11:49 +01:00
|
|
|
|
2014-11-17 04:41:01 +01:00
|
|
|
for (std::vector<std::string>::iterator i = ents.begin()
|
2014-01-19 05:11:49 +01:00
|
|
|
, end(ents.end()); i != end; ++i)
|
|
|
|
{
|
2014-11-17 04:41:01 +01:00
|
|
|
std::string file = path_append(dir_path, *i);
|
2005-10-16 18:58:41 +02:00
|
|
|
|
2011-02-01 10:48:28 +01:00
|
|
|
handles_t::iterator k = files.find(file);
|
|
|
|
if (k != files.end())
|
2005-10-19 02:50:38 +02:00
|
|
|
{
|
|
|
|
valid.insert(file);
|
|
|
|
continue;
|
|
|
|
}
|
2005-10-16 18:58:41 +02:00
|
|
|
|
2005-10-19 02:50:38 +02:00
|
|
|
// the file has been added to the dir, start
|
|
|
|
// downloading it.
|
2011-12-18 08:18:42 +01:00
|
|
|
add_torrent(ses, files, non_files, file, allocation_mode
|
2008-01-11 00:12:58 +01:00
|
|
|
, save_path, true, torrent_upload_limit, torrent_download_limit);
|
2005-10-19 02:50:38 +02:00
|
|
|
valid.insert(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove the torrents that are no longer in the directory
|
|
|
|
|
2011-02-01 10:48:28 +01:00
|
|
|
for (handles_t::iterator i = files.begin(); !files.empty() && i != files.end();)
|
2005-10-19 02:50:38 +02:00
|
|
|
{
|
2007-09-27 18:14:24 +02:00
|
|
|
if (i->first.empty() || valid.find(i->first) != valid.end())
|
|
|
|
{
|
|
|
|
++i;
|
|
|
|
continue;
|
|
|
|
}
|
2005-10-16 18:58:41 +02:00
|
|
|
|
2011-02-01 10:48:28 +01:00
|
|
|
torrent_handle& h = i->second;
|
2005-10-19 02:50:38 +02:00
|
|
|
if (!h.is_valid())
|
|
|
|
{
|
2011-02-01 10:48:28 +01:00
|
|
|
files.erase(i++);
|
2005-10-19 02:50:38 +02:00
|
|
|
continue;
|
|
|
|
}
|
2015-07-11 09:39:00 +02:00
|
|
|
|
2008-05-29 05:37:19 +02:00
|
|
|
h.auto_managed(false);
|
2005-10-19 02:50:38 +02:00
|
|
|
h.pause();
|
2008-04-13 20:54:36 +02:00
|
|
|
// the alert handler for save_resume_data_alert
|
2008-10-19 06:04:12 +02:00
|
|
|
// will save it to disk
|
2011-07-04 07:33:06 +02:00
|
|
|
if (h.need_save_resume_data())
|
|
|
|
{
|
|
|
|
h.save_resume_data();
|
|
|
|
++num_outstanding_resume_data;
|
|
|
|
}
|
2008-04-13 20:54:36 +02:00
|
|
|
|
2011-02-01 10:48:28 +01:00
|
|
|
files.erase(i++);
|
2005-10-19 02:50:38 +02:00
|
|
|
}
|
2005-10-16 11:15:46 +02:00
|
|
|
}
|
|
|
|
|
2014-05-10 22:20:57 +02:00
|
|
|
char const* timestamp()
|
|
|
|
{
|
|
|
|
time_t t = std::time(0);
|
|
|
|
tm* timeinfo = std::localtime(&t);
|
|
|
|
static char str[200];
|
|
|
|
std::strftime(str, 200, "%b %d %X", timeinfo);
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2009-05-13 03:02:06 +02:00
|
|
|
void print_alert(libtorrent::alert const* a, std::string& str)
|
2008-07-12 10:19:17 +02:00
|
|
|
{
|
|
|
|
using namespace libtorrent;
|
|
|
|
|
|
|
|
if (a->category() & alert::error_notification)
|
|
|
|
{
|
2009-05-13 03:02:06 +02:00
|
|
|
str += esc("31");
|
2008-07-12 10:19:17 +02:00
|
|
|
}
|
|
|
|
else if (a->category() & (alert::peer_notification | alert::storage_notification))
|
|
|
|
{
|
2009-05-13 03:02:06 +02:00
|
|
|
str += esc("33");
|
2008-07-12 10:19:17 +02:00
|
|
|
}
|
2009-05-13 03:02:06 +02:00
|
|
|
str += "[";
|
2014-05-10 22:20:57 +02:00
|
|
|
str += timestamp();
|
2009-05-13 03:02:06 +02:00
|
|
|
str += "] ";
|
|
|
|
str += a->message();
|
|
|
|
str += esc("0");
|
2008-07-12 10:19:17 +02:00
|
|
|
|
2009-05-13 03:02:06 +02:00
|
|
|
if (g_log_file)
|
2014-05-10 22:20:57 +02:00
|
|
|
fprintf(g_log_file, "[%s] %s\n", timestamp(), a->message().c_str());
|
2009-05-13 03:02:06 +02:00
|
|
|
}
|
|
|
|
|
2009-10-26 02:29:39 +01:00
|
|
|
int save_file(std::string const& filename, std::vector<char>& v)
|
2009-05-13 03:02:06 +02:00
|
|
|
{
|
2014-10-15 07:07:26 +02:00
|
|
|
FILE* f = fopen(filename.c_str(), "wb");
|
|
|
|
if (f == NULL)
|
|
|
|
return -1;
|
2009-05-13 03:02:06 +02:00
|
|
|
|
2014-10-15 07:07:26 +02:00
|
|
|
int w = fwrite(&v[0], 1, v.size(), f);
|
2015-04-29 06:05:29 +02:00
|
|
|
fclose(f);
|
2014-10-15 07:07:26 +02:00
|
|
|
|
2015-04-29 06:05:29 +02:00
|
|
|
if (w < 0) return -1;
|
2014-10-15 07:07:26 +02:00
|
|
|
if (w != int(v.size())) return -3;
|
2009-05-13 03:02:06 +02:00
|
|
|
return 0;
|
2008-07-12 10:19:17 +02:00
|
|
|
}
|
|
|
|
|
2011-12-23 06:36:13 +01:00
|
|
|
// returns true if the alert was handled (and should not be printed to the log)
|
|
|
|
// returns false if the alert was not handled
|
|
|
|
bool handle_alert(libtorrent::session& ses, libtorrent::alert* a
|
2014-07-09 00:37:13 +02:00
|
|
|
, handles_t& files, std::set<libtorrent::torrent_handle>& non_files)
|
2008-07-06 14:22:56 +02:00
|
|
|
{
|
|
|
|
using namespace libtorrent;
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
if (session_stats_alert* s = alert_cast<session_stats_alert>(a))
|
|
|
|
{
|
2015-04-03 22:15:48 +02:00
|
|
|
ses_view.update_counters(s->values, sizeof(s->values)/sizeof(s->values[0])
|
|
|
|
, duration_cast<microseconds>(s->timestamp().time_since_epoch()).count());
|
2014-07-06 21:18:00 +02:00
|
|
|
return true;
|
|
|
|
}
|
2015-06-07 19:52:19 +02:00
|
|
|
|
2015-01-17 18:02:58 +01:00
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
|
|
|
if (dht_stats_alert* p = alert_cast<dht_stats_alert>(a))
|
|
|
|
{
|
2015-04-03 22:15:48 +02:00
|
|
|
dht_active_requests = p->active_requests;
|
|
|
|
dht_routing_table = p->routing_table;
|
2015-02-14 07:14:15 +01:00
|
|
|
return true;
|
2015-01-17 18:02:58 +01:00
|
|
|
}
|
|
|
|
#endif
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2011-09-12 05:51:49 +02:00
|
|
|
#ifdef TORRENT_USE_OPENSSL
|
|
|
|
if (torrent_need_cert_alert* p = alert_cast<torrent_need_cert_alert>(a))
|
|
|
|
{
|
|
|
|
torrent_handle h = p->handle;
|
2014-11-17 04:41:01 +01:00
|
|
|
std::string base_name = path_append("certificates", to_hex(h.info_hash().to_string()));
|
2014-07-06 21:18:00 +02:00
|
|
|
std::string cert = base_name + ".pem";
|
|
|
|
std::string priv = base_name + "_key.pem";
|
2014-11-19 10:23:08 +01:00
|
|
|
|
|
|
|
#ifdef TORRENT_WINDOWS
|
|
|
|
struct ::_stat st;
|
|
|
|
int ret = ::_stat(cert.c_str(), &st);
|
|
|
|
if (ret < 0 || (st.st_mode & _S_IFREG) == 0)
|
|
|
|
#else
|
|
|
|
struct ::stat st;
|
|
|
|
int ret = ::stat(cert.c_str(), &st);
|
|
|
|
if (ret < 0 || (st.st_mode & S_IFREG) == 0)
|
|
|
|
#endif
|
2011-09-12 05:51:49 +02:00
|
|
|
{
|
|
|
|
char msg[256];
|
2014-11-19 10:23:08 +01:00
|
|
|
snprintf(msg, sizeof(msg), "ERROR. could not load certificate %s: %s\n", cert.c_str(), strerror(errno));
|
2014-05-10 22:20:57 +02:00
|
|
|
if (g_log_file) fprintf(g_log_file, "[%s] %s\n", timestamp(), msg);
|
2011-12-23 06:36:13 +01:00
|
|
|
return true;
|
2011-09-12 05:51:49 +02:00
|
|
|
}
|
2014-11-19 10:23:08 +01:00
|
|
|
|
|
|
|
#ifdef TORRENT_WINDOWS
|
|
|
|
ret = ::_stat(priv.c_str(), &st);
|
|
|
|
if (ret < 0 || (st.st_mode & _S_IFREG) == 0)
|
|
|
|
#else
|
|
|
|
ret = ::stat(priv.c_str(), &st);
|
|
|
|
if (ret < 0 || (st.st_mode & S_IFREG) == 0)
|
|
|
|
#endif
|
2011-09-12 05:51:49 +02:00
|
|
|
{
|
|
|
|
char msg[256];
|
2014-11-19 10:23:08 +01:00
|
|
|
snprintf(msg, sizeof(msg), "ERROR. could not load private key %s: %s\n", priv.c_str(), strerror(errno));
|
2014-05-10 22:20:57 +02:00
|
|
|
if (g_log_file) fprintf(g_log_file, "[%s] %s\n", timestamp(), msg);
|
2011-12-23 06:36:13 +01:00
|
|
|
return true;
|
2011-09-12 05:51:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
char msg[256];
|
|
|
|
snprintf(msg, sizeof(msg), "loaded certificate %s and key %s\n", cert.c_str(), priv.c_str());
|
2014-05-10 22:20:57 +02:00
|
|
|
if (g_log_file) fprintf(g_log_file, "[%s] %s\n", timestamp(), msg);
|
2011-09-12 05:51:49 +02:00
|
|
|
|
2012-01-14 17:04:25 +01:00
|
|
|
h.set_ssl_certificate(cert, priv, "certificates/dhparams.pem", "1234");
|
2011-09-12 05:51:49 +02:00
|
|
|
h.resume();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-02-15 06:17:09 +01:00
|
|
|
// don't log every peer we try to connect to
|
|
|
|
if (alert_cast<peer_connect_alert>(a)) return true;
|
|
|
|
|
|
|
|
if (peer_disconnected_alert* pd = alert_cast<peer_disconnected_alert>(a))
|
|
|
|
{
|
|
|
|
// ignore failures to connect and peers not responding with a
|
|
|
|
// handshake. The peers that we successfully connect to and then
|
|
|
|
// disconnect is more interesting.
|
|
|
|
if (pd->operation == op_connect
|
|
|
|
|| pd->error == error_code(errors::timed_out_no_handshake
|
|
|
|
, get_libtorrent_category()))
|
|
|
|
return true;
|
|
|
|
}
|
2015-01-17 18:02:58 +01:00
|
|
|
|
2012-02-21 06:56:22 +01:00
|
|
|
if (metadata_received_alert* p = alert_cast<metadata_received_alert>(a))
|
|
|
|
{
|
|
|
|
// if we have a monitor dir, save the .torrent file we just received in it
|
|
|
|
// also, add it to the files map, and remove it from the non_files list
|
|
|
|
// to keep the scan dir logic in sync so it's not removed, or added twice
|
|
|
|
torrent_handle h = p->handle;
|
2014-07-08 17:35:14 +02:00
|
|
|
if (h.is_valid())
|
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
boost::shared_ptr<const torrent_info> ti = h.torrent_file();
|
2012-10-02 18:07:55 +02:00
|
|
|
create_torrent ct(*ti);
|
2012-02-21 06:56:22 +01:00
|
|
|
entry te = ct.generate();
|
|
|
|
std::vector<char> buffer;
|
|
|
|
bencode(std::back_inserter(buffer), te);
|
2014-07-06 21:18:00 +02:00
|
|
|
sha1_hash hash = ti->info_hash();
|
|
|
|
std::string filename = ti->name() + "." + to_hex(hash.to_string()) + ".torrent";
|
2014-11-17 04:41:01 +01:00
|
|
|
filename = path_append(monitor_dir, filename);
|
2012-02-24 17:11:31 +01:00
|
|
|
save_file(filename, buffer);
|
2012-02-21 06:56:22 +01:00
|
|
|
|
|
|
|
files.insert(std::pair<std::string, libtorrent::torrent_handle>(filename, h));
|
2014-07-06 21:18:00 +02:00
|
|
|
hash_to_filename.insert(std::make_pair(hash, filename));
|
2012-02-21 06:56:22 +01:00
|
|
|
non_files.erase(h);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (add_torrent_alert* p = alert_cast<add_torrent_alert>(a))
|
2011-10-12 12:27:17 +02:00
|
|
|
{
|
2011-12-24 05:30:27 +01:00
|
|
|
std::string filename;
|
|
|
|
if (p->params.userdata)
|
|
|
|
{
|
|
|
|
filename = (char*)p->params.userdata;
|
|
|
|
free(p->params.userdata);
|
|
|
|
}
|
2011-10-12 12:27:17 +02:00
|
|
|
|
|
|
|
if (p->error)
|
|
|
|
{
|
2015-01-01 19:03:56 +01:00
|
|
|
fprintf(stderr, "failed to add torrent: %s %s\n", filename.c_str()
|
|
|
|
, p->error.message().c_str());
|
2011-10-12 12:27:17 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
torrent_handle h = p->handle;
|
|
|
|
|
2011-12-24 05:30:27 +01:00
|
|
|
if (!filename.empty())
|
2014-07-06 21:18:00 +02:00
|
|
|
files[filename] = h;
|
2012-02-21 00:44:34 +01:00
|
|
|
else
|
|
|
|
non_files.insert(h);
|
2011-10-12 12:27:17 +02:00
|
|
|
|
|
|
|
h.set_max_connections(max_connections_per_torrent);
|
|
|
|
h.set_max_uploads(-1);
|
|
|
|
h.set_upload_limit(torrent_upload_limit);
|
|
|
|
h.set_download_limit(torrent_download_limit);
|
|
|
|
#ifndef TORRENT_DISABLE_RESOLVE_COUNTRIES
|
|
|
|
h.resolve_countries(true);
|
|
|
|
#endif
|
2011-12-23 06:36:13 +01:00
|
|
|
|
2012-01-14 17:04:25 +01:00
|
|
|
// if we have a peer specified, connect to it
|
|
|
|
if (!peer.empty())
|
|
|
|
{
|
|
|
|
char* port = (char*) strrchr((char*)peer.c_str(), ':');
|
2015-10-04 16:57:12 +02:00
|
|
|
if (port != NULL)
|
2012-01-14 17:04:25 +01:00
|
|
|
{
|
|
|
|
*port++ = 0;
|
|
|
|
char const* ip = peer.c_str();
|
|
|
|
int peer_port = atoi(port);
|
2012-02-20 05:13:39 +01:00
|
|
|
error_code ec;
|
2012-01-14 17:04:25 +01:00
|
|
|
if (peer_port > 0)
|
2012-02-20 05:13:39 +01:00
|
|
|
h.connect_peer(tcp::endpoint(address::from_string(ip, ec), peer_port));
|
2012-01-14 17:04:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
sha1_hash info_hash;
|
|
|
|
if (p->params.ti)
|
2011-12-23 06:36:13 +01:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
info_hash = p->params.ti->info_hash();
|
2011-12-23 06:36:13 +01:00
|
|
|
}
|
2014-07-06 21:18:00 +02:00
|
|
|
else if (!p->params.info_hash.is_all_zeros())
|
|
|
|
{
|
|
|
|
info_hash = p->params.info_hash;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
info_hash = h.info_hash();
|
|
|
|
}
|
|
|
|
hash_to_filename.insert(std::make_pair(info_hash, filename));
|
2011-10-12 12:27:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (torrent_finished_alert* p = alert_cast<torrent_finished_alert>(a))
|
2008-07-06 14:22:56 +02:00
|
|
|
{
|
2010-01-12 02:56:48 +01:00
|
|
|
p->handle.set_max_connections(max_connections_per_torrent / 2);
|
2008-07-06 14:22:56 +02:00
|
|
|
|
|
|
|
// write resume data for the finished torrent
|
2008-10-19 06:04:12 +02:00
|
|
|
// the alert handler for save_resume_data_alert
|
|
|
|
// will save it to disk
|
2008-07-06 14:22:56 +02:00
|
|
|
torrent_handle h = p->handle;
|
|
|
|
h.save_resume_data();
|
2011-07-04 07:33:06 +02:00
|
|
|
++num_outstanding_resume_data;
|
2008-07-06 14:22:56 +02:00
|
|
|
}
|
2009-11-02 04:34:16 +01:00
|
|
|
else if (save_resume_data_alert* p = alert_cast<save_resume_data_alert>(a))
|
2008-07-06 14:22:56 +02:00
|
|
|
{
|
2011-07-04 07:33:06 +02:00
|
|
|
--num_outstanding_resume_data;
|
2008-07-06 14:22:56 +02:00
|
|
|
torrent_handle h = p->handle;
|
|
|
|
TORRENT_ASSERT(p->resume_data);
|
|
|
|
if (p->resume_data)
|
|
|
|
{
|
2009-05-13 03:02:06 +02:00
|
|
|
std::vector<char> out;
|
|
|
|
bencode(std::back_inserter(out), *p->resume_data);
|
2013-03-04 04:24:53 +01:00
|
|
|
torrent_status st = h.status(torrent_handle::query_save_path);
|
2015-01-02 12:11:09 +01:00
|
|
|
save_file(path_append(st.save_path, path_append(".resume", leaf_path(
|
2014-07-06 21:18:00 +02:00
|
|
|
hash_to_filename[st.info_hash]) + ".resume")), out);
|
2011-07-14 05:45:24 +02:00
|
|
|
if (h.is_valid()
|
|
|
|
&& non_files.find(h) == non_files.end()
|
2011-02-09 05:54:22 +01:00
|
|
|
&& std::find_if(files.begin(), files.end()
|
|
|
|
, boost::bind(&handles_t::value_type::second, _1) == h) == files.end())
|
2008-09-26 02:26:35 +02:00
|
|
|
ses.remove_torrent(h);
|
2008-07-06 14:22:56 +02:00
|
|
|
}
|
|
|
|
}
|
2009-11-02 04:34:16 +01:00
|
|
|
else if (save_resume_data_failed_alert* p = alert_cast<save_resume_data_failed_alert>(a))
|
2008-11-17 02:19:46 +01:00
|
|
|
{
|
2011-07-04 07:33:06 +02:00
|
|
|
--num_outstanding_resume_data;
|
2008-11-17 02:19:46 +01:00
|
|
|
torrent_handle h = p->handle;
|
2011-07-14 05:45:24 +02:00
|
|
|
if (h.is_valid()
|
|
|
|
&& non_files.find(h) == non_files.end()
|
2011-04-02 09:39:02 +02:00
|
|
|
&& std::find_if(files.begin(), files.end()
|
|
|
|
, boost::bind(&handles_t::value_type::second, _1) == h) == files.end())
|
2008-11-17 02:19:46 +01:00
|
|
|
ses.remove_torrent(h);
|
|
|
|
}
|
2011-06-05 22:48:00 +02:00
|
|
|
else if (torrent_paused_alert* p = alert_cast<torrent_paused_alert>(a))
|
|
|
|
{
|
|
|
|
// write resume data for the finished torrent
|
|
|
|
// the alert handler for save_resume_data_alert
|
|
|
|
// will save it to disk
|
|
|
|
torrent_handle h = p->handle;
|
|
|
|
h.save_resume_data();
|
2011-07-04 07:33:06 +02:00
|
|
|
++num_outstanding_resume_data;
|
2011-06-05 22:48:00 +02:00
|
|
|
}
|
2011-12-23 06:36:13 +01:00
|
|
|
else if (state_update_alert* p = alert_cast<state_update_alert>(a))
|
|
|
|
{
|
2014-07-08 17:35:14 +02:00
|
|
|
view.update_torrents(p->status);
|
2011-12-23 06:36:13 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2008-07-06 14:22:56 +02:00
|
|
|
}
|
|
|
|
|
2011-07-10 10:57:27 +02:00
|
|
|
void print_piece(libtorrent::partial_piece_info* pp
|
|
|
|
, libtorrent::cached_piece_info* cs
|
|
|
|
, std::vector<libtorrent::peer_info> const& peers
|
2014-07-06 21:18:00 +02:00
|
|
|
, torrent_status const* ts
|
2011-07-10 10:57:27 +02:00
|
|
|
, std::string& out)
|
|
|
|
{
|
|
|
|
using namespace libtorrent;
|
|
|
|
|
|
|
|
char str[1024];
|
|
|
|
assert(pp == 0 || cs == 0 || cs->piece == pp->piece_index);
|
|
|
|
int piece = pp ? pp->piece_index : cs->piece;
|
|
|
|
int num_blocks = pp ? pp->blocks_in_piece : cs->blocks.size();
|
|
|
|
|
2014-10-07 05:09:32 +02:00
|
|
|
snprintf(str, sizeof(str), "%5d:[", piece);
|
2011-07-10 10:57:27 +02:00
|
|
|
out += str;
|
|
|
|
char const* last_color = 0;
|
|
|
|
for (int j = 0; j < num_blocks; ++j)
|
|
|
|
{
|
|
|
|
int index = pp ? peer_index(pp->blocks[j].peer(), peers) % 36 : -1;
|
|
|
|
char chr = '+';
|
|
|
|
if (index >= 0)
|
|
|
|
chr = (index < 10)?'0' + index:'A' + index - 10;
|
2014-12-07 23:22:38 +01:00
|
|
|
bool snubbed = index >= 0 ? peers[index].flags & peer_info::snubbed : false;
|
2011-07-10 10:57:27 +02:00
|
|
|
|
|
|
|
char const* color = "";
|
|
|
|
|
|
|
|
if (pp == 0)
|
|
|
|
{
|
|
|
|
color = cs->blocks[j] ? esc("34;7") : esc("0");
|
|
|
|
chr = ' ';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
if (cs && cs->blocks[j] && pp->blocks[j].state != block_info::finished)
|
|
|
|
color = esc("36;7");
|
2011-07-10 10:57:27 +02:00
|
|
|
else if (pp->blocks[j].bytes_progress > 0
|
|
|
|
&& pp->blocks[j].state == block_info::requested)
|
|
|
|
{
|
|
|
|
if (pp->blocks[j].num_peers > 1) color = esc("1;7");
|
2014-12-07 23:22:38 +01:00
|
|
|
else color = snubbed ? esc("35;7") : esc("33;7");
|
2012-02-20 05:13:39 +01:00
|
|
|
chr = '0' + (pp->blocks[j].bytes_progress * 10 / pp->blocks[j].block_size);
|
2011-07-10 10:57:27 +02:00
|
|
|
}
|
|
|
|
else if (pp->blocks[j].state == block_info::finished) color = esc("32;7");
|
|
|
|
else if (pp->blocks[j].state == block_info::writing) color = esc("36;7");
|
2014-12-07 23:22:38 +01:00
|
|
|
else if (pp->blocks[j].state == block_info::requested) color = snubbed ? esc("35;7") : esc("0");
|
2011-07-10 10:57:27 +02:00
|
|
|
else { color = esc("0"); chr = ' '; }
|
|
|
|
}
|
|
|
|
if (last_color == 0 || strcmp(last_color, color) != 0)
|
2015-05-30 08:11:41 +02:00
|
|
|
{
|
2011-07-10 10:57:27 +02:00
|
|
|
snprintf(str, sizeof(str), "%s%c", color, chr);
|
2015-05-30 08:11:41 +02:00
|
|
|
out += str;
|
|
|
|
}
|
2011-07-10 10:57:27 +02:00
|
|
|
else
|
|
|
|
out += chr;
|
|
|
|
|
2015-05-03 04:56:46 +02:00
|
|
|
last_color = color;
|
2011-07-10 10:57:27 +02:00
|
|
|
}
|
|
|
|
out += esc("0");
|
2014-10-07 05:09:32 +02:00
|
|
|
out += "]";
|
|
|
|
/*
|
2014-07-06 21:18:00 +02:00
|
|
|
char const* cache_kind_str[] = {"read", "write", "read-volatile"};
|
2014-10-07 05:09:32 +02:00
|
|
|
snprintf(str, sizeof(str), " %3d cache age: %-5.1f state: %s%s\n"
|
2011-07-10 10:57:27 +02:00
|
|
|
, cs ? cs->next_to_hash : 0
|
2015-03-12 05:34:54 +01:00
|
|
|
, cs ? (total_milliseconds(clock_type::now() - cs->last_use) / 1000.f) : 0.f
|
2014-07-06 21:18:00 +02:00
|
|
|
, cs ? cache_kind_str[cs->kind] : "N/A"
|
|
|
|
, ts && ts->pieces.size() ? (ts->pieces[piece] ? " have" : " dont-have") : "");
|
2011-07-10 10:57:27 +02:00
|
|
|
out += str;
|
2014-10-07 05:09:32 +02:00
|
|
|
*/
|
2011-07-10 10:57:27 +02:00
|
|
|
}
|
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
int main(int argc, char* argv[])
|
2005-10-16 11:15:46 +02:00
|
|
|
{
|
2015-05-26 20:39:49 +02:00
|
|
|
#ifndef _WIN32
|
|
|
|
// sets the terminal to single-character mode
|
|
|
|
// and resets when destructed
|
|
|
|
set_keypress s;
|
|
|
|
#endif
|
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
if (argc == 1)
|
2005-10-19 02:50:38 +02:00
|
|
|
{
|
2010-12-30 02:47:30 +01:00
|
|
|
fprintf(stderr, "usage: client_test [OPTIONS] [TORRENT|MAGNETURL|URL]\n\n"
|
2009-02-26 08:09:56 +01:00
|
|
|
"OPTIONS:\n"
|
2011-04-27 10:07:24 +02:00
|
|
|
"\n CLIENT OPTIONS\n"
|
2009-02-26 08:09:56 +01:00
|
|
|
" -f <log file> logs all events to the given file\n"
|
|
|
|
" -s <path> sets the save path for downloads\n"
|
|
|
|
" -m <path> sets the .torrent monitor directory\n"
|
|
|
|
" -t <seconds> sets the scan interval of the monitor dir\n"
|
2012-01-18 04:37:17 +01:00
|
|
|
" -F <milliseconds> sets the UI refresh rate. This is the number of\n"
|
|
|
|
" milliseconds between screen refreshes.\n"
|
2011-04-27 10:07:24 +02:00
|
|
|
" -k enable high performance settings. This overwrites any other\n"
|
|
|
|
" previous command line options, so be sure to specify this first\n"
|
2011-08-07 08:34:27 +02:00
|
|
|
" -G Add torrents in seed-mode (i.e. assume all pieces\n"
|
|
|
|
" are present and check hashes on-demand)\n"
|
2014-07-06 21:18:00 +02:00
|
|
|
" -E <num-threads> specify how many hashing threads to use\n"
|
2011-04-27 10:07:24 +02:00
|
|
|
"\n BITTORRENT OPTIONS\n"
|
|
|
|
" -c <limit> sets the max number of connections\n"
|
|
|
|
" -T <limit> sets the max number of connections per torrent\n"
|
|
|
|
" -U <rate> sets per-torrent upload rate\n"
|
|
|
|
" -D <rate> sets per-torrent download rate\n"
|
|
|
|
" -d <rate> limits the download rate\n"
|
|
|
|
" -u <rate> limits the upload rate\n"
|
|
|
|
" -S <limit> limits the upload slots\n"
|
2010-01-31 22:30:43 +01:00
|
|
|
" -A <num pieces> allowed pieces set size\n"
|
2011-04-27 10:07:24 +02:00
|
|
|
" -H Don't start DHT\n"
|
2012-01-16 00:34:43 +01:00
|
|
|
" -X Don't start local peer discovery\n"
|
2011-04-27 10:07:24 +02:00
|
|
|
" -n announce to trackers in all tiers\n"
|
|
|
|
" -W <num peers> Set the max number of peers to keep in the peer list\n"
|
2011-05-02 07:32:54 +02:00
|
|
|
" -B <seconds> sets the peer timeout\n"
|
|
|
|
" -Q enables share mode. Share mode attempts to maximize\n"
|
|
|
|
" share ratio rather than downloading\n"
|
2014-07-06 21:18:00 +02:00
|
|
|
" -K enable piece suggestions of read cache\n"
|
2012-01-14 17:04:25 +01:00
|
|
|
" -r <IP:port> connect to specified peer\n"
|
2014-11-23 07:14:47 +01:00
|
|
|
#if !defined(TORRENT_DISABLE_ENCRYPTION) && !defined(TORRENT_DISABLE_EXTENSIONS)
|
2012-01-11 09:48:49 +01:00
|
|
|
" -e force encrypted bittorrent connections\n"
|
2012-06-09 06:48:53 +02:00
|
|
|
#endif
|
2011-04-27 10:07:24 +02:00
|
|
|
"\n QUEING OPTIONS\n"
|
|
|
|
" -v <limit> Set the max number of active downloads\n"
|
|
|
|
" -^ <limit> Set the max number of active seeds\n"
|
|
|
|
"\n NETWORK OPTIONS\n"
|
|
|
|
" -p <port> sets the listen port\n"
|
2014-10-05 21:32:29 +02:00
|
|
|
#ifndef TORRENT_NO_DEPRECATE
|
2011-04-27 10:07:24 +02:00
|
|
|
" -o <limit> limits the number of simultaneous\n"
|
|
|
|
" half-open TCP connections to the\n"
|
|
|
|
" given number.\n"
|
2014-10-05 21:32:29 +02:00
|
|
|
#endif
|
2011-04-27 10:07:24 +02:00
|
|
|
" -w <seconds> sets the retry time for failed web seeds\n"
|
|
|
|
" -x <file> loads an emule IP-filter file\n"
|
2010-07-03 08:56:09 +02:00
|
|
|
" -P <host:port> Use the specified SOCKS5 proxy\n"
|
2010-07-25 03:31:15 +02:00
|
|
|
" -L <user:passwd> Use the specified username and password for the\n"
|
|
|
|
" proxy specified by -P\n"
|
2011-04-27 10:07:24 +02:00
|
|
|
" -h allow multiple connections from the same IP\n"
|
2010-11-29 02:33:05 +01:00
|
|
|
" -M Disable TCP/uTP bandwidth balancing\n"
|
2010-08-03 09:30:38 +02:00
|
|
|
" -N Do not attempt to use UPnP and NAT-PMP to forward ports\n"
|
2010-08-27 16:52:42 +02:00
|
|
|
" -Y Rate limit local peers\n"
|
2010-11-29 02:33:05 +01:00
|
|
|
" -y Disable TCP connections (disable outgoing TCP and reject\n"
|
|
|
|
" incoming TCP connections)\n"
|
2014-07-06 21:18:00 +02:00
|
|
|
" -J Disable uTP connections (disable outgoing uTP and reject\n"
|
|
|
|
" incoming uTP connections)\n"
|
2011-04-27 10:07:24 +02:00
|
|
|
" -b <IP> sets IP of the interface to bind the\n"
|
|
|
|
" listen socket to\n"
|
|
|
|
" -I <IP> sets the IP of the interface to bind\n"
|
|
|
|
" outgoing peer connections to\n"
|
|
|
|
#if TORRENT_USE_I2P
|
|
|
|
" -i <i2p-host> the hostname to an I2P SAM bridge to use\n"
|
|
|
|
#endif
|
2011-05-02 07:32:54 +02:00
|
|
|
" -l <limit> sets the listen socket queue size\n"
|
2011-04-27 10:07:24 +02:00
|
|
|
"\n DISK OPTIONS\n"
|
2014-02-07 09:58:52 +01:00
|
|
|
" -a <mode> sets the allocation mode. [sparse|allocate]\n"
|
2011-04-27 10:07:24 +02:00
|
|
|
" -R <num blocks> number of blocks per read cache line\n"
|
|
|
|
" -C <limit> sets the max cache size. Specified in 16kB blocks\n"
|
|
|
|
" -j disable disk read-ahead\n"
|
|
|
|
" -z disable piece hash checks (used for benchmarking)\n"
|
2014-07-06 21:18:00 +02:00
|
|
|
" -Z <file> mmap the disk cache to the specified file, should be an SSD\n"
|
2011-07-10 22:17:32 +02:00
|
|
|
" -0 disable disk I/O, read garbage and don't flush to disk\n"
|
2009-02-26 08:09:56 +01:00
|
|
|
"\n\n"
|
|
|
|
"TORRENT is a path to a .torrent file\n"
|
2010-12-30 02:47:30 +01:00
|
|
|
"MAGNETURL is a magnet link\n"
|
2011-04-27 10:07:24 +02:00
|
|
|
"URL is a url to a torrent file\n"
|
|
|
|
"\n"
|
|
|
|
"Example for running benchmark:\n\n"
|
2011-05-02 07:32:54 +02:00
|
|
|
" client_test -k -z -N -h -H -M -l 2000 -S 1000 -T 1000 -c 1000 test.torrent\n");
|
2005-10-19 02:50:38 +02:00
|
|
|
;
|
2009-02-26 08:09:56 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2005-10-19 02:50:38 +02:00
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
using namespace libtorrent;
|
2014-07-06 21:18:00 +02:00
|
|
|
namespace lt = libtorrent;
|
|
|
|
|
|
|
|
settings_pack settings;
|
|
|
|
settings.set_int(settings_pack::active_loaded_limit, 20);
|
2014-11-30 05:57:54 +01:00
|
|
|
settings.set_int(settings_pack::choking_algorithm, settings_pack::rate_based_choker);
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
int refresh_delay = 500;
|
2015-08-22 01:22:04 +02:00
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
2010-05-03 07:51:01 +02:00
|
|
|
bool start_dht = true;
|
2015-08-22 01:22:04 +02:00
|
|
|
#endif
|
2015-06-07 19:52:19 +02:00
|
|
|
bool rate_limit_locals = false;
|
2009-05-13 18:02:43 +02:00
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
std::deque<std::string> events;
|
2008-02-28 08:34:07 +01:00
|
|
|
|
2015-03-12 05:34:54 +01:00
|
|
|
time_point next_dir_scan = clock_type::now();
|
2003-12-22 08:14:35 +01:00
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
// the string is the filename of the .torrent file, but only if
|
|
|
|
// it was added through the directory monitor. It is used to
|
|
|
|
// be able to remove torrents that were added via the directory
|
|
|
|
// monitor when they're not in the directory anymore.
|
2011-02-01 10:48:28 +01:00
|
|
|
handles_t files;
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2011-02-09 05:54:22 +01:00
|
|
|
// torrents that were not added via the monitor dir
|
|
|
|
std::set<torrent_handle> non_files;
|
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
// load the torrents given on the commandline
|
2005-10-16 11:15:46 +02:00
|
|
|
|
2010-06-05 23:03:01 +02:00
|
|
|
std::vector<add_torrent_params> magnet_links;
|
|
|
|
std::vector<std::string> torrents;
|
2015-06-07 19:52:19 +02:00
|
|
|
ip_filter loaded_ip_filter;
|
2010-06-05 23:03:01 +02:00
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
for (int i = 1; i < argc; ++i)
|
|
|
|
{
|
|
|
|
if (argv[i][0] != '-')
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2009-02-26 08:09:56 +01:00
|
|
|
// match it against the <hash>@<tracker> format
|
2009-05-13 03:02:06 +02:00
|
|
|
if (strlen(argv[i]) > 45
|
2013-07-20 01:16:57 +02:00
|
|
|
&& ::is_hex(argv[i], 40)
|
2012-03-19 00:31:04 +01:00
|
|
|
&& (strncmp(argv[i] + 40, "@http://", 8) == 0
|
|
|
|
|| strncmp(argv[i] + 40, "@udp://", 7) == 0))
|
2009-02-26 08:09:56 +01:00
|
|
|
{
|
2009-05-13 03:02:06 +02:00
|
|
|
sha1_hash info_hash;
|
|
|
|
from_hex(argv[i], 40, (char*)&info_hash[0]);
|
2009-02-26 08:09:56 +01:00
|
|
|
|
|
|
|
add_torrent_params p;
|
2011-11-08 06:36:22 +01:00
|
|
|
if (seed_mode) p.flags |= add_torrent_params::flag_seed_mode;
|
2011-07-10 22:17:32 +02:00
|
|
|
if (disable_storage) p.storage = disabled_storage_constructor;
|
2011-11-08 06:36:22 +01:00
|
|
|
if (share_mode) p.flags |= add_torrent_params::flag_share_mode;
|
2012-03-09 07:34:23 +01:00
|
|
|
p.trackers.push_back(argv[i] + 41);
|
2009-02-26 08:09:56 +01:00
|
|
|
p.info_hash = info_hash;
|
|
|
|
p.save_path = save_path;
|
2010-01-18 06:29:35 +01:00
|
|
|
p.storage_mode = (storage_mode_t)allocation_mode;
|
2011-11-08 06:36:22 +01:00
|
|
|
p.flags |= add_torrent_params::flag_paused;
|
|
|
|
p.flags &= ~add_torrent_params::flag_duplicate_is_error;
|
|
|
|
p.flags |= add_torrent_params::flag_auto_managed;
|
2014-07-06 21:18:00 +02:00
|
|
|
p.flags |= add_torrent_params::flag_pinned;
|
2010-06-05 23:03:01 +02:00
|
|
|
magnet_links.push_back(p);
|
2009-02-26 08:09:56 +01:00
|
|
|
continue;
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
2009-02-26 08:09:56 +01:00
|
|
|
|
2010-06-05 23:03:01 +02:00
|
|
|
torrents.push_back(argv[i]);
|
2009-02-26 08:09:56 +01:00
|
|
|
continue;
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
// if there's a flag but no argument following, ignore it
|
|
|
|
if (argc == i) continue;
|
|
|
|
char const* arg = argv[i+1];
|
2014-08-22 09:56:10 +02:00
|
|
|
if (arg == NULL) arg = "";
|
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
switch (argv[i][1])
|
|
|
|
{
|
2009-05-13 03:02:06 +02:00
|
|
|
case 'f': g_log_file = fopen(arg, "w+"); break;
|
2014-10-05 21:32:29 +02:00
|
|
|
#ifndef TORRENT_NO_DEPRECATE
|
2014-07-06 21:18:00 +02:00
|
|
|
case 'o': settings.set_int(settings_pack::half_open_limit, atoi(arg)); break;
|
2014-10-05 21:32:29 +02:00
|
|
|
#endif
|
2014-07-06 21:18:00 +02:00
|
|
|
case 'h': settings.set_bool(settings_pack::allow_multiple_connections_per_ip, true); --i; break;
|
2009-02-26 08:09:56 +01:00
|
|
|
case 'p': listen_port = atoi(arg); break;
|
2014-07-06 21:18:00 +02:00
|
|
|
case 'k': high_performance_seed(settings); --i; break;
|
|
|
|
case 'j': settings.set_bool(settings_pack::use_disk_read_ahead, false); --i; break;
|
|
|
|
case 'z': settings.set_bool(settings_pack::disable_hash_checks, true); --i; break;
|
|
|
|
case 'K': settings.set_int(settings_pack::suggest_mode, settings_pack::suggest_read_cache); --i; break;
|
|
|
|
case 'B': settings.set_int(settings_pack::peer_timeout, atoi(arg)); break;
|
|
|
|
case 'n': settings.set_bool(settings_pack::announce_to_all_tiers, true); --i; break;
|
2011-08-07 08:34:27 +02:00
|
|
|
case 'G': seed_mode = true; --i; break;
|
2014-07-06 21:18:00 +02:00
|
|
|
case 'E': settings.set_int(settings_pack::hashing_threads, atoi(arg)); break;
|
|
|
|
case 'd': settings.set_int(settings_pack::download_rate_limit, atoi(arg) * 1000); break;
|
|
|
|
case 'u': settings.set_int(settings_pack::upload_rate_limit, atoi(arg) * 1000); break;
|
2014-11-30 05:57:54 +01:00
|
|
|
case 'S':
|
|
|
|
settings.set_int(settings_pack::unchoke_slots_limit, atoi(arg));
|
|
|
|
settings.set_int(settings_pack::choking_algorithm, settings_pack::fixed_slots_choker);
|
|
|
|
break;
|
2010-01-18 06:29:35 +01:00
|
|
|
case 'a':
|
|
|
|
if (strcmp(arg, "allocate") == 0) allocation_mode = storage_mode_allocate;
|
2014-02-07 09:58:52 +01:00
|
|
|
else if (strcmp(arg, "full") == 0) allocation_mode = storage_mode_allocate;
|
|
|
|
else if (strcmp(arg, "sparse") == 0) allocation_mode = storage_mode_sparse;
|
2010-01-18 06:29:35 +01:00
|
|
|
break;
|
2009-02-26 08:09:56 +01:00
|
|
|
case 's': save_path = arg; break;
|
|
|
|
case 'U': torrent_upload_limit = atoi(arg) * 1000; break;
|
|
|
|
case 'D': torrent_download_limit = atoi(arg) * 1000; break;
|
|
|
|
case 'm': monitor_dir = arg; break;
|
2010-11-29 02:33:05 +01:00
|
|
|
case 'Q': share_mode = true; --i; break;
|
2009-02-26 08:09:56 +01:00
|
|
|
case 'b': bind_to_interface = arg; break;
|
2014-07-06 21:18:00 +02:00
|
|
|
case 'w': settings.set_int(settings_pack::urlseed_wait_retry, atoi(arg)); break;
|
2009-02-26 08:09:56 +01:00
|
|
|
case 't': poll_interval = atoi(arg); break;
|
2009-05-13 18:02:43 +02:00
|
|
|
case 'F': refresh_delay = atoi(arg); break;
|
2014-07-06 21:18:00 +02:00
|
|
|
case 'H':
|
2015-08-22 01:22:04 +02:00
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
2014-07-06 21:18:00 +02:00
|
|
|
start_dht = false;
|
2015-08-22 01:22:04 +02:00
|
|
|
#endif
|
2014-07-06 21:18:00 +02:00
|
|
|
settings.set_bool(settings_pack::enable_dht, false);
|
|
|
|
--i;
|
|
|
|
break;
|
|
|
|
case 'l': settings.set_int(settings_pack::listen_queue_size, atoi(arg)); break;
|
2014-11-23 07:14:47 +01:00
|
|
|
#if !defined(TORRENT_DISABLE_ENCRYPTION) && !defined(TORRENT_DISABLE_EXTENSIONS)
|
2012-01-11 09:48:49 +01:00
|
|
|
case 'e':
|
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
settings.set_int(settings_pack::out_enc_policy, settings_pack::pe_forced);
|
|
|
|
settings.set_int(settings_pack::in_enc_policy, settings_pack::pe_forced);
|
|
|
|
settings.set_int(settings_pack::allowed_enc_level, settings_pack::pe_rc4);
|
|
|
|
settings.set_bool(settings_pack::prefer_rc4, true);
|
2012-12-16 20:07:00 +01:00
|
|
|
--i;
|
2012-01-11 09:48:49 +01:00
|
|
|
break;
|
|
|
|
}
|
2012-06-09 06:48:53 +02:00
|
|
|
#endif
|
2010-10-01 06:07:38 +02:00
|
|
|
case 'W':
|
2014-07-06 21:18:00 +02:00
|
|
|
settings.set_int(settings_pack::max_peerlist_size, atoi(arg));
|
|
|
|
settings.set_int(settings_pack::max_paused_peerlist_size, atoi(arg) / 2);
|
2010-10-01 06:07:38 +02:00
|
|
|
break;
|
2009-02-26 08:09:56 +01:00
|
|
|
case 'x':
|
|
|
|
{
|
2010-06-06 04:31:20 +02:00
|
|
|
FILE* filter = fopen(arg, "r");
|
|
|
|
if (filter)
|
2009-02-26 08:09:56 +01:00
|
|
|
{
|
2010-06-06 04:31:20 +02:00
|
|
|
unsigned int a,b,c,d,e,f,g,h, flags;
|
|
|
|
while (fscanf(filter, "%u.%u.%u.%u - %u.%u.%u.%u %u\n", &a, &b, &c, &d, &e, &f, &g, &h, &flags) == 9)
|
|
|
|
{
|
|
|
|
address_v4 start((a << 24) + (b << 16) + (c << 8) + d);
|
|
|
|
address_v4 last((e << 24) + (f << 16) + (g << 8) + h);
|
|
|
|
if (flags <= 127) flags = ip_filter::blocked;
|
|
|
|
else flags = 0;
|
2015-06-07 19:52:19 +02:00
|
|
|
loaded_ip_filter.add_rule(start, last, flags);
|
2010-06-06 04:31:20 +02:00
|
|
|
}
|
|
|
|
fclose(filter);
|
2009-02-26 08:09:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2014-07-06 21:18:00 +02:00
|
|
|
case 'c': settings.set_int(settings_pack::connections_limit, atoi(arg)); break;
|
2010-01-12 02:56:48 +01:00
|
|
|
case 'T': max_connections_per_torrent = atoi(arg); break;
|
2009-08-20 05:19:12 +02:00
|
|
|
#if TORRENT_USE_I2P
|
|
|
|
case 'i':
|
|
|
|
{
|
2014-08-16 08:51:48 +02:00
|
|
|
settings.set_str(settings_pack::i2p_hostname, arg);
|
2015-06-15 07:48:40 +02:00
|
|
|
settings.set_int(settings_pack::i2p_port, 7656);
|
2014-08-16 08:51:48 +02:00
|
|
|
settings.set_int(settings_pack::proxy_type, settings_pack::i2p_proxy);
|
2009-08-20 05:19:12 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif // TORRENT_USE_I2P
|
2010-01-31 22:30:43 +01:00
|
|
|
case 'C':
|
2014-07-06 21:18:00 +02:00
|
|
|
settings.set_int(settings_pack::cache_size, atoi(arg));
|
|
|
|
settings.set_bool(settings_pack::use_read_cache, atoi(arg) > 0);
|
|
|
|
settings.set_int(settings_pack::cache_buffer_chunk_size, atoi(arg) / 100);
|
|
|
|
break;
|
|
|
|
case 'A': settings.set_int(settings_pack::allowed_fast_set_size, atoi(arg)); break;
|
|
|
|
case 'R': settings.set_int(settings_pack::read_cache_line_size, atoi(arg)); break;
|
|
|
|
case 'M': settings.set_int(settings_pack::mixed_mode_algorithm, settings_pack::prefer_tcp); --i; break;
|
|
|
|
case 'y':
|
|
|
|
settings.set_bool(settings_pack::enable_outgoing_tcp, false);
|
|
|
|
settings.set_bool(settings_pack::enable_incoming_tcp, false);
|
|
|
|
--i;
|
|
|
|
break;
|
|
|
|
case 'J':
|
|
|
|
settings.set_bool(settings_pack::enable_outgoing_utp, false);
|
|
|
|
settings.set_bool(settings_pack::enable_incoming_utp, false);
|
|
|
|
--i;
|
2010-01-31 22:30:43 +01:00
|
|
|
break;
|
2012-01-14 17:04:25 +01:00
|
|
|
case 'r': peer = arg; break;
|
2010-07-03 08:56:09 +02:00
|
|
|
case 'P':
|
|
|
|
{
|
2010-08-03 09:30:38 +02:00
|
|
|
char* port = (char*) strrchr(arg, ':');
|
2010-07-03 08:56:09 +02:00
|
|
|
if (port == 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "invalid proxy hostname, no port found\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*port++ = 0;
|
2014-08-16 08:51:48 +02:00
|
|
|
settings.set_str(settings_pack::proxy_hostname, arg);
|
|
|
|
settings.set_int(settings_pack::proxy_port, atoi(port));
|
|
|
|
if (atoi(port) == 0) {
|
2010-07-03 08:56:09 +02:00
|
|
|
fprintf(stderr, "invalid proxy port\n");
|
|
|
|
break;
|
|
|
|
}
|
2014-08-16 08:51:48 +02:00
|
|
|
if (settings.get_int(settings_pack::proxy_type) == settings_pack::none)
|
|
|
|
settings.set_int(settings_pack::proxy_type, settings_pack::socks5);
|
2010-07-25 03:31:15 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'L':
|
|
|
|
{
|
|
|
|
char* pw = (char*) strchr(arg, ':');
|
|
|
|
if (pw == 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "invalid proxy username and password specified\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*pw++ = 0;
|
2014-08-16 08:51:48 +02:00
|
|
|
settings.set_str(settings_pack::proxy_username, arg);
|
|
|
|
settings.set_str(settings_pack::proxy_password, pw);
|
|
|
|
settings.set_int(settings_pack::proxy_type, settings_pack::socks5_pw);
|
2010-07-03 08:56:09 +02:00
|
|
|
}
|
|
|
|
break;
|
2014-07-06 21:18:00 +02:00
|
|
|
case 'I': settings.set_str(settings_pack::outgoing_interfaces, arg); break;
|
|
|
|
case 'N':
|
|
|
|
settings.set_bool(settings_pack::enable_upnp, false);
|
|
|
|
settings.set_bool(settings_pack::enable_natpmp, false);
|
|
|
|
--i;
|
|
|
|
break;
|
|
|
|
case 'Y':
|
|
|
|
{
|
|
|
|
--i;
|
2015-06-07 19:52:19 +02:00
|
|
|
rate_limit_locals = true;
|
2014-07-06 21:18:00 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'X': settings.set_bool(settings_pack::enable_lsd, false); --i; break;
|
|
|
|
case 'Z':
|
|
|
|
settings.set_str(settings_pack::mmap_cache, arg);
|
|
|
|
settings.set_bool(settings_pack::contiguous_recv_buffer, false);
|
|
|
|
break;
|
|
|
|
case 'v': settings.set_int(settings_pack::active_downloads, atoi(arg));
|
|
|
|
settings.set_int(settings_pack::active_limit, atoi(arg) * 2);
|
2011-03-16 05:46:14 +01:00
|
|
|
break;
|
|
|
|
case '^':
|
2014-07-06 21:18:00 +02:00
|
|
|
settings.set_int(settings_pack::active_seeds, atoi(arg));
|
|
|
|
settings.set_int(settings_pack::active_limit, atoi(arg) * 2);
|
2011-01-20 06:49:36 +01:00
|
|
|
break;
|
2011-07-10 22:17:32 +02:00
|
|
|
case '0': disable_storage = true; --i;
|
2009-02-26 08:09:56 +01:00
|
|
|
}
|
2009-02-28 08:02:30 +01:00
|
|
|
++i; // skip the argument
|
2009-02-26 08:09:56 +01:00
|
|
|
}
|
|
|
|
|
2011-01-31 04:13:22 +01:00
|
|
|
// create directory for resume files
|
2014-11-19 10:23:08 +01:00
|
|
|
#ifdef TORRENT_WINDOWS
|
|
|
|
int ret = _mkdir(path_append(save_path, ".resume").c_str());
|
|
|
|
#else
|
2014-11-17 04:41:01 +01:00
|
|
|
int ret = mkdir(path_append(save_path, ".resume").c_str(), 0777);
|
2014-11-19 10:23:08 +01:00
|
|
|
#endif
|
2014-11-17 04:41:01 +01:00
|
|
|
if (ret < 0)
|
|
|
|
fprintf(stderr, "failed to create resume file directory: (%d) %s\n"
|
|
|
|
, errno, strerror(errno));
|
2011-01-31 04:13:22 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
if (bind_to_interface.empty()) bind_to_interface = "0.0.0.0";
|
2015-01-01 19:03:56 +01:00
|
|
|
char iface_str[100];
|
|
|
|
snprintf(iface_str, sizeof(iface_str), "%s:%d", bind_to_interface.c_str()
|
|
|
|
, listen_port);
|
|
|
|
settings.set_str(settings_pack::listen_interfaces, iface_str);
|
2003-10-30 00:28:09 +01:00
|
|
|
|
2015-06-07 19:52:19 +02:00
|
|
|
settings.set_str(settings_pack::user_agent, "client_test/" LIBTORRENT_VERSION);
|
|
|
|
settings.set_int(settings_pack::alert_mask, alert::all_categories
|
|
|
|
& ~(alert::dht_notification
|
|
|
|
+ alert::progress_notification
|
|
|
|
+ alert::stats_notification
|
|
|
|
+ alert::session_log_notification
|
|
|
|
+ alert::torrent_log_notification
|
|
|
|
+ alert::peer_log_notification
|
|
|
|
+ alert::dht_log_notification
|
2015-11-29 06:58:46 +01:00
|
|
|
+ alert::picker_log_notification
|
2015-06-07 19:52:19 +02:00
|
|
|
));
|
|
|
|
|
|
|
|
libtorrent::session ses(settings);
|
|
|
|
|
|
|
|
if (rate_limit_locals)
|
|
|
|
{
|
|
|
|
ip_filter pcf;
|
|
|
|
// 1 is the global peer class. This should be done properly in the future
|
|
|
|
pcf.add_rule(address_v4::from_string("0.0.0.0")
|
|
|
|
, address_v4::from_string("255.255.255.255"), 1);
|
|
|
|
#if TORRENT_USE_IPV6
|
|
|
|
pcf.add_rule(address_v6::from_string("::")
|
|
|
|
, address_v6::from_string("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"), 1);
|
|
|
|
#endif
|
|
|
|
ses.set_peer_class_filter(pcf);
|
|
|
|
}
|
|
|
|
|
|
|
|
ses.set_ip_filter(loaded_ip_filter);
|
|
|
|
|
2010-12-29 03:17:44 +01:00
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
2013-09-09 09:08:02 +02:00
|
|
|
dht_settings dht;
|
|
|
|
dht.privacy_lookups = true;
|
|
|
|
ses.set_dht_settings(dht);
|
|
|
|
|
2010-05-03 07:51:01 +02:00
|
|
|
if (start_dht)
|
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
settings.set_bool(settings_pack::use_dht_as_fallback, false);
|
2010-05-03 07:51:01 +02:00
|
|
|
|
|
|
|
ses.add_dht_router(std::make_pair(
|
|
|
|
std::string("router.bittorrent.com"), 6881));
|
|
|
|
ses.add_dht_router(std::make_pair(
|
|
|
|
std::string("router.utorrent.com"), 6881));
|
|
|
|
ses.add_dht_router(std::make_pair(
|
|
|
|
std::string("router.bitcomet.com"), 6881));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-06-07 19:52:19 +02:00
|
|
|
ses.set_load_function(&load_torrent);
|
2011-04-27 10:07:24 +02:00
|
|
|
|
2015-06-07 19:52:19 +02:00
|
|
|
std::vector<char> in;
|
|
|
|
error_code ec;
|
|
|
|
if (load_file(".ses_state", in, ec) == 0)
|
|
|
|
{
|
|
|
|
bdecode_node e;
|
|
|
|
if (bdecode(&in[0], &in[0] + in.size(), e, ec) == 0)
|
|
|
|
ses.load_state(e);
|
|
|
|
}
|
2009-02-26 08:09:56 +01:00
|
|
|
|
2010-06-05 23:03:01 +02:00
|
|
|
for (std::vector<add_torrent_params>::iterator i = magnet_links.begin()
|
|
|
|
, end(magnet_links.end()); i != end; ++i)
|
|
|
|
{
|
2011-12-24 05:30:27 +01:00
|
|
|
ses.async_add_torrent(*i);
|
2010-06-05 23:03:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (std::vector<std::string>::iterator i = torrents.begin()
|
|
|
|
, end(torrents.end()); i != end; ++i)
|
|
|
|
{
|
2010-12-30 02:47:30 +01:00
|
|
|
if (std::strstr(i->c_str(), "http://") == i->c_str()
|
|
|
|
|| std::strstr(i->c_str(), "https://") == i->c_str()
|
|
|
|
|| std::strstr(i->c_str(), "magnet:") == i->c_str())
|
2010-06-05 23:03:01 +02:00
|
|
|
{
|
|
|
|
add_torrent_params p;
|
2011-11-08 06:36:22 +01:00
|
|
|
if (seed_mode) p.flags |= add_torrent_params::flag_seed_mode;
|
2011-07-10 22:17:32 +02:00
|
|
|
if (disable_storage) p.storage = disabled_storage_constructor;
|
2011-11-08 06:36:22 +01:00
|
|
|
if (share_mode) p.flags |= add_torrent_params::flag_share_mode;
|
2010-06-05 23:03:01 +02:00
|
|
|
p.save_path = save_path;
|
|
|
|
p.storage_mode = (storage_mode_t)allocation_mode;
|
2010-12-30 02:47:30 +01:00
|
|
|
p.url = *i;
|
|
|
|
|
2011-02-26 08:48:05 +01:00
|
|
|
std::vector<char> buf;
|
|
|
|
if (std::strstr(i->c_str(), "magnet:") == i->c_str())
|
|
|
|
{
|
2012-03-19 00:31:04 +01:00
|
|
|
add_torrent_params tmp;
|
2013-02-28 05:31:55 +01:00
|
|
|
ec.clear();
|
2012-03-19 00:31:04 +01:00
|
|
|
parse_magnet_uri(*i, tmp, ec);
|
2011-02-26 08:48:05 +01:00
|
|
|
|
2012-03-19 00:31:04 +01:00
|
|
|
if (ec) continue;
|
2011-02-26 08:48:05 +01:00
|
|
|
|
2014-11-17 04:41:01 +01:00
|
|
|
std::string filename = path_append(save_path, path_append(".resume"
|
2012-03-19 00:31:04 +01:00
|
|
|
, to_hex(tmp.info_hash.to_string()) + ".resume"));
|
2011-02-26 08:48:05 +01:00
|
|
|
|
2015-07-11 09:39:00 +02:00
|
|
|
load_file(filename, p.resume_data, ec);
|
2011-02-26 08:48:05 +01:00
|
|
|
}
|
|
|
|
|
2010-12-30 02:47:30 +01:00
|
|
|
printf("adding URL: %s\n", i->c_str());
|
2011-12-24 05:30:27 +01:00
|
|
|
ses.async_add_torrent(p);
|
2010-06-05 23:03:01 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if it's a torrent file, open it as usual
|
2011-12-18 08:18:42 +01:00
|
|
|
add_torrent(ses, files, non_files, i->c_str()
|
2010-06-05 23:03:01 +02:00
|
|
|
, allocation_mode, save_path, false
|
|
|
|
, torrent_upload_limit, torrent_download_limit);
|
|
|
|
}
|
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
// main loop
|
|
|
|
std::vector<peer_info> peers;
|
|
|
|
std::vector<partial_piece_info> queue;
|
|
|
|
|
2011-07-04 07:33:06 +02:00
|
|
|
int tick = 0;
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
#ifndef WIN32
|
|
|
|
signal(SIGTERM, signal_handler);
|
|
|
|
signal(SIGINT, signal_handler);
|
|
|
|
#endif
|
2014-07-09 00:37:13 +02:00
|
|
|
|
2014-07-08 17:35:14 +02:00
|
|
|
while (!quit)
|
2009-02-26 08:09:56 +01:00
|
|
|
{
|
2011-07-04 07:33:06 +02:00
|
|
|
++tick;
|
2011-12-23 06:36:13 +01:00
|
|
|
ses.post_torrent_updates();
|
2014-07-06 21:18:00 +02:00
|
|
|
ses.post_session_stats();
|
2015-01-17 18:02:58 +01:00
|
|
|
ses.post_dht_stats();
|
2011-03-23 03:46:22 +01:00
|
|
|
|
2014-07-09 00:37:13 +02:00
|
|
|
int terminal_width = 80;
|
|
|
|
int terminal_height = 50;
|
|
|
|
terminal_size(&terminal_width, &terminal_height);
|
2015-02-08 04:24:56 +01:00
|
|
|
view.set_size(terminal_width, terminal_height / 3);
|
|
|
|
ses_view.set_pos(terminal_height / 3);
|
2013-12-05 08:43:54 +01:00
|
|
|
|
2014-07-09 00:37:13 +02:00
|
|
|
int c = 0;
|
2013-12-05 08:43:54 +01:00
|
|
|
if (sleep_and_input(&c, refresh_delay))
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2014-07-09 00:37:13 +02:00
|
|
|
#ifdef _WIN32
|
2014-07-06 21:18:00 +02:00
|
|
|
#define ESCAPE_SEQ 224
|
|
|
|
#define LEFT_ARROW 75
|
|
|
|
#define RIGHT_ARROW 77
|
|
|
|
#define UP_ARROW 72
|
|
|
|
#define DOWN_ARROW 80
|
|
|
|
#else
|
|
|
|
#define ESCAPE_SEQ 27
|
|
|
|
#define LEFT_ARROW 68
|
|
|
|
#define RIGHT_ARROW 67
|
|
|
|
#define UP_ARROW 65
|
|
|
|
#define DOWN_ARROW 66
|
|
|
|
#endif
|
|
|
|
|
2014-07-08 17:35:14 +02:00
|
|
|
torrent_handle h = view.get_active_handle();
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
if (c == EOF) { break; }
|
2013-12-05 08:43:54 +01:00
|
|
|
do
|
2003-10-30 00:28:09 +01:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
if (c == ESCAPE_SEQ)
|
2013-12-05 08:43:54 +01:00
|
|
|
{
|
|
|
|
// escape code, read another character
|
2014-07-06 21:18:00 +02:00
|
|
|
#ifdef WIN32
|
|
|
|
int c = _getch();
|
2008-05-07 11:28:49 +02:00
|
|
|
#else
|
2013-12-05 08:43:54 +01:00
|
|
|
int c = getc(stdin);
|
|
|
|
if (c == EOF) { break; }
|
|
|
|
if (c != '[') continue;
|
|
|
|
c = getc(stdin);
|
2008-05-07 11:28:49 +02:00
|
|
|
#endif
|
2013-12-05 08:43:54 +01:00
|
|
|
if (c == EOF) break;
|
2014-07-06 21:18:00 +02:00
|
|
|
if (c == LEFT_ARROW)
|
2011-02-01 10:48:28 +01:00
|
|
|
{
|
2013-12-05 08:43:54 +01:00
|
|
|
// arrow left
|
2014-07-08 17:35:14 +02:00
|
|
|
int filter = view.filter();
|
|
|
|
if (filter > 0)
|
2013-12-05 08:43:54 +01:00
|
|
|
{
|
2014-07-08 17:35:14 +02:00
|
|
|
--filter;
|
|
|
|
view.set_filter(filter);
|
|
|
|
h = view.get_active_handle();
|
2013-12-05 08:43:54 +01:00
|
|
|
}
|
2011-02-01 10:48:28 +01:00
|
|
|
}
|
2014-07-06 21:18:00 +02:00
|
|
|
else if (c == RIGHT_ARROW)
|
2011-02-01 10:48:28 +01:00
|
|
|
{
|
2013-12-05 08:43:54 +01:00
|
|
|
// arrow right
|
2014-07-08 17:35:14 +02:00
|
|
|
int filter = view.filter();
|
|
|
|
if (filter < torrent_view::torrents_max - 1)
|
2013-12-05 08:43:54 +01:00
|
|
|
{
|
2014-07-08 17:35:14 +02:00
|
|
|
++filter;
|
|
|
|
view.set_filter(filter);
|
|
|
|
h = view.get_active_handle();
|
2013-12-05 08:43:54 +01:00
|
|
|
}
|
|
|
|
}
|
2014-07-06 21:18:00 +02:00
|
|
|
else if (c == UP_ARROW)
|
2013-12-05 08:43:54 +01:00
|
|
|
{
|
|
|
|
// arrow up
|
2014-07-08 17:35:14 +02:00
|
|
|
view.arrow_up();
|
|
|
|
h = view.get_active_handle();
|
2013-12-05 08:43:54 +01:00
|
|
|
}
|
2014-07-06 21:18:00 +02:00
|
|
|
else if (c == DOWN_ARROW)
|
2013-12-05 08:43:54 +01:00
|
|
|
{
|
|
|
|
// arrow down
|
2014-07-08 17:35:14 +02:00
|
|
|
view.arrow_down();
|
|
|
|
h = view.get_active_handle();
|
2011-02-01 10:48:28 +01:00
|
|
|
}
|
2010-11-06 17:57:15 +01:00
|
|
|
}
|
2013-12-05 08:43:54 +01:00
|
|
|
|
|
|
|
if (c == ' ')
|
2009-02-26 08:09:56 +01:00
|
|
|
{
|
2013-12-05 08:43:54 +01:00
|
|
|
if (ses.is_paused()) ses.resume();
|
|
|
|
else ses.pause();
|
2008-05-07 11:28:49 +02:00
|
|
|
}
|
2013-12-05 08:43:54 +01:00
|
|
|
|
|
|
|
// add magnet link
|
|
|
|
if (c == 'm')
|
2008-06-29 21:08:30 +02:00
|
|
|
{
|
2013-12-05 08:43:54 +01:00
|
|
|
char url[4096];
|
|
|
|
puts("Enter magnet link:\n");
|
2015-10-04 16:57:12 +02:00
|
|
|
scanf("%4095s", url);
|
2013-12-05 08:43:54 +01:00
|
|
|
|
|
|
|
add_torrent_params p;
|
|
|
|
if (seed_mode) p.flags |= add_torrent_params::flag_seed_mode;
|
|
|
|
if (disable_storage) p.storage = disabled_storage_constructor;
|
|
|
|
if (share_mode) p.flags |= add_torrent_params::flag_share_mode;
|
|
|
|
p.save_path = save_path;
|
|
|
|
p.storage_mode = (storage_mode_t)allocation_mode;
|
|
|
|
p.url = url;
|
|
|
|
|
|
|
|
std::vector<char> buf;
|
|
|
|
if (std::strstr(url, "magnet:") == url)
|
|
|
|
{
|
|
|
|
add_torrent_params tmp;
|
|
|
|
parse_magnet_uri(url, tmp, ec);
|
2009-02-26 08:09:56 +01:00
|
|
|
|
2013-12-05 08:43:54 +01:00
|
|
|
if (ec) continue;
|
2008-06-29 21:08:30 +02:00
|
|
|
|
2014-11-17 04:41:01 +01:00
|
|
|
std::string filename = path_append(save_path, path_append(".resume"
|
2013-12-05 08:43:54 +01:00
|
|
|
, to_hex(tmp.info_hash.to_string()) + ".resume"));
|
2012-09-23 03:52:46 +02:00
|
|
|
|
2015-07-11 09:39:00 +02:00
|
|
|
load_file(filename, p.resume_data, ec);
|
2013-12-05 08:43:54 +01:00
|
|
|
}
|
2012-09-23 03:52:46 +02:00
|
|
|
|
2013-12-05 08:43:54 +01:00
|
|
|
printf("adding URL: %s\n", url);
|
|
|
|
ses.async_add_torrent(p);
|
|
|
|
}
|
2012-09-23 03:52:46 +02:00
|
|
|
|
2013-12-05 08:43:54 +01:00
|
|
|
if (c == 'q') break;
|
2012-09-23 03:52:46 +02:00
|
|
|
|
2014-12-30 17:59:37 +01:00
|
|
|
if (c == 'W' && h.is_valid())
|
|
|
|
{
|
|
|
|
std::set<std::string> seeds = h.url_seeds();
|
|
|
|
for (std::set<std::string>::iterator i = seeds.begin()
|
|
|
|
, end(seeds.end()); i != end; ++i)
|
|
|
|
{
|
2014-12-30 18:05:34 +01:00
|
|
|
h.remove_url_seed(*i);
|
2014-12-30 17:59:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
seeds = h.http_seeds();
|
|
|
|
for (std::set<std::string>::iterator i = seeds.begin()
|
|
|
|
, end(seeds.end()); i != end; ++i)
|
|
|
|
{
|
2014-12-30 18:05:34 +01:00
|
|
|
h.remove_http_seed(*i);
|
2014-12-30 17:59:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-08 17:35:14 +02:00
|
|
|
if (c == 'D' && h.is_valid())
|
2013-12-05 08:43:54 +01:00
|
|
|
{
|
2014-07-08 17:35:14 +02:00
|
|
|
torrent_status const& st = view.get_active_torrent();
|
|
|
|
printf("\n\nARE YOU SURE YOU WANT TO DELETE THE FILES FOR '%s'. THIS OPERATION CANNOT BE UNDONE. (y/N)"
|
|
|
|
, st.name.c_str());
|
|
|
|
char response = 'n';
|
|
|
|
scanf("%c", &response);
|
|
|
|
if (response == 'y')
|
2013-12-05 08:43:54 +01:00
|
|
|
{
|
2014-07-08 17:35:14 +02:00
|
|
|
// also delete the .torrent file from the torrent directory
|
|
|
|
handles_t::iterator i = std::find_if(files.begin(), files.end()
|
|
|
|
, boost::bind(&handles_t::value_type::second, _1) == st.handle);
|
|
|
|
if (i != files.end())
|
2013-12-05 08:43:54 +01:00
|
|
|
{
|
2014-07-08 17:35:14 +02:00
|
|
|
error_code ec;
|
|
|
|
std::string path;
|
2014-11-17 04:41:01 +01:00
|
|
|
if (is_absolute_path(i->first)) path = i->first;
|
|
|
|
else path = path_append(monitor_dir, i->first);
|
|
|
|
if (::remove(path.c_str()) < 0)
|
|
|
|
printf("failed to delete .torrent file: %s\n", ec.message().c_str());
|
2014-07-08 17:35:14 +02:00
|
|
|
files.erase(i);
|
2013-12-05 08:43:54 +01:00
|
|
|
}
|
2014-07-08 17:35:14 +02:00
|
|
|
if (st.handle.is_valid())
|
|
|
|
ses.remove_torrent(st.handle, lt::session::delete_files);
|
2013-12-05 08:43:54 +01:00
|
|
|
}
|
|
|
|
}
|
2012-09-23 03:52:46 +02:00
|
|
|
|
2014-07-08 17:35:14 +02:00
|
|
|
if (c == 'j' && h.is_valid())
|
2013-12-05 08:43:54 +01:00
|
|
|
{
|
2014-07-08 17:35:14 +02:00
|
|
|
h.force_recheck();
|
2012-09-23 03:52:46 +02:00
|
|
|
}
|
|
|
|
|
2014-07-08 17:35:14 +02:00
|
|
|
if (c == 'r' && h.is_valid())
|
2013-12-05 08:43:54 +01:00
|
|
|
{
|
2014-07-08 17:35:14 +02:00
|
|
|
h.force_reannounce();
|
2013-12-05 08:43:54 +01:00
|
|
|
}
|
2012-09-23 03:52:46 +02:00
|
|
|
|
2014-07-08 17:35:14 +02:00
|
|
|
if (c == 's' && h.is_valid())
|
2013-12-05 08:43:54 +01:00
|
|
|
{
|
2014-07-08 17:35:14 +02:00
|
|
|
torrent_status const& ts = view.get_active_torrent();
|
|
|
|
h.set_sequential_download(!ts.sequential_download);
|
2013-12-05 08:43:54 +01:00
|
|
|
}
|
2008-01-31 18:52:29 +01:00
|
|
|
|
2013-12-05 08:43:54 +01:00
|
|
|
if (c == 'R')
|
2011-04-10 22:35:45 +02:00
|
|
|
{
|
2013-12-05 08:43:54 +01:00
|
|
|
// save resume data for all torrents
|
2014-07-08 17:35:14 +02:00
|
|
|
std::vector<torrent_status> torrents;
|
|
|
|
ses.get_torrent_status(&torrents, &yes, 0);
|
|
|
|
for (std::vector<torrent_status>::iterator i = torrents.begin()
|
|
|
|
, end(torrents.end()); i != end; ++i)
|
2011-04-10 22:35:45 +02:00
|
|
|
{
|
2014-07-08 17:35:14 +02:00
|
|
|
if (i->need_save_resume)
|
2011-04-10 22:35:45 +02:00
|
|
|
{
|
2014-07-08 17:35:14 +02:00
|
|
|
i->handle.save_resume_data();
|
2013-12-05 08:43:54 +01:00
|
|
|
++num_outstanding_resume_data;
|
2011-04-10 22:35:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-08 17:35:14 +02:00
|
|
|
if (c == 'o' && h.is_valid())
|
2013-12-05 08:43:54 +01:00
|
|
|
{
|
2014-07-08 17:35:14 +02:00
|
|
|
torrent_status const& ts = view.get_active_torrent();
|
2013-12-05 08:43:54 +01:00
|
|
|
int num_pieces = ts.num_pieces;
|
|
|
|
if (num_pieces > 300) num_pieces = 300;
|
|
|
|
for (int i = 0; i < num_pieces; ++i)
|
|
|
|
{
|
2014-07-08 17:35:14 +02:00
|
|
|
h.set_piece_deadline(i, (i+5) * 1000, torrent_handle::alert_when_available);
|
2013-12-05 08:43:54 +01:00
|
|
|
}
|
|
|
|
}
|
2008-09-07 11:59:54 +02:00
|
|
|
|
2014-07-08 17:35:14 +02:00
|
|
|
if (c == 'v' && h.is_valid())
|
2013-12-05 08:43:54 +01:00
|
|
|
{
|
2014-07-08 17:35:14 +02:00
|
|
|
h.scrape_tracker();
|
2013-12-05 08:43:54 +01:00
|
|
|
}
|
2009-02-26 08:09:56 +01:00
|
|
|
|
2014-07-08 17:35:14 +02:00
|
|
|
if (c == 'p' && h.is_valid())
|
2010-03-20 23:00:13 +01:00
|
|
|
{
|
2014-07-08 17:35:14 +02:00
|
|
|
torrent_status const& ts = view.get_active_torrent();
|
2013-12-05 08:43:54 +01:00
|
|
|
if (!ts.auto_managed && ts.paused)
|
2011-07-04 07:33:06 +02:00
|
|
|
{
|
2014-07-08 17:35:14 +02:00
|
|
|
h.auto_managed(true);
|
2013-12-05 08:43:54 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-07-08 17:35:14 +02:00
|
|
|
h.auto_managed(false);
|
|
|
|
h.pause(torrent_handle::graceful_pause);
|
2011-07-04 07:33:06 +02:00
|
|
|
}
|
2010-03-20 23:00:13 +01:00
|
|
|
}
|
|
|
|
|
2013-12-05 08:43:54 +01:00
|
|
|
// toggle force-start
|
2014-07-08 17:35:14 +02:00
|
|
|
if (c == 'k' && h.is_valid())
|
2009-11-02 02:01:07 +01:00
|
|
|
{
|
2014-07-08 17:35:14 +02:00
|
|
|
torrent_status const& ts = view.get_active_torrent();
|
|
|
|
h.auto_managed(!ts.auto_managed);
|
|
|
|
if (ts.auto_managed && ts.paused) h.resume();
|
2009-11-02 02:01:07 +01:00
|
|
|
}
|
|
|
|
|
2014-07-08 17:35:14 +02:00
|
|
|
if (c == 'c' && h.is_valid())
|
2013-12-05 08:43:54 +01:00
|
|
|
{
|
2014-07-08 17:35:14 +02:00
|
|
|
h.clear_error();
|
2013-12-05 08:43:54 +01:00
|
|
|
}
|
2009-02-26 08:09:56 +01:00
|
|
|
|
2013-12-05 08:43:54 +01:00
|
|
|
// toggle displays
|
|
|
|
if (c == 't') print_trackers = !print_trackers;
|
|
|
|
if (c == 'i') print_peers = !print_peers;
|
|
|
|
if (c == 'l') print_log = !print_log;
|
|
|
|
if (c == 'd') print_downloads = !print_downloads;
|
|
|
|
if (c == 'f') print_file_progress = !print_file_progress;
|
|
|
|
if (c == 'P') show_pad_files = !show_pad_files;
|
|
|
|
if (c == 'g') show_dht_status = !show_dht_status;
|
2015-01-04 22:31:02 +01:00
|
|
|
if (c == 'u') ses_view.print_utp_stats(!ses_view.print_utp_stats());
|
2014-07-09 00:37:13 +02:00
|
|
|
if (c == 'x') print_disk_stats = !print_disk_stats;
|
2013-12-05 08:43:54 +01:00
|
|
|
// toggle columns
|
|
|
|
if (c == '1') print_ip = !print_ip;
|
|
|
|
if (c == '3') print_timers = !print_timers;
|
|
|
|
if (c == '4') print_block = !print_block;
|
|
|
|
if (c == '5') print_peer_rate = !print_peer_rate;
|
|
|
|
if (c == '6') print_fails = !print_fails;
|
|
|
|
if (c == '7') print_send_bufs = !print_send_bufs;
|
|
|
|
if (c == 'h')
|
2010-11-06 19:04:07 +01:00
|
|
|
{
|
2014-07-08 17:35:14 +02:00
|
|
|
clear_screen();
|
|
|
|
set_cursor_pos(0,0);
|
|
|
|
print(
|
2013-12-05 08:43:54 +01:00
|
|
|
"HELP SCREEN (press any key to dismiss)\n\n"
|
|
|
|
"CLIENT OPTIONS\n"
|
|
|
|
"[q] quit client [m] add magnet link\n"
|
|
|
|
"\n"
|
|
|
|
"TORRENT ACTIONS\n"
|
|
|
|
"[p] pause/unpause selected torrent\n"
|
|
|
|
"[s] toggle sequential download [j] force recheck\n"
|
|
|
|
"[space] toggle session pause [c] clear error\n"
|
|
|
|
"[v] scrape [D] delete torrent and data\n"
|
|
|
|
"[r] force reannounce [R] save resume data for all torrents\n"
|
|
|
|
"[o] set piece deadlines (sequential dl) [P] toggle auto-managed\n"
|
2014-12-30 17:59:37 +01:00
|
|
|
"[k] toggle force-started [W] remove all web seeds\n"
|
2013-12-05 08:43:54 +01:00
|
|
|
"\n"
|
|
|
|
"DISPLAY OPTIONS\n"
|
|
|
|
"left/right arrow keys: select torrent filter\n"
|
|
|
|
"up/down arrow keys: select torrent\n"
|
|
|
|
"[i] toggle show peers [d] toggle show downloading pieces\n"
|
2015-01-15 08:30:51 +01:00
|
|
|
"[u] show uTP stats [f] toggle show files\n"
|
2013-12-05 08:43:54 +01:00
|
|
|
"[g] show DHT [x] toggle disk cache stats\n"
|
|
|
|
"[t] show trackers [l] show alert log\n"
|
2015-01-15 08:30:51 +01:00
|
|
|
"[P] show pad files (in file list)\n"
|
2013-12-05 08:43:54 +01:00
|
|
|
"\n"
|
|
|
|
"COLUMN OPTIONS\n"
|
2015-05-11 02:49:34 +02:00
|
|
|
"[1] toggle IP column [2]\n"
|
2013-12-05 08:43:54 +01:00
|
|
|
"[3] toggle timers column [4] toggle block progress column\n"
|
|
|
|
"[5] toggle peer rate column [6] toggle failures column\n"
|
|
|
|
"[7] toggle send buffers column\n"
|
|
|
|
);
|
|
|
|
int tmp;
|
|
|
|
while (sleep_and_input(&tmp, 500) == false);
|
2010-11-06 19:04:07 +01:00
|
|
|
}
|
2014-07-09 00:37:13 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
} while (sleep_and_input(&c, 0));
|
2013-12-05 08:43:54 +01:00
|
|
|
if (c == 'q') break;
|
2009-02-26 08:09:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// loop through the alert queue to see if anything has happened.
|
2015-04-03 22:15:48 +02:00
|
|
|
std::vector<alert*> alerts;
|
2011-03-14 03:59:46 +01:00
|
|
|
ses.pop_alerts(&alerts);
|
2014-05-10 22:20:57 +02:00
|
|
|
std::string now = timestamp();
|
2015-04-03 22:15:48 +02:00
|
|
|
for (std::vector<alert*>::iterator i = alerts.begin()
|
2011-03-14 03:59:46 +01:00
|
|
|
, end(alerts.end()); i != end; ++i)
|
2009-02-26 08:09:56 +01:00
|
|
|
{
|
2011-06-21 04:02:58 +02:00
|
|
|
TORRENT_TRY
|
|
|
|
{
|
2014-07-09 00:37:13 +02:00
|
|
|
if (!::handle_alert(ses, *i, files, non_files))
|
2011-12-23 06:36:13 +01:00
|
|
|
{
|
|
|
|
// if we didn't handle the alert, print it to the log
|
|
|
|
std::string event_string;
|
|
|
|
print_alert(*i, event_string);
|
|
|
|
events.push_back(event_string);
|
|
|
|
if (events.size() >= 20) events.pop_front();
|
|
|
|
}
|
2011-06-21 04:02:58 +02:00
|
|
|
} TORRENT_CATCH(std::exception& e) {}
|
2009-02-26 08:09:56 +01:00
|
|
|
}
|
2011-03-14 03:59:46 +01:00
|
|
|
alerts.clear();
|
2004-07-18 02:39:58 +02:00
|
|
|
|
2009-05-13 03:02:06 +02:00
|
|
|
std::string out;
|
2008-07-06 14:22:56 +02:00
|
|
|
|
2009-05-13 03:02:06 +02:00
|
|
|
char str[500];
|
2008-05-07 20:02:11 +02:00
|
|
|
|
2014-07-09 00:37:13 +02:00
|
|
|
int pos = view.height() + ses_view.height();
|
|
|
|
set_cursor_pos(0, pos);
|
2008-09-20 19:42:25 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
int cache_flags = print_downloads ? 0 : lt::session::disk_cache_no_pieces;
|
2014-07-08 17:35:14 +02:00
|
|
|
torrent_handle h = view.get_active_handle();
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
cache_status cs;
|
|
|
|
ses.get_cache_info(&cs, h, cache_flags);
|
|
|
|
|
2009-08-30 08:25:17 +02:00
|
|
|
#ifndef TORRENT_DISABLE_DHT
|
2009-02-26 08:09:56 +01:00
|
|
|
if (show_dht_status)
|
|
|
|
{
|
2015-01-17 18:02:58 +01:00
|
|
|
// TODO: 3 expose these counters as performance counters
|
|
|
|
/*
|
2010-12-12 04:17:08 +01:00
|
|
|
snprintf(str, sizeof(str), "DHT nodes: %d DHT cached nodes: %d "
|
2013-09-25 21:44:29 +02:00
|
|
|
"total DHT size: %" PRId64 " total observers: %d\n"
|
2010-11-06 08:12:57 +01:00
|
|
|
, sess_stat.dht_nodes, sess_stat.dht_node_cache, sess_stat.dht_global_nodes
|
|
|
|
, sess_stat.dht_total_allocations);
|
2009-05-13 03:02:06 +02:00
|
|
|
out += str;
|
2015-01-17 18:02:58 +01:00
|
|
|
*/
|
2015-02-08 04:24:56 +01:00
|
|
|
|
2010-12-12 04:17:08 +01:00
|
|
|
int bucket = 0;
|
2015-01-17 18:02:58 +01:00
|
|
|
for (std::vector<dht_routing_bucket>::iterator i = dht_routing_table.begin()
|
|
|
|
, end(dht_routing_table.end()); i != end; ++i, ++bucket)
|
2010-12-12 04:17:08 +01:00
|
|
|
{
|
2014-11-02 10:41:29 +01:00
|
|
|
char const* progress_bar =
|
|
|
|
"################################"
|
|
|
|
"################################"
|
|
|
|
"################################"
|
|
|
|
"################################";
|
2014-12-09 10:49:14 +01:00
|
|
|
char const* short_progress_bar = "--------";
|
2010-12-12 04:17:08 +01:00
|
|
|
snprintf(str, sizeof(str)
|
2015-02-08 04:24:56 +01:00
|
|
|
, "%3d [%3d, %d] %s%s\x1b[K\n"
|
2014-11-02 10:41:29 +01:00
|
|
|
, bucket, i->num_nodes, i->num_replacements
|
|
|
|
, progress_bar + (128 - i->num_nodes)
|
2014-12-09 10:49:14 +01:00
|
|
|
, short_progress_bar + (8 - (std::min)(8, i->num_replacements)));
|
2010-12-12 04:17:08 +01:00
|
|
|
out += str;
|
2015-02-08 04:24:56 +01:00
|
|
|
pos += 1;
|
2010-12-12 04:17:08 +01:00
|
|
|
}
|
|
|
|
|
2015-01-17 18:02:58 +01:00
|
|
|
for (std::vector<dht_lookup>::iterator i = dht_active_requests.begin()
|
|
|
|
, end(dht_active_requests.end()); i != end; ++i)
|
2008-05-07 11:28:49 +02:00
|
|
|
{
|
2009-10-09 04:34:25 +02:00
|
|
|
snprintf(str, sizeof(str)
|
2010-12-12 04:28:06 +01:00
|
|
|
, " %10s [limit: %2d] "
|
|
|
|
"in-flight: %-2d "
|
|
|
|
"left: %-3d "
|
|
|
|
"1st-timeout: %-2d "
|
|
|
|
"timeouts: %-2d "
|
|
|
|
"responses: %-2d "
|
2015-02-08 04:24:56 +01:00
|
|
|
"last_sent: %-2d\x1b[K\n"
|
2010-12-12 04:28:06 +01:00
|
|
|
, i->type
|
|
|
|
, i->branch_factor
|
|
|
|
, i->outstanding_requests
|
|
|
|
, i->nodes_left
|
|
|
|
, i->first_timeout
|
|
|
|
, i->timeouts
|
|
|
|
, i->responses
|
|
|
|
, i->last_sent);
|
2009-05-13 03:02:06 +02:00
|
|
|
out += str;
|
2015-02-08 04:24:56 +01:00
|
|
|
pos += 1;
|
2009-02-26 08:09:56 +01:00
|
|
|
}
|
|
|
|
}
|
2009-08-30 08:25:17 +02:00
|
|
|
#endif
|
2014-07-06 21:18:00 +02:00
|
|
|
if (h.is_valid())
|
2009-02-26 08:09:56 +01:00
|
|
|
{
|
2014-07-08 17:35:14 +02:00
|
|
|
torrent_status const& s = view.get_active_torrent();
|
2009-02-26 08:09:56 +01:00
|
|
|
|
2015-02-08 04:24:56 +01:00
|
|
|
print((piece_bar(s.pieces, 126) + "\x1b[K\n").c_str());
|
|
|
|
pos += 1;
|
2014-07-10 10:32:32 +02:00
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
if ((print_downloads && s.state != torrent_status::seeding)
|
|
|
|
|| print_peers)
|
|
|
|
h.get_peer_info(peers);
|
2008-05-07 11:28:49 +02:00
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
if (print_peers && !peers.empty())
|
2015-02-08 04:24:56 +01:00
|
|
|
pos += print_peer_info(out, peers, terminal_height - pos - 2);
|
2004-02-25 00:55:42 +01:00
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
if (print_trackers)
|
|
|
|
{
|
|
|
|
std::vector<announce_entry> tr = h.trackers();
|
2015-03-12 05:34:54 +01:00
|
|
|
time_point now = clock_type::now();
|
2009-02-26 08:09:56 +01:00
|
|
|
for (std::vector<announce_entry>::iterator i = tr.begin()
|
|
|
|
, end(tr.end()); i != end; ++i)
|
2008-11-29 09:38:40 +01:00
|
|
|
{
|
2015-02-08 04:24:56 +01:00
|
|
|
if (pos + 1 >= terminal_height) break;
|
|
|
|
snprintf(str, sizeof(str), "%2d %-55s fails: %-3d (%-3d) %s %s %5d \"%s\" %s\x1b[K\n"
|
2010-05-04 18:49:26 +02:00
|
|
|
, i->tier, i->url.c_str(), i->fails, i->fail_limit, i->verified?"OK ":"- "
|
2009-05-14 21:49:33 +02:00
|
|
|
, i->updating?"updating"
|
2013-11-02 10:14:39 +01:00
|
|
|
:to_string(int(total_seconds(i->next_announce - now)), 8).c_str()
|
2014-07-06 21:18:00 +02:00
|
|
|
, int(i->min_announce > now ? total_seconds(i->min_announce - now) : 0)
|
2010-04-13 06:37:39 +02:00
|
|
|
, i->last_error ? i->last_error.message().c_str() : ""
|
|
|
|
, i->message.c_str());
|
2009-05-13 03:02:06 +02:00
|
|
|
out += str;
|
2015-02-08 04:24:56 +01:00
|
|
|
pos += 1;
|
2008-11-29 09:38:40 +01:00
|
|
|
}
|
2009-02-26 08:09:56 +01:00
|
|
|
}
|
2008-11-29 09:38:40 +01:00
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
if (print_downloads)
|
|
|
|
{
|
|
|
|
h.get_download_queue(queue);
|
2011-07-10 10:57:27 +02:00
|
|
|
|
2010-07-22 18:49:40 +02:00
|
|
|
std::sort(queue.begin(), queue.end(), boost::bind(&partial_piece_info::piece_index, _1)
|
|
|
|
< boost::bind(&partial_piece_info::piece_index, _2));
|
2008-02-08 11:22:05 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
std::sort(cs.pieces.begin(), cs.pieces.end(), boost::bind(&cached_piece_info::piece, _1)
|
|
|
|
> boost::bind(&cached_piece_info::piece, _2));
|
2008-02-08 11:22:05 +01:00
|
|
|
|
2015-02-08 22:59:52 +01:00
|
|
|
int p = 0; // this is horizontal position
|
2014-07-06 21:18:00 +02:00
|
|
|
for (std::vector<cached_piece_info>::iterator i = cs.pieces.begin();
|
|
|
|
i != cs.pieces.end(); ++i)
|
2009-02-26 08:09:56 +01:00
|
|
|
{
|
2015-02-08 04:24:56 +01:00
|
|
|
if (pos + 3 >= terminal_height) break;
|
|
|
|
|
2011-07-10 10:57:27 +02:00
|
|
|
partial_piece_info* pp = 0;
|
|
|
|
partial_piece_info tmp;
|
|
|
|
tmp.piece_index = i->piece;
|
|
|
|
std::vector<partial_piece_info>::iterator ppi
|
|
|
|
= std::lower_bound(queue.begin(), queue.end(), tmp
|
|
|
|
, boost::bind(&partial_piece_info::piece_index, _1)
|
|
|
|
< boost::bind(&partial_piece_info::piece_index, _2));
|
|
|
|
if (ppi != queue.end() && ppi->piece_index == i->piece) pp = &*ppi;
|
|
|
|
|
2014-07-08 17:35:14 +02:00
|
|
|
print_piece(pp, &*i, peers, &s, out);
|
2011-07-10 10:57:27 +02:00
|
|
|
|
2014-10-07 05:09:32 +02:00
|
|
|
int num_blocks = pp ? pp->blocks_in_piece : i->blocks.size();
|
2015-02-08 04:24:56 +01:00
|
|
|
p += num_blocks + 8;
|
|
|
|
bool continuous_mode = 8 + num_blocks > terminal_width;
|
|
|
|
if (continuous_mode)
|
|
|
|
{
|
|
|
|
while (p > terminal_width)
|
|
|
|
{
|
|
|
|
p -= terminal_width;
|
|
|
|
++pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (p + num_blocks + 8 > terminal_width)
|
2014-10-07 05:09:32 +02:00
|
|
|
{
|
2015-02-08 04:24:56 +01:00
|
|
|
out += "\x1b[K\n";
|
|
|
|
pos += 1;
|
|
|
|
p = 0;
|
2014-10-07 05:09:32 +02:00
|
|
|
}
|
|
|
|
|
2011-07-10 10:57:27 +02:00
|
|
|
if (pp) queue.erase(ppi);
|
2004-07-24 13:54:17 +02:00
|
|
|
}
|
2006-06-11 15:48:39 +02:00
|
|
|
|
2011-07-10 10:57:27 +02:00
|
|
|
for (std::vector<partial_piece_info>::iterator i = queue.begin()
|
|
|
|
, end(queue.end()); i != end; ++i)
|
2006-06-11 15:48:39 +02:00
|
|
|
{
|
2015-02-08 04:24:56 +01:00
|
|
|
if (pos + 3 >= terminal_height) break;
|
|
|
|
|
2014-07-08 17:35:14 +02:00
|
|
|
print_piece(&*i, 0, peers, &s, out);
|
2014-10-07 05:09:32 +02:00
|
|
|
|
|
|
|
int num_blocks = i->blocks_in_piece;
|
2015-02-08 04:24:56 +01:00
|
|
|
p += num_blocks + 8;
|
|
|
|
bool continuous_mode = 8 + num_blocks > terminal_width;
|
|
|
|
if (continuous_mode)
|
|
|
|
{
|
|
|
|
while (p > terminal_width)
|
|
|
|
{
|
|
|
|
p -= terminal_width;
|
|
|
|
++pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (p + num_blocks + 8 > terminal_width)
|
2014-10-07 05:09:32 +02:00
|
|
|
{
|
2015-02-08 04:24:56 +01:00
|
|
|
out += "\x1b[K\n";
|
|
|
|
pos += 1;
|
|
|
|
p = 0;
|
2014-10-07 05:09:32 +02:00
|
|
|
}
|
2006-06-11 15:48:39 +02:00
|
|
|
}
|
2015-02-08 04:24:56 +01:00
|
|
|
if (p != 0)
|
|
|
|
{
|
|
|
|
out += "\x1b[K\n";
|
|
|
|
pos += 1;
|
|
|
|
}
|
2014-10-07 05:09:32 +02:00
|
|
|
|
2015-02-08 04:24:56 +01:00
|
|
|
snprintf(str, sizeof(str), "%s %s read cache | %s %s downloading | %s %s cached | %s %s flushed | %s %s snubbed\x1b[K\n"
|
2011-07-10 10:57:27 +02:00
|
|
|
, esc("34;7"), esc("0") // read cache
|
|
|
|
, esc("33;7"), esc("0") // downloading
|
|
|
|
, esc("36;7"), esc("0") // cached
|
2014-12-07 23:22:38 +01:00
|
|
|
, esc("32;7"), esc("0") // flushed
|
|
|
|
, esc("35;7"), esc("0") // snubbed
|
|
|
|
);
|
2011-07-10 11:07:40 +02:00
|
|
|
out += str;
|
2015-02-08 04:24:56 +01:00
|
|
|
pos += 1;
|
2003-10-30 00:28:09 +01:00
|
|
|
}
|
2009-05-14 21:49:33 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
if (print_file_progress && s.has_metadata)
|
2003-12-22 08:14:35 +01:00
|
|
|
{
|
2014-12-03 10:05:09 +01:00
|
|
|
std::vector<boost::int64_t> file_progress;
|
2009-02-26 08:09:56 +01:00
|
|
|
h.file_progress(file_progress);
|
2014-07-06 21:18:00 +02:00
|
|
|
std::vector<pool_file_status> file_status;
|
|
|
|
h.file_status(file_status);
|
|
|
|
std::vector<int> file_prio = h.file_priorities();
|
|
|
|
std::vector<pool_file_status>::iterator f = file_status.begin();
|
|
|
|
boost::shared_ptr<const torrent_info> ti = h.torrent_file();
|
2015-02-08 22:59:52 +01:00
|
|
|
|
|
|
|
int p = 0; // this is horizontal position
|
2012-10-02 18:07:55 +02:00
|
|
|
for (int i = 0; i < ti->num_files(); ++i)
|
2004-07-24 13:54:17 +02:00
|
|
|
{
|
2015-02-08 04:24:56 +01:00
|
|
|
if (pos + 1 >= terminal_height) break;
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
bool pad_file = ti->files().pad_file_at(i);
|
|
|
|
if (pad_file)
|
|
|
|
{
|
|
|
|
if (show_pad_files)
|
|
|
|
{
|
2015-02-08 04:24:56 +01:00
|
|
|
snprintf(str, sizeof(str), "\x1b[34m%-70s %s\x1b[0m\x1b[K\n"
|
2014-07-06 21:18:00 +02:00
|
|
|
, ti->files().file_name(i).c_str()
|
|
|
|
, add_suffix(ti->files().file_size(i)).c_str());
|
|
|
|
out += str;
|
2015-02-08 04:24:56 +01:00
|
|
|
pos += 1;
|
2014-07-06 21:18:00 +02:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
int progress = ti->files().file_size(i) > 0
|
|
|
|
?file_progress[i] * 1000 / ti->files().file_size(i):1000;
|
|
|
|
|
|
|
|
bool complete = file_progress[i] == ti->files().file_size(i);
|
|
|
|
|
|
|
|
std::string title = ti->files().file_name(i);
|
|
|
|
if (!complete)
|
|
|
|
{
|
|
|
|
snprintf(str, sizeof(str), " (%.1f%%)", progress / 10.f);
|
|
|
|
title += str;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (f != file_status.end() && f->file_index == i)
|
|
|
|
{
|
|
|
|
title += " [ ";
|
|
|
|
if ((f->open_mode & file::rw_mask) == file::read_write) title += "read/write ";
|
|
|
|
else if ((f->open_mode & file::rw_mask) == file::read_only) title += "read ";
|
|
|
|
else if ((f->open_mode & file::rw_mask) == file::write_only) title += "write ";
|
|
|
|
if (f->open_mode & file::random_access) title += "random_access ";
|
|
|
|
if (f->open_mode & file::lock_file) title += "locked ";
|
|
|
|
if (f->open_mode & file::sparse) title += "sparse ";
|
|
|
|
title += "]";
|
|
|
|
++f;
|
|
|
|
}
|
|
|
|
|
2015-02-08 22:59:52 +01:00
|
|
|
const int file_progress_width = 65;
|
|
|
|
|
|
|
|
// do we need to line-break?
|
|
|
|
if (p + file_progress_width + 13 > terminal_width)
|
|
|
|
{
|
|
|
|
out += "\x1b[K\n";
|
|
|
|
pos += 1;
|
|
|
|
p = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(str, sizeof(str), "%s %7s p: %d ",
|
|
|
|
progress_bar(progress, file_progress_width, complete ? col_green : col_yellow, '-', '#'
|
2014-07-06 21:18:00 +02:00
|
|
|
, title.c_str()).c_str()
|
2009-05-14 21:49:33 +02:00
|
|
|
, add_suffix(file_progress[i]).c_str()
|
2014-07-06 21:18:00 +02:00
|
|
|
, file_prio[i]);
|
2015-02-08 22:59:52 +01:00
|
|
|
|
|
|
|
p += file_progress_width + 13;
|
2009-05-14 21:49:33 +02:00
|
|
|
out += str;
|
2015-02-08 22:59:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (p != 0)
|
|
|
|
{
|
|
|
|
out += "\x1b[K\n";
|
2015-02-08 04:24:56 +01:00
|
|
|
pos += 1;
|
2004-07-24 13:54:17 +02:00
|
|
|
}
|
2003-12-22 08:14:35 +01:00
|
|
|
}
|
2014-07-10 10:32:32 +02:00
|
|
|
}
|
2005-10-19 02:50:38 +02:00
|
|
|
|
2009-02-26 08:09:56 +01:00
|
|
|
if (print_log)
|
|
|
|
{
|
|
|
|
for (std::deque<std::string>::iterator i = events.begin();
|
|
|
|
i != events.end(); ++i)
|
2005-10-19 02:50:38 +02:00
|
|
|
{
|
2015-02-08 04:24:56 +01:00
|
|
|
if (pos + 1 >= terminal_height) break;
|
2009-05-13 03:02:06 +02:00
|
|
|
out += *i;
|
2015-02-08 04:24:56 +01:00
|
|
|
out += "\x1b[K\n";
|
|
|
|
pos += 1;
|
2005-10-19 02:50:38 +02:00
|
|
|
}
|
2003-10-30 00:28:09 +01:00
|
|
|
}
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2015-02-08 04:24:56 +01:00
|
|
|
// clear rest of screen
|
|
|
|
out += "\x1b[J";
|
2014-07-08 17:35:14 +02:00
|
|
|
print(out.c_str());
|
2015-02-08 04:24:56 +01:00
|
|
|
|
2011-06-02 07:44:46 +02:00
|
|
|
fflush(stdout);
|
2009-02-26 08:09:56 +01:00
|
|
|
|
|
|
|
if (!monitor_dir.empty()
|
2015-03-12 05:34:54 +01:00
|
|
|
&& next_dir_scan < clock_type::now())
|
2009-02-26 08:09:56 +01:00
|
|
|
{
|
2011-12-18 08:18:42 +01:00
|
|
|
scan_dir(monitor_dir, ses, files, non_files
|
2010-01-18 06:29:35 +01:00
|
|
|
, allocation_mode, save_path, torrent_upload_limit
|
2009-02-26 08:09:56 +01:00
|
|
|
, torrent_download_limit);
|
2015-03-12 05:34:54 +01:00
|
|
|
next_dir_scan = clock_type::now() + seconds(poll_interval);
|
2008-04-05 06:53:22 +02:00
|
|
|
}
|
2009-02-26 08:09:56 +01:00
|
|
|
}
|
2008-04-05 06:53:22 +02:00
|
|
|
|
2010-01-31 22:30:43 +01:00
|
|
|
// keep track of the number of resume data
|
|
|
|
// alerts to wait for
|
2011-01-24 01:11:13 +01:00
|
|
|
int num_paused = 0;
|
|
|
|
int num_failed = 0;
|
|
|
|
|
2010-01-31 22:30:43 +01:00
|
|
|
ses.pause();
|
2011-01-24 01:11:13 +01:00
|
|
|
printf("saving resume data\n");
|
2011-02-01 10:48:28 +01:00
|
|
|
std::vector<torrent_status> temp;
|
|
|
|
ses.get_torrent_status(&temp, &yes, 0);
|
|
|
|
for (std::vector<torrent_status>::iterator i = temp.begin();
|
|
|
|
i != temp.end(); ++i)
|
2010-01-31 22:30:43 +01:00
|
|
|
{
|
2011-02-01 10:48:28 +01:00
|
|
|
torrent_status& st = *i;
|
2011-07-04 07:33:06 +02:00
|
|
|
if (!st.handle.is_valid())
|
|
|
|
{
|
|
|
|
printf(" skipping, invalid handle\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!st.has_metadata)
|
|
|
|
{
|
2013-03-04 04:24:53 +01:00
|
|
|
printf(" skipping %s, no metadata\n", st.name.c_str());
|
2011-07-04 07:33:06 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!st.need_save_resume)
|
|
|
|
{
|
2013-03-04 04:24:53 +01:00
|
|
|
printf(" skipping %s, resume file up-to-date\n", st.name.c_str());
|
2011-07-04 07:33:06 +02:00
|
|
|
continue;
|
|
|
|
}
|
2010-01-31 22:30:43 +01:00
|
|
|
|
|
|
|
// save_resume_data will generate an alert when it's done
|
2011-02-01 10:48:28 +01:00
|
|
|
st.handle.save_resume_data();
|
2011-07-04 07:33:06 +02:00
|
|
|
++num_outstanding_resume_data;
|
|
|
|
printf("\r%d ", num_outstanding_resume_data);
|
2010-01-31 22:30:43 +01:00
|
|
|
}
|
2011-07-04 07:33:06 +02:00
|
|
|
printf("\nwaiting for resume data [%d]\n", num_outstanding_resume_data);
|
2010-01-31 22:30:43 +01:00
|
|
|
|
2011-07-04 07:33:06 +02:00
|
|
|
while (num_outstanding_resume_data > 0)
|
2010-01-31 22:30:43 +01:00
|
|
|
{
|
2011-01-24 01:11:13 +01:00
|
|
|
alert const* a = ses.wait_for_alert(seconds(10));
|
|
|
|
if (a == 0) continue;
|
2010-01-31 22:30:43 +01:00
|
|
|
|
2015-04-03 22:15:48 +02:00
|
|
|
std::vector<alert*> alerts;
|
2011-03-14 03:59:46 +01:00
|
|
|
ses.pop_alerts(&alerts);
|
2014-05-10 22:20:57 +02:00
|
|
|
std::string now = timestamp();
|
2015-04-03 22:15:48 +02:00
|
|
|
for (std::vector<alert*>::iterator i = alerts.begin()
|
2011-03-14 03:59:46 +01:00
|
|
|
, end(alerts.end()); i != end; ++i)
|
2011-01-24 01:11:13 +01:00
|
|
|
{
|
2015-04-03 22:15:48 +02:00
|
|
|
torrent_paused_alert* tp = alert_cast<torrent_paused_alert>(*i);
|
2011-03-14 03:59:46 +01:00
|
|
|
if (tp)
|
|
|
|
{
|
|
|
|
++num_paused;
|
|
|
|
printf("\rleft: %d failed: %d pause: %d "
|
2011-07-04 07:33:06 +02:00
|
|
|
, num_outstanding_resume_data, num_failed, num_paused);
|
2011-03-14 03:59:46 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (alert_cast<save_resume_data_failed_alert>(*i))
|
|
|
|
{
|
|
|
|
++num_failed;
|
2011-07-04 07:33:06 +02:00
|
|
|
--num_outstanding_resume_data;
|
2011-03-14 03:59:46 +01:00
|
|
|
printf("\rleft: %d failed: %d pause: %d "
|
2011-07-04 07:33:06 +02:00
|
|
|
, num_outstanding_resume_data, num_failed, num_paused);
|
2011-03-14 03:59:46 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-04-03 22:15:48 +02:00
|
|
|
save_resume_data_alert* rd = alert_cast<save_resume_data_alert>(*i);
|
2011-03-14 03:59:46 +01:00
|
|
|
if (!rd) continue;
|
2011-07-04 07:33:06 +02:00
|
|
|
--num_outstanding_resume_data;
|
2011-01-24 01:11:13 +01:00
|
|
|
printf("\rleft: %d failed: %d pause: %d "
|
2011-07-04 07:33:06 +02:00
|
|
|
, num_outstanding_resume_data, num_failed, num_paused);
|
2010-01-31 22:30:43 +01:00
|
|
|
|
2011-03-14 03:59:46 +01:00
|
|
|
if (!rd->resume_data) continue;
|
2010-01-31 22:30:43 +01:00
|
|
|
|
2011-03-14 03:59:46 +01:00
|
|
|
torrent_handle h = rd->handle;
|
2013-03-04 04:24:53 +01:00
|
|
|
torrent_status st = h.status(torrent_handle::query_save_path);
|
2011-03-14 03:59:46 +01:00
|
|
|
std::vector<char> out;
|
|
|
|
bencode(std::back_inserter(out), *rd->resume_data);
|
2014-11-17 04:41:01 +01:00
|
|
|
save_file(path_append(st.save_path, path_append(".resume"
|
2015-01-02 12:11:09 +01:00
|
|
|
, leaf_path(hash_to_filename[st.info_hash]) + ".resume")), out);
|
2011-03-14 03:59:46 +01:00
|
|
|
}
|
2010-01-31 22:30:43 +01:00
|
|
|
}
|
2011-03-14 03:59:46 +01:00
|
|
|
|
2011-02-20 23:49:55 +01:00
|
|
|
if (g_log_file) fclose(g_log_file);
|
2011-01-24 01:11:13 +01:00
|
|
|
printf("\nsaving session state\n");
|
2010-03-04 17:42:39 +01:00
|
|
|
{
|
|
|
|
entry session_state;
|
|
|
|
ses.save_state(session_state);
|
2009-05-13 03:02:06 +02:00
|
|
|
|
|
|
|
std::vector<char> out;
|
|
|
|
bencode(std::back_inserter(out), session_state);
|
|
|
|
save_file(".ses_state", out);
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
2009-02-26 08:09:56 +01:00
|
|
|
|
2009-05-13 03:02:06 +02:00
|
|
|
printf("closing session");
|
2006-08-01 17:27:08 +02:00
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2005-10-16 11:15:46 +02:00
|
|
|
|