Recently I received a wonderful gift, a Piratebox! Now I have my own Internet! With a forum, a chat and a file sharing server! So cool!

What is not cool is that my hardware is very limited and the platform is stable only when the custom part is kept at minimum.

Definitely the best part of the box is the shared file server, connect, upload/download and go, a single download can go as up as 1Mb/s (using scp) or 30Mb/s with wget but not everyone can use a terminal or have credentials to access the box, the easiest option is definitely the HTML interface.

Under the hood the piratebox is powered by lighttp, a very resource-light webserver that fits good in the great picture of the project but we are a little late with the updates. Piratebox ships whit v0.9 and lighttp is now at v1.4, maybe we can squeeze more from the hardware with asyncronous I/O or some modern witchcraft but that’s not the point.

Before diving into the development part of this article I would like to say a few word about Droopy. Droopy is the magic that runs the piratebox most loved feature, the file sharing. It provides a dead simple upload page that you can encapsulate inside an iframe and you can use python to make more magic happen during the upload process. What I don’t really like about this approach is that it’s good only if you have small files or you are really patient.

A better approach would be using a file-sharing protocol like bittorrent, the file is avaiable to the network, not uploaded to the piratebox storage and there is virtually no size limit to what you can share.

Enough with the tecno-blabbery, let’s run our PirateBay!

Everything has been made possible by the bnbt tracker project, I think it has been abandoned a long time ago (2006) but the docs is something good and I did most of the experimentation for you.

The package we are gonna install is cbtt and you can find it in the package list for the OpenWrt attitude adjustment release. We will also need the uclibcxx package, a C++ library on diet, required to run our tracker.

Move the packages to the piratebox either with scp or upload it to the shared folder.

As in every linux distro we have a package manager, opkg; here you can find more info about the usage.

# sobstitute the package filename to the generic name root@piratebox:~# opkg install uclibcxx -d ext --cache = /mnt/usb/install/cache root@piratebox:~# opkg install cbct -d ext --cache = /mnt/usb/install/cache

This commmand will install both the library and the tracker software on a extended-root partition we have “mounted” as ext, se opkg.conf file for more info.

Link this new executable in /bin and you will be ready to go on.

root@piratebox:~# ln -s /usr/local/usr/bin/bnbt /bin/bnbt

Go on and give it a try, start the server with the command bnbt , a lot of log lines will come out and you can see what happens.

[ Sun Aug 9 11:10:56 2015] config warning - unable to open bnbt.cfg for reading [ Sun Aug 9 11:10:57 2015] server - listening on port 26213 ( "port" ) [ Sun Aug 9 11:10:57 2015] warning - unable to open dstate.bnbt for reading [ Sun Aug 9 11:10:57 2015] warning - unable to open tags.bnbt for reading [ Sun Aug 9 11:10:57 2015] warning - unable to open users.bnbt for reading [ Sun Aug 9 11:10:57 2015] warning - unable to open clientbans.bnbt for reading [ Sun Aug 9 11:10:57 2015] Client Banlist parse called [ Sun Aug 9 11:10:57 2015] warning - unable to open bans.bnbt for reading [ Sun Aug 9 11:10:57 2015] IP Banlist parse called [ Sun Aug 9 11:10:57 2015] server - start ## Ctrl + c stop the process and kill the server [ Sun Aug 9 11:11:03 2015] server warning - select error ( error 4 ) [ Sun Aug 9 11:11:04 2015] server - exit

Good news, it works! After you killed the server you will find some files around that it has generated for you.

root@piratebox:~# ls -l -rw-r--r-- 1 root root 3616 Aug 9 11:10 bnbt.cfg -rw-r--r-- 1 root root 36 Aug 9 11:11 dstate.bnbt -rw-r--r-- 1 root root 2 Aug 9 11:11 tags.bnbt

You can open them and look around, bnbt.cfg is the configuration file, dstate.bnbt is the internal file to keep some stats as number of completed torrents and tags.bnbt is the internal file to keep track of the tags you can add to torrents.

But I don’t like it this way, these files are polluting my limited memory on the root filesystem and they can grow very large, let’s move them to somewhere else.

