Jay Taylor's notes

back to listing index

atomashpolskiy/bt: BitTorrent library and client with DHT, magnet links, encryption and more

[web search]
Original source (github.com)
Tags: java torrent library bittorrent libtorrent content-distribution github.com
Clipped on: 2020-02-05

Skip to content
Image (Asset 1/10) alt= You have unread notifications
Type Name Latest commit message Commit time
.github/ISSUE_TEMPLATE Update issue templates 3 months ago
bt-bencoding Cleanup 2 months ago
bt-cli Cleanup 2 months ago
bt-core Adopt findbugs 2 months ago
bt-dht [maven-release-plugin] prepare for next development iteration 2 months ago
bt-http-tracker-client Cleanup 2 months ago
bt-tests Cleanup 2 months ago
bt-upnp [maven-release-plugin] prepare for next development iteration 2 months ago
examples Cleanup 2 months ago
jacoco-report [maven-release-plugin] prepare for next development iteration 2 months ago
script Disable LSD test on Travis 8 months ago
.gitignore Issue #80: UPNP for automatic port forwarding 2 years ago
.travis.yml Switch Travis build to JDK 9 3 months ago
LICENSE update license 3 years ago
README.md Update readme 2 months ago
RELEASE-NOTES.md Allow announce key to be missing in the torrent dictionary #42 2 months ago
UPGRADE.md Support for file selection (aka partial downloads) 2 years ago
appveyor.yml bump appveyor build version to 1.4 3 years ago
codecov.yml Try enabling codecov for PRs 2 years ago
findbugs.xml Adopt findbugs 2 months ago
pom.xml Adopt findbugs 2 months ago


Bt Image (Asset 4/10) alt=peer exchange | magnet links | DHT | encryption | LSD | private trackers | extended protocol | partial downloads | port forwarding

Image (Asset 5/10) alt=Runnable apps and demos



Currently, all peer connections are established via encryption negotation protocol (also called MSE handshake). If you're using Oracle JDK (pre 8u152), in order to be able to connect to peers you must install Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy. The reason for this requirement is that the MSE RC4 cipher uses 160 bit keys, while default Java installation allows at most 128 bit keys.


Most recent version available in Maven Central is 1.9.

Declare the following dependencies in your project’s pom.xml:


Building from source

git clone https://github.com/atomashpolskiy/bt.git
cd bt
mvn clean install -DskipTests

Code sample

// enable multithreaded verification of torrent data
Config config = new Config() {
    public int getNumOfHashingThreads() {
        return Runtime.getRuntime().availableProcessors() * 2;

// enable bootstrapping from public routers
Module dhtModule = new DHTModule(new DHTConfig() {
    public boolean shouldUseRouterBootstrap() {
        return true;

// get download directory
Path targetDirectory = Paths.get(System.getProperty("user.home"), "Downloads");

// create file system based backend for torrent data
Storage storage = new FileSystemStorage(targetDirectory);

// create client with a private runtime
BtClient client = Bt.client()

// launch

What makes Bt stand out from the crowd


Being built around the Guice DI, Bt provides many options for tailoring the system for your specific needs. If something is a part of Bt, then it can be modified or substituted for your custom code.

Custom backends

Bt is shipped with a standard file-system based backend (i.e. you can download the torrent file to a storage device). However, the backend details are abstracted from the message-level code. This means that you can use your own backend by providing a storage unit implementation.

Protocol extensions

One notable customization scenario is extending the standard BitTorrent protocol with your own messages. BitTorrent's BEP-10 provides a native support for protocol extensions, and implementation of this standard is already included in Bt. Contribute your own Messages, byte manipulating MessageHandlers, message consumers and producers; supply any additional info in ExtendedHandshake.

Test infrastructure

To allow you test the changes that you've made to the core, Bt ships with a specialized framework for integration tests. Create an arbitrary-sized swarm of peers inside a simple JUnit test, set the number of seeders and leechers and start a real torrent session on your localhost. E.g. create one seeder and many leechers to stress test the network overhead; use a really large file and multiple peers to stress test your newest laptop's expensive SSD storage; or just launch the whole swarm in no-files mode and test your protocol extensions.

Parallel downloads

Bt has out-of-the-box support for multiple simultaneous torrent sessions with minimal system overhead.

Partial downloads

Bt has an API for selecting only a subset of torrent files to download. See the bt.TorrentClientBuilder.fileSelector(TorrentFileSelector) client builder method. File selection works for both .torrent file-based and magnet link downloads.

Java 8 CompletableFuture

Client API leverages the asynchronous java.util.concurrent.CompletableFuture to provide the most natural way for co-ordinating multiple torrent sessions. E.g. use CompletableFuture.allOf(client1.startAsync(...), client2.startAsync(...), ...).join(). Or create a more sophisticated processing pipeline.

And much more...


Can't connect to peers, everything else seems to work

If you're using an Oracle JDK, make sure that you have installed Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy.

Other BitTorrent clients can't connect to a Bt client / No incoming connections when seeding

a) If you're behind a firewall and/or a NAT (e.g. a router), make sure they are configured to allow incoming TCP and UDP connections on the ports used by Bt. Default Bt ports are 6891 and 49001 for BitTorrent and DHT respectively. NAT must additionally be configured to forward all incoming traffic on these ports to the host, that Bt is running on.

b) Many popular BitTorrent clients use UPnP and NAT-PMP to automatically configure port forwarding on NATs. Since 1.8 Bt does this as well, so make sure to include bt-upnp module in the list of dependencies.

There are exceptions in the build log (but the build completes successfully)

This is perfectly fine. Some of the tests verify that the exceptions are thrown in certain cases, hence the exception messages.

Can't run the CLI on Windows XP (java.io.IOException: Cannot run program "/bin/stty")

CLI GUI indeed does not work on Windows XP. Run in headless mode by using -H flag.

Can't connect to peers on Windows 7/8/10

There seem to be some issues with dual-stack networking in Windows JDK (e.g. see this question on SO), with Java trying to use IPv6 address, when it's not really available in the system. The simplest solution is to force Java to use IPv4 by setting java.net.preferIPv4Stack property to true.

Support and feedback

Any thoughts, ideas, criticism, etc. are welcome, as well as votes for new features and BEPs to be added. You have the following options to share your ideas, receive help or report bugs:

List of supported BEPs