passkey guard + cleanup batch lefttime

This commit is contained in:
xiaomlove
2023-07-27 02:44:04 +08:00
parent 47346c4844
commit f1da9843d4
6 changed files with 27 additions and 9 deletions

View File

@@ -31,7 +31,7 @@ class TorrentLoadPiecesHash extends Command
$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;

View File

@@ -64,6 +64,10 @@ class AuthServiceProvider extends ServiceProvider
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)

View File

@@ -39,7 +39,7 @@ class CleanupRepository extends BaseRepository
{
$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();
@@ -82,9 +82,10 @@ class CleanupRepository extends BaseRepository
}
//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;
@@ -129,7 +130,7 @@ class CleanupRepository extends BaseRepository
}
/**
* 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
*/
@@ -142,7 +143,7 @@ for k, v in pairs(batchList) do
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
@@ -157,7 +158,7 @@ for k, v in pairs(batchList) do
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;
@@ -200,4 +201,10 @@ LUA;
return floor($base + $offset);
}
private static function getCacheKeyLeftTime(): int
{
$value = get_setting("main.autoclean_interval_three");
return intval($value) + 600;
}
}

View File

@@ -52,6 +52,9 @@ return [
'nexus-web' => [
'driver' => 'nexus-web',
],
'passkey' => [
'driver' => 'passkey',
],
],
/*

View File

@@ -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");

View File

@@ -28,7 +28,6 @@ Route::group(['middleware' => ['auth:sanctum', 'locale']], function () {
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);
@@ -93,3 +92,8 @@ Route::group(['middleware' => ['auth:sanctum', 'locale']], function () {
});
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");
});