diff --git a/app/Console/Commands/Test.php b/app/Console/Commands/Test.php index 9968c204..fbc619e0 100644 --- a/app/Console/Commands/Test.php +++ b/app/Console/Commands/Test.php @@ -95,8 +95,10 @@ class Test extends Command */ public function handle() { - $logFile = format_description('[img=http://www.baidu.com][img]http://www.baidu.com[/img]'); - dd($logFile); + $r = log(10); + $r2 = exp(10); + + dd($r, $r2); } } diff --git a/app/Repositories/TorrentRepository.php b/app/Repositories/TorrentRepository.php index 2a3973e0..ae7363e3 100644 --- a/app/Repositories/TorrentRepository.php +++ b/app/Repositories/TorrentRepository.php @@ -706,7 +706,8 @@ HTML; public function removeDuplicateSnatch() { $size = 2000; - $stickyPromotionExists = NexusDB::hasTable(''); + $stickyPromotionParticipatorsTable = 'sticky_promotion_participators'; + $stickyPromotionExists = NexusDB::hasTable($stickyPromotionParticipatorsTable); while (true) { $snatchRes = NexusDB::select("select userid, torrentid, group_concat(id) as ids from snatched group by userid, torrentid having(count(*)) > 1 limit $size"); if (empty($snatchRes)) { @@ -723,6 +724,10 @@ HTML; do_log("[DELETE_DUPLICATED_SNATCH], torrent: $torrentId, user: $userId, snatchIdStr: $delIdStr"); NexusDB::statement("delete from snatched where id in ($delIdStr)"); NexusDB::statement("update claims set snatched_id = $remainId where torrent_id = $torrentId and uid = $userId"); + NexusDB::statement("update hit_and_runs set snatched_id = $remainId where torrent_id = $torrentId and uid = $userId"); + if ($stickyPromotionExists) { + NexusDB::statement("update $stickyPromotionParticipatorsTable set snatched_id = $remainId where torrent_id = $torrentId and uid = $userId"); + } } } } diff --git a/database/migrations/2022_04_18_030257_handle_peers_peer_id_unique.php b/database/migrations/2022_04_18_030257_handle_peers_peer_id_unique.php index a99986aa..f0e7d848 100644 --- a/database/migrations/2022_04_18_030257_handle_peers_peer_id_unique.php +++ b/database/migrations/2022_04_18_030257_handle_peers_peer_id_unique.php @@ -27,10 +27,10 @@ return new class extends Migration DB::statement($sql); } - $sql = "alter table peers add index idx_torrent_peer(`torrent`, `peer_id`(20))"; + $sql = "alter table $tableName add index idx_torrent_peer(`torrent`, `peer_id`(20))"; DB::statement($sql); - $sql = "alter table peers add index idx_peer(`peer_id`(20))"; + $sql = "alter table $tableName add index idx_peer(`peer_id`(20))"; DB::statement($sql); } diff --git a/database/migrations/2023_03_29_021950_handle_snatched_user_torrent_unique.php b/database/migrations/2023_03_29_021950_handle_snatched_user_torrent_unique.php new file mode 100644 index 00000000..d77fac0a --- /dev/null +++ b/database/migrations/2023_03_29_021950_handle_snatched_user_torrent_unique.php @@ -0,0 +1,49 @@ +Column_name, ['torrentid', 'userid'])) { + if ($item->Non_unique == 0) { + return; + } + $indexToDrop[$item->Key_name] = "drop index " . $item->Key_name; + } + } + if (!empty($indexToDrop)) { + $sql = sprintf("alter table %s %s", $tableName, implode(', ', $indexToDrop)); + DB::statement($sql); + } + + $sql = "alter table $tableName add unique unique_torrent_user(`torrentid`, `userid`)"; + DB::statement($sql); + + $sql = "alter table $tableName add index idx_user(`userid`)"; + DB::statement($sql); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + // + } +}; diff --git a/database/migrations/2023_04_01_005409_add_unique_torrent_peer_user_to_peers_table.php b/database/migrations/2023_04_01_005409_add_unique_torrent_peer_user_to_peers_table.php new file mode 100644 index 00000000..908ed19b --- /dev/null +++ b/database/migrations/2023_04_01_005409_add_unique_torrent_peer_user_to_peers_table.php @@ -0,0 +1,41 @@ +Key_name == $toDropIndex) { + DB::statement("alter table $tableName drop index $toDropIndex"); + break; + } + } + DB::statement("alter table $tableName add unique unique_torrent_peer_user(`torrent`, `peer_id`, `userid`)"); + + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('peers', function (Blueprint $table) { + // + }); + } +}; diff --git a/nexus/Install/Update.php b/nexus/Install/Update.php index f6ddb6d7..3421bc0d 100644 --- a/nexus/Install/Update.php +++ b/nexus/Install/Update.php @@ -22,6 +22,7 @@ use App\Repositories\ExamRepository; use App\Repositories\SearchBoxRepository; use App\Repositories\TagRepository; use App\Repositories\ToolRepository; +use App\Repositories\TorrentRepository; use Carbon\Carbon; use GuzzleHttp\Client; use Illuminate\Database\Eloquent\Model; @@ -289,6 +290,12 @@ class Update extends Install } } + if (!$this->isSnatchedTableTorrentUserUnique()) { + $torrentRep = new TorrentRepository(); + $torrentRep->removeDuplicateSnatch(); + $this->runMigrate('database/migrations/2023_03_29_021950_handle_snatched_user_torrent_unique.php'); + } + } public function runExtraMigrate() @@ -481,6 +488,18 @@ class Update extends Install } } + private function isSnatchedTableTorrentUserUnique(): bool + { + $tableName = 'snatched'; + $result = NexusDB::select('show index from ' . $tableName); + foreach ($result as $item) { + if (in_array($item->Column_name, ['torrentid', 'userid']) && $item->Non_unique == 0) { + return true; + } + } + return false; + } + diff --git a/public/announce.php b/public/announce.php index 205f1d38..7de2eff0 100644 --- a/public/announce.php +++ b/public/announce.php @@ -177,7 +177,7 @@ if ($newnumpeers > $rsize) else $limit = ""; $announce_wait = \App\Repositories\TrackerRepository::MIN_ANNOUNCE_WAIT_SECOND; -$fields = "seeder, peer_id, ip, ipv4, ipv6, port, uploaded, downloaded, last_action, UNIX_TIMESTAMP(last_action) as last_action_unix_timestamp, prev_action, (".TIMENOW." - UNIX_TIMESTAMP(last_action)) AS announcetime, UNIX_TIMESTAMP(prev_action) AS prevts"; +$fields = "id, seeder, peer_id, ip, ipv4, ipv6, port, uploaded, downloaded, userid, last_action, UNIX_TIMESTAMP(last_action) as last_action_unix_timestamp, prev_action, (".TIMENOW." - UNIX_TIMESTAMP(last_action)) AS announcetime, UNIX_TIMESTAMP(prev_action) AS prevts"; //$peerlistsql = "SELECT ".$fields." FROM peers WHERE torrent = ".$torrentid." AND connectable = 'yes' ".$only_leech_query.$limit; /** * return all peers,include connectable no @@ -230,7 +230,7 @@ if (isset($event) && $event == "stopped") { $row["peer_id"] = hash_pad($row["peer_id"]); // $peer_id is the announcer's peer_id while $row["peer_id"] is randomly selected from the peers table - if ($row["peer_id"] === $peer_id) { + if ($row["peer_id"] === $peer_id && $row['userid'] == $userid) { $self = $row; continue; } @@ -275,7 +275,7 @@ if (isset($event) && $event == "stopped") { } } } -$selfwhere = "torrent = $torrentid AND " . hash_where("peer_id", $peer_id); +$selfwhere = "torrent = $torrentid AND " . hash_where("peer_id", $peer_id) . " AND userid = $userid"; //no found in the above random selection if (!isset($self)) { @@ -497,12 +497,12 @@ if (!isset($event)) $event = ""; if (isset($self) && $event == "stopped") { - sql_query("DELETE FROM peers WHERE $selfwhere") or err("D Err"); - if (mysql_affected_rows()) + sql_query("DELETE FROM peers WHERE id = {$self['id']}") or err("D Err"); + if (mysql_affected_rows() && !empty($snatchInfo)) { // $updateset[] = ($self["seeder"] == "yes" ? "seeders = seeders - 1" : "leechers = leechers - 1"); $hasChangeSeederLeecher = true; - sql_query("UPDATE snatched SET uploaded = uploaded + $trueupthis, downloaded = downloaded + $truedownthis, to_go = $left, $announcetime, last_action = ".$dt." WHERE torrentid = $torrentid AND userid = $userid") or err("SL Err 1"); + sql_query("UPDATE snatched SET uploaded = uploaded + $trueupthis, downloaded = downloaded + $truedownthis, to_go = $left, $announcetime, last_action = ".$dt." WHERE id = {$snatchInfo['id']}") or err("SL Err 1"); } } elseif(isset($self)) @@ -516,7 +516,7 @@ elseif(isset($self)) $updateset[] = "times_completed = times_completed + 1"; } - sql_query("UPDATE peers SET ip = ".sqlesc($ip).", port = $port, uploaded = $uploaded, downloaded = $downloaded, to_go = $left, prev_action = last_action, last_action = $dt, seeder = '$seeder', agent = ".sqlesc($agent).", is_seed_box = ". intval($isIPSeedBox) . " $finished $peerIPV46 WHERE $selfwhere") or err("PL Err 1"); + sql_query("UPDATE peers SET ip = ".sqlesc($ip).", port = $port, uploaded = $uploaded, downloaded = $downloaded, to_go = $left, prev_action = last_action, last_action = $dt, seeder = '$seeder', agent = ".sqlesc($agent).", is_seed_box = ". intval($isIPSeedBox) . " $finished $peerIPV46 WHERE id = {$self['id']}") or err("PL Err 1"); if (mysql_affected_rows()) { @@ -525,7 +525,7 @@ elseif(isset($self)) $hasChangeSeederLeecher = true; } if (!empty($snatchInfo)) { - sql_query("UPDATE snatched SET uploaded = uploaded + $trueupthis, downloaded = downloaded + $truedownthis, to_go = $left, $announcetime, last_action = ".$dt." $finished_snatched WHERE torrentid = $torrentid AND userid = $userid") or err("SL Err 2"); + sql_query("UPDATE snatched SET uploaded = uploaded + $trueupthis, downloaded = downloaded + $truedownthis, to_go = $left, $announcetime, last_action = ".$dt." $finished_snatched WHERE id = {$snatchInfo['id']}") or err("SL Err 2"); do_action('snatched_saved', $torrent, $snatchInfo); if ($event == 'completed' && $az['class'] < \App\Models\HitAndRun::MINIMUM_IGNORE_USER_CLASS && !$isDonor && isset($torrent['mode'])) { //think about H&R @@ -567,11 +567,12 @@ else { // $updateset[] = ($seeder == "yes" ? "seeders = seeders + 1" : "leechers = leechers + 1"); $hasChangeSeederLeecher = true; - $check = @mysql_fetch_row(@sql_query("SELECT COUNT(*) FROM snatched WHERE torrentid = $torrentid AND userid = $userid")); - if (!$check['0']) +// $check = @mysql_fetch_row(@sql_query("SELECT COUNT(*) FROM snatched WHERE torrentid = $torrentid AND userid = $userid")); + $checkSnatchedRes = mysql_fetch_assoc(sql_query("SELECT id FROM snatched WHERE torrentid = $torrentid AND userid = $userid limit 1")); + if (empty($checkSnatchedRes['id'])) sql_query("INSERT INTO snatched (torrentid, userid, ip, port, uploaded, downloaded, to_go, startdat, last_action) VALUES ($torrentid, $userid, ".sqlesc($ip).", $port, $uploaded, $downloaded, $left, $dt, $dt)") or err("SL Err 4"); else - sql_query("UPDATE snatched SET to_go = $left, last_action = ".$dt ." WHERE torrentid = $torrentid AND userid = $userid") or err("SL Err 3.1"); + sql_query("UPDATE snatched SET to_go = $left, last_action = ".$dt ." WHERE id = {$checkSnatchedRes['id']}") or err("SL Err 3.1"); } } catch (\Exception $exception) { do_log("[INSERT PEER] error: " . $exception->getMessage());