add is_seed_box to peers

This commit is contained in:
xiaomlove
2022-09-12 20:00:07 +08:00
parent 771e7ad88f
commit d5e81ac009
8 changed files with 99 additions and 25 deletions
+1 -1
View File
@@ -11,7 +11,7 @@ class Peer extends NexusModel
protected $fillable = [ protected $fillable = [
'torrent', 'peer_id', 'ip', 'port', 'uploaded', 'downloaded', 'to_go', 'seeder', 'started', 'last_action', 'torrent', 'peer_id', 'ip', 'port', 'uploaded', 'downloaded', 'to_go', 'seeder', 'started', 'last_action',
'prev_action', 'connectable', 'userid', 'agent', 'finishedat', 'downloadoffset', 'uploadedoffset', 'passkey', 'prev_action', 'connectable', 'userid', 'agent', 'finishedat', 'downloadoffset', 'uploadedoffset', 'passkey',
'ipv4', 'ipv6', 'ipv4', 'ipv6', 'is_seed_box'
]; ];
const CONNECTABLE_YES = 'yes'; const CONNECTABLE_YES = 'yes';
+9 -1
View File
@@ -138,12 +138,20 @@ class SeedBoxRepository extends BaseRepository
} }
foreach (Arr::wrap($ipArr) as $ip) { foreach (Arr::wrap($ipArr) as $ip) {
if ((isIPV4($ip) || isIPV6($ip)) && $enableSeedBox && isIPSeedBox($ip, $uid)) { if ((isIPV4($ip) || isIPV6($ip)) && $enableSeedBox && isIPSeedBox($ip, $uid)) {
return '<img src="pic/misc/seed-box.png" style="vertical-align: bottom; height: 16px; margin-left: 4px" title="SeedBox" />'; return $this->getSeedBoxIcon();
} }
} }
return ''; return '';
} }
public function getSeedBoxIcon($isSeedBox = true): string
{
if (!$isSeedBox) {
return '';
}
return '<img src="pic/misc/seed-box.png" style="vertical-align: bottom; height: 16px; margin-left: 4px" title="SeedBox" />';
}
private function clearCache() private function clearCache()
{ {
return true; return true;
+6
View File
@@ -841,11 +841,16 @@ class TrackerRepository extends BaseRepository
'agent' => $queries['user_agent'], 'agent' => $queries['user_agent'],
'connectable' => $this->getConnectable($queries['ip'], $queries['port'], $queries['user_agent']) 'connectable' => $this->getConnectable($queries['ip'], $queries['port'], $queries['user_agent'])
]; ];
$isSeedBox = false;
if (!empty($queries['ipv4'])) { if (!empty($queries['ipv4'])) {
$update['ipv4'] = $queries['ipv4']; $update['ipv4'] = $queries['ipv4'];
$isSeedBox = isIPSeedBox($queries['ipv4'], $peer->userid);
} }
if (!empty($queries['ipv6'])) { if (!empty($queries['ipv6'])) {
$update['ipv6'] = $queries['ipv6']; $update['ipv6'] = $queries['ipv6'];
if (!$isSeedBox) {
$isSeedBox = isIPSeedBox($queries['ipv6'], $peer->userid);
}
} }
if ($peer->exists) { if ($peer->exists) {
@@ -865,6 +870,7 @@ class TrackerRepository extends BaseRepository
$update['last_action'] = $nowStr; $update['last_action'] = $nowStr;
$update['uploaded'] = $queries['uploaded']; $update['uploaded'] = $queries['uploaded'];
$update['downloaded'] = $queries['downloaded']; $update['downloaded'] = $queries['downloaded'];
$update['is_seed_box'] = intval($isSeedBox);
$logData = json_encode(Arr::except($update, ['peer_id'])); $logData = json_encode(Arr::except($update, ['peer_id']));
if ($peer->exists) { if ($peer->exists) {
@@ -0,0 +1,32 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('peers', function (Blueprint $table) {
$table->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');
});
}
};
+2 -2
View File
@@ -1,6 +1,6 @@
<?php <?php
defined('VERSION_NUMBER') || define('VERSION_NUMBER', '1.7.24'); defined('VERSION_NUMBER') || define('VERSION_NUMBER', '1.7.25');
defined('RELEASE_DATE') || define('RELEASE_DATE', '2022-09-11'); defined('RELEASE_DATE') || define('RELEASE_DATE', '2022-09-12');
defined('IN_TRACKER') || define('IN_TRACKER', false); defined('IN_TRACKER') || define('IN_TRACKER', false);
defined('PROJECTNAME') || define("PROJECTNAME","NexusPHP"); defined('PROJECTNAME') || define("PROJECTNAME","NexusPHP");
defined('NEXUSPHPURL') || define("NEXUSPHPURL","https://nexusphp.org"); defined('NEXUSPHPURL') || define("NEXUSPHPURL","https://nexusphp.org");
+5 -6
View File
@@ -3339,11 +3339,11 @@ function torrenttable($rows, $variant = "torrent", $searchBoxId = 0) {
//seedBoxIcon //seedBoxIcon
if ($showSeedBoxIcon) { if ($showSeedBoxIcon) {
$seedBoxRep = new \App\Repositories\SeedBoxRepository(); $seedBoxRep = new \App\Repositories\SeedBoxRepository();
$ownerPeerInfo = \App\Models\Peer::query() $seedBoxPeerInfo = \App\Models\Peer::query()
->whereIn('torrent', $torrentIdArr) ->whereIn('torrent', $torrentIdArr)
->whereIn('userid', array_unique($ownerIdArr))
->where('seeder', 'yes') ->where('seeder', 'yes')
->get(['torrent', 'ipv4', 'ipv6']) ->where('is_seed_box', '1')
->get(['torrent', 'is_seed_box'])
->keyBy('torrent'); ->keyBy('torrent');
} }
@@ -3559,9 +3559,8 @@ foreach ($rows as $row)
$banned_torrent = ($row["banned"] == 'yes' ? " <b>(<font class=\"striking\">".$lang_functions['text_banned']."</font>)</b>" : ""); $banned_torrent = ($row["banned"] == 'yes' ? " <b>(<font class=\"striking\">".$lang_functions['text_banned']."</font>)</b>" : "");
$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); $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']); $approvalStatusIcon = $torrentRep->renderApprovalStatus($row['approval_status']);
if ($showSeedBoxIcon && $ownerPeerInfo->has($row['id'])) { if ($showSeedBoxIcon && $seedBoxPeerInfo->has($row['id'])) {
$ownerPeer = $ownerPeerInfo->get($row['id']); $seedBoxIcon = $seedBoxRep->getSeedBoxIcon();
$seedBoxIcon = $seedBoxRep->renderIcon([$ownerPeer->ipv4, $ownerPeer->ipv6], $row['owner']);
} else { } else {
$seedBoxIcon = ''; $seedBoxIcon = '';
} }
+8 -3
View File
@@ -332,7 +332,12 @@ if(isset($self) && empty($_GET['event']) && $self['prevts'] > (TIMENOW - $announ
$isSeedBoxRuleEnabled = get_setting('seed_box.enabled') == 'yes'; $isSeedBoxRuleEnabled = get_setting('seed_box.enabled') == 'yes';
$isIPSeedBox = false; $isIPSeedBox = false;
if ($isSeedBoxRuleEnabled && !($az['class'] >= \App\Models\User::CLASS_VIP || $isDonor)) { 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"; $log .= ", [SEED_BOX], isSeedBoxRuleEnabled: $isSeedBoxRuleEnabled, isIPSeedBox: $isIPSeedBox";
@@ -537,7 +542,7 @@ elseif(isset($self))
$updateset[] = "times_completed = times_completed + 1"; $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()) if (mysql_affected_rows())
{ {
@@ -578,7 +583,7 @@ else
} }
return 'no'; 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"); do_log("[INSERT PEER] peer not exists for $selfwhere, do insert with $insertPeerSql");
try { try {
+36 -12
View File
@@ -11,11 +11,11 @@ header("Content-Type: text/xml; charset=utf-8");
$id = intval($_GET['id'] ?? 0); $id = intval($_GET['id'] ?? 0);
$seedBoxRep = new \App\Repositories\SeedBoxRepository(); $seedBoxRep = new \App\Repositories\SeedBoxRepository();
function get_location_column($e, $isStrongPrivacy, $canView): array
function get_location_column($e, $isStrongPrivacy, $canView): string
{ {
global $enablelocation_tweak, $seedBoxRep, $lang_functions, $lang_viewpeerlist; global $enablelocation_tweak, $seedBoxRep, $lang_functions, $lang_viewpeerlist;
$address = $ips = []; $address = $ips = [];
$isSeedBox = false;
//First, build the location //First, build the location
if ($enablelocation_tweak == 'yes') { if ($enablelocation_tweak == 'yes') {
if (!empty($e['ipv4'])) { if (!empty($e['ipv4'])) {
@@ -39,10 +39,12 @@ function get_location_column($e, $isStrongPrivacy, $canView): string
$location = '<div style="margin-right: 6px" title="'.$title.'">'.$addressStr.'</div>'; $location = '<div style="margin-right: 6px" title="'.$title.'">'.$addressStr.'</div>';
} else { } else {
if (!empty($e['ipv4'])) { 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'])) { 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 = '<div style="margin-right: 6px">'.implode('<br/>', $ips).'</div>'; $location = '<div style="margin-right: 6px">'.implode('<br/>', $ips).'</div>';
} }
@@ -55,8 +57,13 @@ function get_location_column($e, $isStrongPrivacy, $canView): string
} else { } else {
$result = $location; $result = $location;
} }
if (isset($seedBoxIcon) && !empty($seedBoxIcon)) {
return "<td class=rowfollow align=left width=1%><div style='display: flex;white-space: nowrap;align-items: center'>" . $result . "</div></td>\n"; $isSeedBox = true;
}
return [
"td" => "<td class=rowfollow align=left width=1%><div style='display: flex;white-space: nowrap;align-items: center'>" . $result . "</div></td>",
"is_seed_box" => $isSeedBox,
];
} }
function get_username_seed_box_icon($e): string function get_username_seed_box_icon($e): string
@@ -74,7 +81,7 @@ function get_username_seed_box_icon($e): string
if(isset($CURUSER)) if(isset($CURUSER))
{ {
function dltable($name, $arr, $torrent) function dltable($name, $arr, $torrent, &$isSeedBoxCaseWhens)
{ {
global $lang_viewpeerlist,$viewanonymous_class,$userprofile_class,$enablelocation_tweak; global $lang_viewpeerlist,$viewanonymous_class,$userprofile_class,$enablelocation_tweak;
global $CURUSER; global $CURUSER;
@@ -101,7 +108,8 @@ function dltable($name, $arr, $torrent)
$now = time(); $now = time();
$num = 0; $num = 0;
$privacyData = \App\Models\User::query()->whereIn('id', array_column($arr, 'userid'))->get(['id', 'privacy'])->keyBy('id'); $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 ?? ''; $privacy = $privacyData->get($e['userid'])->privacy ?? '';
++$num; ++$num;
@@ -112,10 +120,14 @@ function dltable($name, $arr, $torrent)
$isStrongPrivacy = $privacy == "strong" || ($torrent['anonymous'] == 'yes' && $e['userid'] == $torrent['owner']); $isStrongPrivacy = $privacy == "strong" || ($torrent['anonymous'] == 'yes' && $e['userid'] == $torrent['owner']);
$canView = user_can('viewanonymous') || $e['userid'] == $CURUSER['id']; $canView = user_can('viewanonymous') || $e['userid'] == $CURUSER['id'];
if ($showLocationColumn) { 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 { } else {
$usernameSeedBoxIcon = get_username_seed_box_icon($e); $usernameSeedBoxIcon = get_username_seed_box_icon($e);
$isSeedBox = !empty($usernameSeedBoxIcon);
} }
$isSeedBoxCaseWhens[$e['id']] = sprintf("when %s then %s", $e['id'], intval($isSeedBox));
if ($isStrongPrivacy) { if ($isStrongPrivacy) {
$columnUsername = "<td class=rowfollow align=left width=1%><i>".$lang_viewpeerlist['text_anonymous']."</i>".$usernameSeedBoxIcon; $columnUsername = "<td class=rowfollow align=left width=1%><i>".$lang_viewpeerlist['text_anonymous']."</i>".$usernameSeedBoxIcon;
if ($canView) { if ($canView) {
@@ -159,7 +171,7 @@ function dltable($name, $arr, $torrent)
$downloaders = array(); $downloaders = array();
$seeders = array(); $seeders = array();
$torrent = \App\Models\Torrent::query()->findOrFail($id, ['id', 'seeders', 'leechers']); $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)) { while ($subrow = mysql_fetch_array($subres)) {
if ($subrow["seeder"] == "yes") if ($subrow["seeder"] == "yes")
$seeders[] = $subrow; $seeders[] = $subrow;
@@ -176,6 +188,7 @@ function dltable($name, $arr, $torrent)
$torrent->update($update); $torrent->update($update);
do_log("[UPDATE_TORRENT_SEEDERS_LEECHERS], torrent: $id, original: " . $torrent->toJson() . ", update: " . json_encode($update)); do_log("[UPDATE_TORRENT_SEEDERS_LEECHERS], torrent: $id, original: " . $torrent->toJson() . ", update: " . json_encode($update));
} }
function leech_sort($a,$b) { function leech_sort($a,$b) {
$x = $a["to_go"]; $x = $a["to_go"];
$y = $b["to_go"]; $y = $b["to_go"];
@@ -199,7 +212,18 @@ function dltable($name, $arr, $torrent)
usort($seeders, "seed_sort"); usort($seeders, "seed_sort");
usort($downloaders, "leech_sort"); usort($downloaders, "leech_sort");
print(dltable($lang_viewpeerlist['text_seeders'], $seeders, $row)); $isSeedBoxCaseWhens = [];
print(dltable($lang_viewpeerlist['text_leechers'], $downloaders, $row)); $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;
} }
?> ?>