Stuck at discover peers

this is my code

 const trackerUrl = announce;
    const peerId = "AaBbCcDdEeFfGgHhIiJj";
    const port = 6881;
    const uploaded = 0;
    const left = pieceLength;
    const compact = 1;
    const downloaded = 0;
    const urlEncodedInfoHash = infoHash
      .match(/.{1,2}/g)
      .map((byte: any) => `%${byte}`)
      .join("");
    const requestUrl = `${trackerUrl}?info_hash=${urlEncodedInfoHash}&peer_id=${peerId}&port=${port}&uploaded=${uploaded}&downloaded=${downloaded}&left=${left}&compact=${compact}`;
    axios
      .get(requestUrl, { responseType: "arraybuffer" })
      .then((response: { data: any }) => {
        const decodedResponse = decodeBencode(
          response.data.toString("binary")
        ) as unknown as TrackerResponse;
        // console.log(decodedResponse);
        const peers: any = Buffer.from(decodedResponse.peers);
        // console.log("Peers:", peers);
        const peerList: string[] = [];
        for (let i = 0; i < peers.length - 6; i += 6) {
          const ip = `${peers[i]}.${peers[i + 1]}.${peers[i + 2]}.${
            peers[i + 3]
          }`;
          const port = (peers[i + 4] << 8) + peers[i + 5];

          peerList.push(`${ip}:${port}`);
        }
        // console.log("Peers: ");
        peerList.forEach((x) => console.log(x));
      })
      .catch((error: { message: any }) => {
        console.error(error.message);
      });
  }
}

Output

[your_program] 106.72.195.132:194
[your_program] 162.13.194.188:30525
[your_program] 194.177.26.195:41218
[your_program] 7.195.181.20:50056
[your_program] 195.149.71.195:40960
[your_program] 29.195.136.195:38437
[your_program] 48.74.20.194:44738
[your_program] 153.82.194.149:50083
[your_program] 195.165.26.195:43592
[your_program] 194.175.28.2:50086
[your_program] 86.45.67.195:42314
[your_program] 195.170.103.195:33626
[your_program] 195.151.195.157:49842
[your_program] 114.66.55.195:36422
[your_program] 195.170.96.69:13588
[your_program] 194.159.195.170:24771
[your_program] 152.195.131.194:33051
[tester::#FI9] Expected stdout to contain "106.72.196.0:41485", got: "106.72.195.132:194\n162.13.194.188:30525\n194.177.26.195:41218\n7.195.181.20:50056\n195.149.71.195:40960\n29.195.136.195:38437\n48.74.20.194:44738\n153.82.194.149:50083\n195.165.26.195:43592\n194.175.28.2:50086\n86.45.67.195:42314\n195.170.103.195:33626\n195.151.195.157:49842\n114.66.55.195:36422\n195.170.96.69:13588\n194.159.195.170:24771\n152.195.131.194:33051\n"
[tester::#FI9] Test failed

if i dont do -6 on peerslength then i get out of bound error

@rafterjob1 Could you upload your code to GitHub and share the link? It will be much easier to debug if I can run it directly.

Closing this thread due to inactivity. If you still need assistance, feel free to reopen or start a new discussion!

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.