dumb by default


dir: Home / Posts / (Bit)Torrent as File Transfer Protocol
published-date: 27 Sep 2024 09:39 +0700
categories: [misc]
tags: [server-mgmt]

(Bit)Torrent as File Transfer Protocol


Before I begin I might need to address that: I do not endorse the act and practice of piracy. This post is intended to be personal documentation on alternative use of BitTorrent and BitTorrent protocol as a way to transfer file over network.

Recently I had to move multiple fairly large files between a hosted server into my pc, which sums up to 300gb worth of compressed processed text gz files (yikes). One thing to consider I could not just create split archives since the storage is nearing full, so all files had to be transferred in one go.

Normally scp can do the job just fine, but due to frequent disconnects on my end, straight scp is just asking for insanity. Especially in this shithole I live in, ISPs tend to throttle commoners networks to an absolute fuckall.

For all I know scp does not handle partial transfers. There are other options to do partial/interrupted file transfer, such as rsync over ssh or sftp reget. Or even more sophisticated approach to invoke dd over ssh, iterate seek and skip for each chunks of target file, then write each chunk blob into a allocated file on my local machine. Should there be a failed transfer, then keep record on last chunk that has been transferred and continue from there. Compare each file crc/hash so final file integrity can be assured.

With that many options, using torrent (i.e. BitTorrent) is just an obtuse excuse, really. It might be wiser to not user torrent since the nature of the protocol is meant for p2p network file sync. I just need to get everything done once, and not planning to host onto others. There are also security concerns regarding torrent, we might get into that later.

But then again it is an opportunity to learn how to use torrent especially configuring both serving and receiving ends. I do not have any time constraint on this task too, so everything is just one setup and forget type of scheme.

Here are my considerations for using torrent, or you might say, the feasibility of using such technology for this one-shot project is as follows.

Here are also antithesis of using torrent.

BitTorrent Tidbits

BitTorrent, or often mentioned as torrent in the wild, is a peer-to-peer (p2p) communication protocol mainly used to share files across interconnected machines (I’m just rephrasing from wiki lol).

.torrent itself is a file that contains descriptor of hosted files hash and sometimes the tracker announce server address. If tracker address is not included then you have to add manually.

Tracker is an endpoint where you can query active peers (seeder and leecher) and health of a torrent. Often times it has url leading path to /announce. You might recall certain site that kick some ass and bay of pirates, those are also serves as tracker for their hosted torrent. When you announce to tracker, you exposes your address and get peers addresses for your torrent files.

BitTorrent Clients

There are various torrent client available in the market, FOSS options are qBittorrent, Transmission, and LibreTorrent.

PSA Avoid uTorrent. Free version of uTorrent has become such an adware it’s straight up cancer. Unless you’re willing to spend for subscription then I’m not stopping you. Fun side-note: uTorrent and the bittorrent protocol was developed under same company.

I will be using headless qbittorrent-nox on my server, because there’s something that needed to be addressed if you’re using web-host gui to configure stuffs.


Hosting a Private Torrent

Init Preps

Assuming you’re on unix environment, say ubuntu, its always best practice to separate whatever you’re hosting to another userspace with limited authorization.

Here I’m using ubuntu. Here’s a bit refresher how to create new user and disable login access.

# add new user with dir /home/<username>
sudo adduser <username>

# disable login
sudo usermod -s /usr/sbin/nologin <username>

# if you ever needed to reactivate login
# sudo usermod -s /bin/bash <username>

# to change from current userspace
sudo su - <username>

And move target folder of files to newly created user.

Client Configuration

Install the preferred client which again in this case qbittorrent-nox. For ubuntu user there are two options to get the client, build from source or to get from apt. One catch though apt might not have the latest stable release, which I have covered on previous post on how to add ppa on apt.

Now you can run the program as background process by

qbittorrent-nox --daemon
# which will spawn a web server on port 8080 by default
# to use another port
qbittorrent-nox --daemon --webui-port=<port>

where you can access from <your-server-address>:<web-gui-port> on your browser.

Once that done, head to the web gui and head over Tools > Options > Web UI > Authentication and change username and password accordingly. This is a must else unauthorized users can just gain access with admin:admin tomfoolery.

Now the reason using qbittorrent-nox is the embedded tracker feature, so we don’t need to setup a separate tracker announce server. To enable head over Tools > Options > Advanced > qBittorrent Section and enable the embedded tracker. Change port if needed, by default it’s set to 9000. now we’re exposing an tracker announcer to <address>:9000/announce

