2019-12-29 14:02:50 -05:00
|
|
|
package torrentfile
|
2019-12-21 23:14:33 -05:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"crypto/rand"
|
|
|
|
|
"crypto/sha1"
|
|
|
|
|
"fmt"
|
|
|
|
|
"io"
|
|
|
|
|
|
|
|
|
|
"github.com/jackpal/bencode-go"
|
2019-12-22 17:43:39 -05:00
|
|
|
"github.com/veggiedefender/torrent-client/p2p"
|
2019-12-21 23:14:33 -05:00
|
|
|
)
|
|
|
|
|
|
2019-12-22 14:54:54 -05:00
|
|
|
// Port to listen on
|
|
|
|
|
const Port uint16 = 6881
|
2019-12-21 23:14:33 -05:00
|
|
|
|
2019-12-29 14:02:50 -05:00
|
|
|
// TorrentFile encodes the metadata from a .torrent file
|
|
|
|
|
type TorrentFile struct {
|
2019-12-22 13:22:03 -05:00
|
|
|
Announce string
|
2019-12-22 15:19:46 -05:00
|
|
|
InfoHash [20]byte
|
2019-12-22 21:56:45 -05:00
|
|
|
PieceHashes [][20]byte
|
2019-12-22 13:22:03 -05:00
|
|
|
PieceLength int
|
|
|
|
|
Length int
|
|
|
|
|
Name string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type bencodeInfo struct {
|
2019-12-21 23:14:33 -05:00
|
|
|
Pieces string `bencode:"pieces"`
|
|
|
|
|
PieceLength int `bencode:"piece length"`
|
|
|
|
|
Length int `bencode:"length"`
|
|
|
|
|
Name string `bencode:"name"`
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-22 13:22:03 -05:00
|
|
|
type bencodeTorrent struct {
|
|
|
|
|
Announce string `bencode:"announce"`
|
|
|
|
|
Info bencodeInfo `bencode:"info"`
|
2019-12-21 23:14:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Download downloads a torrent
|
2019-12-29 14:02:50 -05:00
|
|
|
func (t *TorrentFile) Download() ([]byte, error) {
|
2019-12-22 17:43:39 -05:00
|
|
|
var peerID [20]byte
|
2019-12-22 15:19:46 -05:00
|
|
|
_, err := rand.Read(peerID[:])
|
2019-12-21 23:14:33 -05:00
|
|
|
if err != nil {
|
2019-12-26 21:53:11 -05:00
|
|
|
return nil, err
|
2019-12-21 23:14:33 -05:00
|
|
|
}
|
|
|
|
|
|
2019-12-26 21:53:11 -05:00
|
|
|
peers, err := t.getPeers(peerID, Port)
|
2019-12-29 14:02:50 -05:00
|
|
|
torrent := p2p.Torrent{
|
2019-12-22 23:51:31 -05:00
|
|
|
Peers: peers,
|
|
|
|
|
PeerID: peerID,
|
|
|
|
|
InfoHash: t.InfoHash,
|
|
|
|
|
PieceHashes: t.PieceHashes,
|
|
|
|
|
Length: t.Length,
|
2019-12-22 17:43:39 -05:00
|
|
|
}
|
2019-12-29 14:02:50 -05:00
|
|
|
buf, err := torrent.Download()
|
2019-12-26 21:53:11 -05:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return buf, nil
|
2019-12-21 23:14:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Open parses a torrent file
|
2019-12-29 14:02:50 -05:00
|
|
|
func Open(r io.Reader) (*TorrentFile, error) {
|
2019-12-22 13:22:03 -05:00
|
|
|
bto := bencodeTorrent{}
|
|
|
|
|
err := bencode.Unmarshal(r, &bto)
|
2019-12-21 23:14:33 -05:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2019-12-22 14:54:54 -05:00
|
|
|
t, err := bto.toTorrent()
|
2019-12-22 13:22:03 -05:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2019-12-22 14:54:54 -05:00
|
|
|
return t, nil
|
2019-12-21 23:14:33 -05:00
|
|
|
}
|
|
|
|
|
|
2019-12-22 15:19:46 -05:00
|
|
|
func (i *bencodeInfo) hash() ([20]byte, error) {
|
2019-12-21 23:14:33 -05:00
|
|
|
var buf bytes.Buffer
|
|
|
|
|
err := bencode.Marshal(&buf, *i)
|
|
|
|
|
if err != nil {
|
2019-12-22 15:19:46 -05:00
|
|
|
return [20]byte{}, err
|
2019-12-21 23:14:33 -05:00
|
|
|
}
|
|
|
|
|
h := sha1.Sum(buf.Bytes())
|
2019-12-22 15:19:46 -05:00
|
|
|
return h, nil
|
2019-12-21 23:14:33 -05:00
|
|
|
}
|
2019-12-22 13:22:03 -05:00
|
|
|
|
2019-12-22 21:56:45 -05:00
|
|
|
func (i *bencodeInfo) splitPieceHashes() ([][20]byte, error) {
|
2019-12-22 13:22:03 -05:00
|
|
|
hashLen := 20 // Length of SHA-1 hash
|
|
|
|
|
buf := []byte(i.Pieces)
|
|
|
|
|
if len(buf)%hashLen != 0 {
|
|
|
|
|
err := fmt.Errorf("Received malformed pieces of length %d", len(buf))
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
numHashes := len(buf) / hashLen
|
2019-12-22 21:56:45 -05:00
|
|
|
hashes := make([][20]byte, numHashes)
|
2019-12-22 13:22:03 -05:00
|
|
|
|
|
|
|
|
for i := 0; i < numHashes; i++ {
|
2019-12-22 21:56:45 -05:00
|
|
|
copy(hashes[i][:], buf[i*hashLen:(i+1)*hashLen])
|
2019-12-22 13:22:03 -05:00
|
|
|
}
|
|
|
|
|
return hashes, nil
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-29 14:02:50 -05:00
|
|
|
func (bto *bencodeTorrent) toTorrent() (*TorrentFile, error) {
|
2019-12-22 13:22:03 -05:00
|
|
|
infoHash, err := bto.Info.hash()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
pieceHashes, err := bto.Info.splitPieceHashes()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2019-12-29 14:02:50 -05:00
|
|
|
t := TorrentFile{
|
2019-12-22 13:22:03 -05:00
|
|
|
Announce: bto.Announce,
|
|
|
|
|
InfoHash: infoHash,
|
|
|
|
|
PieceHashes: pieceHashes,
|
|
|
|
|
PieceLength: bto.Info.PieceLength,
|
|
|
|
|
Length: bto.Info.Length,
|
|
|
|
|
Name: bto.Info.Name,
|
|
|
|
|
}
|
2019-12-22 14:54:54 -05:00
|
|
|
return &t, nil
|
2019-12-22 13:22:03 -05:00
|
|
|
}
|