implement scrape request

This commit is contained in:
mykola2312 2022-04-03 14:41:28 +03:00
parent 340cf17b29
commit f152dba437
3 changed files with 55 additions and 13 deletions

View file

@ -6,19 +6,6 @@ $DB_NAME = "torrent";
$ANN_MIN_INTERVAL = 15;
$ANN_INTERVAL = 60;
function bad_request($msg)
{
http_response_code(400);
echo($msg);
exit;
}
function do_error($msg)
{
die($msg);
}
if (!isset($_GET["info_hash"])) {
bad_request("No info_hash");
}

View file

@ -1,5 +1,17 @@
<?php
function bad_request($msg)
{
http_response_code(400);
echo($msg);
exit;
}
function do_error($msg)
{
die($msg);
}
function ip4_loopback()
{
return inet_pton("127.0.0.1");

43
scrape.php Normal file
View file

@ -0,0 +1,43 @@
<?php
require("../php/config.php");
require("network.php");
require("tracker.php");
$DB_NAME = "torrent";
if (!isset($_GET["info_hash"])) {
bad_request("No info_hash");
}
try {
$hash = hash_decode($_GET["info_hash"]);
$hashLen = strlen($hash);
if ($hashLen < 40 || $hashLen > 40) {
bad_request("Invalid info_hash");
}
} catch (Exception $e) {
bad_request("Invalid info_hash");
}
$conn = new PDO("mysql:host=$DB_HOST;dbname=$DB_NAME", $DB_USER, $DB_PASS);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT count(*) FROM `peers` WHERE info_hash = :info_hash");
$stmt->bindValue(":info_hash", $hash, PDO::PARAM_STR);
$stmt->execute();
$name = "torrent-" . $hash;
$count = intval($stmt->fetchColumn());
$response = array(
"files" => array(
hash_encode($hash) => array(
"complete" => $count,
"downloaded" => $count,
"incomplete" => 0,
"name" => $name,
),
),
);
echo(bencode($response));
?>