Using Server URL:

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

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

<?php

    // exit("Service Shut Down");


	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;
	
	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 = intval ( $_GET['port'] ) ?? 0;
	
	global $uploaded;
    $uploaded = intval ( $_GET['uploaded'] ) ?? 0;
	
	global $downloaded;
    $downloaded = intval ( $_GET['downloaded'] ) ?? 0;
	
	global $left;
    $left = intval ( $_GET['left'] ) ?? 0;
    
    global $compact;
    $compact = intval ( $_GET['compact'] ) ?? 0;
    
	
    if 
	( 
		strlen ( $info_hash ) >= 1
		AND
		strlen ( $peer_id ) >= 1
		AND
		intval ( $port ) >= 0 AND intval ( $port ) <= 65535
	)
	{
        exit ( QueryRecored() );
	}
	else
	{
		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 bin2hexsafe
	
	function Response ( $interval = 10, $info_hash )
	{
		global $db;
		global $compact;
		global $peers;
		
		$peers = getPeers ( $db, $info_hash );

        $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' => intval ( $interval ) // bir sonraki talpe süresi uymak zorunlu değil 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 addPeer ( $memcache, $info_hash, $peer_id, $ip, $port, $uploaded, $downloaded, $remaining, $timeout = 3600 ) 
    {
        $peers = json_decode ( $memcache->get ( "peers:$info_hash" ), TRUE ) ?? [];
		
		foreach ( $peers AS $peer ) 
		{
			if ( $peer['peer_id'] === $peer_id ) 
			{
				return;
			}
		}

		$peers[] = array
    	(
            'ip' => $ip,
            'port' => $port,
            'uploaded' => $uploaded,
            'downloaded' => $downloaded,
            'remaining' => $remaining,
            'peer_id' => $peer_id
        );
        $memcache->set ( "peers:$info_hash", json_encode ( $peers ), 0, $timeout );
		
    } // Function addPeer
    
    function getPeers ( $memcache, $info_hash ) 
    {
        $peers = json_decode ( $memcache->get ( "peers:$info_hash" ), TRUE );
    
        return $peers;
    } // Function getPeers
	
	function QueryRecored ()
	{
        global $db;
    	global $info_hash;
    	global $peer_id;
    	global $ip;
    	global $port;
    	global $uploaded;
    	global $downloaded;
    	global $left;
    	
	    $memcache = new Memcache();
        $memcache->connect ( '127.0.0.1', 11211 ) OR exit 
        (
            bencode 
        	(  
        	    array
        	    (
        	        "failure reason" => "Database error occurred"
        	    )
        	)
        );
        
        $db=$memcache;
    	
    	
    	addPeer ( $db, $info_hash, $peer_id, $ip, $port, $uploaded, $downloaded, $left, 10000 ); // 72 saat 259200 saniyelik timeout - 10000 saniye 
		
		
		// return Response ( $DBRead = TRUE, $interval = 10800, $info_hash );
		return Response ( $interval = mt_rand ( 100, 10000 ), $info_hash );
	}
?>
Categories: PHP language

404 Comments

Leave a Reply

Avatar placeholder