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

66 lines
1.1 KiB
Go
Raw Normal View History

2019-12-21 23:14:33 -05:00
package torrent
import (
"bytes"
"crypto/rand"
"crypto/sha1"
"fmt"
"io"
"github.com/jackpal/bencode-go"
)
const port = 6881
// Info info
type Info struct {
Pieces string `bencode:"pieces"`
PieceLength int `bencode:"piece length"`
Length int `bencode:"length"`
Name string `bencode:"name"`
}
// Torrent torrent
type Torrent struct {
Announce string `bencode:"announce"`
Info Info `bencode:"info"`
}
// Download downloads a torrent
func (to *Torrent) Download() error {
peerID := make([]byte, 20)
_, err := rand.Read(peerID)
if err != nil {
return err
}
tracker := Tracker{
PeerID: peerID,
Torrent: to,
Port: port,
}
peers, err := tracker.getPeers()
fmt.Println(peers)
return nil
}
// Open parses a torrent file
func Open(r io.Reader) (*Torrent, error) {
to := Torrent{}
err := bencode.Unmarshal(r, &to)
if err != nil {
return nil, err
}
return &to, nil
}
func (i *Info) hash() ([]byte, error) {
var buf bytes.Buffer
err := bencode.Marshal(&buf, *i)
if err != nil {
return nil, err
}
h := sha1.Sum(buf.Bytes())
return h[:], nil
}