Bt
BitTorrent library and client with DHT, magnet links, encryption and more
Install / Use
/learn @atomashpolskiy/BtREADME
Supported BEPs and extensions
- BEP-3: The BitTorrent Protocol Specification
- BEP-5: DHT Protocol
- BEP-9: Extension for Peers to Send Metadata Files
- BEP-10: Extension Protocol
- BEP-11: Peer Exchange (PEX)
- BEP-12: Multitracker metadata extension
- BEP-14: Local Service Discovery
- BEP-15: UDP Tracker Protocol
- BEP-20: Peer ID Conventions
- BEP-23: Tracker Returns Compact Peer Lists
- BEP-27: Private Torrents
- BEP-41: UDP Tracker Protocol Extensions
- Message Stream Encryption
Resources
- HOME – website with documentation and tutorials
- RELEASE NOTES – list of features, bugfixes and improments for each version
- UPGRADE INSTRUCTIONS – version migration guide
- FORUM – Google group for support and feedback
- TROUBLESHOOTING - solutions for some common problems
- LICENSE – licensed under Apache License 2.0
Runnable apps and demos
- CLI – command-line downloader
- PEER TRACKER – tracking of swarm statistics via events
- YOURIP MESSENGER – usage of custom messages
Media
- HABRAHABR.RU: Пишем свой BitTorrent клиент на базе библиотеки Bt - Introductory article and demonstration of basic capabilities (in Russian)
- SMARTSPATE.COM: How To Write Your Own BitTorrent Client By Using Bt Library - English translation of the above article
Prerequisites
<sup>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.</sup>
Usage
Most recent version available in Maven Central is 1.10.
Declare the following dependencies in your project’s pom.xml:
<dependency>
<groupId>com.github.atomashpolskiy</groupId>
<artifactId>bt-core</artifactId>
<version>${bt-version}</version>
</dependency>
<dependency>
<groupId>com.github.atomashpolskiy</groupId>
<artifactId>bt-http-tracker-client</artifactId>
<version>${bt-version}</version>
</dependency>
<dependency>
<groupId>com.github.atomashpolskiy</groupId>
<artifactId>bt-dht</artifactId>
<version>${bt-version}</version>
</dependency>
<dependency>
<groupId>com.github.atomashpolskiy</groupId>
<artifactId>bt-upnp</artifactId>
<version>${bt-version}</version>
</dependency>
Building from source
git clone https://github.com/atomashpolskiy/bt.git
cd bt
mvn clean install -DskipTests
Code sample
Download a torrent from a magnet link
// enable multithreaded verification of torrent data
Config config = new Config() {
@Override
public int getNumOfHashingThreads() {
return Runtime.getRuntime().availableProcessors() * 2;
}
};
// enable bootstrapping from public routers
Module dhtModule = new DHTModule(new DHTConfig() {
@Override
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()
.config(config)
.storage(storage)
.magnet("magnet:?xt=urn:btih:af0d9aa01a9ae123a73802cfa58ccaf355eb19f1")
.autoLoadModules()
.module(dhtModule)
.stopWhenDownloaded()
.build();
// launch
client.startAsync().join();
Create a torrent
Path torrentRoot = Paths.get("/home/torrents/mytorrent");
Path file1 = Paths.get("/home/torrents/mytorrent/file1.bin");
Path file2 = Paths.get("/home/torrents/mytorrent/file2.bin");
Path dirToAdd = Paths.get("/home/torrents/mytorrent/dir_with_files");
byte[] torrentBytes = new TorrentBuilder()
.rootPath(torrentRoot)
.addFiles(file1, file2, dirToAdd)
.announce("http://example.com/announce")
.build();
Files.write(Paths.get("/home/torrents/mytorrent.torrent"), torrentBytes);
What makes Bt stand out from the crowd
Flexibility
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.
Related Skills
node-connect
336.9kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
83.0kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
336.9kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
commit-push-pr
83.0kCommit, push, and open a PR
