From d5e81ac0096b8d24f54d477b0b3f8d2ca72d88f8 Mon Sep 17 00:00:00 2001 From: xiaomlove Date: Mon, 12 Sep 2022 20:00:07 +0800 Subject: [PATCH] add is_seed_box to peers --- app/Models/Peer.php | 2 +- app/Repositories/SeedBoxRepository.php | 10 +++- app/Repositories/TrackerRepository.php | 6 +++ ..._181952_add_is_seed_box_to_peers_table.php | 32 +++++++++++++ include/constants.php | 4 +- include/functions.php | 11 ++--- public/announce.php | 11 +++-- public/viewpeerlist.php | 48 ++++++++++++++----- 8 files changed, 99 insertions(+), 25 deletions(-) create mode 100644 database/migrations/2022_09_12_181952_add_is_seed_box_to_peers_table.php diff --git a/app/Models/Peer.php b/app/Models/Peer.php index aa585799..6243e55c 100644 --- a/app/Models/Peer.php +++ b/app/Models/Peer.php @@ -11,7 +11,7 @@ class Peer extends NexusModel protected $fillable = [ 'torrent', 'peer_id', 'ip', 'port', 'uploaded', 'downloaded', 'to_go', 'seeder', 'started', 'last_action', 'prev_action', 'connectable', 'userid', 'agent', 'finishedat', 'downloadoffset', 'uploadedoffset', 'passkey', - 'ipv4', 'ipv6', + 'ipv4', 'ipv6', 'is_seed_box' ]; const CONNECTABLE_YES = 'yes'; diff --git a/app/Repositories/SeedBoxRepository.php b/app/Repositories/SeedBoxRepository.php index a29a8d7f..13666832 100644 --- a/app/Repositories/SeedBoxRepository.php +++ b/app/Repositories/SeedBoxRepository.php @@ -138,12 +138,20 @@ class SeedBoxRepository extends BaseRepository } foreach (Arr::wrap($ipArr) as $ip) { if ((isIPV4($ip) || isIPV6($ip)) && $enableSeedBox && isIPSeedBox($ip, $uid)) { - return ''; + return $this->getSeedBoxIcon(); } } return ''; } + public function getSeedBoxIcon($isSeedBox = true): string + { + if (!$isSeedBox) { + return ''; + } + return ''; + } + private function clearCache() { return true; diff --git a/app/Repositories/TrackerRepository.php b/app/Repositories/TrackerRepository.php index 73edf972..251151be 100644 --- a/app/Repositories/TrackerRepository.php +++ b/app/Repositories/TrackerRepository.php @@ -841,11 +841,16 @@ class TrackerRepository extends BaseRepository 'agent' => $queries['user_agent'], 'connectable' => $this->getConnectable($queries['ip'], $queries['port'], $queries['user_agent']) ]; + $isSeedBox = false; if (!empty($queries['ipv4'])) { $update['ipv4'] = $queries['ipv4']; + $isSeedBox = isIPSeedBox($queries['ipv4'], $peer->userid); } if (!empty($queries['ipv6'])) { $update['ipv6'] = $queries['ipv6']; + if (!$isSeedBox) { + $isSeedBox = isIPSeedBox($queries['ipv6'], $peer->userid); + } } if ($peer->exists) { @@ -865,6 +870,7 @@ class TrackerRepository extends BaseRepository $update['last_action'] = $nowStr; $update['uploaded'] = $queries['uploaded']; $update['downloaded'] = $queries['downloaded']; + $update['is_seed_box'] = intval($isSeedBox); $logData = json_encode(Arr::except($update, ['peer_id'])); if ($peer->exists) { diff --git a/database/migrations/2022_09_12_181952_add_is_seed_box_to_peers_table.php b/database/migrations/2022_09_12_181952_add_is_seed_box_to_peers_table.php new file mode 100644 index 00000000..fa574c69 --- /dev/null +++ b/database/migrations/2022_09_12_181952_add_is_seed_box_to_peers_table.php @@ -0,0 +1,32 @@ +tinyInteger('is_seed_box')->default(0); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('peers', function (Blueprint $table) { + $table->dropColumn('is_seed_box'); + }); + } +}; diff --git a/include/constants.php b/include/constants.php index d26c4885..17b6d75f 100644 --- a/include/constants.php +++ b/include/constants.php @@ -1,6 +1,6 @@ whereIn('torrent', $torrentIdArr) - ->whereIn('userid', array_unique($ownerIdArr)) ->where('seeder', 'yes') - ->get(['torrent', 'ipv4', 'ipv6']) + ->where('is_seed_box', '1') + ->get(['torrent', 'is_seed_box']) ->keyBy('torrent'); } @@ -3559,9 +3559,8 @@ foreach ($rows as $row) $banned_torrent = ($row["banned"] == 'yes' ? " (".$lang_functions['text_banned'].")" : ""); $sp_torrent_sub = get_torrent_promotion_append_sub($row['sp_state'],"",true,$row['added'], $row['promotion_time_type'], $row['promotion_until'], $row['__ignore_global_sp_state'] ?? false); $approvalStatusIcon = $torrentRep->renderApprovalStatus($row['approval_status']); - if ($showSeedBoxIcon && $ownerPeerInfo->has($row['id'])) { - $ownerPeer = $ownerPeerInfo->get($row['id']); - $seedBoxIcon = $seedBoxRep->renderIcon([$ownerPeer->ipv4, $ownerPeer->ipv6], $row['owner']); + if ($showSeedBoxIcon && $seedBoxPeerInfo->has($row['id'])) { + $seedBoxIcon = $seedBoxRep->getSeedBoxIcon(); } else { $seedBoxIcon = ''; } diff --git a/public/announce.php b/public/announce.php index 6430c311..37182ad4 100644 --- a/public/announce.php +++ b/public/announce.php @@ -332,7 +332,12 @@ if(isset($self) && empty($_GET['event']) && $self['prevts'] > (TIMENOW - $announ $isSeedBoxRuleEnabled = get_setting('seed_box.enabled') == 'yes'; $isIPSeedBox = false; if ($isSeedBoxRuleEnabled && !($az['class'] >= \App\Models\User::CLASS_VIP || $isDonor)) { - $isIPSeedBox = isIPSeedBox($ip, $userid); + if (!empty($ipv4)) { + $isIPSeedBox = isIPSeedBox($ipv4, $userid); + } + if (!$isIPSeedBox && !empty($ipv6)) { + $isIPSeedBox = isIPSeedBox($ipv6, $userid); + } } $log .= ", [SEED_BOX], isSeedBoxRuleEnabled: $isSeedBoxRuleEnabled, isIPSeedBox: $isIPSeedBox"; @@ -537,7 +542,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)." $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 $selfwhere") or err("PL Err 1"); if (mysql_affected_rows()) { @@ -578,7 +583,7 @@ else } return 'no'; }); - $insertPeerSql = "INSERT INTO peers (torrent, userid, peer_id, ip, port, connectable, uploaded, downloaded, to_go, started, last_action, seeder, agent, downloadoffset, uploadoffset, passkey, ipv4, ipv6) VALUES ($torrentid, $userid, ".sqlesc($peer_id).", ".sqlesc($ip).", $port, '$connectable', $uploaded, $downloaded, $left, $dt, $dt, '$seeder', ".sqlesc($agent).", $downloaded, $uploaded, ".sqlesc($passkey).",".sqlesc($ipv4).",".sqlesc($ipv6).")"; + $insertPeerSql = "INSERT INTO peers (torrent, userid, peer_id, ip, port, connectable, uploaded, downloaded, to_go, started, last_action, seeder, agent, downloadoffset, uploadoffset, passkey, ipv4, ipv6, is_seed_box) VALUES ($torrentid, $userid, ".sqlesc($peer_id).", ".sqlesc($ip).", $port, '$connectable', $uploaded, $downloaded, $left, $dt, $dt, '$seeder', ".sqlesc($agent).", $downloaded, $uploaded, ".sqlesc($passkey).", ".sqlesc($ipv4).", ".sqlesc($ipv6).", ".intval($isIPSeedBox).")"; do_log("[INSERT PEER] peer not exists for $selfwhere, do insert with $insertPeerSql"); try { diff --git a/public/viewpeerlist.php b/public/viewpeerlist.php index 585a438f..8337ec20 100644 --- a/public/viewpeerlist.php +++ b/public/viewpeerlist.php @@ -11,11 +11,11 @@ header("Content-Type: text/xml; charset=utf-8"); $id = intval($_GET['id'] ?? 0); $seedBoxRep = new \App\Repositories\SeedBoxRepository(); - -function get_location_column($e, $isStrongPrivacy, $canView): string +function get_location_column($e, $isStrongPrivacy, $canView): array { global $enablelocation_tweak, $seedBoxRep, $lang_functions, $lang_viewpeerlist; $address = $ips = []; + $isSeedBox = false; //First, build the location if ($enablelocation_tweak == 'yes') { if (!empty($e['ipv4'])) { @@ -39,10 +39,12 @@ function get_location_column($e, $isStrongPrivacy, $canView): string $location = '
'.$addressStr.'
'; } else { if (!empty($e['ipv4'])) { - $ips[] = $e['ipv4'] . $seedBoxRep->renderIcon($e['ipv4'], $e['userid']); + $seedBoxIcon = $seedBoxRep->renderIcon($e['ipv4'], $e['userid']); + $ips[] = $e['ipv4'] . $seedBoxIcon; } if (!empty($e['ipv6'])) { - $ips[] = $e['ipv6'] . $seedBoxRep->renderIcon($e['ipv6'], $e['userid']); + $seedBoxIcon = $seedBoxRep->renderIcon($e['ipv6'], $e['userid']); + $ips[] = $e['ipv6'] . $seedBoxIcon; } $location = '
'.implode('
', $ips).'
'; } @@ -55,8 +57,13 @@ function get_location_column($e, $isStrongPrivacy, $canView): string } else { $result = $location; } - - return "
" . $result . "
\n"; + if (isset($seedBoxIcon) && !empty($seedBoxIcon)) { + $isSeedBox = true; + } + return [ + "td" => "
" . $result . "
", + "is_seed_box" => $isSeedBox, + ]; } function get_username_seed_box_icon($e): string @@ -74,7 +81,7 @@ function get_username_seed_box_icon($e): string if(isset($CURUSER)) { -function dltable($name, $arr, $torrent) +function dltable($name, $arr, $torrent, &$isSeedBoxCaseWhens) { global $lang_viewpeerlist,$viewanonymous_class,$userprofile_class,$enablelocation_tweak; global $CURUSER; @@ -101,7 +108,8 @@ function dltable($name, $arr, $torrent) $now = time(); $num = 0; $privacyData = \App\Models\User::query()->whereIn('id', array_column($arr, 'userid'))->get(['id', 'privacy'])->keyBy('id'); - foreach ($arr as $e) { + + foreach ($arr as $e) { $privacy = $privacyData->get($e['userid'])->privacy ?? ''; ++$num; @@ -112,10 +120,14 @@ function dltable($name, $arr, $torrent) $isStrongPrivacy = $privacy == "strong" || ($torrent['anonymous'] == 'yes' && $e['userid'] == $torrent['owner']); $canView = user_can('viewanonymous') || $e['userid'] == $CURUSER['id']; if ($showLocationColumn) { - $columnLocation = get_location_column($e, $isStrongPrivacy, $canView); + $columnLocationResult = get_location_column($e, $isStrongPrivacy, $canView); + $columnLocation = $columnLocationResult['td']; + $isSeedBox = $columnLocationResult['is_seed_box']; } else { $usernameSeedBoxIcon = get_username_seed_box_icon($e); + $isSeedBox = !empty($usernameSeedBoxIcon); } + $isSeedBoxCaseWhens[$e['id']] = sprintf("when %s then %s", $e['id'], intval($isSeedBox)); if ($isStrongPrivacy) { $columnUsername = "".$lang_viewpeerlist['text_anonymous']."".$usernameSeedBoxIcon; if ($canView) { @@ -159,7 +171,7 @@ function dltable($name, $arr, $torrent) $downloaders = array(); $seeders = array(); $torrent = \App\Models\Torrent::query()->findOrFail($id, ['id', 'seeders', 'leechers']); - $subres = sql_query("SELECT seeder, finishedat, downloadoffset, uploadoffset, ip, ipv4, ipv6, port, uploaded, downloaded, to_go, UNIX_TIMESTAMP(started) AS st, connectable, agent, peer_id, UNIX_TIMESTAMP(last_action) AS la, userid FROM peers WHERE torrent = $id") or sqlerr(); + $subres = sql_query("SELECT id, seeder, finishedat, downloadoffset, uploadoffset, ip, ipv4, ipv6, port, uploaded, downloaded, to_go, UNIX_TIMESTAMP(started) AS st, connectable, agent, peer_id, UNIX_TIMESTAMP(last_action) AS la, userid FROM peers WHERE torrent = $id") or sqlerr(); while ($subrow = mysql_fetch_array($subres)) { if ($subrow["seeder"] == "yes") $seeders[] = $subrow; @@ -176,6 +188,7 @@ function dltable($name, $arr, $torrent) $torrent->update($update); do_log("[UPDATE_TORRENT_SEEDERS_LEECHERS], torrent: $id, original: " . $torrent->toJson() . ", update: " . json_encode($update)); } + function leech_sort($a,$b) { $x = $a["to_go"]; $y = $b["to_go"]; @@ -199,7 +212,18 @@ function dltable($name, $arr, $torrent) usort($seeders, "seed_sort"); usort($downloaders, "leech_sort"); - print(dltable($lang_viewpeerlist['text_seeders'], $seeders, $row)); - print(dltable($lang_viewpeerlist['text_leechers'], $downloaders, $row)); + $isSeedBoxCaseWhens = []; + $seederTable = dltable($lang_viewpeerlist['text_seeders'], $seeders, $row, $isSeedBoxCaseWhens); + $leecherTable = dltable($lang_viewpeerlist['text_leechers'], $downloaders, $row, $isSeedBoxCaseWhens); + //update peer is_seed_box + if (!empty($isSeedBoxCaseWhens)) { + $sql = sprintf( + "update peers set is_seed_box = case id %s end where id in (%s)", + implode(' ', array_values($isSeedBoxCaseWhens)), implode(',', array_keys($isSeedBoxCaseWhens)) + ); + do_log("[IS_SEED_BOX], $sql"); + sql_query($sql); + } + print $seederTable . $leecherTable; } ?>