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,6 +68,7 @@ $stmt = $conn->prepare("SELECT * FROM `trackers`");
$stmt->execute(); $stmt->execute();
$trackers = $stmt->fetchAll(PDO::FETCH_CLASS, Tracker::class); $trackers = $stmt->fetchAll(PDO::FETCH_CLASS, Tracker::class);
foreach($trackers as $tracker) { foreach($trackers as $tracker) {
try {
$remotePeers = $tracker->announce($hash, $remotePeers = $tracker->announce($hash,
$clientPeer->getIp(), $clientPeer->getPort(), $clientPeer->getIp(), $clientPeer->getPort(),
isset($_GET["peer_id"]) ? $_GET["peer_id"] : null, isset($_GET["peer_id"]) ? $_GET["peer_id"] : null,
@ -79,6 +80,8 @@ foreach($trackers as $tracker) {
if ($remotePeers != false) { if ($remotePeers != false) {
$peers = array_merge($peers, $remotePeers); $peers = array_merge($peers, $remotePeers);
} }
} catch (Exception $e) {
}
} }
foreach($peers as $peer) { foreach($peers as $peer) {

View file

@ -26,6 +26,14 @@ class Tracker {
return $this->proto . "://" . $this->host . $this->path; 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) { public function doHttpAnnounce($infoHash, $peerIp, $peerPort, $peerId = null, $uploaded = 0, $downloaded = 0, $left = 0) {
$peers = array(); $peers = array();
@ -44,6 +52,7 @@ class Tracker {
curl_setopt($ch, CURLOPT_URL, $this->getHttpUrl() . "?" . $passkey . $params); curl_setopt($ch, CURLOPT_URL, $this->getHttpUrl() . "?" . $passkey . $params);
curl_setopt($ch, CURLOPT_PORT, intval($this->port)); curl_setopt($ch, CURLOPT_PORT, intval($this->port));
curl_setopt($ch, CURLOPT_USERAGENT, self::ANN_USERAGENT); curl_setopt($ch, CURLOPT_USERAGENT, self::ANN_USERAGENT);
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if (!is_null($this->proxy)) { if (!is_null($this->proxy)) {
curl_setopt($ch, CURLOPT_PROXY, $this->proxy); curl_setopt($ch, CURLOPT_PROXY, $this->proxy);
@ -57,7 +66,7 @@ class Tracker {
} }
$response = bdecode($result); $response = bdecode($result);
if (array_key_exists($response, "failure reason")) { if (!array_key_exists("peers", $response)) {
return false; return false;
} }