## remove the previous files root@piratebox:~# rm bnbt.cfg dstate.bnbt tags.bnbt ## change the working directory root@piratebox:~# cd /mnt/ext/etc/config/ ## create a new directory and step into it root@piratebox:/mnt/ext/etc/config/# mkdir bnbt && cd bnbt ## run the tracker program inside the new directory root@piratebox:/mnt/ext/etc/config/bnbt/# bnbt

This is definitely better, now the files are on the external memory. Let’s move on and let the tracker start at boot.

init files

I don’t know if this is the best way to handle this but I found that init files are the simplest option so I will go that way.

## change the working directory root@piratebox:~# cd /etc/init.d/ ## create a new file, will contain the tracker instructions root@piratebox:/etc/init.d/# touch tracker ## make it executable root@piratebox:/etc/init.d/# chmod +x tracker

Init files are all alike, so I will just post mine, just copy and paste it into the tracker file you created before.

#!/bin/sh /etc/rc.common # description: run tracker server on startup # the new galaxy most resilient tracker NAME = "torrent tracker" # Start order START = 99 # Stop order # STOP= # move to config directory cd /mnt/ext/etc/config/bnbt # Start the torrent tracker start () { service_start /bin/bnbt & ## create a lock file ## touch /var/lock/subsys/bnbt ## success $"Tracker Server startup" ## echo } # Stop the torrent tracker stop () { service_stop /bin/bnbt }

Now the final touch, enable it. This will write a file, S99tracker to /etc/rc.d/ and it will start the tracker at boot.

To enable it

root@piratebox:~# /etc/init.d/tracker enable

Reboot the piratebox and wait until it is back online, go to http://piratebox.lan:26213/users.html, here you can create a user with every permission, it will be your administrator. Do me a favor and don’t call it admin, administrator or root and give it a decent password. The email is required but you can put anything in there, so be creative.

The next page you will see is the tracker’s index page, there you can see what your tracker is doing with the torrent network you set up.

There won’t be a lot of info but this is good, we prefer anoninmity and no logging.

configuring your tracker

You can find a lot of info on the official documentation of the bnbt project, so I won’t cover (now) what these config means, just copy and paste it to your config file (which is now /mnt/ext/etc/config/bnbt/bnbt.cfg ).

I know working on the Piratebox is not that simple, so here it is the config file, upload it to the piratebox and mv it to the bnbt config folder.

