Skip to content

Commit

Permalink
passkey guard + cleanup batch lefttime
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaomlove committed Jul 26, 2023
1 parent 47346c4 commit f1da984
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 9 deletions.
2 changes: 1 addition & 1 deletion app/Console/Commands/TorrentLoadPiecesHash.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function handle()
$begin = time();
$id = $this->option('id');
$rep = new TorrentRepository();
$this->info("id: $id");
$this->info("id: $id, going to load pieces hash...");
$total = $rep->loadPiecesHashCache($id);
$this->info(sprintf("total: %s, cost time: %s seconds.", $total, time() - $begin));
return Command::SUCCESS;
Expand Down
4 changes: 4 additions & 0 deletions app/Providers/AuthServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ public function boot()
return new NexusWebGuard($app['request'], new NexusWebUserProvider());
});

Auth::viaRequest('passkey', function (Request $request) {
return User::query()->where('passkey', $request->passkey)->first();
});

}

private function getUserByCookie($cookie)
Expand Down
19 changes: 13 additions & 6 deletions app/Repositories/CleanupRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static function recordBatch(\Redis $redis, $uid, $torrentId)
{
$args = [
self::USER_SEED_BONUS_BATCH_KEY, self::USER_SEEDING_LEECHING_TIME_BATCH_KEY, self::TORRENT_SEEDERS_ETC_BATCH_KEY,
$uid, $uid, $torrentId, self::getHashKeySuffix()
$uid, $uid, $torrentId, self::getHashKeySuffix(), self::getCacheKeyLeftTime()
];
$result = $redis->eval(self::getAddRecordLuaScript(), $args, 3);
$err = $redis->getLastError();
Expand Down Expand Up @@ -82,9 +82,10 @@ private static function runBatchJob($batchKey, $requestId)
}
//update the batch key
$newBatch = $batchKey . ":" . self::getHashKeySuffix();
$redis->set($batchKey, $newBatch, ['ex' => self::KEY_LIFETIME]);
$leftTime = self::getCacheKeyLeftTime();
$redis->set($batchKey, $newBatch, ['ex' => $leftTime]);
$redis->hSetNx($newBatch, -1, 1);
$redis->expire($newBatch, self::KEY_LIFETIME);
$redis->expire($newBatch, $leftTime);


$count = 0;
Expand Down Expand Up @@ -129,7 +130,7 @@ private static function getBatch(\Redis $redis, $batchKey)
}

/**
* USER_SEED_BONUS, USER_SEEDING_LEECHING_TIME, TORRENT_SEEDERS_ETC, uid, uid, torrentId, timeStr
* USER_SEED_BONUS, USER_SEEDING_LEECHING_TIME, TORRENT_SEEDERS_ETC, uid, uid, torrentId, timeStr, cacheLifeTime
*
* @return string
*/
Expand All @@ -142,7 +143,7 @@ private static function getAddRecordLuaScript(): string
local isBatchKeyNew = false
if batchKey == false then
batchKey = v .. ":" .. ARGV[4]
redis.call("SET", v, batchKey, "EX", 3600*3)
redis.call("SET", v, batchKey, "EX", ARGV[5])
isBatchKeyNew = true
end
local hashKey
Expand All @@ -157,7 +158,7 @@ private static function getAddRecordLuaScript(): string
end
redis.call("HSETNX", batchKey, hashKey, 1)
if isBatchKeyNew then
redis.call("EXPIRE", batchKey, 3600*3)
redis.call("EXPIRE", batchKey, ARGV[5])
end
end
LUA;
Expand Down Expand Up @@ -200,4 +201,10 @@ private static function getDelay(int $taskIndex, int $length, int $page): float
return floor($base + $offset);
}

private static function getCacheKeyLeftTime(): int
{
$value = get_setting("main.autoclean_interval_three");
return intval($value) + 600;
}

}
3 changes: 3 additions & 0 deletions config/auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
'nexus-web' => [
'driver' => 'nexus-web',
],
'passkey' => [
'driver' => 'passkey',
],
],

/*
Expand Down
2 changes: 1 addition & 1 deletion include/constants.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
defined('VERSION_NUMBER') || define('VERSION_NUMBER', '1.8.5');
defined('RELEASE_DATE') || define('RELEASE_DATE', '2023-07-26');
defined('RELEASE_DATE') || define('RELEASE_DATE', '2023-07-27');
defined('IN_TRACKER') || define('IN_TRACKER', false);
defined('PROJECTNAME') || define("PROJECTNAME","NexusPHP");
defined('NEXUSPHPURL') || define("NEXUSPHPURL","https://nexusphp.org");
Expand Down
6 changes: 5 additions & 1 deletion routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
Route::resource('messages', \App\Http\Controllers\MessageController::class);
Route::get('messages-unread', [\App\Http\Controllers\MessageController::class, 'listUnread']);
Route::resource('torrents', \App\Http\Controllers\TorrentController::class);
Route::get("pieces-hash", [\App\Http\Controllers\TorrentController::class, "queryByPiecesHash"])->name("torrent.pieces_hash.query");
Route::resource('comments', \App\Http\Controllers\CommentController::class);
Route::resource('peers', \App\Http\Controllers\PeerController::class);
Route::resource('files', \App\Http\Controllers\FileController::class);
Expand Down Expand Up @@ -93,3 +92,8 @@
});

Route::post('login', [\App\Http\Controllers\AuthenticateController::class, 'login']);


Route::group(['middleware' => ['auth.nexus:passkey', 'locale']], function () {
Route::get("pieces-hash", [\App\Http\Controllers\TorrentController::class, "queryByPiecesHash"])->name("torrent.pieces_hash.query");
});

0 comments on commit f1da984

Please sign in to comment.