Trying to talk with torrent trackers via DHT using PHP (part 1 - Ping)
Yo, I have decided to implement an interesting thing: Why not try to create a DHT client in PHP, just for fun? Maybe I would be able to scrap the DHT network with PHP?
So, first thing first: I have no idea how DHT works 😞
From a quick Google search, I see that there is no working example of this in PHP 😞 😞 😞
But I found this:
So, I should be able to implement this in PHP, should be fun:D
Bencode
A quick search, and I got this:
composer require rych/bencode
Somebody would call that cheating, I call it: progress 😈
Final ping result
<?php
use Rych\Bencode\Bencode;
include_once __DIR__ . '/vendor/autoload.php';
$socket=@fsockopen("udp://router.bittorrent.com",6881,$err_no,$err_str,1);
if(!$socket) die('socket failed');
$myId = generateId();
print 'ping'.PHP_EOL;
fwrite($socket, Bencode::encode([
'y' => 'q',
't' => '0f',
'q' => 'ping',
'a' => [
'id' => $myId
]]));
$packetReceived=fread($socket,1024);
var_dump(Bencode::decode($packetReceived));