/ announce.php?info_hash =% c4N % 8f % cc % f4 % e5lz % af % 7b % b6M % c0 % cez % da % 25 % ac % e7 % 8f & peer_id =- UT2000 - xGv % adq % 1b % b7 % e4 % 29 % a1 % f1 % ad & port = 57911 & uploaded = 0 & downloaded = 0 & left = 2335117312 & corrupt = 0 & key = AEC92B93 & event = started & numwant = 200 & compact = 1 & no_peer_id = 1
- CREATE TABLE IF NOT EXISTS `peers` (
- `info _ hash` binary ( 20 ) NOT NULL ,
- `ip` int ( 11 ) NOT NULL ,
- `port` smallint ( 5 ) unsigned NOT NULL ,
- `peer _ id` binary ( 20 ) NOT NULL ,
- `uploaded` bigint ( 20 ) unsigned NOT NULL default '0' ,
- `downloaded` bigint ( 20 ) unsigned NOT NULL default '0' ,
- `left` bigint ( 20 ) unsigned NOT NULL default '0' ,
- `update _ time` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP ,
- `expire _ time` timestamp NOT NULL default '0000-00-00 00:00:00' ,
- PRIMARY KEY ( `info _ hash` , ` ip` , `port` )
- ) ENGINE = MEMORY DEFAULT CHARSET = cp1251 ;
- <? php
- $ db_server = 'localhost' ;
- $ db_user = 'user base' ;
- $ db_pass = 'User Password' ;
- $ db_db = 'Name byzy' ;
- $ db_table = 'peers' ; // data storage table
- $ min_announce_interval = 900 ; // about this variable in the next post
- $ max_announce_rate = 500 ; // about this variable in the next post
- $ expire_factor = 1.2 ; // about this variable in the next post
- $ scrape_factor = 0.5 ; // about this variable in the next post
- $ require_announce_protocol = 'standard' ; // about this variable in the next post
- ?>
- <?
- require_once 'bencoding.php' ;
- ?>
- <? php
- function errorexit ( $ reason ) {
- exit ( bencode ( array ( 'failure reason' => $ reason ) ) ) ;
- }
- ?>
- <? php
- function resolve_ip ( $ host ) {
- $ ip = ip2long ( $ host ) ;
- if ( ( $ ip === false ) || ( $ ip == - 1 ) ) {
- $ ip = ip2long ( gethostbyname ( $ host ) ) ;
- if ( ( $ ip === false ) || ( $ ip == - 1 ) ) {
- return false ;
- }
- }
- return $ ip ;
- }
- ?>
- <? php
- header ( 'Content-Type: text / plain' ) ;
- ?>
- <? php
- if ( empty ( $ _GET [ 'info_hash' ] ) || empty ( $ _GET [ 'port' ] ) || ! is_numeric ( $ _GET [ 'port' ] ) || empty ( $ _GET [ 'peer_id' ] ) | | ! isset ( $ _GET [ 'uploaded' ] ) || ! is_numeric ( $ _GET [ 'uploaded' ] ) || ! isset ( $ _GET [ 'downloaded' ] ) || ! is_numeric ( $ _GET [ 'downloaded' ] ) || ! isset ( $ _GET [ 'left' ] ) || ! is_numeric ( $ _GET [ 'left' ] ) || ( ! empty ( $ _GET [ 'event' ] ) && ( $ _GET [ 'event' ] ! = 'started' ) && ( $ _GET [ 'event' ] ! = 'completed' ) && ( $ _GET [ 'event' ] ! = 'stopped' ) ) ) {
- errorexit ( 'invalid request (see bitconjurer.org/BitTorrent/protocol.html)' ) ;
- }
- ?>
- <? php
- if ( $ require_announce_protocol == 'no_peer_id' ) {
- if ( empty ( $ _GET [ 'compact' ] ) && empty ( $ _GET [ 'no_peer_id' ] ) ) {
- errorexit ( 'standard announces not allowed; use no_peer_id or compact option' ) ;
- }
- }
- else if ( $ require_announce_protocol == 'compact' ) {
- if ( empty ( $ _GET [ 'compact' ] ) ) {
- errorexit ( 'tracker requires use of compact option' ) ;
- }
- }
- $ ip = resolve_ip ( empty ( $ _GET [ 'ip' ] ) ? $ _SERVER [ 'REMOTE_ADDR' ] : $ _GET [ 'ip' ] ) ;
- if ( $ ip === false ) {
- errorexit ( "unable to resolve host name $ _GET [ip] " ) ;
- }
- ?>
- <? php
- @ mysql_pconnect ( $ db_server , $ db_user , $ db_pass ) or errorexit ( 'Connection failed' ) ;
- @ mysql_select_db ( $ db_db ) or errorexit ( 'Error base' ) ;
- $ query = @ mysql_query ( "SELECT COUNT (*) FROM` $ db_table `WHERE` expire_time`> NOW (); " ) or errorexit ( 'database error' ) ;
- $ num_peers = mysql_result ( $ query , 0 ) ;
- $ query = @ mysql_query ( "SELECT COUNT (*) FROM` $ db_table `WHERE` update_time`> NOW () - INTERVAL 1 MINUTE; " ) or errorexit ( 'database error' ) ;
- $ announce_rate = mysql_result ( $ query , 0 ) ;
- $ announce_interval = max ( $ num_peers * $ announce_rate / ( $ max_announce_rate * $ max_announce_rate ) * 60 , $ min_announce_interval ) ;
- ?>
- <? php
- if ( ! empty ( $ _GET [ 'event' ] ) && ( $ _GET [ 'event' ] == 'stopped' ) ) {
- $ expire_time = 0 ;
- }
- else {
- $ expire_time = $ announce_interval * $ expire_factor ;
- }
- ?>
- <? php
- $ columns = `` info_hash`, `ip`,` port`, `peer_id`,` uploaded`, `downloaded`,` left`, `expire_time` ' ;
- $ values = '\' '. mysql_escape_string ($ _ GET [' info_hash ']). ' \ ', ' . $ ip . ',' . $ _GET [ 'port' ] . ', \' '. mysql_escape_string ($ _ GET [' peer_id ']). ' \ ', ' . $ _GET [ 'uploaded' ] . ',' . $ _GET [ 'downloaded' ] . ',' . $ _GET [ 'left' ] . ", NOW () + INTERVAL $ expire_time SECOND" ;
- @ mysql_query ( "REPLACE INTO` $ db_table `( $ columns ) VALUES ( $ values );" ) or errorexit ( 'database error' ) ;
- ?>
- <? php
- $ peers = array ( ) ;
- $ numwant = empty ( $ _GET [ 'numwant' ] ) ? 50 : intval ( $ _GET [ 'numwant' ] ) ;
- $ query = @ mysql_query ( "SELECT` ip`, `port`,` peer_id` FROM ` $ db_table` WHERE `info_hash` = '" . mysql_escape_string ( $ _GET [ ' info_hash ' ] ) . "' AND` expire_time`> NOW () ORDER BY RAND () LIMIT $ numwant ; " ) or errorexit ( 'database error' ) ;
- if ( ! empty ( $ _REQUEST [ 'compact' ] ) ) {
- $ peers = " ;
- while ( $ array = mysql_fetch_assoc ( $ query ) ) {
- $ peers . = pack ( 'Nn' , $ array [ 'ip' ] , $ array [ 'port' ] ) ;
- }
- }
- else if ( ! empty ( $ _REQUEST [ 'no_peer_id' ] ) ) {
- while ( $ array = mysql_fetch_assoc ( $ query ) ) {
- $ peers [ ] = array ( 'ip' => long2ip ( $ array [ 'ip' ] ) , 'port' => intval ( $ array [ 'port' ] ) ) ;
- }
- }
- else {
- while ( $ array = mysql_fetch_assoc ( $ query ) ) {
- $ peers [ ] = array ( 'ip' => long2ip ( $ array [ 'ip' ] ) , 'port' => intval ( $ array [ 'port' ] ) , 'peer id' => $ array [ 'peer_id ' ] ) ;
- }
- }
- ?>
<?php
exit(bencode(array('interval' => intval($announce_interval), 'peers' => $peers)));
?>
Source: https://habr.com/ru/post/96880/
All Articles