Build a P2P Black Box

legitimate

Arguably, the best technology today for distributing a focused archive of content to a widespread audience is BitTorrent, often simply called "torrent." Unlike many other P2P technologies, torrents do not rely on unstable or seedy distribution servers. Using BitTorrent, a company can run its own clean, members-only distribution network, cooperatively spreading data with great speed.

In this TechBuilder Recipe, I'll show you how to construct a dedicated Linux server to act as a sort of P2P black box. The server will act as a self-contained torrent-serving machine.

Torrent Lingo

You'll understand this process better--and be better able to explain it to clients--if you have a basic command of BitTorrent vocabulary. To get you started, here are the basics:

id
unit-1659132512259
type
Sponsored post

For additional reading on torrent technology, visit the official BitTorrent FAQ.

Ingredients

Here's what you'll need to build this system:

22 Steps To Building a P2P Black Box

Now that you have your components assembled, let's start building the server. Here are the steps:

1. The machine can be assembled either in your own shop or on the customer premises. For the latter, the customer must provide you with basic network information, such as whether the unit will run on DHCP or need a dedicated IP address and DNS data. Once you're ready, download Fedora Core 4 in either the DVD-R archive or the series of CD-R .iso images. Burn the image to disc.

2. Boot the server that will become the P2P black box from the newly burned Fedora Core 4 disc. You may skip the media test it offers, after which Fedora will load its graphical installer. Select the appropriate options for your language, keyboard, and time zone. When prompted to select the profile of machine to create, select the Server installation type.

3. At the disc-partitioning step, select Automatically Partition. Approve the removal of all partitions on the system, if any exist. Then select Next.

4. Fedora's installer will probe all known Ethernet devices. If the server has more than one device, select the primary network adapter by hardware type. If the client network environment uses DHCP, allow the settings to be discovered "automatically via DHCP." Otherwise, switch to manual, then define the IP address, gateway, and primary DNS.

5. Although Fedora will offer to turn on the firewall by default, you should turn this option off. Then click Next, and proceed to confirm. Next, select a root administration password. Click Next.

6. Fedora will prompt for packages to install. Be sure to enable the X Window System and the GNOME desktop, as well as the Editors and Graphical Internet packages. To begin installing, select Next. Fedora will format the hard drive and install all needed packages to disk. This may take several minutes or more, depending on the machine. When prompted, remove the DVD and click to reboot the computer.

7. In the final setup phase, you will be asked to approve the license agreement, set the date and time, and configure the optimal monitor resolution. Give appropriate answers. When asked to create an everyday user account, create a user called torrserv with an appropriate password. Fedora will then finish booting. Log in with the newly created torrserv account.

8. With the OS installed, our next priority is to install Java. Some Linux experts believe that the automated RPM packages for Java installation are less reliable than the manual install, so we will perform the manual install. Open the Firefox browser in Applications/Internet/Firefox and browse to java.sun.com/j2se/. As of this writing, Java 2 Standard Edition is at version 5.0. Select the latest version, and browse to the Downloads page.

9. Firefox by default downloads everything to the user desktop. Change this behavior by selecting Edit/Preferences/Downloads and enabling "Ask me where to save every file." Once this is done, scroll down the browser page to the latest edition of JRE (Java Runtime Environment), and click its download page. Currently, this is JRE 5.0 Update 4. Scroll to the license agreement radio button and click Accept. Then scroll to "Linux self-extracting file," and click to download. Save this package in your Home directory (/home/torrserv) as jre.bin.

10. You will need super-user access and the system console for the next steps. Open a Terminal window under Applications/System Tools/Terminal and enter su to reach the super-user prompt. Enter the root password you created in step 5.

