Skip to content
Merged
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
13 changes: 12 additions & 1 deletion internal/torrent/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,19 @@ import (
"time"
)

type RemotePeer struct {
PeerAddress
peerId [20]byte
amChoking int32 // This client is choking the peer
amInterested int32 // This client is interested in the peer
peerChoking int32 // Peer is choking this client
peerInterested int32 // Peer is interested in this client
}

type Client struct {
peerId [20]byte
peerId [20]byte
PeerAddress // ip & port of the client
remotePeers []RemotePeer
}

func NewClient() *Client {
Expand Down
62 changes: 62 additions & 0 deletions internal/torrent/handhsake.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* MIT License
*
* Copyright (c) 2026 git-sudo-404
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* Of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* Copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* Copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package torrent

type PeerHandshakeRequest struct {
pstr []byte // identifier of the protocol
reserved [8]byte // eight (8) reserved bytes. All current implementations use all zeroes. Each bit in these bytes can be used to change the behavior of the protocol. An email from Bram suggests that trailing bits should be used first, so that leading bits may be used to change the meaning of trailing bits.
infoHash [20]byte // 20-byte SHA1 hash of the info key in the metainfo file. This is the same info_hash that is transmitted in tracker requests.
peerId [20]byte // 20-byte string used as a unique ID for the client. This is usually the same peer_id that is transmitted in tracker requests (but not always e.g. an anonymity option in Azureus).
}

func (phr *PeerHandshakeRequest) SetPstr(pstr []byte) *PeerHandshakeRequest {
phr.pstr = pstr
return phr
}

func (phr *PeerHandshakeRequest) SetReserved(reserved [8]byte) *PeerHandshakeRequest {
phr.reserved = reserved
return phr
}

func (phr *PeerHandshakeRequest) SetInfoHash(infoHash [20]byte) *PeerHandshakeRequest {
phr.infoHash = infoHash
return phr
}

func (phr *PeerHandshakeRequest) SetPeerId(peerId [20]byte) *PeerHandshakeRequest {
phr.peerId = peerId
return phr
}

func (phr *PeerHandshakeRequest) Serialize() []byte {
buf := make([]byte, 49+len(phr.pstr))
buf[0] = byte(len(phr.pstr))
copy(buf[1:], phr.pstr)
copy(buf[1+len(phr.pstr):], phr.reserved[:])
copy(buf[1+len(phr.pstr)+8:], phr.infoHash[:])
copy(buf[1+len(phr.pstr)+28:], phr.peerId[:])
return buf
}
6 changes: 3 additions & 3 deletions internal/torrent/trackerResponse.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ package torrent

import "net"

type Peer struct { // no peerId when in compact mode = 1
type PeerAddress struct { // no peerId when in compact mode = 1
ip net.IP // peer's IP address either IPv6 (hexed) or IPv4 (dotted quad) or DNS name (string)
port int64 // peer's port number (integer)
}
Expand All @@ -37,10 +37,10 @@ type TrackerResponse struct {
trackerId string // A string that the client should send back on its next announcements. If absent and a previous announce sent a tracker id, do not discard the old value; keep using it.
complete int64 // number of peers with the entire file, i.e. seeders (integer)
incomplete int64 // number of non-seeder peers, aka "leechers" (integer)
peers []Peer
peers []PeerAddress
}

func (tr *TrackerResponse) SetPeers(peers []Peer) *TrackerResponse {
func (tr *TrackerResponse) SetPeers(peers []PeerAddress) *TrackerResponse {
tr.peers = peers
return tr
}
Expand Down
6 changes: 3 additions & 3 deletions internal/torrent/trackerResponseParser.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
"net"
)

func parsePeersCompactString(peersCompactString string) ([]Peer, error) {
func parsePeersCompactString(peersCompactString string) ([]PeerAddress, error) {
// first 4-bytes -> IP
// next 2-bytes -> port

Expand All @@ -43,7 +43,7 @@ func parsePeersCompactString(peersCompactString string) ([]Peer, error) {
}

numberOfPeers := dataLen / 6
peers := make([]Peer, numberOfPeers)
peers := make([]PeerAddress, numberOfPeers)

for i := 0; i < numberOfPeers; i++ {
offset := i * 6
Expand All @@ -53,7 +53,7 @@ func parsePeersCompactString(peersCompactString string) ([]Peer, error) {

port := int64(binary.BigEndian.Uint16(data[offset+4 : offset+6]))

peers[i] = Peer{
peers[i] = PeerAddress{
ip: ip,
port: port,
}
Expand Down
Loading