Login
Username: Password:
Remember me

[Sign Up] / [Recover Password]

Theme / Language

Popular
New Torrents
new name
Size: 18.40 MB
Most active
new name
S: 74 L: 3
New Posters
Peer Connection Limiter
Controls ·  Search New posts · Mark unread

You are in: Forum / Forum / V4 / Downloads & Updates

2023-05-13 01:52:26
This is my last forum post. So just wanted to share the last task I was assigned with.
I'm too far ahead to be any use here. I love to help but I like to see activity and feedback too. Both of those are lacking unfortunately. Time is also not on my side going forward, which effects my own site in the process. I've managed a good balance.

Now, this code checks the number of connections a user has established on the tracker for the torrent their starting/resuming, and if the number of connections exceeds a certain limit, it takes actions to adjust or reduce the number of connections, notifies user and logs the event.

Its up to you to port it for your tracker. I dont do old-school conversions anymore, code is as-is.


if (array_key_exists('key', $data)) {
$max_connections = $user_row['max_conn_allowed'];
$torrent_max_connections = 50;
$reduced_connections = min(5, $torrent_max_connections);
$checkdata = $db->query_params("SELECT clientkey FROM peers WHERE user_id = :user_id AND torrent_id = :torrent_id AND seeder = 'no' AND completed = 0", array(
'user_id' => $user_id,
'torrent_id' => $torrent_id
));
$num_connections = count(array_unique(array_column($checkdata->fetchAll(), 'clientkey')));
if ($user_row['max_conn_increased'] === 0) {
if ($num_connections > $max_connections) {
$db->query_params("DELETE p FROM peers p LEFT JOIN (SELECT clientkey FROM peers WHERE user_id = :user_id ORDER BY RAND() LIMIT :limit) excluded ON p.clientkey = excluded.clientkey WHERE p.user_id = :user_id AND p.torrent_id = :torrent_id AND p.seeder = 'no' AND p.completed = 0 AND excluded.clientkey IS NOT NULL", array(
'user_id' => $user_id,
'torrent_id' => $torrent_id,
'limit' => $reduced_connections
));
$num_connections = $reduced_connections;
}
if ($num_connections > 0) {
header('X-Connection-Limit: ' . $num_connections);
$account->sendNotif($user_row['user_id'], 'Connections reduced (' . $num_connections . ')', 'Tracker');
$logevent = $db->query_params('INSERT INTO announce_log (message, added) VALUES (:message, NOW())', array(
'message' => 'Connections reduced (' . $num_connections . ')'
));
}
} else {
header('X-Connection-Limit: ' . $max_connections);
}
}



ALTER TABLE users ADD COLUMN max_conn_increased TINYINT DEFAULT 0 NULL;
ALTER TABLE users ADD COLUMN max_conn_allowed TINYINT DEFAULT 5 NULL;


This is a good idea for most, who dont want their server flooded with so many connections. This manages the clients connection pool and makes decisions based on unique client keys (connections). uTorrent generates a new client key per each connection. So we compare keys and deny or accept the connection while providing the appropriate headers to maintain limitation state.

This is at user-level, so insert code after passkey authentication logic. I'm aware people can find other ways to flood, but that is outside of the scope of this mod.
Last edited by STorrents2020 on 2023-05-13 05:42:06
2023-05-13 14:05:52
Torrents 2020, thank you for your help and support, I wish you a lot of luck and hope to see you again with us.

Amitier tiloup alise spiritblue ✌

the code recommend you in which file or to place it (torrents.php?)

// Peer Connection Limiter By STorrents2020 [2023-05-13]
if (array_key_exists('key', $data)) {
$max_connections = $user_row['max_conn_allowed'];
$torrent_max_connections = 50;
$reduced_connections = min(5, $torrent_max_connections);
$checkdata = $db->query_params("SELECT clientkey FROM peers WHERE user_id = :user_id AND torrent_id = :torrent_id AND seeder = 'no' AND completed = 0", array(
'user_id' => $user_id,
'torrent_id' => $torrent_id
));
$num_connections = count(array_unique(array_column($checkdata->fetchAll(), 'clientkey')));
if ($user_row['max_conn_increased'] === 0) {
if ($num_connections > $max_connections) {
$db->query_params("DELETE p FROM peers p LEFT JOIN (SELECT clientkey FROM peers WHERE user_id = :user_id ORDER BY RAND() LIMIT :limit) excluded ON p.clientkey = excluded.clientkey WHERE p.user_id = :user_id AND p.torrent_id = :torrent_id AND p.seeder = 'no' AND p.completed = 0 AND excluded.clientkey IS NOT NULL", array(
'user_id' => $user_id,
'torrent_id' => $torrent_id,
'limit' => $reduced_connections
));
$num_connections = $reduced_connections;
}
if ($num_connections > 0) {
header('X-Connection-Limit: ' . $num_connections);
$account->sendNotif($user_row['user_id'], 'Connections reduced (' . $num_connections . ')', 'Tracker');
$logevent = $db->query_params('INSERT INTO announce_log (message, added) VALUES (:message, NOW())', array(
'message' => 'Connections reduced (' . $num_connections . ')'
));
}
} else {
header('X-Connection-Limit: ' . $max_connections);
}
}
// End!

