tracker user update use variable instead of properity

This commit is contained in:
xiaomlove
2022-04-21 17:54:26 +08:00
parent a33394c2aa
commit e306b1f6ed
3 changed files with 22 additions and 24 deletions
+18 -20
View File
@@ -52,15 +52,13 @@ class TrackerRepository extends BaseRepository
6699, // Port used by p2p software, such as WinMX, Napster. 6699, // Port used by p2p software, such as WinMX, Napster.
]; ];
private array $userUpdates = [];
public function announce(Request $request): \Illuminate\Http\Response public function announce(Request $request): \Illuminate\Http\Response
{ {
do_log("queryString: " . $request->getQueryString()); do_log("queryString: " . $request->getQueryString());
/** /**
* Note: In Octane this class will be reused, must reset this property !!! * Note: In Octane this class will be reused, use variable is better !!!
*/ */
$this->userUpdates = []; $userUpdates = [];
try { try {
$withPeers = false; $withPeers = false;
$queries = $this->checkAnnounceFields($request); $queries = $this->checkAnnounceFields($request);
@@ -117,16 +115,16 @@ class TrackerRepository extends BaseRepository
$this->updateTorrent($torrent, $queries, $isPeerExists); $this->updateTorrent($torrent, $queries, $isPeerExists);
if ($dataTraffic['uploaded_increment_for_user'] > 0) { if ($dataTraffic['uploaded_increment_for_user'] > 0) {
$this->userUpdates['uploaded'] = DB::raw('uploaded + ' . $dataTraffic['uploaded_increment_for_user']); $userUpdates['uploaded'] = DB::raw('uploaded + ' . $dataTraffic['uploaded_increment_for_user']);
} }
if ($dataTraffic['downloaded_increment_for_user'] > 0) { if ($dataTraffic['downloaded_increment_for_user'] > 0) {
$this->userUpdates['downloaded'] = DB::raw('downloaded + ' . $dataTraffic['downloaded_increment_for_user']); $userUpdates['downloaded'] = DB::raw('downloaded + ' . $dataTraffic['downloaded_increment_for_user']);
} }
if ($user->clientselect != $clientAllow->id) { if ($user->clientselect != $clientAllow->id) {
$this->userUpdates['clientselect'] = $clientAllow->id; $userUpdates['clientselect'] = $clientAllow->id;
} }
if ($user->showclienterror == 'yes') { if ($user->showclienterror == 'yes') {
$this->userUpdates['showclienterror'] = 'no'; $userUpdates['showclienterror'] = 'no';
} }
} }
} }
@@ -134,7 +132,7 @@ class TrackerRepository extends BaseRepository
} catch (ClientNotAllowedException $exception) { } catch (ClientNotAllowedException $exception) {
do_log("[ClientNotAllowedException] " . $exception->getMessage()); do_log("[ClientNotAllowedException] " . $exception->getMessage());
if (isset($user) && $user->showclienterror == 'no') { if (isset($user) && $user->showclienterror == 'no') {
$this->userUpdates['showclienterror'] = 'yes'; $userUpdates['showclienterror'] = 'yes';
} }
$repDict = $this->generateFailedAnnounceResponse($exception->getMessage()); $repDict = $this->generateFailedAnnounceResponse($exception->getMessage());
} catch (TrackerException $exception) { } catch (TrackerException $exception) {
@@ -145,7 +143,7 @@ class TrackerRepository extends BaseRepository
$repDict = $this->generateFailedAnnounceResponse("system error, report to sysop please, hint: " . nexus()->getRequestId()); $repDict = $this->generateFailedAnnounceResponse("system error, report to sysop please, hint: " . nexus()->getRequestId());
} finally { } finally {
if (isset($user)) { if (isset($user)) {
$this->updateUser($user); $this->updateUser($user, $userUpdates);
} }
return $this->sendFinalAnnounceResponse($repDict); return $this->sendFinalAnnounceResponse($repDict);
} }
@@ -955,25 +953,25 @@ class TrackerRepository extends BaseRepository
{ {
do_log("queryString: " . $request->getQueryString()); do_log("queryString: " . $request->getQueryString());
/** /**
* Note: In Octane this class will be reused, must reset this property !!! * Note: In Octane this class will be reused, use variable is better !!!
*/ */
$this->userUpdates = []; $userUpdates = [];
try { try {
$infoHashArr = $this->checkScrapeFields($request); $infoHashArr = $this->checkScrapeFields($request);
$user = $this->checkUser($request); $user = $this->checkUser($request);
$clientAllow = $this->checkClient($request); $clientAllow = $this->checkClient($request);
if ($user->clientselect != $clientAllow->id) { if ($user->clientselect != $clientAllow->id) {
$this->userUpdates['clientselect'] = $clientAllow->id; $userUpdates['clientselect'] = $clientAllow->id;
} }
if ($user->showclienterror == 'yes') { if ($user->showclienterror == 'yes') {
$this->userUpdates['showclienterror'] = 'no'; $userUpdates['showclienterror'] = 'no';
} }
$repDict = $this->generateScrapeResponse($infoHashArr); $repDict = $this->generateScrapeResponse($infoHashArr);
} catch (ClientNotAllowedException $exception) { } catch (ClientNotAllowedException $exception) {
do_log("[ClientNotAllowedException] " . $exception->getMessage()); do_log("[ClientNotAllowedException] " . $exception->getMessage());
if (isset($user) && $user->showclienterror == 'no') { if (isset($user) && $user->showclienterror == 'no') {
$this->userUpdates['showclienterror'] = 'yes'; $userUpdates['showclienterror'] = 'yes';
} }
$repDict = $this->generateFailedAnnounceResponse($exception->getMessage()); $repDict = $this->generateFailedAnnounceResponse($exception->getMessage());
} catch (TrackerException $exception) { } catch (TrackerException $exception) {
@@ -984,7 +982,7 @@ class TrackerRepository extends BaseRepository
$repDict = $this->generateFailedAnnounceResponse("system error, report to sysop please, hint: " . nexus()->getRequestId()); $repDict = $this->generateFailedAnnounceResponse("system error, report to sysop please, hint: " . nexus()->getRequestId());
} finally { } finally {
if (isset($user)) { if (isset($user)) {
$this->updateUser($user); $this->updateUser($user, $userUpdates);
} }
return $this->sendFinalAnnounceResponse($repDict); return $this->sendFinalAnnounceResponse($repDict);
} }
@@ -1066,13 +1064,13 @@ class TrackerRepository extends BaseRepository
DB::insert($sql); DB::insert($sql);
} }
private function updateUser(User $user) private function updateUser(User $user, array $update)
{ {
$log = "update: " . json_encode($this->userUpdates); $log = "update: " . json_encode($update);
if (count($this->userUpdates) === 0) { if (empty($update)) {
$log .= ", no update..."; $log .= ", no update...";
} else { } else {
$user->fill($this->userUpdates); $user->fill($update);
$log .= ", dirty: " . json_encode($user->getDirty()); $log .= ", dirty: " . json_encode($user->getDirty());
$user->save(); $user->save();
$log .= ", query: " . last_query(); $log .= ", query: " . last_query();
@@ -19,7 +19,7 @@ class CreatePeersTable extends Migration
Schema::create('peers', function (Blueprint $table) { Schema::create('peers', function (Blueprint $table) {
$table->increments('id'); $table->increments('id');
$table->unsignedMediumInteger('torrent')->default(0); $table->unsignedMediumInteger('torrent')->default(0);
$table->char('peer_id', 20)->charset('binary'); $table->char('peer_id', 20)->charset('binary')->index();
$table->string('ip', 64)->default(''); $table->string('ip', 64)->default('');
$table->unsignedSmallInteger('port')->default(0); $table->unsignedSmallInteger('port')->default(0);
$table->unsignedBigInteger('uploaded')->default(0); $table->unsignedBigInteger('uploaded')->default(0);
@@ -30,13 +30,13 @@ class CreatePeersTable extends Migration
$table->dateTime('last_action')->nullable(); $table->dateTime('last_action')->nullable();
$table->dateTime('prev_action')->nullable(); $table->dateTime('prev_action')->nullable();
$table->enum('connectable', ['yes', 'no'])->default('yes'); $table->enum('connectable', ['yes', 'no'])->default('yes');
$table->unsignedMediumInteger('userid')->default(0)->index('userid'); $table->unsignedMediumInteger('userid')->default(0)->index();
$table->string('agent', 60)->default(''); $table->string('agent', 60)->default('');
$table->unsignedInteger('finishedat')->default(0); $table->unsignedInteger('finishedat')->default(0);
$table->unsignedBigInteger('downloadoffset')->default(0); $table->unsignedBigInteger('downloadoffset')->default(0);
$table->unsignedBigInteger('uploadoffset')->default(0); $table->unsignedBigInteger('uploadoffset')->default(0);
$table->char('passkey', 32)->default(''); $table->char('passkey', 32)->default('');
$table->unique(['torrent', 'peer_id']); $table->index(['torrent', 'peer_id']);
}); });
} }
@@ -28,7 +28,7 @@ return new class extends Migration
} }
Schema::table($tableName, function (Blueprint $table) { Schema::table($tableName, function (Blueprint $table) {
$table->unique(['torrent', 'peer_id']); $table->index(['torrent', 'peer_id']);
$table->index('peer_id'); $table->index('peer_id');
}); });
} }