From aeda1d2718a151b937712ad24fbe8b2df91a0f11 Mon Sep 17 00:00:00 2001 From: mykola2312 <49044616+mykola2312@users.noreply.github.com> Date: Fri, 18 Mar 2022 20:13:10 +0200 Subject: [PATCH] retracker: implemented proxy-announcing to all trackers registered in database & retrieve peers to database. --- tracker.php | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 tracker.php diff --git a/tracker.php b/tracker.php new file mode 100644 index 0000000..6efac53 --- /dev/null +++ b/tracker.php @@ -0,0 +1,79 @@ +proto . "://" . $this->host + . ":" . $this->port . $this->path; + } + + public function getHttpUrl() { + return $this->proto . "://" . $this->host . $this->path; + } + + public function doHttpAnnounce($infoHash, $peerIp, $peerPort, $peerId = null, $uploaded = 0, $downloaded = 0, $left = 0) { + $peers = array(); + + $passkey = is_null($this->passkey) ? "" : $this->passkey . "&"; + $params = http_build_query(array( + "info_hash" => hash_encode($infoHash), + "peer_id" => is_null($peerId) ? self::ANN_PEER_ID : $peerId, + "ip" => $peerIp, + "port" => $peerPort, + "uploaded" => $uploaded, + "downloaded" => $downloaded, + "left" => $left, + )); + + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $this->getHttpUrl() . "?" . $passkey . $params); + curl_setopt($ch, CURLOPT_PORT, intval($this->port)); + curl_setopt($ch, CURLOPT_USERAGENT, self::ANN_USERAGENT); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + if (!is_null($this->proxy)) { + curl_setopt($ch, CURLOPT_PROXY, $this->proxy); + } + + $result = curl_exec($ch); + $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); + curl_close($ch); + if ($code != 200 || $result == false) { + return false; + } + + $response = bdecode($result); + $peersEnc = $response["peers"]; + for ($i = 0; $i < strlen($peersEnc); $i+=6) { + $remoteIp = long2ip(unpack("N", $peersEnc, $i)[1]); + $remotePort = unpack("n", $peersEnc, $i + 4)[1]; + array_push($peers, new Peer($infoHash, $remoteIp, $remotePort)); + } + + return $peers; + } + + public function announce($infoHash, $peerIp, $peerPort, $peerId = null, $uploaded = 0, $downloaded = 0, $left = 0) { + if ($this->proto == "http" || $this->proto == "https") { + return $this->doHttpAnnounce($infoHash, $peerIp, $peerPort, $peerId, $uploaded, $downloaded, $left); + } + + return false; + } +} + +?> \ No newline at end of file