11. As the superuser (prompt changes to #), move the installation package to the Linux /opt directory by typing mv jre.bin /opt. Then Use cd /opt to reach the /opt directory, and unroll the archive by entering ./jre.bin. You will need to type "yes" to approve the license agreement once more. As shown below, the Java archive unpacks and installs itself:

12. Still as the super-user, you will now create a couple of environment variables for Java to properly function. Type:

nano /etc/profile.d/java.sh export J2RE_HOME=/opt/jre1.5.0_04 export PATH=$J2RE_HOME/bin:$PATH

Hit Control-X to save, and exit. If you are using a newer version of the JRE than 1.5 Update 4, the "jre1.5.0_04" will be different. In this case, enter ls /opt to confirm the directory pathname. To enable these variables, type: source /etc/profile.d/java.sh

Java should now be ready for use. Type exit to disable super-user mode, or simply close the terminal.

13. In Firefox, browse to azureus.sourceforge.net, and download the Linux GTK version of Azureus. Save the package to your home directory.

14. To open the archive, double-click "torrserv's Home" on the desktop, then double-click on Azureus_2.3.0.4_linux.GTK.tar.bz2. Click the Azureus folder, then click Extract. By default, this will extract to the desktop in a new folder called azureus. Drag the azureus folder into the torrserv home directory, as shown below:

15. Click to the azureus directory, then double-click the file named azureus. The system will prompt you whether to read or run the file; select Run. After a brief delay, Azureus will open.

16. The configuration wizard will launch. Click Next until you reach the Configuration Wizard Transfer and Connection setup screen. Running a torrent consumes bandwidth, and the nature of torrents is to consume as much bandwidth as possible so long as users are connected to the torrent. So the Max Up Speed defined on this screen should be a value the customer can afford to tie up with uploading on a 24 x 7 basis. In no event should you exceed about 80% of the peak upstream bandwidth available, even if the file-serving is to be the primary online activity; it may impact the efficiency of other outbound traffic.

For our example, we will presume a client who has 25 KB/sec. of upstream bandwidth to devote to serving this torrent. Select Line type of Custom and enter 25 KB/sec. in the Max Up Speed section. When finished, click Next.

As shown below, you will set the maximum upload bandwidth for all torrents here. Don't encroach on the company's maximum bandwidth, as torrents tend to consistently consume all the resources they are given:

17. Configure the port the torrent will be served on. By default, this is port 6881, which should be opened for this machine at the client's firewall. Open incremental ports (6882, 6883, etc.) for each additional simultaneous torrent this machine will serve. For example, if the machine will serve three separate torrent archives, then open ports 6881, 6882 and 6883. Ports 6969 through 7000 will also need to be forwarded to this machine, for reasons which will become clear in Step 19. A different port may be used, if necessary, but avoid overlap with other common traffic ports (such as port 80 HTTP, etc.) Once you have completed configuring the port, click Next.

18. Azureus now selects a directory to store torrent files. By default, this is /home/torrserv/.Azureus/torrents (this is not the same folder the Azureus binaries are stored in!). To accept this setting and launch Azureus, click Finish. The window may open minimized; if so, click the Azureus icon in the bottom taskbar to bring it to the front.

19. Before torrents can be shared, the external tracker must be configured. Open Tools/Options, select the Tracker drop-down, and click Server. This is a large configuration window, but most of the options may be retained as the defaults.

The first field, Tracker external IP address, should be populated by either the computer's IP address if it has a fully public IP, or the client's external network IP if this machine is behind network address translation (NAT). The current IP address can be obtained in a terminal window with the command /sbin/ifconfig.

The next two boxes, Enable tracker on HTTP port, should be checked. This will share the public torrent data on port 6969, the default. But if you require secure HTTP transfer of the torrent data (the information that tells other computers how to download the torrent), turn on the HTTPS tracker on port 7000 instead.

Do not enable external torrents unless the client plans to develop a community where other files may be shared via torrent. Under most conditions, enabling external torrents can create a risk for infringing behavior.

If the torrent should be password-protected (in the case of a limited-audience file), turn on Enable password on torrents. Then input the relevant username and password that downloaders will need to participate.

The Tracker client poll interval, Scrape and cache, and Processing limits sections provide controls for fine-tuning the performance of the torrent and relates to checking the status of each of the peers in the torrent. Accept the defaults. Further down in the window, turn on Log Periodic Statistics.

In the Networks section, leave all of the networks enabled if the torrent will be made publicly available. But if the audience will remain selective, disable I2P and The Onion Router. Click Save to commit these changes. Below is a view of the built-in tracker, a convenient feature of Azureus with many configurable options:

20. Select a file to share with a torrent audience. To do this, load the media on the machine you wish to share via torrent. A single torrent can serve either a single file archive (such as a .zip or .tgz file) or a large directory structure.

For this recipe, we will serve a single archive dubbed fileshare.zip. Obtain the media you wish to share, and copy it via the file browser or shell to a convenient location in the /home/torrserv path. Here's a view of Azureus with no torrent loaded and waiting to seed:

21. In the window menu, click File/Share/File, and browse to the location of fileshare.zip (in this example, in the root of /home/torrserv.) Click OK. This will add the archive to the list of Azureus's torrent data by dividing the file into chunks and creating a .torrent file that contains download information.

Depending on the size of the archive and the speed of the system, this may take a minute or more. Once the filename appears in the "Seeding" list, the file is ready for download. In the screen shot below, our torrent is loaded and a peer is connected, downloading the file as fast as we can send it:

22. All that remains is to publicize the torrent's availability. There are two ways to do this. One is to provide the tracker Web page to outside users. The format for the URL is: http://machine-address:6969 ... So for our recipe, this would be: http://192.168.20.2:6969

This is the view customers will see when they log in to download the torrent. The Web page is provided by Azureus's internal tracker:

The second way is to distribute the direct URL to the .torrent management file by appending /torrents/filename.torrent to the above URL. In our example, this would be: http://192.168.20.2:6969/torrents/fileshare.zip.torrent

Once customers sign in using the BitTorrent client of their choice, the torrent will begin sharing, indicated by the number of Peers listed and the usage of upload traffic. Once a peer is 100% complete, it becomes a seed. Then it will act as an upload-only client until the user stops participating.

As I hope this Recipe shows, P2P needn't be a dirty acronym anymore. By giving your clients a safe and effective way of sharing files with their customers, you can help them stretch their bandwidth dollar and open up new avenues of data distribution.

JASON COMPTON is a technology writer who has covered topics ranging from 8-bit entertainment to supercomputing for more than a decade.