From 29cbe19c21e203b4cdfcffe7e61540e5e71cae0b Mon Sep 17 00:00:00 2001 From: xiaomlove Date: Tue, 30 May 2023 03:02:49 +0800 Subject: [PATCH] improe announce --- app/Console/Commands/Test.php | 4 +- app/Repositories/TorrentRepository.php | 14 ++++--- include/constants.php | 2 +- include/globalfunctions.php | 4 +- nexus/Plugin/BasePlugin.php | 1 + public/announce.php | 51 ++++++++++++++++++++++++-- 6 files changed, 64 insertions(+), 12 deletions(-) diff --git a/app/Console/Commands/Test.php b/app/Console/Commands/Test.php index dffc8664..dc65ca62 100644 --- a/app/Console/Commands/Test.php +++ b/app/Console/Commands/Test.php @@ -97,7 +97,9 @@ class Test extends Command */ public function handle() { - $r = NexusDB::cache_get("user_all_permissions:10001"); + $authkey = "12|52|abc"; + $subAuthkey = substr($authkey, 0, strrpos($authkey, "|")); + $r = $subAuthkey; dd($r); } diff --git a/app/Repositories/TorrentRepository.php b/app/Repositories/TorrentRepository.php index 7f74182c..dbee91b1 100644 --- a/app/Repositories/TorrentRepository.php +++ b/app/Repositories/TorrentRepository.php @@ -381,12 +381,14 @@ class TorrentRepository extends BaseRepository private function getTrackerReportAuthKeySecret($id, $uid, $initializeIfNotExists = false) { - $secret = TorrentSecret::query() - ->where('uid', $uid) - ->whereIn('torrent_id', [0, $id]) - ->orderBy('torrent_id', 'desc') - ->orderBy('id', 'desc') - ->first(); + $secret = NexusDB::remember("torrent_secret_{$uid}_{$id}", 3600, function () use ($id, $uid) { + return TorrentSecret::query() + ->where('uid', $uid) + ->whereIn('torrent_id', [0, $id]) + ->orderBy('torrent_id', 'desc') + ->orderBy('id', 'desc') + ->first(); + }); if ($secret) { return $secret->secret; diff --git a/include/constants.php b/include/constants.php index ea7c3d81..dc53c8e5 100644 --- a/include/constants.php +++ b/include/constants.php @@ -1,6 +1,6 @@ doAction(...func_get_args()); } -function isIPSeedBox($ip, $uid = null, $withoutCache = false): bool +function isIPSeedBox($ip, $uid, $withoutCache = false): bool { $key = "nexus_is_ip_seed_box:ip:$ip:uid:$uid"; $cacheData = \Nexus\Database\NexusDB::cache_get($key); @@ -995,6 +995,8 @@ function clear_user_cache($uid, $passkey = '') \Nexus\Database\NexusDB::cache_del("user_{$uid}_roles"); \Nexus\Database\NexusDB::cache_del("announce_user_passkey_$uid");//announce.php \Nexus\Database\NexusDB::cache_del(\App\Models\Setting::DIRECT_PERMISSION_CACHE_KEY_PREFIX . $uid); + \Nexus\Database\NexusDB::cache_del("user_role_ids:$uid"); + \Nexus\Database\NexusDB::cache_del("direct_permissions:$uid"); if ($passkey) { \Nexus\Database\NexusDB::cache_del('user_passkey_'.$passkey.'_content');//announce.php } diff --git a/nexus/Plugin/BasePlugin.php b/nexus/Plugin/BasePlugin.php index 86b96edc..afda0ca4 100644 --- a/nexus/Plugin/BasePlugin.php +++ b/nexus/Plugin/BasePlugin.php @@ -4,6 +4,7 @@ namespace Nexus\Plugin; use App\Repositories\BaseRepository; use Illuminate\Support\Arr; use Illuminate\Support\Facades\Artisan; +use Nexus\Database\NexusDB; abstract class BasePlugin extends BaseRepository { diff --git a/public/announce.php b/public/announce.php index 80f498be..6546d001 100644 --- a/public/announce.php +++ b/public/announce.php @@ -1,6 +1,47 @@ getRedis(); +$torrentNotExistsKey = "torrent_not_exists"; +$authKeyInvalidKey = "authkey_invalid"; +$passkeyInvalidKey = "passkey_invalid"; +if (!empty($_GET['authkey'])) { + $authkey = $_GET['authkey']; + $parts = explode("|", $authkey); + if (count($parts) != 3) { + err("authkey format error"); + } + $tid = $parts[0]; + $uid = $parts[1]; + $subAuthkey = sprintf("%s|%s", $tid, $uid); + if (!$redis->set($subAuthkey, TIMENOW, ['nx', 'ex' => 60])) { + $msg = "Request too frequent(a)"; + do_log("[ANNOUNCE] $msg"); + err($msg); + } + if ($redis->get("$authKeyInvalidKey:$authkey")) { + $msg = "Invalid authkey"; + do_log("[ANNOUNCE] $msg"); + err($msg); + } +} +if (!empty($_GET['passkey'])) { + $passkey = $_GET['passkey']; + if ($redis->get("$passkeyInvalidKey:$passkey")) { + $msg = "Passkey invalid"; + do_log("[ANNOUNCE] $msg"); + err($msg); + } +} +if (!empty($_GET['info_hash'])) { + $info_hash = $_GET['info_hash']; + if ($redis->get("$torrentNotExistsKey:$info_hash")) { + $msg = "Torrent not exists"; + do_log("[ANNOUNCE] $msg"); + err($msg); + } +} + //do_log(nexus_json_encode($_SERVER)); //1. BLOCK ACCESS WITH WEB BROWSERS AND CHEATS! $agent = $_SERVER["HTTP_USER_AGENT"] ?? ''; @@ -11,6 +52,7 @@ if (!empty($_REQUEST['authkey'])) { try { $_GET['passkey'] = get_passkey_by_authkey($_REQUEST['authkey']); } catch (\Exception $exception) { + $redis->set("$authKeyInvalidKey:".$_REQUEST['authkey'], TIMENOW, ['ex' => 3600*24]); err($exception->getMessage()); } } @@ -87,7 +129,10 @@ if (!$az = $Cache->get_value('user_passkey_'.$passkey.'_content')){ do_log("[check passkey], currentUser: " . nexus_json_encode($az)); $Cache->cache_value('user_passkey_'.$passkey.'_content', $az, 3600); } -if (!$az) err("Invalid passkey! Re-download the .torrent from $BASEURL"); +if (!$az) { + $redis->set("$passkeyInvalidKey:$passkey", TIMENOW, ['ex' => 24*3600]); + err("Invalid passkey! Re-download the .torrent from $BASEURL"); +} $userid = intval($az['id'] ?? 0); unset($GLOBALS['CURUSER']); $CURUSER = $GLOBALS["CURUSER"] = $az; @@ -138,7 +183,7 @@ if (!$torrent) { $end = strpos($queryString, "&", $start); $infoHashUrlEncode = substr($queryString, $start, $end - $start); do_log("[TORRENT NOT EXISTS] $checkTorrentSql, params: $queryString, infoHashUrlEncode: $infoHashUrlEncode"); - + $redis->set("$torrentNotExistsKey:$info_hash", TIMENOW, ['ex' => 24*3600]); err("torrent not registered with this tracker"); } if ($torrent['banned'] == 'yes') { @@ -224,7 +269,7 @@ foreach(['info_hash', 'passkey', 'peer_id'] as $lockField) { $lockString = http_build_query($lockParams); $lockKey = "isReAnnounce:" . md5($lockString); $log .= ", [CHECK_RE_ANNOUNCE], lockString: $lockString, lockKey: $lockKey"; -$redis = $Cache->getRedis(); + if (!$redis->set($lockKey, TIMENOW, ['nx', 'ex' => 5])) { do_log("$log, [YES_RE_ANNOUNCE]"); benc_resp($rep_dict);