set timeout for tracker requests, added error handlers

This commit is contained in:
mykola2312 2022-03-18 21:15:39 +02:00
parent da82b6829a
commit 2258e0062c
2 changed files with 22 additions and 10 deletions

View file

@ -68,16 +68,19 @@ $stmt = $conn->prepare("SELECT * FROM `trackers`");
$stmt->execute();
$trackers = $stmt->fetchAll(PDO::FETCH_CLASS, Tracker::class);
foreach($trackers as $tracker) {
$remotePeers = $tracker->announce($hash,
$clientPeer->getIp(), $clientPeer->getPort(),
isset($_GET["peer_id"]) ? $_GET["peer_id"] : null,
isset($_GET["uploaded"]) ? $_GET["uploaded"] : 0,
isset($_GET["downloaded"]) ? $_GET["downloaded"] : 0,
isset($_GET["left"]) ? $_GET["left"] : 0
);
try {
$remotePeers = $tracker->announce($hash,
$clientPeer->getIp(), $clientPeer->getPort(),
isset($_GET["peer_id"]) ? $_GET["peer_id"] : null,
isset($_GET["uploaded"]) ? $_GET["uploaded"] : 0,
isset($_GET["downloaded"]) ? $_GET["downloaded"] : 0,
isset($_GET["left"]) ? $_GET["left"] : 0
);
if ($remotePeers != false) {
$peers = array_merge($peers, $remotePeers);
if ($remotePeers != false) {
$peers = array_merge($peers, $remotePeers);
}
} catch (Exception $e) {
}
}

View file

@ -26,6 +26,14 @@ class Tracker {
return $this->proto . "://" . $this->host . $this->path;
}
public function getPasskeyName() {
if (!is_null($this->passkey)) {
return explode(' ', $this->passkey)[0];
}
return false;
}
public function doHttpAnnounce($infoHash, $peerIp, $peerPort, $peerId = null, $uploaded = 0, $downloaded = 0, $left = 0) {
$peers = array();
@ -44,6 +52,7 @@ class Tracker {
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_TIMEOUT, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if (!is_null($this->proxy)) {
curl_setopt($ch, CURLOPT_PROXY, $this->proxy);
@ -57,7 +66,7 @@ class Tracker {
}
$response = bdecode($result);
if (array_key_exists($response, "failure reason")) {
if (!array_key_exists("peers", $response)) {
return false;
}