A BitTorrent client implementation in Go. This project implements the core BitTorrent protocol including support for magnet links via the Extension Protocol (BEP 10) and Metadata Extension (BEP 9).
- Parse and display torrent file information
- Discover peers via HTTP trackers
- Perform BitTorrent handshakes with peers
- Download files from torrent files
- Magnet link support:
- Parse magnet URIs
- Extension protocol handshake
- Metadata fetching (ut_metadata)
- Download files using only a magnet link
go build -o cottorrent ./cmd/main.goDisplay torrent info:
./cottorrent info <torrent-file>List peers from tracker:
./cottorrent peers <torrent-file>Perform handshake with a peer:
./cottorrent handshake <torrent-file> <peer_ip>:<peer_port>Download a file:
./cottorrent download -o <output-path> <torrent-file>Parse a magnet link:
./cottorrent magnet_parse "<magnet-link>"Perform extension handshake:
./cottorrent magnet_handshake "<magnet-link>"Fetch and display torrent metadata:
./cottorrent magnet_info "<magnet-link>"Download a single piece:
./cottorrent magnet_download_piece -o <output-path> "<magnet-link>" <piece-index>Download entire file:
./cottorrent magnet_download -o <output-path> "<magnet-link>"Using the included sample torrent file:
# View torrent info
./cottorrent info sample.torrent
# List available peers
./cottorrent peers sample.torrent
# Download the file
./cottorrent download -o debian.iso sample.torrentUsing a magnet link:
# Download a file via magnet link
./cottorrent magnet_download -o /tmp/output.gif "magnet:?xt=urn:btih:c5fb9894bdaba464811b088d806bdd611ba490af&dn=magnet3.gif&tr=http%3A%2F%2Fbittorrent-test-tracker.codecrafters.io%2Fannounce"cottorrent/
├── cmd/
│ └── main.go # CLI entry point
├── internal/
│ ├── bitfield/ # Bitfield handling for piece availability
│ ├── client/ # Peer connection and messaging
│ ├── download/ # Download orchestration
│ ├── handshake/ # BitTorrent handshake protocol
│ ├── message/ # Protocol message encoding/decoding
│ ├── peers/ # Peer representation
│ └── torrent/ # Torrent file parsing and tracker communication
├── sample.torrent # Sample torrent file for testing
├── go.mod
└── go.sum
- BEP 3: The BitTorrent Protocol Specification
- BEP 10: Extension Protocol
- BEP 9: Extension for Peers to Send Metadata Files (ut_metadata)
- github.com/jackpal/bencode-go - Bencode encoding/decoding
- Supports only HTTP trackers
- Does not support multi file torrents