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

139 lines
2.8 KiB
Go
Raw Normal View History

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"
2020-01-02 12:17:45 -05:00
"os"
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-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
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
}
2020-01-02 12:17:45 -05:00
// DownloadToFile downloads a torrent and writes it to a file
func (t *TorrentFile) DownloadToFile(path string) 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 {
2020-01-02 12:17:45 -05:00
return err
2019-12-21 23:14:33 -05:00
}
2020-01-02 10:32:20 -05:00
peers, err := t.requestPeers(peerID, Port)
2020-01-02 10:23:18 -05:00
if err != nil {
2020-01-02 12:17:45 -05:00
return err
2020-01-02 10:23:18 -05:00
}
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,
PieceLength: t.PieceLength,
2019-12-22 23:51:31 -05:00
Length: t.Length,
Name: t.Name,
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 {
2020-01-02 12:17:45 -05:00
return err
2019-12-26 21:53:11 -05:00
}
2020-01-02 12:17:45 -05:00
outFile, err := os.Create(path)
if err != nil {
return err
}
defer outFile.Close()
_, err = outFile.Write(buf)
if err != nil {
return err
}
return nil
2019-12-21 23:14:33 -05:00
}
// Open parses a torrent file
2020-01-02 12:17:45 -05:00
func Open(path string) (TorrentFile, error) {
file, err := os.Open(path)
if err != nil {
return TorrentFile{}, err
}
defer file.Close()
2019-12-22 13:22:03 -05:00
bto := bencodeTorrent{}
2020-01-02 12:17:45 -05:00
err = bencode.Unmarshal(file, &bto)
2019-12-21 23:14:33 -05:00
if err != nil {
2020-01-02 10:27:00 -05:00
return TorrentFile{}, err
2019-12-21 23:14:33 -05:00
}
2019-12-31 23:36:27 -05:00
return bto.toTorrentFile()
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
}
2020-01-02 10:27:00 -05:00
func (bto *bencodeTorrent) toTorrentFile() (TorrentFile, error) {
2019-12-22 13:22:03 -05:00
infoHash, err := bto.Info.hash()
if err != nil {
2020-01-02 10:27:00 -05:00
return TorrentFile{}, err
2019-12-22 13:22:03 -05:00
}
pieceHashes, err := bto.Info.splitPieceHashes()
if err != nil {
2020-01-02 10:27:00 -05:00
return TorrentFile{}, err
2019-12-22 13:22:03 -05:00
}
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,
}
2020-01-02 10:27:00 -05:00
return t, nil
2019-12-22 13:22:03 -05:00
}