1
0
mirror of https://github.com/veggiedefender/torrent-client.git synced 2025-11-06 09:29:16 +02:00
Files
torrent-client/torrent/torrent.go

120 lines
2.4 KiB
Go
Raw Normal View History

2019-12-21 23:14:33 -05:00
package torrent
import (
"bytes"
"crypto/rand"
"crypto/sha1"
"fmt"
"io"
2019-12-26 17:04:32 -05:00
"net"
2019-12-21 23:14:33 -05:00
"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-22 13:22:03 -05:00
// Torrent encodes the metadata from a .torrent file
type Torrent struct {
Announce string
2019-12-22 15:19:46 -05:00
InfoHash [20]byte
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-22 14:54:54 -05:00
func (t *Torrent) Download() 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 {
return err
}
2019-12-26 17:04:32 -05:00
// peers, err := t.getPeers(peerID, Port)
peers := []p2p.Peer{{IP: net.IP{127, 0, 0, 1}, Port: 51413}}
2019-12-24 13:23:46 -05:00
downloader := p2p.Download{
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-22 22:36:07 -05:00
err = downloader.Download()
2019-12-22 22:25:57 -05:00
return err
2019-12-21 23:14:33 -05:00
}
// Open parses a torrent file
func Open(r io.Reader) (*Torrent, 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
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
hashes := make([][20]byte, numHashes)
2019-12-22 13:22:03 -05:00
for i := 0; i < numHashes; i++ {
copy(hashes[i][:], buf[i*hashLen:(i+1)*hashLen])
2019-12-22 13:22:03 -05:00
}
return hashes, nil
}
func (bto *bencodeTorrent) toTorrent() (*Torrent, error) {
infoHash, err := bto.Info.hash()
if err != nil {
return nil, err
}
pieceHashes, err := bto.Info.splitPieceHashes()
if err != nil {
return nil, err
}
2019-12-22 14:54:54 -05:00
t := Torrent{
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
}