2010-05-20 23:07:21 +00: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.
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2014-04-14 14:02:44 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								import ./make-test.nix ({ pkgs, ... }:
							 | 
						
					
						
							
								
									
										
										
										
											2010-05-20 14:56:04 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								let
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  # Some random file to serve.
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  file = pkgs.nixUnstable.src;
							 | 
						
					
						
							
								
									
										
										
										
											2010-05-20 23:07:21 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2010-05-21 14:31:05 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  miniupnpdConf = nodes: pkgs.writeText "miniupnpd.conf"
							 | 
						
					
						
							
								
									
										
										
										
											2010-05-20 23:07:21 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    ''
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      ext_ifname=eth1
							 | 
						
					
						
							
								
									
										
										
										
											2014-09-01 04:39:45 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      listening_ip=${(pkgs.lib.head nodes.router.config.networking.interfaces.eth2.ip4).address}/24
							 | 
						
					
						
							
								
									
										
										
										
											2010-05-20 23:07:21 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      allow 1024-65535 192.168.2.0/24 1024-65535
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    '';
							 | 
						
					
						
							
								
									
										
										
										
											2011-09-14 18:20:50 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2010-05-20 14:56:04 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								in
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								{
							 | 
						
					
						
							
								
									
										
										
										
											2014-06-28 16:04:49 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  name = "bittorrent";
							 | 
						
					
						
							
								
									
										
										
										
											2015-07-12 12:09:40 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  meta = with pkgs.stdenv.lib.maintainers; {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    maintainers = [ iElectric eelco chaoflow rob wkennington ];
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  };
							 | 
						
					
						
							
								
									
										
										
										
											2010-05-20 14:56:04 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  nodes =
							 | 
						
					
						
							
								
									
										
										
										
											2011-09-14 18:20:50 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    { tracker =
							 | 
						
					
						
							
								
									
										
										
										
											2010-05-20 14:56:04 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        { config, pkgs, ... }:
							 | 
						
					
						
							
								
									
										
										
										
											2014-10-07 17:21:49 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        { environment.systemPackages = [ pkgs.transmission pkgs.opentracker ];
							 | 
						
					
						
							
								
									
										
										
										
											2010-05-20 14:56:04 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								          # We need Apache on the tracker to serve the torrents.
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								          services.httpd.enable = true;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								          services.httpd.adminAddr = "foo@example.org";
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								          services.httpd.documentRoot = "/tmp";
							 | 
						
					
						
							
								
									
										
										
										
											2014-04-11 17:15:56 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								          networking.firewall.enable = false; # FIXME: figure out what ports we actually need
							 | 
						
					
						
							
								
									
										
										
										
											2010-05-20 14:56:04 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        };
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2011-09-14 18:20:50 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      router =
							 | 
						
					
						
							
								
									
										
										
										
											2010-05-20 23:07:21 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        { config, pkgs, ... }:
							 | 
						
					
						
							
								
									
										
										
										
											2011-03-21 13:14:50 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        { environment.systemPackages = [ pkgs.miniupnpd ];
							 | 
						
					
						
							
								
									
										
										
										
											2010-05-20 23:07:21 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								          virtualisation.vlans = [ 1 2 ];
							 | 
						
					
						
							
								
									
										
										
										
											2011-03-21 13:14:50 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								          networking.nat.enable = true;
							 | 
						
					
						
							
								
									
										
										
										
											2014-04-15 14:35:34 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								          networking.nat.internalInterfaces = [ "eth2" ];
							 | 
						
					
						
							
								
									
										
										
										
											2011-03-21 13:14:50 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								          networking.nat.externalInterface = "eth1";
							 | 
						
					
						
							
								
									
										
										
										
											2014-04-15 14:35:34 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								          networking.firewall.enable = false;
							 | 
						
					
						
							
								
									
										
										
										
											2010-05-20 23:07:21 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        };
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2011-09-14 18:20:50 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      client1 =
							 | 
						
					
						
							
								
									
										
										
										
											2010-05-21 14:12:03 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        { config, pkgs, nodes, ... }:
							 | 
						
					
						
							
								
									
										
										
										
											2010-05-20 23:07:21 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        { environment.systemPackages = [ pkgs.transmission ];
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								          virtualisation.vlans = [ 2 ];
							 | 
						
					
						
							
								
									
										
										
										
											2010-05-21 14:12:03 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								          networking.defaultGateway =
							 | 
						
					
						
							
								
									
										
										
										
											2014-09-01 04:39:45 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            (pkgs.lib.head nodes.router.config.networking.interfaces.eth2.ip4).address;
							 | 
						
					
						
							
								
									
										
										
										
											2014-04-11 17:15:56 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								          networking.firewall.enable = false;
							 | 
						
					
						
							
								
									
										
										
										
											2010-05-20 23:07:21 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        };
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2011-09-14 18:20:50 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      client2 =
							 | 
						
					
						
							
								
									
										
										
										
											2010-05-20 14:56:04 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        { config, pkgs, ... }:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        { environment.systemPackages = [ pkgs.transmission ];
							 | 
						
					
						
							
								
									
										
										
										
											2014-04-11 17:15:56 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								          networking.firewall.enable = false;
							 | 
						
					
						
							
								
									
										
										
										
											2010-05-20 14:56:04 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        };
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    };
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  testScript =
							 | 
						
					
						
							
								
									
										
										
										
											2010-05-21 14:31:05 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    { nodes, ... }:
							 | 
						
					
						
							
								
									
										
										
										
											2010-05-20 14:56:04 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    ''
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      startAll;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2010-05-20 23:07:21 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      # Enable NAT on the router and start miniupnpd.
							 | 
						
					
						
							
								
									
										
										
										
											2012-10-24 18:22:53 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      $router->waitForUnit("nat");
							 | 
						
					
						
							
								
									
										
										
										
											2011-03-21 13:14:50 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      $router->succeed(
							 | 
						
					
						
							
								
									
										
										
										
											2014-04-11 17:15:56 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								          "iptables -w -t nat -N MINIUPNPD",
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								          "iptables -w -t nat -A PREROUTING -i eth1 -j MINIUPNPD",
							 | 
						
					
						
							
								
									
										
										
										
											2010-05-20 23:07:21 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								          "echo 1 > /proc/sys/net/ipv4/ip_forward",
							 | 
						
					
						
							
								
									
										
										
										
											2010-05-21 14:31:05 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								          "miniupnpd -f ${miniupnpdConf nodes}"
							 | 
						
					
						
							
								
									
										
										
										
											2010-05-20 23:07:21 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      );
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2010-05-20 14:56:04 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      # Create the torrent.
							 | 
						
					
						
							
								
									
										
										
										
											2011-03-21 13:14:50 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      $tracker->succeed("mkdir /tmp/data");
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      $tracker->succeed("cp ${file} /tmp/data/test.tar.bz2");
							 | 
						
					
						
							
								
									
										
										
										
											2014-12-09 12:08:13 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      $tracker->succeed("transmission-create /tmp/data/test.tar.bz2 -p -t http://${(pkgs.lib.head nodes.tracker.config.networking.interfaces.eth1.ip4).address}:6969/announce -o /tmp/test.torrent");
							 | 
						
					
						
							
								
									
										
										
										
											2011-03-21 13:14:50 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      $tracker->succeed("chmod 644 /tmp/test.torrent");
							 | 
						
					
						
							
								
									
										
										
										
											2010-05-20 14:56:04 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      # Start the tracker.  !!! use a less crappy tracker
							 | 
						
					
						
							
								
									
										
										
										
											2012-10-24 18:22:53 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      $tracker->waitForUnit("network.target");
							 | 
						
					
						
							
								
									
										
										
										
											2014-10-07 17:21:49 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      $tracker->succeed("opentracker -p 6969 >&2 &");
							 | 
						
					
						
							
								
									
										
										
										
											2010-05-20 14:56:04 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      $tracker->waitForOpenPort(6969);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      # Start the initial seeder.
							 | 
						
					
						
							
								
									
										
										
										
											2011-03-21 13:14:50 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      my $pid = $tracker->succeed("transmission-cli /tmp/test.torrent -M -w /tmp/data >&2 & echo \$!");
							 | 
						
					
						
							
								
									
										
										
										
											2010-05-20 14:56:04 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2010-05-20 23:07:21 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      # Now we should be able to download from the client behind the NAT.
							 | 
						
					
						
							
								
									
										
										
										
											2012-10-24 18:22:53 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      $tracker->waitForUnit("httpd");
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      $client1->waitForUnit("network.target");
							 | 
						
					
						
							
								
									
										
										
										
											2011-03-21 13:14:50 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      $client1->succeed("transmission-cli http://tracker/test.torrent -w /tmp >&2 &");
							 | 
						
					
						
							
								
									
										
										
										
											2010-05-20 23:07:21 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      $client1->waitForFile("/tmp/test.tar.bz2");
							 | 
						
					
						
							
								
									
										
										
										
											2011-03-21 13:14:50 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      $client1->succeed("cmp /tmp/test.tar.bz2 ${file}");
							 | 
						
					
						
							
								
									
										
										
										
											2010-05-20 23:07:21 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      # Bring down the initial seeder.
							 | 
						
					
						
							
								
									
										
										
										
											2011-03-21 13:14:50 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      $tracker->succeed("kill -9 $pid");
							 | 
						
					
						
							
								
									
										
										
										
											2010-05-20 23:07:21 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      # Now download from the second client.  This can only succeed if
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      # the first client created a NAT hole in the router.
							 | 
						
					
						
							
								
									
										
										
										
											2012-10-24 18:22:53 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      $client2->waitForUnit("network.target");
							 | 
						
					
						
							
								
									
										
										
										
											2011-03-21 13:14:50 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      $client2->succeed("transmission-cli http://tracker/test.torrent -M -w /tmp >&2 &");
							 | 
						
					
						
							
								
									
										
										
										
											2010-05-20 23:07:21 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      $client2->waitForFile("/tmp/test.tar.bz2");
							 | 
						
					
						
							
								
									
										
										
										
											2011-03-21 13:14:50 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      $client2->succeed("cmp /tmp/test.tar.bz2 ${file}");
							 | 
						
					
						
							
								
									
										
										
										
											2010-05-20 14:56:04 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    '';
							 | 
						
					
						
							
								
									
										
										
										
											2011-09-14 18:20:50 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2014-04-14 14:02:44 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								})
							 |