Using Server URL:

http://torrent.tracker.durukanbal.com/

https://torrent.tracker.durukanbal.com/

<?php

    // exit();
    

	ini_set ( 'display_errors', FALSE );
	ini_set ( 'display_startup_errors', FALSE );
	// ini_set ( 'display_errors', TRUE );
	// ini_set ( 'display_startup_errors', TRUE );
	// error_reporting ( E_ALL );
	
	header ( "content-type: text/plain" );
	
	global $db;
	// $db = new PDO ( 'mysql:host=localhost;dbname=DataBaseName', 'UserName', 'Password' );
	
	try 
    {
        global $db;
	    $db = new PDO ( 'mysql:host=localhost;dbname=DataBaseName', 'UserName', 'Password' );
    }
    catch (PDOException $e) 
    {
        // error_log("Database error: " . $e->getMessage());
        // Handle error appropriately
        exit();
    }

	global $info_hash;
	$info_hash = urldecode ( $_GET['info_hash'] ?? '' );
	$info_hash = bin2hexsafe ( $info_hash );
	
	global $peer_id;
    $peer_id = urldecode ( $_GET['peer_id'] ?? '' );
	$peer_id = bin2hexsafe ( $peer_id );
	
	global $ip;
    $ip = $_SERVER['REMOTE_ADDR'];
	
	global $port;
    $port = $_GET['port'] ?? 0;
	
	global $uploaded;
    $uploaded = $_GET['uploaded'] ?? 0;
	
	global $downloaded;
    $downloaded = $_GET['downloaded'] ?? 0;
	
	global $left;
    $left = $_GET['left'] ?? 0;
    
    global $compact;
    $compact = $_GET['compact'] ?? 0;
    
    global $cronjob;
    $cronjob = $_GET['cronjob'] ?? '';
	
	
    
    if 
	( 
		strlen ( $info_hash ) >= 1
		AND
		strlen ( $peer_id ) >= 1
		AND
		intval ( $port ) >= 0 AND intval ( $port ) <= 65535
	)
	{
		echo QueryRecored();
	}
	else if ( strlen ( $cronjob ) >= 1 )
	{
	    $ip = $_SERVER['REMOTE_ADDR'];
        $actual_ip = gethostbyname('torrent.tracker.durukanbal.com');
        
        if ( $ip === $actual_ip )
        {
            RemoveOldData();
        }
        else
        {
            exit 
    		(
    		    bencode 
        		(  
        		    array
        		    (
        		        "failure reason" => "unauthorized action"
        		    )
        		)
        	);
        }
	}
	else
	{
		// echo "Bad Request !";
		exit 
		(
		    bencode 
    		(  
    		    array
    		    (
    		        "failure reason" => "info_hash, peer_id, port parameters are required, port parameter must be between 0 and 65535"
    		    )
    		)
    	);
	}

    function bencode ( $data ) 
    {	
        if ( is_string ( $data ) ) 
        {
            return strlen ( $data ) . ':' . $data;
        } 
        else if ( is_int ( $data ) ) 
        {
            return 'i' . $data . 'e';
        } 
        else if ( is_array ( $data ) ) 
        {
            if ( array_values ( $data ) === $data ) 
            {
                return 'l' . implode ( '', array_map ( 'bencode', $data ) ) . 'e';
            } 
            else 
            {
                $encoded_elements = array();
                foreach ( $data as $key => $value ) 
                {
                    $encoded_elements[] = bencode ( $key );
                    $encoded_elements[] = bencode ( $value );
                }
                return 'd' . implode ( '', $encoded_elements ) . 'e';
            }
        }
    
        return NULL;
    } // Function bencode
	
	function bin2hexsafe ( $hexString ) 
	{
		if ( ctype_xdigit ( $hexString ) ) 
		{
			return $hexString;
		} 
		else 
		{
			return bin2hex ( $hexString );
		}
	}
	
	function Response ( $info_hash )
	{
		global $db;
		global $compact;
		global $peers;
		
		try
		{
		    global $peers;
		    
    		$query = 
    		'
    			SELECT 
    				peer_id, 
    				ip, 
    				port 
    			FROM 
    				peers 
    			WHERE 
    				info_hash = ?
    		';
    		$stmt = $db->prepare ( $query );
    		$stmt->bindParam ( 1, $info_hash, PDO::PARAM_STR );
    		$stmt->execute();
    		$peers = $stmt->fetchAll ( PDO::FETCH_ASSOC );
		}
		catch (PDOException $e)
		{
		    exit 
    		(
    		    bencode 
        		(  
        		    array
        		    (
        		        "failure reason" => "Database error occurred"
        		    )
        		)
        	);
		}

        $filteredPeersIP4 = array_filter 
        ( 
            $peers, 
            function ( $peer ) 
            {
                return filter_var ( $peer['ip'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 );
            }
        );
        
        $filteredPeersIP6 = array_filter 
        ( 
            $peers, 
            function ( $peer ) 
            {
                return filter_var ( $peer['ip'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV6 );
            }
        );
        
        $response = array
    	(
    		'interval' => 10800
    		// 'min interval' => 900,  // Ekledim
    		// 'complete' => 0,        // Ekledim
    		// 'incomplete' => 2,      // Ekledim
    		// 'peers' => array ()
    	);

        if ( $compact == 0 )
        {
    		if ( !empty ( $filteredPeersIP4 ) ) 
    		{
    		    $response[] = array
    		    (
    		        'peers' => array_map 
        			( 
        				function ( $peer )
        				{
        					return array
        					(
        						'peer id' => hex2bin ( $peer['peer_id'] ),
        						'ip' => $peer['ip'],
        						'port' => intval ( $peer['port'] )
        					);
        				}, 
        				$filteredPeersIP4 
        			)
    		    );
    		}
    		
    		if ( !empty ( $filteredPeersIP6 ) ) 
    		{
    		    $response[] = array
    		    (
    		        'peers6' => array_map 
        			( 
        				function ( $peer )
        				{
        					return array
        					(
        						'peer id' => hex2bin ( $peer['peer_id'] ),
        						'ip' => $peer['ip'],
        						'port' => intval ( $peer['port'] )
        					);
        				}, 
        				$filteredPeersIP6 
        			)
    		    );
    		}
        }
        else
        {
    		if ( !empty ( $filteredPeersIP4 ) ) 
    		{
    		    $AllIP4PeerData = NULL;
    		    
    		    foreach ( $filteredPeersIP4 AS $index4 => $data4 )
    		    {
    		        $ip = $data4['ip'];
    		        $port = $data4['port'];
                    $ipParts = explode ( '.', $ip );
                    $ipBinary = chr ( $ipParts[0] ) . chr ( $ipParts[1] ) . chr ( $ipParts[2] ) . chr ( $ipParts[3] );
                    $portBinary = chr ( $port >> 8 ) . chr ( $port & 0xFF );
                    $peerBinary = $ipBinary . $portBinary;
                    $AllIP4PeerData .= $peerBinary;
    		    }
    		    
    		    $response['peers'] = $AllIP4PeerData;
    		}
    		
    		if ( !empty ( $filteredPeersIP6 ) ) 
    		{
    		    $AllIP6PeerData = NULL;
    		    
    		    foreach ( $filteredPeersIP6 AS $index6 => $data6 )
    		    {
    		        $ip = $data6['ip'];
    		        $port = $data6['port'];
    		        $ipParts = unpack ( "C*", inet_pton ( $ip ) );
                    $ipBinary = implode ( array_map ( "chr", $ipParts ) );
                    $portBinary = chr ( $port >> 8 ) . chr ( $port & 0xFF );
                    $peerBinary = $ipBinary . $portBinary;
                    $AllIP6PeerData .= $peerBinary;
    		    }
    		    
    		    $response['peers6'] = $AllIP6PeerData;
    		}
        }
		
		return bencode ( $response );
	}
	
	function RemoveOldData ()
	{
		global $db;
	
        try 
        {
            global $db;
            
            $truncateQuery = "TRUNCATE TABLE peers";
            $db->exec ( $truncateQuery );
        }
        catch (PDOException $e) 
        {
            // error_log("Database error: " . $e->getMessage());
            // Handle error appropriately
        }
		
	} // Function RemoveOldData
	
	function QueryRecored ()
	{
	    try 
        {
    		global $db;
    		global $info_hash;
    		global $peer_id;
    		global $ip;
    		global $port;
    		global $uploaded;
    		global $downloaded;
    		global $left;
    		
            $query = 
    		'
    			INSERT INTO peers 
    			(
    				info_hash, 
    				peer_id, 
    				ip, 
    				port, 
    				uploaded, 
    				downloaded, 
    				remaining
    			) 
    			VALUES 
    			(
    				:info_hash, 
    				:peer_id, 
    				:ip1, 
    				:port1, 
    				:uploaded1, 
    				:downloaded1, 
    				:left1
    			) ON DUPLICATE KEY UPDATE 
    				ip = :ip2,
    				port = :port2,
    				uploaded = :uploaded2,
    				downloaded = :downloaded2,
    				remaining = :left2
    		';
    
    		$stmt = $db->prepare($query);
    
    		// Insert
    		$stmt->bindParam(':info_hash', $info_hash, PDO::PARAM_STR);
    		$stmt->bindParam(':peer_id', $peer_id, PDO::PARAM_STR);
    		$stmt->bindParam(':ip1', $ip, PDO::PARAM_STR);
    		$stmt->bindParam(':port1', $port, PDO::PARAM_INT);
    		$stmt->bindParam(':uploaded1', $uploaded, PDO::PARAM_INT);
    		$stmt->bindParam(':downloaded1', $downloaded, PDO::PARAM_INT);
    		$stmt->bindParam(':left1', $left, PDO::PARAM_INT);
    
    		// Update
    		$stmt->bindParam(':ip2', $ip, PDO::PARAM_STR);
    		$stmt->bindParam(':port2', $port, PDO::PARAM_INT);
    		$stmt->bindParam(':uploaded2', $uploaded, PDO::PARAM_INT);
    		$stmt->bindParam(':downloaded2', $downloaded, PDO::PARAM_INT);
    		$stmt->bindParam(':left2', $left, PDO::PARAM_INT);
    
    		$stmt->execute();
        }
        catch (PDOException $e) 
        {
            // error_log("Database error: " . $e->getMessage());
            // Handle error appropriately
            exit();
        }
		
		return Response ( $info_hash );
	}
?>
-- phpMyAdmin SQL Dump
-- version 5.2.1
-- https://www.phpmyadmin.net/
--
-- Host: localhost:3306
-- Generation Time: May 16, 2024 at 10:38 PM
-- Server version: 8.0.37
-- PHP Version: 8.1.27

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `durukanbal_torrent_tracker`
--

-- --------------------------------------------------------

--
-- Table structure for table `peers`
--

CREATE TABLE `peers` (
  `info_hash` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
  `peer_id` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
  `ip` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
  `port` int NOT NULL,
  `uploaded` bigint NOT NULL,
  `downloaded` bigint NOT NULL,
  `remaining` bigint NOT NULL,
  `updated_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=MEMORY DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

--
-- Indexes for dumped tables
--

--
-- Indexes for table `peers`
--
ALTER TABLE `peers`
  ADD PRIMARY KEY (`info_hash`,`peer_id`);
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
Categories: PHP language

404 Comments

Leave a Reply

Avatar placeholder