1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
|
/*
Copyright (c) 2016, 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 "libtorrent/session.hpp"
#include "libtorrent/torrent_handle.hpp"
#include "libtorrent/settings_pack.hpp"
#include "libtorrent/alert_types.hpp"
#include "libtorrent/deadline_timer.hpp"
#include "simulator/http_server.hpp"
#include "settings.hpp"
#include "create_torrent.hpp"
#include "setup_transfer.hpp" // for addr()
#include "simulator/simulator.hpp"
#include "setup_swarm.hpp"
#include "utils.hpp"
#include "simulator/socks_server.hpp"
#include "simulator/utils.hpp"
#include "fake_peer.hpp"
#include <iostream>
using namespace sim;
using namespace lt;
using socks_flags_t = lt::flags::bitfield_flag<std::uint64_t, struct socks_test_type_tag>;
constexpr socks_flags_t proxy_hostname = 0_bit;
struct sim_config : sim::default_config
{
chrono::high_resolution_clock::duration hostname_lookup(
asio::ip::address const& requestor
, std::string hostname
, std::vector<asio::ip::address>& result
, boost::system::error_code& ec) override
{
if (hostname == "tracker.hostname.org")
{
result.push_back(make_address_v4("2.2.2.2"));
return duration_cast<chrono::high_resolution_clock::duration>(chrono::milliseconds(100));
}
return default_config::hostname_lookup(requestor, hostname, result, ec);
}
};
// this is the general template for these tests. create the session with custom
// settings (Settings), set up the test, by adding torrents with certain
// arguments (Setup), run the test and verify the end state (Test)
template <typename Setup, typename HandleAlerts, typename Test>
void run_test(Setup const& setup
, HandleAlerts const& on_alert
, Test const& test
, std::uint32_t const flags = 0)
{
// setup the simulation
sim_config network_cfg;
sim::simulation sim{network_cfg};
std::unique_ptr<sim::asio::io_context> ios = make_io_context(sim, 0);
lt::session_proxy zombie;
sim::asio::io_context proxy_ios{sim, addr("50.50.50.50") };
sim::socks_server socks4(proxy_ios, 4444, 4, flags);
sim::socks_server socks5(proxy_ios, 5555, 5, flags);
lt::settings_pack pack = settings();
// create session
std::shared_ptr<lt::session> ses = std::make_shared<lt::session>(pack, *ios);
// set up test, like adding torrents (customization point)
setup(*ses);
// only monitor alerts for session 0 (the downloader)
print_alerts(*ses, [=](lt::session& ses, lt::alert const* a) {
on_alert(ses, a);
});
lt::add_torrent_params params = ::create_torrent(1);
params.flags &= ~lt::torrent_flags::auto_managed;
params.flags &= ~lt::torrent_flags::paused;
params.save_path = save_path(0);
ses->async_add_torrent(params);
// set up a timer to fire later, to verify everything we expected to happen
// happened
sim::timer t(sim, lt::seconds(100), [&](boost::system::error_code const&)
{
std::printf("shutting down\n");
// shut down
zombie = ses->abort();
ses.reset();
});
test(sim, *ses, params.ti);
}
TORRENT_TEST(socks5_tcp_announce)
{
using namespace lt;
int tracker_port = -1;
int alert_port = -1;
run_test(
[](lt::session& ses)
{
set_proxy(ses, settings_pack::socks5);
lt::add_torrent_params params;
params.info_hashes.v1 = sha1_hash("abababababababababab");
params.trackers.push_back("http://2.2.2.2:8080/announce");
params.save_path = ".";
ses.async_add_torrent(params);
},
[&alert_port](lt::session&, lt::alert const* alert) {
if (auto* a = lt::alert_cast<lt::listen_succeeded_alert>(alert))
{
if (a->socket_type == socket_type_t::utp)
{
alert_port = a->port;
}
}
},
[&tracker_port](sim::simulation& sim, lt::session&
, std::shared_ptr<lt::torrent_info> ti)
{
sim::asio::io_context web_server(sim, make_address_v4("2.2.2.2"));
// listen on port 8080
sim::http_server http(web_server, 8080);
http.register_handler("/announce"
, [&tracker_port](std::string method, std::string req
, std::map<std::string, std::string>&)
{
if (req.find("&event=started") != std::string::npos)
{
std::string::size_type port_pos = req.find("&port=");
TEST_CHECK(port_pos != std::string::npos);
if (port_pos != std::string::npos)
{
tracker_port = atoi(req.c_str() + port_pos + 6);
}
}
return sim::send_response(200, "OK", 27) + "d8:intervali1800e5:peers0:e";
});
sim.run();
}
);
TEST_EQUAL(tracker_port, 1);
TEST_CHECK(alert_port != -1);
}
void test_udp_tracker(std::uint32_t const flags, socks_flags_t const sflags)
{
using namespace lt;
bool tracker_alert = false;
bool connected = false;
bool announced = false;
run_test(
[sflags](lt::session& ses)
{
set_proxy(ses, settings_pack::socks5);
// The socks server in libsimulator does not support forwarding UDP
// packets to hostnames (just IPv4 destinations)
settings_pack p;
p.set_bool(settings_pack::proxy_hostnames, bool(sflags & proxy_hostname));
ses.apply_settings(p);
lt::add_torrent_params params;
params.info_hashes.v1 = sha1_hash("abababababababababab");
if (sflags & proxy_hostname)
params.trackers.push_back("udp://tracker.hostname.org:8080/announce");
else
params.trackers.push_back("udp://2.2.2.2:8080/announce");
params.save_path = ".";
ses.async_add_torrent(params);
},
[&tracker_alert](lt::session&, lt::alert const* alert) {
if (lt::alert_cast<lt::tracker_announce_alert>(alert))
tracker_alert = true;
},
[&](sim::simulation& sim, lt::session&
, std::shared_ptr<lt::torrent_info> ti)
{
// listen on port 8080
udp_server tracker(sim, "2.2.2.2", 8080,
[&](char const* msg, int size)
{
using namespace lt::aux;
std::vector<char> ret;
TEST_CHECK(size >= 16);
if (size < 16) return ret;
std::uint64_t connection_id = read_uint64(msg);
std::uint32_t action = read_uint32(msg);
std::uint32_t transaction_id = read_uint32(msg);
std::uint64_t const conn_id = 0xfeedface1337ull;
if (action == 0)
{
std::printf("udp connect\n");
// udp tracker connect
TEST_CHECK(connection_id == 0x41727101980ull);
auto inserter = std::back_inserter(ret);
write_uint32(0, inserter); // connect
write_uint32(transaction_id, inserter);
write_uint64(conn_id, inserter);
connected = true;
}
else if (action == 1)
{
std::printf("udp announce\n");
// udp tracker announce
TEST_EQUAL(connection_id, conn_id);
auto inserter = std::back_inserter(ret);
write_uint32(1, inserter); // announce
write_uint32(transaction_id, inserter);
write_uint32(1800, inserter);
write_uint32(0, inserter); // leechers
write_uint32(0, inserter); // seeders
announced = true;
}
else
{
std::printf("unsupported udp tracker action: %u\n", action);
}
return ret;
});
sim.run();
}
, flags
);
TEST_CHECK(tracker_alert);
TEST_CHECK(connected);
TEST_CHECK(announced);
}
TORRENT_TEST(udp_tracker)
{
test_udp_tracker(0, {});
}
TORRENT_TEST(udp_tracker_empty_domainname)
{
test_udp_tracker(socks_flag::udp_associate_respond_empty_hostname, {});
}
TORRENT_TEST(udp_tracker_hostname)
{
test_udp_tracker(0, proxy_hostname);
}
TORRENT_TEST(socks5_udp_retry)
{
// this test is asserting that when a UDP associate command fails, we have a
// 5 second delay before we try again. There is no need to actually add a
// torrent for this test, just to open the udp socket with a socks5 proxy
using namespace libtorrent;
// setup the simulation
sim::default_config network_cfg;
sim::simulation sim{network_cfg};
std::unique_ptr<sim::asio::io_context> ios = make_io_context(sim, 0);
lt::session_proxy zombie;
sim::asio::io_context proxy_ios{sim, addr("50.50.50.50") };
// close UDP associate connections prematurely
sim::socks_server socks5(proxy_ios, 5555, 5, socks_flag::disconnect_udp_associate);
lt::settings_pack pack = settings();
// create session
std::shared_ptr<lt::session> ses = std::make_shared<lt::session>(pack, *ios);
print_alerts(*ses);
set_proxy(*ses, settings_pack::socks5);
sim::timer t(sim, lt::seconds(60), [&](boost::system::error_code const&)
{
fprintf(stderr, "shutting down\n");
// shut down
zombie = ses->abort();
ses.reset();
});
sim.run();
// number of UDP ASSOCIATE commands invoked on the socks proxy
// We run for 60 seconds. The sokcks5 retry interval is expected to be 5
// seconds, meaning there should have been 12 connection attempts
TEST_EQUAL(socks5.cmd_counts()[2], 12);
}
|