Feature to ban specific torrent clients (#1175)

* Feature to ban specific torrent clients

* Code re-formatting for announce.php

---------

Co-authored-by: Roman Kelesidis <roman25052006.kelesh@gmail.com>
This commit is contained in:
Cønstantine Kovalensky 2023-11-30 09:26:04 +04:00 committed by GitHub
commit 3f0ce54877
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 35 additions and 13 deletions

View file

@ -65,6 +65,15 @@ if (strlen($peer_id) !== 20) {
msg_die('Invalid peer_id: ' . $peer_id);
}
// Check for client ban
if ($bb_cfg['client_ban']['enabled']) {
foreach (array_keys($bb_cfg['client_ban']['clients']) as $client) {
if (str_starts_with($peer_id, $client)) {
msg_die($bb_cfg['client_ban']['clients'][$client]);
}
}
}
// Verify info_hash
if (!isset($info_hash)) {
msg_die('info_hash was not provided');
@ -102,8 +111,7 @@ $ip = $_SERVER['REMOTE_ADDR'];
if (!$bb_cfg['ignore_reported_ip'] && isset($_GET['ip']) && $ip !== $_GET['ip']) {
if (!$bb_cfg['verify_reported_ip']) {
$ip = $_GET['ip'];
}
elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && preg_match_all('#\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}#', $_SERVER['HTTP_X_FORWARDED_FOR'], $matches)) {
} elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && preg_match_all('#\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}#', $_SERVER['HTTP_X_FORWARDED_FOR'], $matches)) {
foreach ($matches[0] as $x_ip) {
if ($x_ip === $_GET['ip']) {
if (!$bb_cfg['allow_internal_ip'] && preg_match("#(127\.([0-9]{1,3}\.){2}[0-9]{1,3}|10\.([0-9]{1,3}\.){2}[0-9]{1,3}|172\.[123][0-9]\.[0-9]{1,3}\.[0-9]{1,3}|192\.168\.[0-9]{1,3}\.[0-9]{1,3})#", $x_ip)) {
@ -243,8 +251,7 @@ if ($lp_info) {
if ($row = DB()->fetch_row($sql)) {
if ($seeder && $bb_cfg['tracker']['limit_seed_count'] && $row['active_torrents'] >= $bb_cfg['tracker']['limit_seed_count']) {
msg_die('Only ' . $bb_cfg['tracker']['limit_seed_count'] . ' torrent(s) allowed for seeding');
}
elseif (!$seeder && $bb_cfg['tracker']['limit_leech_count'] && $row['active_torrents'] >= $bb_cfg['tracker']['limit_leech_count']) {
} elseif (!$seeder && $bb_cfg['tracker']['limit_leech_count'] && $row['active_torrents'] >= $bb_cfg['tracker']['limit_leech_count']) {
msg_die('Only ' . $bb_cfg['tracker']['limit_leech_count'] . ' torrent(s) allowed for leeching' . $rating_msg);
}
}
@ -267,8 +274,7 @@ if ($lp_info) {
if ($row = DB()->fetch_row($sql)) {
if ($seeder && $bb_cfg['tracker']['limit_seed_ips'] && $row['ips'] >= $bb_cfg['tracker']['limit_seed_ips']) {
msg_die('You can seed only from ' . $bb_cfg['tracker']['limit_seed_ips'] . " IP's");
}
elseif (!$seeder && $bb_cfg['tracker']['limit_leech_ips'] && $row['ips'] >= $bb_cfg['tracker']['limit_leech_ips']) {
} elseif (!$seeder && $bb_cfg['tracker']['limit_leech_ips'] && $row['ips'] >= $bb_cfg['tracker']['limit_leech_ips']) {
msg_die('You can leech only from ' . $bb_cfg['tracker']['limit_leech_ips'] . " IP's");
}
}
@ -352,7 +358,7 @@ if ((!$lp_info || !$peer_info_updated) && !$stopped && empty($hybrid_unrecord))
// Exit if stopped
if ($stopped) {
silent_exit('Cache will be reset within 30 seconds');
dummy_exit();
}
// Store peer info in cache
@ -462,7 +468,6 @@ if (!$output) {
}
$output['external ip'] = inet_pton($ip);
$output['warning message'] = 'Statistics were updated';
// Return data to client
echo \Arokettu\Bencode\Bencode::encode($output);

View file

@ -69,7 +69,6 @@ function dummy_exit($interval = 1800, $cache_dict = [])
$output['complete'] = $cache_dict['complete'];
$output['incomplete'] = $cache_dict['incomplete'];
$output['downloaded'] = $cache_dict['downloaded'];
$output['warning message'] = 'Next statistics update in: ' . (floor($interval / 60) % 60) . ' minutes';
}
if (isset($cache_dict['peers'])) {

View file

@ -101,6 +101,16 @@ $bb_cfg['passkey_key'] = 'uk'; // Passkey key name in GET request
$bb_cfg['ignore_reported_ip'] = false; // Ignore IP reported by client
$bb_cfg['verify_reported_ip'] = true; // Verify IP reported by client against $_SERVER['HTTP_X_FORWARDED_FOR']
$bb_cfg['allow_internal_ip'] = false; // Allow internal IP (10.xx.. etc.)
$bb_cfg['client_ban'] = [
'enabled' => false,
// Clients to be blocked, for example, peer id '-UT' will block all uTorrent clients, '-UT2' will block builds starting with 2 (default: false)
// The second argument is being shown in the torrent client as a failure message
// Handy client list: https://github.com/transmission/transmission/blob/f85c3b6f8db95d5363f6ec38eee603f146c6adb6/libtransmission/clients.cc#L504
'clients' => [
'-UT' => "uTorrent — NOT ad-free and open-source",
'-MG' => 'Mostly leeching client'
]
];
// Ocelot
$bb_cfg['ocelot'] = [
@ -670,7 +680,8 @@ $bb_cfg['tracker'] = [
'search_by_tor_status' => true,
'freeleech' => false, // freeleech mode (If enabled, then disable "gold_silver_enabled")
'gold_silver_enabled' => true, // golden / silver days mode (If enabled, then disable "freeleech")
'disabled_v2_torrents' => false // allow registration of v2-only torrents
'disabled_v1_torrents' => false, // disallow registration of v1-only torrents, for future implementations where client will use v2 only and there won't be need for v1, relieving tracker
'disabled_v2_torrents' => false // disallow registration of v2-only torrents
];
// Ratio settings

View file

@ -1072,6 +1072,7 @@ $lang['BT_ADDED'] = 'Added';
$lang['BT_REG_ON_TRACKER'] = 'Register on tracker';
$lang['BT_REG_FAIL'] = 'Could not register torrent on tracker';
$lang['BT_REG_FAIL_SAME_HASH'] = 'Another torrent with same info_hash already <a href="%s"><b>registered</b></a>';
$lang['BT_V1_ONLY_DISALLOWED'] = 'v1-only torrents have been disabled by the administrator at the moment, allowed: v2 and hybrids';
$lang['BT_V2_ONLY_DISALLOWED'] = 'v2-only torrents have been disabled by the administrator at the moment, allowed: v1 and hybrids';
$lang['BT_V2_FILE_LIST_ONLY'] = 'Currently, only torrents with BitTorrent version 2 support are enabled for separate file listing';
$lang['BT_UNREG_FROM_TRACKER'] = 'Remove from tracker';

View file

@ -349,15 +349,21 @@ class Torrent
}
// Check if torrent contains info_hash v2 or v1
if (($info['meta version'] ?? null) == 2) {
if (is_array($info['file tree'] ?? null)) {
if (isset($info['meta version'], $info['file tree'])) {
if ($info['meta version'] === 2 && is_array($info['file tree'])) {
$bt_v2 = true;
}
}
if (isset($info['pieces'])) {
$bt_v1 = true;
}
if ($bb_cfg['tracker']['disabled_v2_torrents'] && isset($bt_v2) && !isset($bt_v1)) {
if ($bb_cfg['tracker']['disabled_v1_torrents'] && isset($bt_v1) && !isset($bt_v2)) {
self::torrent_error_exit($lang['BT_V1_ONLY_DISALLOWED']);
}
if ($bb_cfg['tracker']['disabled_v2_torrents'] && !isset($bt_v1) && isset($bt_v2)) {
self::torrent_error_exit($lang['BT_V2_ONLY_DISALLOWED']);
}