allowed_dir = announce_interval = 60 bind = bnbt_access_log_dir = bnbt_access_log_file_pattern = % Y -% m -% d . log bnbt_allow_comments = 0 bnbt_allow_info_link = 0 bnbt_allow_scrape = 1 bnbt_allow_scrape_global = 1 bnbt_allow_search = 0 bnbt_allow_sort = 0 bnbt_allow_torrent_downloads = 0 bnbt_archive_dir = bnbt_charset = iso - 8859 - 1 bnbt_comment_length = 800 bnbt_comments_file = bnbt_compression_level = 0 bnbt_count_unique_peers = 0 bnbt_debug = 0 bnbt_delete_invalid = 1 bnbt_delete_own_torrents = 1 bnbt_disable_html = 0 bnbt_dump_xml_file = bnbt_dump_xml_interval = 600 bnbt_dump_xml_peers = 1 bnbt_error_log_dir = bnbt_error_log_file_pattern = % Y -% m -% de . log bnbt_external_torrent_dir = bnbt_file_dir = bnbt_file_expires = 180 bnbt_flush_interval = 100 bnbt_force_announce_on_download = 0 bnbt_force_announce_url = bnbt_guest_access = 3 bnbt_max_conns = 64 bnbt_max_peers_display = 500 bnbt_max_recv_size = 128 bnbt_max_torrents = 0 bnbt_member_access = 79 bnbt_name_length = 32 bnbt_parse_on_upload = 1 bnbt_per_page = 100 bnbt_private_tracker_flag = 0 bnbt_realm = BNBT bnbt_refresh_fast_cache_interval = 30 bnbt_refresh_static_interval = 10 bnbt_require_announce_key = 1 bnbt_robots_txt = bnbt_rss_channel_copyright = bnbt_rss_channel_description = BitTorrent RSS Feed for BNBT bnbt_rss_channel_image_height = 0 bnbt_rss_channel_image_url = bnbt_rss_channel_image_width = 0 bnbt_rss_channel_language = en - us bnbt_rss_channel_link = http :// localhost : 26213 / bnbt_rss_channel_title = My BNBT RSS Feed bnbt_rss_channel_ttl = 60 bnbt_rss_file = bnbt_rss_file_mode = 0 bnbt_rss_interval = 30 bnbt_rss_limit = 25 bnbt_rss_online_dir = bnbt_show_added = 0 bnbt_show_average_dl_rate = 0 bnbt_show_average_left = 0 bnbt_show_average_ul_rate = 0 bnbt_show_completed = 0 bnbt_show_file_comment = 0 bnbt_show_file_contents = 0 bnbt_show_gen_time = 1 bnbt_show_info_hash = 0 bnbt_show_left_as_progress = 0 bnbt_show_max_left = 0 bnbt_show_min_left = 0 bnbt_show_num_files = 0 bnbt_show_share_ratios = 0 bnbt_show_size = 0 bnbt_show_stats = 0 bnbt_show_transferred = 0 bnbt_show_uploader = 0 bnbt_static_footer = bnbt_static_header = bnbt_style_sheet = bnbt_swap_torrent_link = 0 bnbt_tag_file = tags . bnbt bnbt_tlink_bind = bnbt_tlink_connect = bnbt_tlink_password = bnbt_tlink_port = 5204 bnbt_tlink_server = 0 bnbt_tracker_title = The Piratebox Bay bnbt_upload_dir = bnbt_use_announce_key = 1 bnbt_users_file = users . bnbt bnbt_users_per_page = 50 cbtt_abuse_detection = 0 cbtt_abuse_hammer_limit = 10 cbtt_abuse_limit = 5 cbtt_ban_file = clientbans . bnbt cbtt_ban_mode = 0 cbtt_blacklist_below_1024 = 0 cbtt_blacklist_common_p2p_ports = 0 cbtt_block_private_ip = 0 cbtt_dont_compress_torrents = 1 cbtt_download_link_image = cbtt_ip_ban_mode = 0 cbtt_ipban_file = bans . bnbt cbtt_page_number_count = 3 cbtt_require_compact = 0 cbtt_require_no_peer_id = 0 cbtt_restrict_overflow = 0 cbtt_restrict_overflow_limit = 1099511627776 cbtt_restricted_peer_spoofing = 0 cbtt_scrape_file = cbtt_scrape_save_interval = 0 cbtt_service_name = BNBT Service cbtt_stats_link_image = dfile = dstate . bnbt downloader_timeout_interval = 2700 favicon = image_bar_fill = image_bar_trans = keep_dead = 0 max_give = 200 min_announce_interval = 1500 min_request_interval = 18000 mysql_cbtt_ttrader_support = 0 mysql_database = bnbt mysql_host = mysql_override_dstate = 0 mysql_password = mysql_port = 0 mysql_refresh_allowed_interval = 0 mysql_refresh_stats_interval = 600 mysql_user = only_local_override_ip = 0 parse_allowed_interval = 0 port = 26213 response_size = 50 save_dfile_interval = 300 show_names = 1 socket_timeout = 15

Now you only need a torrent to upload and we will see if the tracker is really working.

make a torrent

I will be only covering how to do this with a command line but google is your friend.

We will use mktorrent because it’s dead simple, the problem is it’s not avaiable on the current release of the piratebox so you have to use another pc to create the torrent file.

edoput@edoput~: mktorrent path/to/my/file -a http://piratebox.lan:26213/announce

let it run and we are done, upload the torrent file to the piratebox, download it from the Shared file folder and open it with your torrent client of choiche. When prompted where to save the file choose the current file location on your pc and Enable local peer discovery to share your file with the local net.

Web seeds

Okay but if I already have a file on the piratebox and I want it to stay there how can this help me achieving a better download speed?

Intrducing Web Seeds, just use the option --web-seed with the file url like this.

edoput@edoput~: mktorrent path/to/my/file -a http://piratebox.lan:26213/announce -w http://piratebox.lan/Shared/myfile

Basically this will enable the piratebox to be a seed box, serving the files both on http and torrent.

Conclusion

I had fun with this project and it all went very well, it took maybe three days of thinkering and it’s now working quite well.

What I really like about this implementation is that if the configuration of the tracker on every box is the same then you can jump across networks with your files and you can keep sharing, there is the option to have the tracker display every torrent it is tracking and that would mean that we can add a page to the piratebox where avaiable torrents are listed for everyone to go and get it.