To check if port is open to listen, we can use net-tools netstat and check addresses and port which are bind to qbittorrent-nox configuration.

# if not yet installed
# apt get net-tools

netstat -nltp

# where
#   -n numeric addresses
#   -t tcp
#   -l listening
#   -p display PID and program names

You might want to keep note on the address and port, which we’ll need on the next section.

Creating .torrent

There are various tools to generate torrent descriptors, even most client aforementioned are shipped with one. Unfortunately qbittorrent-nox doesn’t so we’d need to use aftermarket solution.

One tool available on unix systems, and also mentioned on qbittorrent wiki, is mktorrent and is available on apt.

Assuming you have the files ready inside a <path> directory, we can start generating the descriptors by following.

mktorrent -p -a "<address>:<tracker-port>/announce" <path>

# where
#   -a to set announce address
#   -p to set as private torrent, which disables DHT, LSD, and PeX trackers 
# optional but not needed
#   -n set name
#   -o set output filename
#   -l set piece (or block) length, leave to set default of 256kb

The principle stays the same if you’re using other tool, especially the announce address.

Start hosting the file

Now to add our .torrent torrent descriptors to qtbittorrent-nox on the server can be done in two ways.

First one is to download the generated .torrent to our local machine and add it manually from the web gui. This way you’d still need to configure the filepath to be the <path> where the files resides on the server.

The other way is to add the torrent by

qbittorrent-nox <dot-torrent-file> --save-path=<path>

It’s important to set save path since by default it’ll set to Downloads folder.

Once the torrent is added the status on the web gui should be seeding. If it does not resolve to seed then you’ll need to change path again similar to the first method.

Begin the Transfer

After all that, we can add the .torrent on whatever client you use in your local machine or other server. Now we watch as the torrent client try to retrieve peers from our own tracker and allocate new space for the files.

Once you’re done, do not forget to stop the torrent, remove from the list if needed, and kill the qbitorrent process.


Afterword

Again as I’ve said before, do not use torrent for piracy. You should not trust free floating cracked software on the internet after all. Apart from malware piggybacking, you can get a hefty surprise from govt officials.

I think torrent has its place especially for distributed file sharing where hosting a dedicated server is not feasible due to operating cost, availability, and demand.

In my case its just alternative due to bad network, and I want to just sit back and relax watching the download status bar go up (or right, depends on your screen orientation) with minimal maintenance.

Other reasons is just to support seeding torrents out of goodwill, like Internet Archive.

In this post I might update on troubleshooting trackers refusing to work, or other stuff that might precede on using torrent.

glhf.


Troubleshooting

Windows: client can’t update tracker with status message End of File

When client running on windows can’t resolve tracker with status message EOF, there’s a chance that somewhere the request to tracker is being dropped due to filtering rules. To check whether packet is being dropped you might need to enable firewall logging. From cmd or powershell:

auditpol /set /subcategory "Filtering Platform Packet Drop" /failure:enable /success:disable

Then force re-announce tracker(s) or curl to the announce address to invoke event logging. Successful get request without additional header or payload should return 200 even though no meaningful result can be parsed.

Check from Event Viewer (or run eventvwr.msc) and go to Windows Logs > Security and filter logs with keyword Audit Failure and find entries with application name of the client, and is being dropped on Inbound/Outbound rule to the destination address.

If you find the entries, go to general tab and take note on “Filter Run-Time ID”.

With cmd or powershell change dir to, idk, Documents? because we’ll need to flush filters info with

netsh wfp show filters

which will later generate filters.xml file. With this we can check filtering rules that holds back our packet from being sent by searching the FilterRTID.

If that’s an application blocking, try closing the suspected application and rerun force announce or try invoke get request with curl or wget like above.

If needed then you might need to add a rule from Windows Defender Firewall with Advanced Security (hell of a name) and go to Inbound/Outbound Rules and create new rule based on event log. Sometimes it will be protocol 6 which correspond to tcp on port 9000 or whatever port you’re using for the tracker.

You can check the details for blocked connections entry event log here.

Be sure to disable audit once you’re done to prevent unnecessary log flooding.

auditpol /set /subcategory "Filtering Platform Packet Drop" /success:disable /failure:disable

Or even remove the rule once you’re done with the business.





Built with Hugo | previoip (c) 2024