2019-12-22 17:43:39 -05:00
|
|
|
package p2p
|
|
|
|
|
|
|
|
|
|
import (
|
2019-12-22 23:51:31 -05:00
|
|
|
"crypto/sha1"
|
|
|
|
|
"encoding/hex"
|
|
|
|
|
"fmt"
|
|
|
|
|
"math"
|
2019-12-22 17:43:39 -05:00
|
|
|
"net"
|
|
|
|
|
"strconv"
|
2019-12-22 23:51:31 -05:00
|
|
|
|
2019-12-24 10:39:22 -05:00
|
|
|
"github.com/veggiedefender/torrent-client/handshake"
|
2019-12-22 23:51:31 -05:00
|
|
|
"github.com/veggiedefender/torrent-client/message"
|
2019-12-22 17:43:39 -05:00
|
|
|
)
|
|
|
|
|
|
2019-12-22 22:25:57 -05:00
|
|
|
// Peer encodes connection information for a peer
|
2019-12-22 17:43:39 -05:00
|
|
|
type Peer struct {
|
|
|
|
|
IP net.IP
|
|
|
|
|
Port uint16
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-22 22:25:57 -05:00
|
|
|
// Downloader holds data required to download a torrent from a list of peers
|
|
|
|
|
type Downloader struct {
|
2019-12-22 23:51:31 -05:00
|
|
|
Peers []Peer
|
|
|
|
|
PeerID [20]byte
|
|
|
|
|
InfoHash [20]byte
|
|
|
|
|
PieceHashes [][20]byte
|
|
|
|
|
Length int
|
2019-12-22 22:25:57 -05:00
|
|
|
}
|
2019-12-22 17:43:39 -05:00
|
|
|
|
2019-12-22 22:25:57 -05:00
|
|
|
// Download downloads a torrent
|
|
|
|
|
func (d *Downloader) Download() error {
|
2019-12-22 23:51:31 -05:00
|
|
|
conn, err := d.Peers[0].connect(d.PeerID, d.InfoHash)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2019-12-24 10:19:40 -05:00
|
|
|
defer conn.Close()
|
2019-12-23 12:56:11 -05:00
|
|
|
h, err := d.handshake(conn)
|
2019-12-22 23:51:31 -05:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2019-12-23 12:56:11 -05:00
|
|
|
fmt.Println(h)
|
2019-12-22 23:51:31 -05:00
|
|
|
|
|
|
|
|
choked := false
|
|
|
|
|
pieceSize := d.Length / len(d.PieceHashes)
|
|
|
|
|
buf := make([]byte, pieceSize)
|
|
|
|
|
i := 0
|
|
|
|
|
for i < pieceSize {
|
2019-12-24 10:39:22 -05:00
|
|
|
msg, err := message.Read(conn)
|
2019-12-22 23:51:31 -05:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-23 12:56:11 -05:00
|
|
|
if msg.ID != message.MsgPiece {
|
|
|
|
|
fmt.Println(msg.String())
|
|
|
|
|
} else {
|
|
|
|
|
fmt.Println("Received", len(msg.Payload), "bytes")
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-22 23:51:31 -05:00
|
|
|
switch msg.ID {
|
|
|
|
|
case message.MsgChoke:
|
|
|
|
|
choked = true
|
|
|
|
|
case message.MsgUnchoke:
|
|
|
|
|
choked = false
|
|
|
|
|
case message.MsgPiece:
|
2019-12-23 12:56:11 -05:00
|
|
|
n, err := message.ParsePiece(0, buf, msg)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
i += n
|
2019-12-22 23:51:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !choked {
|
|
|
|
|
index := 0 // Piece number
|
|
|
|
|
begin := i // Offset
|
|
|
|
|
remain := pieceSize - i
|
|
|
|
|
length := int(math.Min(float64(16384), float64(pieceSize)))
|
|
|
|
|
length = int(math.Min(float64(remain), float64(length)))
|
2019-12-23 12:56:11 -05:00
|
|
|
_, err := conn.Write(message.FormatRequest(index, begin, length).Serialize())
|
2019-12-22 23:51:31 -05:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
s := sha1.Sum(buf)
|
2019-12-23 12:56:11 -05:00
|
|
|
fmt.Printf("Downloaded %d bytes.\n", len(buf))
|
|
|
|
|
fmt.Printf("Got SHA1\t%s\n", hex.EncodeToString(s[:]))
|
|
|
|
|
fmt.Printf("Expected\t%s\n", hex.EncodeToString(d.PieceHashes[0][:]))
|
2019-12-22 23:51:31 -05:00
|
|
|
|
2019-12-22 22:25:57 -05:00
|
|
|
return nil
|
|
|
|
|
}
|
2019-12-22 17:43:39 -05:00
|
|
|
|
2019-12-22 23:51:31 -05:00
|
|
|
func (p *Peer) connect(peerID [20]byte, infoHash [20]byte) (net.Conn, error) {
|
2019-12-22 22:25:57 -05:00
|
|
|
hostPort := net.JoinHostPort(p.IP.String(), strconv.Itoa(int(p.Port)))
|
|
|
|
|
conn, err := net.Dial("tcp", hostPort)
|
2019-12-22 17:43:39 -05:00
|
|
|
if err != nil {
|
2019-12-22 22:25:57 -05:00
|
|
|
return nil, err
|
2019-12-22 17:43:39 -05:00
|
|
|
}
|
2019-12-22 22:25:57 -05:00
|
|
|
return conn, nil
|
2019-12-22 17:43:39 -05:00
|
|
|
}
|
2019-12-22 23:51:31 -05:00
|
|
|
|
2019-12-24 10:39:22 -05:00
|
|
|
func (d *Downloader) handshake(conn net.Conn) (*handshake.Handshake, error) {
|
2019-12-24 11:05:22 -05:00
|
|
|
req := handshake.New(d.InfoHash, d.PeerID)
|
2019-12-22 23:51:31 -05:00
|
|
|
_, err := conn.Write(req.Serialize())
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-24 10:39:22 -05:00
|
|
|
res, err := handshake.Read(conn)
|
2019-12-22 23:51:31 -05:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return res, nil
|
|
|
|
|
}
|