Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions bitfield/bitfield.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package bitfield

type Bitfield []byte
33 changes: 33 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package client

import (
"BitTorrent-Client/bitfield"
"BitTorrent-Client/peers"
"net"
"time"
)

type Client struct {
Conn net.Conn
Choked bool
Bitfield bitfield.Bitfield
peer peers.Peer
infoHash [20]byte
peerID [20]byte
}

func New(peer peers.Peer, peerID, infoHash [20]byte) (*Client, error) {
conn, err := net.DialTimeout("tcp", peer.String(), 3*time.Second)
if err != nil {
return nil, err
}
return &Client{
Conn: conn,
Choked: true,
peer: peer,
infoHash: infoHash,
peerID: peerID,
}, nil
}

// Read reads and consumes a message from the connection
20 changes: 20 additions & 0 deletions hand_shake/hand_shake.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package handshake

// A Handshake is a special message that a peer uses to identify itself
type Handshake struct {
Pstr string
InfoHash [20]byte
PeerID [20]byte
}

// Serialize serializes the handshake to a buffer
func (h *Handshake) Serialize() []byte {
buf := make([]byte, len(h.Pstr)+49)
buf[0] = byte(len(h.Pstr))
curr := 1
curr += copy(buf[curr:], h.Pstr)
curr += copy(buf[curr:], make([]byte, 8)) // 8 reserved bytes
curr += copy(buf[curr:], h.InfoHash[:])
curr += copy(buf[curr:], h.PeerID[:])
return buf
}
17 changes: 17 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package main

import (
torrentfile "BitTorrent-Client/torrent_file"
"log"
"os"
)

func main() {
inPath := os.Args[1]

_, err := torrentfile.Open(inPath)
if err != nil {
log.Fatal(err)
}

}
4 changes: 4 additions & 0 deletions peers/peers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/binary"
"fmt"
"net"
"strconv"
)

type Peer struct {
Expand All @@ -27,3 +28,6 @@ func Unmarshal(peersBin []byte) ([]Peer, error) {
}
return peers, nil
}
func (p Peer) String() string {
return net.JoinHostPort(p.IP.String(), strconv.Itoa(int(p.Port)))
}
16 changes: 10 additions & 6 deletions torrent_file/torrent_file.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package torrentfile

import (
"io"
"os"

"github.com/jackpal/bencode-go"
)
Expand Down Expand Up @@ -29,11 +29,15 @@ type bencodeTorrent struct {
}

// Open parses a torrent file
func Open(r io.Reader) (*bencodeTorrent, error) {
bto := bencodeTorrent{}
err := bencode.Unmarshal(r, &bto)
func Open(path string) (TorrentFile, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
return TorrentFile{}, err
}
return &bto, nil
defer file.Close()

bto := bencodeTorrent{}
err = bencode.Unmarshal(file, &bto)
return TorrentFile{}, err

}