2010-05-21 03:07:21 +04:00
|
|
|
# This test runs a Bittorrent tracker on one machine, and verifies
|
|
|
|
# that two client machines can download the torrent using
|
|
|
|
# `transmission'. The first client (behind a NAT router) downloads
|
|
|
|
# from the initial seeder running on the tracker. Then we kill the
|
|
|
|
# initial seeder. The second client downloads from the first client,
|
|
|
|
# which only works if the first client successfully uses the UPnP-IGD
|
|
|
|
# protocol to poke a hole in the NAT.
|
|
|
|
|
2010-05-20 18:56:04 +04:00
|
|
|
{ pkgs, ... }:
|
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
# Some random file to serve.
|
|
|
|
file = pkgs.nixUnstable.src;
|
2010-05-21 03:07:21 +04:00
|
|
|
|
2010-05-21 18:31:05 +04:00
|
|
|
miniupnpdConf = nodes: pkgs.writeText "miniupnpd.conf"
|
2010-05-21 03:07:21 +04:00
|
|
|
''
|
|
|
|
ext_ifname=eth1
|
2010-05-21 18:31:05 +04:00
|
|
|
listening_ip=${nodes.router.config.networking.ifaces.eth2.ipAddress}/24
|
2010-05-21 03:07:21 +04:00
|
|
|
allow 1024-65535 192.168.2.0/24 1024-65535
|
|
|
|
'';
|
2010-05-20 18:56:04 +04:00
|
|
|
|
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
nodes =
|
|
|
|
{ tracker =
|
|
|
|
{ config, pkgs, ... }:
|
|
|
|
{ environment.systemPackages = [ pkgs.transmission pkgs.bittorrent ];
|
|
|
|
|
|
|
|
# We need Apache on the tracker to serve the torrents.
|
|
|
|
services.httpd.enable = true;
|
|
|
|
services.httpd.adminAddr = "foo@example.org";
|
|
|
|
services.httpd.documentRoot = "/tmp";
|
|
|
|
};
|
|
|
|
|
2010-05-21 03:07:21 +04:00
|
|
|
router =
|
|
|
|
{ config, pkgs, ... }:
|
|
|
|
{ environment.systemPackages = [ pkgs.iptables pkgs.miniupnpd ];
|
|
|
|
virtualisation.vlans = [ 1 2 ];
|
|
|
|
};
|
|
|
|
|
|
|
|
client1 =
|
2010-05-21 18:12:03 +04:00
|
|
|
{ config, pkgs, nodes, ... }:
|
2010-05-21 03:07:21 +04:00
|
|
|
{ environment.systemPackages = [ pkgs.transmission ];
|
|
|
|
virtualisation.vlans = [ 2 ];
|
2010-05-21 18:12:03 +04:00
|
|
|
networking.defaultGateway =
|
|
|
|
nodes.router.config.networking.ifaces.eth2.ipAddress;
|
2010-05-21 03:07:21 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
client2 =
|
2010-05-20 18:56:04 +04:00
|
|
|
{ config, pkgs, ... }:
|
|
|
|
{ environment.systemPackages = [ pkgs.transmission ];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
testScript =
|
2010-05-21 18:31:05 +04:00
|
|
|
{ nodes, ... }:
|
2010-05-20 18:56:04 +04:00
|
|
|
''
|
|
|
|
startAll;
|
|
|
|
|
2010-05-21 03:07:21 +04:00
|
|
|
# Enable NAT on the router and start miniupnpd.
|
|
|
|
$router->mustSucceed(
|
|
|
|
"iptables -t nat -F",
|
|
|
|
"iptables -t nat -A POSTROUTING -s 192.168.2.0/24 -d 192.168.2.0/24 -j ACCEPT",
|
2010-05-21 18:31:05 +04:00
|
|
|
"iptables -t nat -A POSTROUTING -s 192.168.2.0/24 -j SNAT "
|
|
|
|
. "--to-source ${nodes.router.config.networking.ifaces.eth1.ipAddress}",
|
2010-05-21 03:07:21 +04:00
|
|
|
"iptables -t nat -N MINIUPNPD",
|
|
|
|
"iptables -t nat -A PREROUTING -i eth1 -j MINIUPNPD",
|
|
|
|
"echo 1 > /proc/sys/net/ipv4/ip_forward",
|
2010-05-21 18:31:05 +04:00
|
|
|
"miniupnpd -f ${miniupnpdConf nodes}"
|
2010-05-21 03:07:21 +04:00
|
|
|
);
|
|
|
|
|
2010-05-20 18:56:04 +04:00
|
|
|
# Create the torrent.
|
|
|
|
$tracker->mustSucceed("mkdir /tmp/data");
|
|
|
|
$tracker->mustSucceed("cp ${file} /tmp/data/test.tar.bz2");
|
2011-01-05 14:52:45 +03:00
|
|
|
$tracker->mustSucceed("transmission-create /tmp/data/test.tar.bz2 -t http://tracker:6969/announce -o /tmp/test.torrent");
|
2010-05-20 18:56:04 +04:00
|
|
|
$tracker->mustSucceed("chmod 644 /tmp/test.torrent");
|
|
|
|
|
|
|
|
# Start the tracker. !!! use a less crappy tracker
|
|
|
|
$tracker->mustSucceed("bittorrent-tracker --port 6969 --dfile /tmp/dstate >&2 &");
|
|
|
|
$tracker->waitForOpenPort(6969);
|
|
|
|
|
|
|
|
# Start the initial seeder.
|
2011-01-05 14:52:45 +03:00
|
|
|
my $pid = $tracker->mustSucceed("transmission-cli /tmp/test.torrent -M -w /tmp/data >&2 & echo \$!");
|
2010-05-20 18:56:04 +04:00
|
|
|
|
2010-05-21 03:07:21 +04:00
|
|
|
# Now we should be able to download from the client behind the NAT.
|
2010-05-20 18:56:04 +04:00
|
|
|
$tracker->waitForJob("httpd");
|
2011-01-05 14:52:45 +03:00
|
|
|
$client1->mustSucceed("transmission-cli http://tracker/test.torrent -w /tmp >&2 &");
|
2010-05-21 03:07:21 +04:00
|
|
|
$client1->waitForFile("/tmp/test.tar.bz2");
|
|
|
|
$client1->mustSucceed("cmp /tmp/test.tar.bz2 ${file}");
|
|
|
|
|
|
|
|
# Bring down the initial seeder.
|
|
|
|
$tracker->mustSucceed("kill -9 $pid");
|
|
|
|
|
|
|
|
# Now download from the second client. This can only succeed if
|
|
|
|
# the first client created a NAT hole in the router.
|
2011-01-05 14:52:45 +03:00
|
|
|
$client2->mustSucceed("transmission-cli http://tracker/test.torrent -M -w /tmp >&2 &");
|
2010-05-21 03:07:21 +04:00
|
|
|
$client2->waitForFile("/tmp/test.tar.bz2");
|
|
|
|
$client2->mustSucceed("cmp /tmp/test.tar.bz2 ${file}");
|
2010-05-20 18:56:04 +04:00
|
|
|
'';
|
|
|
|
|
|
|
|
}
|