我目前正在尝试将libtorrent设置为使用代理。但是,当我将配置添加到设置包时,我无法从中下载任何对等项。我已验证用户名和密码对我的socks5代理正确。我在下面粘贴了一个最小的代码。我正在使用1.2.0版本的libtorrent-rasterbar。
#include <iostream>
#include <thread>
#include <chrono>
#include <string_view>
#include <libtorrent/session.hpp>
#include <libtorrent/add_torrent_params.hpp>
#include <libtorrent/torrent_handle.hpp>
#include <libtorrent/alert_types.hpp>
#include <libtorrent/magnet_uri.hpp>
static std::string PROXY_HOST = "proxy-nl.privateinternetaccess.com";
static constexpr unsigned short PROXY_PORT = 1080;
static std::string PROXY_UNAME = "omitted";
static std::string PROXY_PASSWD = "omitted";
static constexpr std::string_view IP_TORRENT_LINK = "magnet:?xt=urn:btih:c466035da5de7b04df065831e87ac368456e7fbe&dn=kali-linux-light-2019-1a-armhf-img-xz";
int main(int argc, char const* argv[]) try
{
lt::settings_pack p;
p.set_int(lt::settings_pack::alert_mask
, lt::alert::status_notification
| lt::alert::error_notification
| lt::alert::storage_notification
| lt::alert::dht_notification
);
p.set_str(lt::settings_pack::proxy_hostname, PROXY_HOST);
p.set_int(lt::settings_pack::proxy_port, PROXY_PORT);
p.set_str(lt::settings_pack::proxy_username, PROXY_UNAME);
p.set_str(lt::settings_pack::proxy_password, PROXY_PASSWD);
p.set_int(lt::settings_pack::proxy_type, lt::settings_pack::socks5_pw);
// p.set_bool(lt::settings_pack::proxy_hostnames, true);
// p.set_bool(lt::settings_pack::proxy_peer_connections, true);
// p.set_bool(lt::settings_pack::proxy_tracker_connections, true);
lt::session ses(p);
ses.add_dht_node(std::make_pair("router.utorrent.com", 6881));
ses.add_dht_node(std::make_pair("router.bittorrent.com", 6881));
ses.add_dht_node(std::make_pair("dht.transmissionbt.com", 6881));
ses.add_dht_node(std::make_pair("dht.aelitis.com", 6881));
lt::add_torrent_params atp = lt::parse_magnet_uri(IP_TORRENT_LINK.data());
atp.save_path = "."; // save in current dir
lt::torrent_handle h = ses.add_torrent(std::move(atp));
for (;;) {
std::vector<lt::alert*> alerts;
ses.pop_alerts(&alerts);
for (lt::alert const* a : alerts) {
std::cout << a->message() << std::endl;
// if we receive the finished alert or an error, we're done
if (lt::alert_cast<lt::torrent_finished_alert>(a)) {
goto done;
}
if (lt::alert_cast<lt::torrent_error_alert>(a)) {
goto done;
}
}
std::this_thread::sleep_for(std::chrono::milliseconds(200));
}
done:
std::cout << "done, shutting down" << std::endl;
}
catch (std::exception& e)
{
std::cerr << "Error: " << e.what() << std::endl;
}
运行上面的代码时,我的输出是:
successfully listening on [UDP] 0.0.0.0:6881
successfully listening on [TCP] [***IP AND INTERFACE OMITTED***]:6881
successfully listening on [UDP] [***IP AND INTERFACE OMITTED***]:6881
added torrent: kali-linux-light-2019-1a-armhf-img-xz
kali-linux-light-2019-1a-armhf-img-xz added
kali-linux-light-2019-1a-armhf-img-xz: state changed to: dl metadata
DHT bootstrap complete
kali-linux-light-2019-1a-armhf-img-xz resumed
outgoing dht get_peers : c466035da5de7b04df065831e87ac368456e7fbe -> 192.3.31.116:25401
outgoing dht get_peers : c466035da5de7b04df065831e87ac368456e7fbe -> 192.3.31.116:25401
然后就停在那里。但是,如果删除socks5代理,我会得到更多的同龄人并能够下载torrent。
successfully listening on [TCP] 0.0.0.0:6881
successfully listening on [UDP] 0.0.0.0:6881
successfully listening on [TCP] [***IP AND INTERFACE OMITTED***]:6881
successfully listening on [UDP] [***IP AND INTERFACE OMITTED***]:6881
added torrent: kali-linux-light-2019-1a-armhf-img-xz
kali-linux-light-2019-1a-armhf-img-xz added
kali-linux-light-2019-1a-armhf-img-xz: state changed to: dl metadata
external IP received: ***IP OMITTED***
kali-linux-light-2019-1a-armhf-img-xz resumed
outgoing dht get_peers : c466035da5de7b04df065831e87ac368456e7fbe -> 82.221.103.244:6881
outgoing dht get_peers : c466035da5de7b04df065831e87ac368456e7fbe -> 93.100.185.121:38165
outgoing dht get_peers : c466035da5de7b04df065831e87ac368456e7fbe -> 67.215.246.10:6881
outgoing dht get_peers : c466035da5de7b04df065831e87ac368456e7fbe -> 87.98.162.88:6881
outgoing dht get_peers : c466035da5de7b04df065831e87ac368456e7fbe -> 92.140.209.218:54986
outgoing dht get_peers : c466035da5de7b04df065831e87ac368456e7fbe -> 92.12.147.60:44452
outgoing dht get_peers : c466035da5de7b04df065831e87ac368456e7fbe -> 120.29.96.164:56742
outgoing dht get_peers : c466035da5de7b04df065831e87ac368456e7fbe -> 103.75.166.13:25786
outgoing dht get_peers : c466035da5de7b04df065831e87ac368456e7fbe -> 178.21.48.188:38241
outgoing dht get_peers : c466035da5de7b04df065831e87ac368456e7fbe -> 126.224.91.32:16001
*** MORE DHT PEERS FOLLOW ***
kali-linux-light-2019-1a-armhf-img-xz metadata successfully received
kali-linux-light-2019-1a-armhf-img-xz: state changed to: checking (r)
kali-linux-light-2019-1a-armhf-img-xz: state changed to: downloading
kali-linux-light-2019-1a-armhf-img-xz checked
任何帮助将不胜感激。