mirror of
https://github.com/lkddi/nexusphp.git
synced 2026-04-24 03:57:22 +08:00
finish support with otane
This commit is contained in:
@@ -41,7 +41,7 @@ class AttendanceMigrate extends Command
|
||||
{
|
||||
$rep = new AttendanceRepository();
|
||||
$result = $rep->migrateAttendance();
|
||||
$log = sprintf('[%s], %s, result: %s, query: %s', REQUEST_ID, __METHOD__, var_export($result, true), last_query());
|
||||
$log = sprintf('[%s], %s, result: %s, query: %s', nexus() ? nexus()->getRequestId() : 'NO_REQUEST_ID', __METHOD__, var_export($result, true), last_query());
|
||||
$this->info($log);
|
||||
do_log($log);
|
||||
return 0;
|
||||
|
||||
@@ -42,7 +42,7 @@ class BackuAll extends Command
|
||||
$result = $rep->backupAll();
|
||||
$log = sprintf(
|
||||
'[%s], %s, result: %s',
|
||||
REQUEST_ID, __METHOD__, var_export($result, true)
|
||||
nexus()->getRequestId(), __METHOD__, var_export($result, true)
|
||||
);
|
||||
$this->info($log);
|
||||
do_log($log);
|
||||
|
||||
@@ -42,7 +42,7 @@ class BackupCronjob extends Command
|
||||
$result = $rep->cronjobBackup();
|
||||
$log = sprintf(
|
||||
'[%s], %s, result: %s',
|
||||
REQUEST_ID, __METHOD__, var_export($result, true)
|
||||
nexus()->getRequestId(), __METHOD__, var_export($result, true)
|
||||
);
|
||||
$this->info($log);
|
||||
do_log($log);
|
||||
|
||||
@@ -40,7 +40,7 @@ class BackupDatabase extends Command
|
||||
{
|
||||
$rep = new ToolRepository();
|
||||
$result = $rep->backupDatabase();
|
||||
$log = sprintf('[%s], %s, result: %s', REQUEST_ID, __METHOD__, var_export($result, true));
|
||||
$log = sprintf('[%s], %s, result: %s', nexus()->getRequestId(), __METHOD__, var_export($result, true));
|
||||
$this->info($log);
|
||||
do_log($log);
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ class BackupWeb extends Command
|
||||
{
|
||||
$rep = new ToolRepository();
|
||||
$result = $rep->backupWeb();
|
||||
$log = sprintf('[%s], %s, result: %s', REQUEST_ID, __METHOD__, var_export($result, true));
|
||||
$log = sprintf('[%s], %s, result: %s', nexus()->getRequestId(), __METHOD__, var_export($result, true));
|
||||
$this->info($log);
|
||||
do_log($log);
|
||||
}
|
||||
|
||||
@@ -55,7 +55,7 @@ class DeleteExpiredToken extends Command
|
||||
|
||||
$query->where('last_used_at', '<', Carbon::now()->subDays($days));
|
||||
$result = $query->delete();
|
||||
$log = sprintf('[%s], %s, result: %s, query: %s', REQUEST_ID, __METHOD__, var_export($result, true), last_query());
|
||||
$log = sprintf('[%s], %s, result: %s, query: %s', nexus()->getRequestId(), __METHOD__, var_export($result, true), last_query());
|
||||
$this->info($log);
|
||||
do_log($log);
|
||||
return 0;
|
||||
|
||||
@@ -45,7 +45,7 @@ class ExamAssign extends Command
|
||||
$end = $this->option('end');
|
||||
$this->info(sprintf('uid: %s, examId: %s, begin: %s, end: %s', $uid, $examId, $begin, $end));
|
||||
$result = $examRep->assignToUser($uid, $examId, $begin, $end);
|
||||
$log = sprintf('[%s], %s, result: %s', REQUEST_ID, __METHOD__, var_export($result, true));
|
||||
$log = sprintf('[%s], %s, result: %s', nexus()->getRequestId(), __METHOD__, var_export($result, true));
|
||||
$this->info($log);
|
||||
do_log($log);
|
||||
return 0;
|
||||
|
||||
@@ -40,7 +40,7 @@ class ExamAssignCronjob extends Command
|
||||
{
|
||||
$examRep = new ExamRepository();
|
||||
$result = $examRep->cronjonAssign();
|
||||
$log = sprintf('[%s], %s, result: %s', REQUEST_ID, __METHOD__, var_export($result, true));
|
||||
$log = sprintf('[%s], %s, result: %s', nexus()->getRequestId(), __METHOD__, var_export($result, true));
|
||||
$this->info($log);
|
||||
do_log($log);
|
||||
return 0;
|
||||
|
||||
@@ -42,7 +42,7 @@ class ExamCheckoutCronjob extends Command
|
||||
$ignoreTimeRange = $this->option('ignore-time-range');
|
||||
$this->info('ignore-time-range: ' . var_export($ignoreTimeRange, true));
|
||||
$result = $examRep->cronjobCheckout($ignoreTimeRange);
|
||||
$log = sprintf('[%s], %s, result: %s', REQUEST_ID, __METHOD__, var_export($result, true));
|
||||
$log = sprintf('[%s], %s, result: %s', nexus()->getRequestId(), __METHOD__, var_export($result, true));
|
||||
$this->info($log);
|
||||
do_log($log);
|
||||
return 0;
|
||||
|
||||
@@ -42,7 +42,7 @@ class ExamUpdateProgress extends Command
|
||||
$uid = $this->argument('uid');
|
||||
$examRep = new ExamRepository();
|
||||
$result = $examRep->updateProgress($uid);
|
||||
$this->info(REQUEST_ID . ", result: " . var_export($result, true));
|
||||
$this->info(nexus()->getRequestId() . ", result: " . var_export($result, true));
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,7 +44,7 @@ class HitAndRunUpdateStatus extends Command
|
||||
$result = $rep->cronjobUpdateStatus($uid, $torrentId);
|
||||
$log = sprintf(
|
||||
'[%s], %s, uid: %s, torrentId: %s, result: %s',
|
||||
REQUEST_ID, __METHOD__, $uid, $torrentId, var_export($result, true)
|
||||
nexus()->getRequestId(), __METHOD__, $uid, $torrentId, var_export($result, true)
|
||||
);
|
||||
$this->info($log);
|
||||
do_log($log);
|
||||
|
||||
@@ -43,7 +43,7 @@ class MigrateTorrentTag extends Command
|
||||
{
|
||||
$rep = new TagRepository();
|
||||
$result = $rep->migrateTorrentTag();
|
||||
$log = sprintf('[%s], %s, result: %s, query: %s', REQUEST_ID, __METHOD__, var_export($result, true), last_query());
|
||||
$log = sprintf('[%s], %s, result: %s, query: %s', nexus()->getRequestId(), __METHOD__, var_export($result, true), last_query());
|
||||
$this->info($log);
|
||||
do_log($log);
|
||||
return 0;
|
||||
|
||||
@@ -65,8 +65,11 @@ class Test extends Command
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$redis = Redis::connection()->client();
|
||||
$r = $redis->get('5da94a358e67cb5181166ae2611c2fd9');
|
||||
$peerId = '-UT355W-%af%b0ky%86N%a6%17i%f8%c1%0a';
|
||||
$peerId = '-UT355W-%af%b0ky%86N%a6%17i%f8%c1%0a';
|
||||
$peerId = '-UT355W-%AF%B0ky%86N%A6%17i%F8%C1';
|
||||
$peerId = '-UT355W-%AF%B0ky%86N%A6%17i%F8%C1%0A';
|
||||
$r = strlen(urldecode($peerId));
|
||||
dd($r);
|
||||
}
|
||||
|
||||
|
||||
@@ -47,7 +47,7 @@ class UserResetPassword extends Command
|
||||
|
||||
$rep = new UserRepository();
|
||||
$result = $rep->resetPassword($uid, $password, $passwordConfirmation);
|
||||
$log = sprintf('[%s], %s, result: %s', REQUEST_ID, __METHOD__, var_export($result, true));
|
||||
$log = sprintf('[%s], %s, result: %s', nexus()->getRequestId(), __METHOD__, var_export($result, true));
|
||||
$this->info($log);
|
||||
do_log($log);
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ class Kernel extends HttpKernel
|
||||
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
|
||||
\App\Http\Middleware\TrimStrings::class,
|
||||
// \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
|
||||
\App\Http\Middleware\BootNexus::class,
|
||||
];
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Http\Request;
|
||||
use Nexus\Nexus;
|
||||
|
||||
class BootNexus
|
||||
{
|
||||
/**
|
||||
* Handle an incoming request.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param \Closure $next
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle(Request $request, Closure $next)
|
||||
{
|
||||
Nexus::boot();
|
||||
do_log(sprintf(
|
||||
"Nexus booted. request.server: %s, request.header: %s, request.query: %s, request.input: %s",
|
||||
nexus_json_encode($request->server()), nexus_json_encode($request->header()), nexus_json_encode($request->query()), nexus_json_encode($request->input())
|
||||
));
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -20,7 +20,7 @@ class Permission
|
||||
{
|
||||
/** @var User $user */
|
||||
$user = $request->user();
|
||||
if (!$user || (IS_PLATFORM_ADMIN && !$user->canAccessAdmin())) {
|
||||
if (!$user || (nexus()->isPlatformAdmin() && !$user->canAccessAdmin())) {
|
||||
do_log("denied!");
|
||||
throw new UnauthorizedException('Unauthorized!');
|
||||
}
|
||||
|
||||
@@ -17,11 +17,10 @@ class Platform
|
||||
*/
|
||||
public function handle(Request $request, Closure $next)
|
||||
{
|
||||
do_log(nexus_json_encode($_SERVER));
|
||||
if (empty(CURRENT_PLATFORM)) {
|
||||
if (empty(nexus()->getPlatform())) {
|
||||
throw new \InvalidArgumentException("Require platform header.");
|
||||
}
|
||||
if (!in_array(CURRENT_PLATFORM, PLATFORMS)) {
|
||||
if (!nexus()->isPlatformValid()) {
|
||||
throw new \InvalidArgumentException("Invalid platform: " . CURRENT_PLATFORM);
|
||||
}
|
||||
return $next($request);
|
||||
|
||||
@@ -15,5 +15,7 @@ class TrimStrings extends Middleware
|
||||
'current_password',
|
||||
'password',
|
||||
'password_confirmation',
|
||||
'peer_id',
|
||||
'info_hash',
|
||||
];
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace App\Listeners;
|
||||
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Nexus\Nexus;
|
||||
|
||||
class ResetNexus
|
||||
{
|
||||
/**
|
||||
* Handle the event.
|
||||
*
|
||||
* @param mixed $event
|
||||
* @return void
|
||||
*/
|
||||
public function handle($event): void
|
||||
{
|
||||
Nexus::flush();
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,7 @@ class NexusFormatter
|
||||
|
||||
protected function formatter()
|
||||
{
|
||||
$format = "[%datetime%] [" . REQUEST_ID . "] %channel%.%level_name%: %message% %context% %extra%\n";
|
||||
$format = "[%datetime%] [" . nexus()->getRequestId() . "] %channel%.%level_name%: %message% %context% %extra%\n";
|
||||
return tap(new LineFormatter($format, 'Y-m-d H:i:s', true, true), function ($formatter) {
|
||||
$formatter->includeStacktraces();
|
||||
});
|
||||
|
||||
@@ -6,6 +6,7 @@ use Illuminate\Support\Facades\App;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
use Nexus\Nexus;
|
||||
|
||||
class AppServiceProvider extends ServiceProvider
|
||||
{
|
||||
@@ -28,5 +29,6 @@ class AppServiceProvider extends ServiceProvider
|
||||
{
|
||||
// JsonResource::withoutWrapping();
|
||||
DB::connection(config('database.default'))->enableQueryLog();
|
||||
// Nexus::boot();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -82,8 +82,8 @@ class AgentAllowRepository extends BaseRepository
|
||||
$agentAllowId = $agentAllow->id;
|
||||
$logPrefix = "[ID: $agentAllowId]";
|
||||
$isPeerIdAllowed = $isAgentAllowed = $isPeerIdTooLow = $isAgentTooLow = false;
|
||||
//check peer_id
|
||||
if ($agentAllow->peer_id_pattern == '') {
|
||||
//check peer_id, when handle scrape request, no peer_id, so let it pass
|
||||
if ($agentAllow->peer_id_pattern == '' || $peerId === null) {
|
||||
$isPeerIdAllowed = true;
|
||||
} else {
|
||||
$pattern = $agentAllow->peer_id_pattern;
|
||||
|
||||
@@ -17,7 +17,7 @@ class AuthenticateRepository extends BaseRepository
|
||||
if (!$user || md5($user->secret . $password . $user->secret) != $user->passhash) {
|
||||
throw new \InvalidArgumentException('Username or password invalid.');
|
||||
}
|
||||
if (IS_PLATFORM_ADMIN && !$user->canAccessAdmin()) {
|
||||
if (nexus()->isPlatformAdmin() && !$user->canAccessAdmin()) {
|
||||
throw new UnauthorizedException('Unauthorized!');
|
||||
}
|
||||
$user->checkIsNormal();
|
||||
|
||||
@@ -54,7 +54,7 @@ class DashboardRepository extends BaseRepository
|
||||
$result[$name] = [
|
||||
'name' => $name,
|
||||
'text' => nexus_trans("dashboard.system_info.$name"),
|
||||
'value' => $_SERVER['SERVER_SOFTWARE'],
|
||||
'value' => $_SERVER['SERVER_SOFTWARE'] ?? '',
|
||||
];
|
||||
$name = 'load_average';
|
||||
$result[$name] = [
|
||||
|
||||
@@ -51,11 +51,10 @@ class TrackerRepository extends BaseRepository
|
||||
|
||||
public function announce(Request $request): \Illuminate\Http\Response
|
||||
{
|
||||
do_log(nexus_json_encode($_SERVER));
|
||||
do_log("queryString: " . $request->getQueryString());
|
||||
try {
|
||||
$withPeers = false;
|
||||
$queries = $this->checkAnnounceFields($request);
|
||||
do_log("[QUERIES] " . json_encode(Arr::only($queries, ['ip', 'user_agent', 'uploaded', 'downloaded', 'left', 'event'])), 'debug');
|
||||
$user = $this->checkUser($request);
|
||||
$clientAllow = $this->checkClient($request);
|
||||
$torrent = $this->checkTorrent($queries, $user);
|
||||
@@ -108,7 +107,7 @@ class TrackerRepository extends BaseRepository
|
||||
} catch (\Exception $exception) {
|
||||
//other system exception
|
||||
do_log("[" . get_class($exception) . "] " . $exception->getMessage() . "\n" . $exception->getTraceAsString(), 'error');
|
||||
$repDict = $this->generateFailedAnnounceResponse("system error, report to sysop please, hint: " . REQUEST_ID);
|
||||
$repDict = $this->generateFailedAnnounceResponse("system error, report to sysop please, hint: " . nexus()->getRequestId());
|
||||
} finally {
|
||||
if (isset($user) && count($this->userUpdates)) {
|
||||
$user->update($this->userUpdates);
|
||||
@@ -218,8 +217,8 @@ class TrackerRepository extends BaseRepository
|
||||
}
|
||||
|
||||
foreach (['info_hash', 'peer_id'] as $item) {
|
||||
if (\strlen((string) $queries[$item]) !== 20) {
|
||||
throw new TrackerException("Invalid $item ! $item is not 20 bytes long");
|
||||
if (($length = \strlen((string) $queries[$item])) !== 20) {
|
||||
throw new TrackerException("Invalid $item ! $item is not 20 bytes long($length)");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -502,7 +501,7 @@ class TrackerRepository extends BaseRepository
|
||||
unset($queries['key']);
|
||||
$lockKey = md5(http_build_query($queries));
|
||||
$redis = Redis::connection()->client();
|
||||
if (!$redis->set($lockKey, NEXUS_START, ['nx', 'ex' => self::MIN_ANNOUNCE_WAIT_SECOND])) {
|
||||
if (!$redis->set($lockKey, nexus()->getStartTimestamp(), ['nx', 'ex' => 5])) {
|
||||
do_log('[RE_ANNOUNCE]');
|
||||
return true;
|
||||
}
|
||||
@@ -654,6 +653,7 @@ class TrackerRepository extends BaseRepository
|
||||
|
||||
protected function sendFinalAnnounceResponse($repDict): \Illuminate\Http\Response
|
||||
{
|
||||
do_log("[repDict] " . nexus_json_encode($repDict));
|
||||
return \response(Bencode::encode($repDict))
|
||||
->withHeaders(['Content-Type' => 'text/plain; charset=utf-8'])
|
||||
->withHeaders(['Connection' => 'close'])
|
||||
@@ -776,7 +776,7 @@ class TrackerRepository extends BaseRepository
|
||||
|
||||
public function scrape(Request $request): \Illuminate\Http\Response
|
||||
{
|
||||
do_log(nexus_json_encode($_SERVER));
|
||||
do_log("queryString: " . $request->getQueryString());
|
||||
try {
|
||||
$infoHashArr = $this->checkScrapeFields($request);
|
||||
$user = $this->checkUser($request);
|
||||
@@ -785,6 +785,9 @@ class TrackerRepository extends BaseRepository
|
||||
if ($user->clientselect != $clientAllow->id) {
|
||||
$this->userUpdates['clientselect'] = $clientAllow->id;
|
||||
}
|
||||
if ($user->showclienterror == 'yes') {
|
||||
$this->userUpdates['showclienterror'] = 'no';
|
||||
}
|
||||
$repDict = $this->generateScrapeResponse($infoHashArr);
|
||||
} catch (ClientNotAllowedException $exception) {
|
||||
do_log("[ClientNotAllowedException] " . $exception->getMessage());
|
||||
@@ -797,8 +800,9 @@ class TrackerRepository extends BaseRepository
|
||||
} catch (\Exception $exception) {
|
||||
//other system exception
|
||||
do_log("[" . get_class($exception) . "] " . $exception->getMessage() . "\n" . $exception->getTraceAsString(), 'error');
|
||||
$repDict = $this->generateFailedAnnounceResponse("system error, report to sysop please, hint: " . REQUEST_ID);
|
||||
$repDict = $this->generateFailedAnnounceResponse("system error, report to sysop please, hint: " . nexus()->getRequestId());
|
||||
} finally {
|
||||
do_log("userUpdates: " . nexus_json_encode($this->userUpdates));
|
||||
if (isset($user) && count($this->userUpdates)) {
|
||||
$user->update($this->userUpdates);
|
||||
do_log(last_query(), 'debug');
|
||||
@@ -817,7 +821,7 @@ class TrackerRepository extends BaseRepository
|
||||
} else {
|
||||
foreach ($info_hash_array as $item) {
|
||||
if (strlen($item) != 20) {
|
||||
throw new TrackerException("Invalid info_hash ! :attribute is not 20 bytes long");
|
||||
throw new TrackerException("Invalid info_hash ! info_hash is not 20 bytes long");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -852,7 +856,7 @@ class TrackerRepository extends BaseRepository
|
||||
return Cache::remember($cacheKey, 60, function () use ($infoHash) {
|
||||
$fieldRaw = 'id, owner, sp_state, seeders, leechers, added, banned, hr, visible, last_action, times_completed';
|
||||
$torrent = Torrent::query()->where('info_hash', $infoHash)->selectRaw($fieldRaw)->first();
|
||||
do_log("cache miss, from database: " . last_query() . ", and get: " . $torrent->id);
|
||||
do_log("[getTorrentByInfoHash] cache miss, from database: " . last_query() . ", and get: " . $torrent->id);
|
||||
return $torrent;
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user