diff --git a/app/Exceptions/SeedBoxYesException.php b/app/Exceptions/SeedBoxYesException.php new file mode 100644 index 00000000..1b0c499d --- /dev/null +++ b/app/Exceptions/SeedBoxYesException.php @@ -0,0 +1,20 @@ +id = $id; + } + + public function getId(): int + { + return $this->id; + } +} + diff --git a/app/Filament/Resources/System/SeedBoxRecordResource/Pages/ListSeedBoxRecords.php b/app/Filament/Resources/System/SeedBoxRecordResource/Pages/ListSeedBoxRecords.php index 5adaab52..65d4d6bf 100644 --- a/app/Filament/Resources/System/SeedBoxRecordResource/Pages/ListSeedBoxRecords.php +++ b/app/Filament/Resources/System/SeedBoxRecordResource/Pages/ListSeedBoxRecords.php @@ -2,10 +2,11 @@ namespace App\Filament\Resources\System\SeedBoxRecordResource\Pages; +use App\Exceptions\SeedBoxYesException; use App\Filament\PageList; use App\Filament\Resources\System\SeedBoxRecordResource; use Filament\Pages\Actions; -use Filament\Resources\Pages\ListRecords; +use Filament\Forms; class ListSeedBoxRecords extends PageList { @@ -15,6 +16,24 @@ class ListSeedBoxRecords extends PageList { return [ Actions\CreateAction::make(), + Actions\Action::make('check') + ->label(__('admin.resources.seed_box_record.check_modal_btn')) + ->form([ + Forms\Components\TextInput::make('ip')->required()->label('IP'), + Forms\Components\TextInput::make('uid')->required()->label('UID'), + ]) + ->modalHeading(__('admin.resources.seed_box_record.check_modal_header')) + ->action(function ($data) { + try { + isIPSeedBox($data['ip'], $data['uid'], true, true); + $this->notify('success', nexus_trans("seed-box.is_seed_box_no")); + } catch (SeedBoxYesException $exception) { + $this->notify('danger', nexus_trans("seed-box.is_seed_box_yes", ['id' => $exception->getId()])); + } catch (\Throwable $throwable) { + do_log($throwable->getMessage() . $throwable->getTraceAsString(), "error"); + $this->notify('danger', $throwable->getMessage()); + } + }) ]; } } diff --git a/include/constants.php b/include/constants.php index 98b0f7cf..db8121eb 100644 --- a/include/constants.php +++ b/include/constants.php @@ -1,6 +1,6 @@ doAction(...func_get_args()); } -function isIPSeedBoxFromASN($ip): bool +function isIPSeedBoxFromASN($ip, $exceptionWhenYes = false): bool { try { static $reader; @@ -875,14 +875,18 @@ function isIPSeedBoxFromASN($ip): bool return false; } $row = \Nexus\Database\NexusDB::getOne("seed_box_records", "asn = $asn", "id"); - return !empty($row); } catch (\Throwable $throwable) { do_log("ip: $ip, error: " . $throwable->getMessage(), "error"); return false; } + $result = !empty($row); + if ($result && $exceptionWhenYes) { + throw new \App\Exceptions\SeedBoxYesException($row['id']); + } + return $result; } -function isIPSeedBox($ip, $uid, $withoutCache = false): bool +function isIPSeedBox($ip, $uid, $withoutCache = false, $exceptionWhenYes = false): bool { $key = "nexus_is_ip_seed_box:ip:$ip:uid:$uid"; $cacheData = \Nexus\Database\NexusDB::cache_get($key); @@ -891,7 +895,7 @@ function isIPSeedBox($ip, $uid, $withoutCache = false): bool return (bool)$cacheData; } //check from asn - $res = isIPSeedBoxFromASN($ip); + $res = isIPSeedBoxFromASN($ip, $exceptionWhenYes); if (!empty($res)) { \Nexus\Database\NexusDB::cache_put($key, 1, 300); do_log("$key, get result from asn, true"); @@ -920,6 +924,9 @@ function isIPSeedBox($ip, $uid, $withoutCache = false): bool if (!empty($res)) { \Nexus\Database\NexusDB::cache_put($key, 1, 300); do_log("$key, get result from admin, true"); + if ($exceptionWhenYes) { + throw new \App\Exceptions\SeedBoxYesException($res[0]['id']); + } return true; } if ($uid !== null) { @@ -931,6 +938,9 @@ function isIPSeedBox($ip, $uid, $withoutCache = false): bool if (!empty($res)) { \Nexus\Database\NexusDB::cache_put($key, 1, 300); do_log("$key, get result from user, true"); + if ($exceptionWhenYes) { + throw new \App\Exceptions\SeedBoxYesException($res[0]['id']); + } return true; } } @@ -1313,3 +1323,13 @@ function convertNamespaceToSnake(string $str): string { return str_replace(["\\", "::"], ["_", "."], $str); } + +function get_user_locale(int $uid): string +{ + $sql = "select language.site_lang_folder from useers inner join language on users.lang = language.id where users.id = $uid limit 1"; + $result = \Nexus\Database\NexusDB::select($sql); + if (empty($result) || empty($result['site_lang_folder'])) { + return "en"; + } + return \App\Http\Middleware\Locale::$languageMaps[$result['site_lang_folder']] ?? 'en'; +} diff --git a/resources/lang/en/admin.php b/resources/lang/en/admin.php index 3ecb402d..ab571e68 100644 --- a/resources/lang/en/admin.php +++ b/resources/lang/en/admin.php @@ -118,6 +118,8 @@ return [ ], 'seed_box_record' => [ 'toggle_status' => 'Change status', + 'check_modal_btn' => 'Check', + 'check_modal_header' => 'Detect if IP is SeedBox', ], 'tag' => [ 'detach_torrents' => 'Detach all torrents', diff --git a/resources/lang/en/seed-box.php b/resources/lang/en/seed-box.php index c4632fbb..25d6d20f 100644 --- a/resources/lang/en/seed-box.php +++ b/resources/lang/en/seed-box.php @@ -14,4 +14,6 @@ return [ 'subject' => 'SeedBox record status changed', 'body' => 'The status of your SeedBox record with ID :id was changed by :operator from :old_status to :new_status. Reason: :reason', ], + 'is_seed_box_yes' => 'This IP is SeedBox, identified by the record with ID :id', + 'is_seed_box_no' => 'This IP is not SeedBox', ]; diff --git a/resources/lang/zh_CN/admin.php b/resources/lang/zh_CN/admin.php index 610581c6..b08da80e 100644 --- a/resources/lang/zh_CN/admin.php +++ b/resources/lang/zh_CN/admin.php @@ -116,6 +116,8 @@ return [ ], 'seed_box_record' => [ 'toggle_status' => '更改状态', + 'check_modal_btn' => '检测', + 'check_modal_header' => '检测 IP 是否为 SeedBox', ], 'tag' => [ 'detach_torrents' => '分离所有种子', diff --git a/resources/lang/zh_CN/seed-box.php b/resources/lang/zh_CN/seed-box.php index 4416632c..33613956 100644 --- a/resources/lang/zh_CN/seed-box.php +++ b/resources/lang/zh_CN/seed-box.php @@ -14,4 +14,6 @@ return [ 'subject' => 'SeedBox 记录状态变更', 'body' => '你的 ID 为 :id 的 SeedBox 记录状态被 :operator 由 :old_status 变更为 :new_status。原因::reason', ], + 'is_seed_box_yes' => '此 IP 是 SeedBox, 由 ID 为 :id 的记录确定', + 'is_seed_box_no' => '此 IP 不是 SeedBox', ]; diff --git a/resources/lang/zh_TW/admin.php b/resources/lang/zh_TW/admin.php index c0c2ef37..4c7f13f5 100644 --- a/resources/lang/zh_TW/admin.php +++ b/resources/lang/zh_TW/admin.php @@ -118,6 +118,8 @@ return [ ], 'seed_box_record' => [ 'toggle_status' => '更改狀態', + 'check_modal_btn' => '檢測', + 'check_modal_header' => '檢測 IP 是否為 SeedBox', ], 'tag' => [ 'detach_torrents' => '分離所有種子', diff --git a/resources/lang/zh_TW/seed-box.php b/resources/lang/zh_TW/seed-box.php index d2747782..b02352b2 100644 --- a/resources/lang/zh_TW/seed-box.php +++ b/resources/lang/zh_TW/seed-box.php @@ -14,4 +14,6 @@ return [ 'subject' => 'SeedBox 記錄狀態變更', 'body' => '你的 ID 為 :id 的 SeedBox 記錄狀態被 :operator 由 :old_status 變更為 :new_status。原因::reason', ], + 'is_seed_box_yes' => '此 IP 是 SeedBox, 由 ID 為 :id 的記錄確定', + 'is_seed_box_no' => '此 IP 不是 SeedBox', ];