You've already forked torrent-client
mirror of
https://github.com/veggiedefender/torrent-client.git
synced 2025-11-06 09:29:16 +02:00
Stub out p2p interface
This commit is contained in:
47
p2p/p2p.go
47
p2p/p2p.go
@@ -1,47 +1,34 @@
|
||||
package p2p
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"net"
|
||||
"strconv"
|
||||
|
||||
"github.com/veggiedefender/torrent-client/message"
|
||||
)
|
||||
|
||||
// Peer encodes information for connecting to a peer
|
||||
// Peer encodes connection information for a peer
|
||||
type Peer struct {
|
||||
IP net.IP
|
||||
Port uint16
|
||||
}
|
||||
|
||||
// Connect connects to a peer
|
||||
func Connect(p *Peer, peerID [20]byte, infoHash [20]byte) error {
|
||||
// Downloader holds data required to download a torrent from a list of peers
|
||||
type Downloader struct {
|
||||
Peers []Peer
|
||||
InfoHash [20]byte
|
||||
PieceLength int
|
||||
Length int
|
||||
}
|
||||
|
||||
// Download downloads a torrent
|
||||
func (d *Downloader) Download() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func connect(p *Peer, peerID [20]byte, infoHash [20]byte) (net.Conn, error) {
|
||||
hostPort := net.JoinHostPort(p.IP.String(), strconv.Itoa(int(p.Port)))
|
||||
conn, err := net.Dial("tcp", hostPort)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
h := message.Handshake{
|
||||
Pstr: "BitTorrent protocol",
|
||||
InfoHash: infoHash,
|
||||
PeerID: infoHash,
|
||||
}
|
||||
_, err = conn.Write(h.Serialize())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
reply, err := message.ReadHandshake(conn)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Println("Handshake received:")
|
||||
fmt.Println(reply.Pstr)
|
||||
fmt.Println("Peer ID", string(reply.PeerID[:]))
|
||||
fmt.Println("Hash", hex.EncodeToString(reply.InfoHash[:]))
|
||||
|
||||
return nil
|
||||
return conn, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user