From 2e51a1c98f41c8f434a48ddba833b187aa2a7eb0 Mon Sep 17 00:00:00 2001 From: git-sudo-404 Date: Sun, 13 Sep 2026 19:45:43 +0530 Subject: [PATCH] added handshake --- internal/torrent/client.go | 13 ++++- internal/torrent/handhsake.go | 62 +++++++++++++++++++++++ internal/torrent/trackerResponse.go | 6 +-- internal/torrent/trackerResponseParser.go | 6 +-- 4 files changed, 80 insertions(+), 7 deletions(-) create mode 100644 internal/torrent/handhsake.go diff --git a/internal/torrent/client.go b/internal/torrent/client.go index fb08cf2..51c06e8 100644 --- a/internal/torrent/client.go +++ b/internal/torrent/client.go @@ -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 { diff --git a/internal/torrent/handhsake.go b/internal/torrent/handhsake.go new file mode 100644 index 0000000..07060b5 --- /dev/null +++ b/internal/torrent/handhsake.go @@ -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 +} diff --git a/internal/torrent/trackerResponse.go b/internal/torrent/trackerResponse.go index 51c570f..c2e735d 100644 --- a/internal/torrent/trackerResponse.go +++ b/internal/torrent/trackerResponse.go @@ -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) } @@ -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 } diff --git a/internal/torrent/trackerResponseParser.go b/internal/torrent/trackerResponseParser.go index f860509..88c91b9 100644 --- a/internal/torrent/trackerResponseParser.go +++ b/internal/torrent/trackerResponseParser.go @@ -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 @@ -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 @@ -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, }