Last edited by spiritblue on 2023-05-13 14:39:49
2023-05-13 15:49:28
You are most welcome!

This code will go into announce.php

My server has been handling 1300 peers and peak load, while still slow its a big improvement compared to how the server was before I wrote this code. I gave it 12 hours (overnight), and it works great!

Thank you for the credit, for that I will do you a favor. Give me a little bit.

---

Open announce.php and find


$userid = 0;
if ($MEMBERSONLY){
//check passkey is valid, and get users details
$res = SQL_Query_exec("SELECT $userfields FROM users u INNER JOIN groups g ON u.class = g.group_id WHERE u.passkey=".sqlesc($passkey)." AND u.enabled = 'yes' AND u.status = 'confirmed' LIMIT 1") or err("Cannot Get User Details");
$user = mysqli_fetch_assoc($res);
if (!$user)
err("Cannot locate a user with that passkey!");
   if ($user["can_download"] == "no")
       err("You do not have permission to download.");
$userid = $user["id"]; //etc
}


and replace with


if ($MEMBERSONLY){
//check passkey is valid, and get users details
$res = SQL_Query_exec("SELECT $userfields FROM users u INNER JOIN groups g ON u.class = g.group_id WHERE u.passkey=".sqlesc($passkey)." AND u.enabled = 'yes' AND u.status = 'confirmed' LIMIT 1") or err("Cannot Get User Details");
$user = mysqli_fetch_assoc($res);
if (!$user)
err("Cannot locate a user with that passkey!");
   if ($user["can_download"] == "no")
       err("You do not have permission to download.");
$userid = $user["id"]; //etc

$torrent_id = 0;
$res2 = SQL_Query_exec("SELECT id FROM torrents WHERE info_hash = ".sqlesc($GLOBALS['info_hash'])) or err("Cannot Get Torrent Details");
$torrent = mysqli_fetch_assoc($res2);
$torrent_id = $torrent['id'];
}


Now the code is ready


if (array_key_exists('key', $data)) {
$max_connections = $site_config['max_conn_allowed']; // default: 5
$torrent_max_connections = 50;
$reduced_connections = min(5, $torrent_max_connections);

$user_id = sqlesc($userid);
$torrent_id = sqlesc($torrent_id);

$checkdata = mysqli_query($GLOBALS['DBconnector'], "SELECT clientkey FROM peers WHERE id = $user_id AND torrent_id = $torrent_id AND seeder = 'no' AND completed = 0");
$num_connections = mysqli_num_rows($checkdata);
$unique_clientkeys = array();
while ($row = mysqli_fetch_assoc($checkdata)) {
$unique_clientkeys[] = $row['clientkey'];
}
$num_unique_clientkeys = count(array_unique($unique_clientkeys));
if ($site_config['max_conn_increased'] === 0) {
if ($num_unique_clientkeys > $max_connections) {
mysqli_query($GLOBALS['DBconnector'], "DELETE p FROM peers p LEFT JOIN (SELECT clientkey FROM peers WHERE id = $user_id ORDER BY RAND() LIMIT $user_id) excluded ON p.clientkey = excluded.clientkey WHERE p.user_id = $user_id AND p.torrent = $torrent_id AND p.seeder = 'no' AND p.completed = 0 AND excluded.clientkey IS NOT NULL");
$num_connections = $reduced_connections;
}
if ($num_unique_clientkeys > 0) {
header('X-Connection-Limit: ' . $num_unique_clientkeys);
}
} else {
header('X-Connection-Limit: ' . $max_connections);
}
mysqli_close($GLOBALS['DBconnector']);
}


Add to site config file


'max_conn_allowed' = 5;
'max_conn_increased' = 0; // 0: manage connections, 1: no control (open)



Last edited by STorrents2020 on 2023-05-13 17:09:58
2023-05-13 18:06:07
thank you very much, small correction for config  👍

// Peer Connection Limiter
$site_config['max_conn_allowed'] = 5;
$site_config['max_conn_increased'] = 0; // 0: manage connections, 1: no control (open)


did you do a reboot of the server or a simple update of the bdd was enough ?
Last edited by spiritblue on 2023-05-13 18:08:21
2023-05-14 05:55:48
I rebooted because I wanted a fresh take at the adjustment. You do not have to reboot. Changes should be immediate. Just make sure your torrents are still downloading, and let me know if anything comes up of error.

Working on this end 100%. Automatically reducing some peers to 1-2 connections according to my logs. Server is faster.
2023-05-14 13:10:40
ok i will let you know if there is any error. thanks again for this great code  👍
Users online

No users online

Search



Statistics

    Torrents

  • Tracking: 3
  • Todays torrents: 0
  • Seeders: 0
  • Leechers: 0
  • Peers: 0
  • Downloaded: 89.96 GB
  • Uploaded: 9.86 GB

  • Members

  • We have: 838
  • New today: 0
  • Guest today: 9

  • Online

  • Total online: 2
  • Members: 0
  • Guests online: 2
  • Comments: 3
  • Messages: 1,728