You've already forked torrent-client
mirror of
https://github.com/veggiedefender/torrent-client.git
synced 2025-11-06 09:29:16 +02:00
Super janky download first piece
This commit is contained in:
85
p2p/p2p.go
85
p2p/p2p.go
@@ -1,8 +1,15 @@
|
|||||||
package p2p
|
package p2p
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/sha1"
|
||||||
|
"encoding/binary"
|
||||||
|
"encoding/hex"
|
||||||
|
"fmt"
|
||||||
|
"math"
|
||||||
"net"
|
"net"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/veggiedefender/torrent-client/message"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Peer encodes connection information for a peer
|
// Peer encodes connection information for a peer
|
||||||
@@ -14,16 +21,74 @@ type Peer struct {
|
|||||||
// Downloader holds data required to download a torrent from a list of peers
|
// Downloader holds data required to download a torrent from a list of peers
|
||||||
type Downloader struct {
|
type Downloader struct {
|
||||||
Peers []Peer
|
Peers []Peer
|
||||||
|
PeerID [20]byte
|
||||||
InfoHash [20]byte
|
InfoHash [20]byte
|
||||||
|
PieceHashes [][20]byte
|
||||||
Length int
|
Length int
|
||||||
}
|
}
|
||||||
|
|
||||||
// Download downloads a torrent
|
// Download downloads a torrent
|
||||||
func (d *Downloader) Download() error {
|
func (d *Downloader) Download() error {
|
||||||
|
conn, err := d.Peers[0].connect(d.PeerID, d.InfoHash)
|
||||||
|
defer conn.Close()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = d.handshake(conn)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
choked := false
|
||||||
|
pieceSize := d.Length / len(d.PieceHashes)
|
||||||
|
buf := make([]byte, pieceSize)
|
||||||
|
i := 0
|
||||||
|
for i < pieceSize {
|
||||||
|
msg, err := message.ReadMessage(conn)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
switch msg.ID {
|
||||||
|
case message.MsgChoke:
|
||||||
|
choked = true
|
||||||
|
case message.MsgUnchoke:
|
||||||
|
choked = false
|
||||||
|
case message.MsgPiece:
|
||||||
|
begin := binary.BigEndian.Uint32(msg.Payload[4:8])
|
||||||
|
copy(buf[begin:], msg.Payload[8:])
|
||||||
|
i += (len(msg.Payload) - 8)
|
||||||
|
}
|
||||||
|
|
||||||
|
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)))
|
||||||
|
payload := make([]byte, 12)
|
||||||
|
binary.BigEndian.PutUint32(payload[0:4], uint32(index))
|
||||||
|
binary.BigEndian.PutUint32(payload[4:8], uint32(begin))
|
||||||
|
binary.BigEndian.PutUint32(payload[8:12], uint32(length))
|
||||||
|
request := message.Message{
|
||||||
|
ID: message.MsgRequest,
|
||||||
|
Payload: payload,
|
||||||
|
}
|
||||||
|
_, err := conn.Write(request.Serialize())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
s := sha1.Sum(buf)
|
||||||
|
fmt.Println(hex.EncodeToString(s[:]))
|
||||||
|
fmt.Println(hex.EncodeToString(d.PieceHashes[0][:]))
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func connect(p *Peer, peerID [20]byte, infoHash [20]byte) (net.Conn, error) {
|
func (p *Peer) connect(peerID [20]byte, infoHash [20]byte) (net.Conn, error) {
|
||||||
hostPort := net.JoinHostPort(p.IP.String(), strconv.Itoa(int(p.Port)))
|
hostPort := net.JoinHostPort(p.IP.String(), strconv.Itoa(int(p.Port)))
|
||||||
conn, err := net.Dial("tcp", hostPort)
|
conn, err := net.Dial("tcp", hostPort)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -31,3 +96,21 @@ func connect(p *Peer, peerID [20]byte, infoHash [20]byte) (net.Conn, error) {
|
|||||||
}
|
}
|
||||||
return conn, nil
|
return conn, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *Downloader) handshake(conn net.Conn) (*message.Handshake, error) {
|
||||||
|
req := message.Handshake{
|
||||||
|
Pstr: "BitTorrent protocol",
|
||||||
|
InfoHash: d.InfoHash,
|
||||||
|
PeerID: d.PeerID,
|
||||||
|
}
|
||||||
|
_, err := conn.Write(req.Serialize())
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
res, err := message.ReadHandshake(conn)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return res, nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -50,7 +50,9 @@ func (t *Torrent) Download() error {
|
|||||||
peers := []p2p.Peer{{IP: net.IP{127, 0, 0, 1}, Port: 51413}}
|
peers := []p2p.Peer{{IP: net.IP{127, 0, 0, 1}, Port: 51413}}
|
||||||
downloader := p2p.Downloader{
|
downloader := p2p.Downloader{
|
||||||
Peers: peers,
|
Peers: peers,
|
||||||
|
PeerID: peerID,
|
||||||
InfoHash: t.InfoHash,
|
InfoHash: t.InfoHash,
|
||||||
|
PieceHashes: t.PieceHashes,
|
||||||
Length: t.Length,
|
Length: t.Length,
|
||||||
}
|
}
|
||||||
err = downloader.Download()
|
err = downloader.Download()
|
||||||
|
|||||||
Reference in New Issue
Block a user