Bravo List

Bravo List (http://www.bvlist.com/index.php)
-   SZ Edition (http://www.bvlist.com/forumdisplay.php?f=24)
-   -   Announce Fix For TBDev SZ 3.0+ (http://www.bvlist.com/showthread.php?t=470)

AutoSnipe 17th June 2008 07:11

Announce Fix For TBDev SZ 3.0+
 
hey guys....
heres a fully fixed up announce url for you :)
any questions.. ask :)

Code:

define('IN_ANNOUNCE', true);
$tracker_root_path = './';
require($tracker_root_path . 'include/config.php');
require($tracker_root_path . 'include/benc.php');
@include($tracker_root_path . '/languages/lang_' . $config['default_lang'] . '/lang_announce.php');

$db->sql_return_on_error(true);

$updateset = $events_ary = array();

//start receive and check main data from peer
$agent = ( isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : getenv('HTTP_USER_AGENT') );

foreach ( array('passkey','info_hash','peer_id') AS $x) {
        if( isset($_GET[$x]) ) {
                $GLOBALS[$x] = strval($_GET[$x]);
    }
}

if ( strpos($passkey, '?') ) {
        $tmp = substr($passkey, strpos($passkey, '?'));
        $passkey = substr($passkey, 0, strpos($passkey, '?'));
        $tmpname = substr($tmp, 1, strpos($tmp, '=')-1);
        $tmpvalue = substr($tmp, strpos($tmp, '=')+1);
        $GLOBALS[$tmpname] = $tmpvalue;
}

foreach ( array('passkey','info_hash','peer_id') AS $x) {
        if( !isset($x) ) {
            err(sprintf($lang['announce_missing_key'], $x) );
    }
}

foreach ( array('port','downloaded','uploaded','left') AS $x) {
        if ( isset($_GET[$x]) ) {
            $GLOBALS[$x] = 0 + $_GET[$x];
    }
        else {
            err(sprintf($lang['announce_missing_key'], $x) );
    }
}

preg_match_all('/event=([^&]*)/i', $_SERVER['QUERY_STRING'], $matches);
if ( sizeof($matches[1]) ) {
        $events_ary = $matches[1];
}

$compact = ( isset($_GET['compact']) ? 1 : 0 );

$no_peer_id = ( isset($_GET['no_peer_id']) ? 1 : 0 );

$rsize = 50;

foreach( array('num want', 'numwant', 'num_want') AS $k ) {
        if ( isset($_GET[$k]) ) {
                $rsize = intval($_GET[$k]);
          break;
    }
}

if ( !$port || $port > 0xffff ) {
        err($lang['announce_invalid_port']);
}

foreach ( array('info_hash','peer_id') AS $x ) {
        if ( strlen($GLOBALS[$x]) != 20 ) {
                    err(sprintf($lang['announce_invalid_parametr'], $x, strlen($GLOBALS[$x]), urlencode($GLOBALS[$x])));
            }
}

if ( strlen($passkey) != 32 ) {
        err(sprintf($lang['announce_invalid_passkey'], strlen($passkey), $passkey));
}
//end receive and check main data from peer

//start check bad clients
$bad_agents_arr = array('Gecko', 'Opera', 'MSIE', 'Links', 'Lynx', 'Mozilla', '0P3R4H');
for ( $i = 0; $i < sizeof($bad_agents_arr); ++$i ) {
        if ( strpos($agent, $bad_agents_arr[$i]) !== FALSE ) {
                err($lang['announce_bad_client']);
        }
}

if ( strpos($peer_id, 'Torrent/3') && $agent == '0' ) {
        err($lang['announce_bad_client']);
}

if(substr($peer_id, 0, 4) == 'exbc' || substr($peer_id, 0, 3) == '-BC' || substr($peer_id, 1, 2) == 'TS' || substr($peer_id,-4) == 'UDP0' || substr($peer_id, 0, 3) == '-FG') {
        err($lang['announce_client_banned']);
}
if(substr($peer_id, 0, 1) == 'A') {
    if(substr($peer_id, 1, 3) < 300) {
                err($lang['announce_plz_update_client']);
    }
}
// start anti trackerpro (cheaters)
if ($_SERVER['HTTP_ACCEPT_ENCODING'] == 'identity' && substr($peer_id, 0, 6) == 'M4-1-3') {
        err($lang['announce_bad_client']);
}
// end anti trackerpro (cheaters)

//end check bad clients

$sql_priority = ($db_type == 'mysql') ? ' LOW_PRIORITY' : '';

//start select userdata for user
$sql = 'SELECT uid, uploaded, downloaded, class, hiddentorrents, parked, can_leech, torrents_limit  FROM ' . USERS_TABLE . ' WHERE torrent_pass = ' . "'" . $db->sql_escape($passkey) . "'" . ' AND enabled = 1';
if( !($result = $db->sql_query($sql)) ) {
        err('user select error');
}
if ( !($userdata = $db->sql_fetchrow($result)) ) {
        err('Unknown passkey. Please redownload torrent from tracker');
}
$db->sql_freeresult($result);
//end select userdata for user

//start select data for torrent
$sql = 'SELECT fid, banned, seeders + leechers AS numpeers, seeders, ctime, hidden, free FROM ' . TORRENTS_TABLE . ' WHERE ' . hash_where('info_hash', $info_hash);
if( !($result = $db->sql_query($sql)) ) {
        err('torrent select error');
}
if ( !($torrentdata = $db->sql_fetchrow($result)) ) {
        err('Torrent not registered with this tracker');
}
$db->sql_freeresult($result);
//end select data for torrent

$fields = 'peer_id, `left`, ip, port, uploaded, downloaded, uid, mtime';

$limit = '';
if ( $torrentdata['numpeers'] > $rsize ) {
        $limit = ' LIMIT ' . $rsize;
}

$sql = 'SELECT ' . $fields . ' FROM ' . PEERS_TABLE . ' WHERE fid = ' . $torrentdata['fid'] . $limit;
if( !($result = $db->sql_query($sql)) ) {
$error = $db->sql_error();
  err('' . $error['message']);
}

$resp = 'd' . benc_str('interval') . 'i' . $config['announce_interval'] . 'e' . benc_str('min interval') . 'i' . $config['min_announce_interval'] . ( !$compact ? 'e' . benc_str('peers') . 'l' : 'e5:peers');
$peer = array();
$peer_num = 0;
while ( $row = $db->sql_fetchrow($result) ) {
        $row['peer_id'] = hash_pad($row['peer_id']);
        $peer_id_replaced = preg_replace('/ *$/s', '', $row['peer_id']);
        if ( $row['peer_id'] == $peer_id || $peer_id_replaced == $peer_id ) {
                $self = $row;
                continue;
        }
        if( !$compact ) {
                $resp .= 'd' . benc_str('ip') . benc_str($row['ip']);
                if ( !$no_peer_id ) {
                        $resp .= benc_str('peer id') . benc_str($row['peer_id']);
                }
                $resp .= benc_str('port') . 'i' . $row['port'] . 'ee';
        }
        else {
                $peer_ip = explode('.', $row['ip']);
                $peer_ip = pack('C*', $peer_ip[0], $peer_ip[1], $peer_ip[2], $peer_ip[3]);
                $peer_port = pack('n*', intval($row['port']));
                $time = intval((time() % 7680) / 60);
                if( !$left ) {
                        $time += 128;
                }
                $time = pack('C', $time);
                $peer[] = $time . $peer_ip . $peer_port;
                $peer_num++;
        }
}
$db->sql_freeresult($result);

if ( !$compact ) {
        $resp .= 'ee';
}
else {
        $o = '';
        for( $i = 0; $i < $peer_num; $i++ ) {
                $o .= substr($peer[$i], 1, 6);
        }
        $resp .= benc_str($o) . 'e';
}
$selfwhere = 'fid = ' . $torrentdata['fid'] . ' AND peer_id = ' . hash_where('peer_id', $peer_id);

if ( !isset($self) ) {
        $sql = 'SELECT ' . $fields . ' FROM ' . PEERS_TABLE . ' WHERE ' . $selfwhere;
        if( !($result = $db->sql_query($sql)) ) {
                err('peer select error');
        }
        if ( $row = $db->sql_fetchrow($result) ) {
                $self = $row;
        }
        $db->sql_freeresult($result);
}

if ( in_array('completed', $events_ary) ) {
        $sql = 'UPDATE ' . SNATCHED_TABLE . ' SET completedat = ' . time() . ' WHERE torrentid = ' . $torrentdata['fid'] . ' AND userid = ' . $userdata['uid'];
        if( !($db->sql_query($sql)) ) {
                err('Snatched compl error');
        }
        $updateset[] = 'completed = completed + 1';
}

//start user not in peers table, it's first connect. let's insert data
if ( !isset($self) ) {
        //start hidden torrent check
        if ( $torrentdata['hidden'] && ( !$userdata['hiddentorrents'] && $userdata['class'] < UC_MODERATOR ) ) {
                err('Hash not recognized with user! Hide!');
        }
        //end hidden torrent check

        //start seed & leech limits check
        $leech_count = $seed_count = 0;
        $sql = 'SELECT `left`, COUNT(*) AS c FROM ' . PEERS_TABLE . ' WHERE uid = ' . $userdata['uid'] . ' GROUP BY fid';
        $result = $db->sql_query($sql);
        if ( $row = $db->sql_fetchrow($result) ) {
                do {
                        if ( $row['left'] ) {
                                $leech_count = $leech_count + $row['c'];
                        }
                        else {
                                $seed_count = $seed_count + $row['c'];
                        }
                }
                while ( $row = $db->sql_fetchrow($result) );
        }
        $db->sql_freeresult($result);

        if ( $left ) {
                if ( !empty($userdata['torrents_limit']) && $userdata['can_leech'] && ( $leech_count >= $userdata['torrents_limit'] ) ) {
                        err('Vi kachaete slishkom mnogo torrentov! Odnovremenno mozhno kachat ' . $userdata['torrents_limit'] . ' torrentov');
                }
    }
        elseif ( !empty($config['max_torrent_allow_seed']) && ( $seed_count >= $config['max_torrent_allow_seed'] ) && $userdata['class'] < UC_UPLOADER ) {
                err('Vi sidiruete slishkom mnogo torrentov. Odnovremenno mozhno sidirovat ' . $config['max_torrent_allow_seed'] . ' torrentov');
    }
        //end seed & leech limits check

        //start wait time check
        if ( $config['waittime'] && $left && $userdata['class'] < UC_VIP ) {
            $gigs = $userdata['uploaded'] / (1024*1024*1024);
                $elapsed = floor((time() - $torrentdata['ctime']) / 3600);
                $ratio = (($userdata['downloaded'] > 0) ? ($userdata['uploaded'] / $userdata['downloaded']) : 1);
                if ($ratio < 0.5 || $gigs < 5) {
                        $wait = 48;
                }
                elseif ($ratio < 0.65 || $gigs < 6.5) {
                        $wait = 24;
                }
                elseif ($ratio < 0.8 || $gigs < 8) {
                        $wait = 12;
                }
                elseif ($ratio < 0.95 || $gigs < 9.5) {
                        $wait = 6;
                }
                else {
                    $wait = 0;
                }
                if ( $elapsed < $wait ) {
                        err('Vam nado podozhdat (' . ($wait - $elapsed) . ' chasov) - Prochtite FAQ!');
                }
        }
    //end wait time check

        //start parked check
        if ( $userdata['parked'] ) {
                err('Error, your account is parked! Please read the FAQ!');
    }
    //end parked check

    //start can leech check
    if ( !$userdata['can_leech'] && $left ) {
            err('Vi ne mozhete kachat torrenti.');
    }
    //end can leech check

        //start connect to port to know connectable user or not
        $connectable = 0;
        if ( $sockres = @fsockopen($user_ip, $port, $errno, $errstr, 5) ) {
                $connectable = 1;
                @fclose($sockres);
        }
        //end connect to port to know connectable user or not

        //start insert data into peers table
        $sql = 'INSERT INTO ' . PEERS_TABLE . ' ' . $db->sql_build_array('INSERT', array(
                        'connectable'  => $connectable,
                        'fid'              => $torrentdata['fid'],
                        'peer_id'      => $peer_id,
                        'ip'            => $user_ip,
                        'port'          => $port,
                        'uploaded'      => $uploaded,
                        'downloaded'    => $downloaded,
                        '`left`'        => $left,
                        'started'      => time(),
                        'mtime'        => time(),
                        'uid'          => $userdata['uid'],
                        'useragent'    => $agent ));

        if ( $db->sql_query($sql) ) {
                if ( !$left ) {
                        $updateset[] = 'seeders = seeders + 1';
                        if ( !$torrentdata['seeders'] ) {
                                $updateset[] = 'visible = 1';
                        }
                }
                else {
                        $updateset[] = 'leechers = leechers + 1';
                }
        }
        else {
            $error = $db->sql_error();
                if ( $error['code'] == 1062 ) { //we have unique key in database at fid-uid-ip rows
                        err('You can\'t leech or seed one torrent from one IP more than one time');
                }
                err('Insert peers error');
        }
        //end insert data into peers table

        //start try to insert data unto snatched table
        $sql = 'INSERT INTO ' . SNATCHED_TABLE . ' ' . $db->sql_build_array('INSERT', array(
                        'torrentid'    => $torrentdata['fid'],
                        'userid'        => $userdata['uid'],
                        'uploaded'      => $uploaded,
                        'downloaded'    => $downloaded,
                        'port'                => $port,
                        'last_action'  => time(),
                        'connectable'  => $connectable,
                        'sna_peer_id'  => $peer_id,
                        'sna_useragent' => $agent ));

        if( !($db->sql_query($sql)) ) {
                $error = $db->sql_error();
                if ( $error['code'] != 1062 ) { //we have unique key in database at torrentid-userid rows
                        err('Insert snatched error');
                    }
    }
    //end try to insert data unto snatched table or update it
}
//end user not in peers table, it's first connect. let's insert data

//start user already in peers table, it's first connect. let's update data
else {
        $upthis = max(0, $uploaded - $self['uploaded']);
        $downthis = max(0, $downloaded - $self['downloaded']);

        if ( ($upthis > 0 || $downthis > 0 ) && $userdata['class'] != UC_VIP) {
                $sql = 'UPDATE ' . USERS_TABLE . ' SET uploaded = uploaded + ' .  $upthis . ( !$torrentdata['free'] ?' , downloaded = downloaded + ' . $downthis : ''). ' WHERE uid = ' . $userdata['uid'];
                if( !($db->sql_query($sql)) ) {
                        err('Up/Down Stats Error');
                }
                // Initial sanity check xMB/s for 1 second
                if( $upthis > 2097152 && $self['mtime'] < time() ) {
                        //Work out time difference
                        $endtime = time();
                        $starttime = $self['mtime'];
                        $diff = ($endtime - $starttime);
                        //Normalise to prevent divide by zero.
                        $rate = ($upthis / ($diff + 1));
                        //Currently 2MB/s. Increase to 5MB/s once finished testing.
                        if ($rate > 2097152) {
                                if ($userdata['class'] < UC_MODERATOR) {
                                        $sql = 'INSERT INTO ' . CHEATERS_TABLE . ' ' . $db->sql_build_array('INSERT', array(
                                              'added'        => time(),
                                              'userid'        => $userdata['uid'],
                                              'rate'          => $rate,
                                              'beforeup'      => $userdata['uploaded'],
                                              'upthis'        => $upthis,
                                              'timediff'      => $diff,
                                              'userip'        => $user_ip,
                                              'torrentid'    => $torrentdata['fid'],
                                              'che_useragent' => $agent,
                                              'che_peer_id'  => $peer_id));
                                        if( !($db->sql_query($sql)) ) {
                                                err('Check Error');
                                        }
                                }
                        }
                }
        }

        $upd_sql = ( $upthis ? 'uploaded = uploaded + ' .  $upthis . ',' : '' );
        $upd_sql .= ( $downthis ? 'downloaded = downloaded + ' . $downthis . ',' : '' );
        $upd_sql .= ( $port <> $self['port'] ? 'port = ' . $port . ',' : '' );
        $sql = 'UPDATE' . $sql_priority . ' ' . SNATCHED_TABLE . ' SET ' . $upd_sql . ' last_action = ' . time() . ' WHERE torrentid = ' . $torrentdata['fid'] . ' AND userid = ' . $userdata['uid'];

        if( !($db->sql_query($sql)) ) {
                err('Update snatched error');
        }

        if ( !$left && $self['mtime'] < time() ) {
                $sql = 'SELECT COUNT(*) AS count FROM ' . PEERS_TABLE . ' WHERE `left` = 0 AND uid = ' . $userdata['uid'];
                $result = $db->sql_query($sql);
                $count = ( $row = $db->sql_fetchrow($result) ) ? intval($row['count']) : 0;
                $db->sql_freeresult($result);
                if ( $count ) {
                        $current_total_time = round( ( ( time() - $self['mtime'] ) / $count ) );
                        $sql = 'UPDATE ' . USERS_TABLE . ' SET total_seed_time = total_seed_time + ' . $current_total_time . ' WHERE uid = ' . $userdata['uid'];
                        if( !($db->sql_query($sql)) ) {
                                err('Total seedtime error');
                        }
                }
        }

        if ( in_array('stopped', $events_ary) ) {
                $sql = 'DELETE FROM ' . PEERS_TABLE . ' WHERE ' . $selfwhere;
                $result = $db->sql_query($sql);
                if ( $db->sql_affectedrows($result) ) {
                        if ( !$self['left'] ) {
                                $updateset[] = 'seeders = seeders - 1';
                                if ( $torrentdata['seeders'] == 1 ) {
                                        $updateset[] = 'visible = 0';
                                }
                        }
                        else {
                                $updateset[] = 'leechers = leechers - 1';
                        }
                }
        }
        else {
                $secs = time() - $self['mtime'];
                $uprate = ( $secs ? round($upthis / $secs) : 0 ); //we calculate upl speed for last session
                $downrate = ( $left ? ( $secs ? round($downthis / $secs) : 0 ) : 0 ); //we calculate down speed for last session

                $sql = 'UPDATE' . $sql_priority . ' ' . PEERS_TABLE . ' SET ' . $db->sql_build_array('UPDATE', array(
                      'uploaded'  => $uploaded,
                      'upspeed'    => $uprate,
                      'downloaded' => $downloaded,
                      'downspeed'  => $downrate,
                      'port'      => $port,
                      '`left`'    => $left,
                      'mtime'      => time() )) .
                      ' WHERE ' . $selfwhere;

                if( !($db->sql_query($sql)) ) {
                        err('Update peers error');
                }
                if ( $db->sql_affectedrows() && $self['left'] != $left && !$left ) {
                        $updateset[] = 'seeders = seeders + 1';
                        $updateset[] = 'leechers = leechers - 1';
                        if ( !$torrentdata['seeders'] ) {
                                $updateset[] = 'visible = 1';
                        }
                }
        }
}
//end user already in peers table, it's first connect. let's update data

if ( !$left ) {
        $updateset[] = 'mtime = ' . time();
}

if ( sizeof($updateset) ) {
        $sql = 'UPDATE' . $sql_priority . ' ' . TORRENTS_TABLE . ' SET ' . implode(',', $updateset) . ' WHERE fid = ' . $torrentdata['fid'];
        if( !($db->sql_query($sql)) ) {
                err('Update torrents error');
        }
}

benc_resp_raw($resp);
$db->sql_close();
?>


Fynnon 17th June 2008 20:39

Re: Announce Fix For TBDev SZ 3.0+
 
10x

Are you using this source?
if you know a tracker using this source i hope you don`t mind showing to us :ok:

AutoSnipe 18th June 2008 06:55

Re: Announce Fix For TBDev SZ 3.0+
 
had it running on my site for a little while, (my site is just getting started up...) but it wasn't really what i was looking for.. but i found where the bugs were in the announce so i did that and posted them...
i'm about to look at the new Yuna Scatari... so be prepared for a few bug fixes :)

Fynnon 18th June 2008 11:11

Re: Announce Fix For TBDev SZ 3.0+
 
Quote:

Originally Posted by AutoSnipe
i'm about to look at the new Yuna Scatari... so be prepared for a few bug fixes :)

Can`t wait :P

Nafania 26th June 2008 19:22

Re: Announce Fix For TBDev SZ 3.0+
 
Hmm, i see only one changed line, it's
Code:

    $selfwhere = 'fid = ' . $torrentdata['fid'] . ' AND peer_id = ' . hash_where('peer_id', $peer_id);
it was
Code:

$selfwhere = 'fid = ' . $torrentdata['fid'] . ' AND ' . hash_where('peer_id', $peer_id);
but this "fix" not correct and if you use it, you'll broke announce.php, because function hash_where return table name which added with this "fix"


All times are GMT +2. The time now is 10:36.

Powered by vBulletin® Version 3.8.11 Beta 3
Copyright ©2000 - 2024, vBulletin Solutions Inc.