Skip to content

Commit

Permalink
Detect if IP is SeedBox
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaomlove committed Nov 21, 2024
1 parent c864a34 commit c537ebc
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 6 deletions.
20 changes: 20 additions & 0 deletions app/Exceptions/SeedBoxYesException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\Exceptions;

class SeedBoxYesException extends NexusException
{
private int $id;

public function __construct($id)
{
parent::__construct();
$this->id = $id;
}

public function getId(): int
{
return $this->id;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -15,6 +16,24 @@ protected function getActions(): array
{
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());
}
})
];
}
}
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.14');
defined('RELEASE_DATE') || define('RELEASE_DATE', '2024-11-08');
defined('RELEASE_DATE') || define('RELEASE_DATE', '2024-11-22');
defined('IN_TRACKER') || define('IN_TRACKER', false);
defined('PROJECTNAME') || define("PROJECTNAME","NexusPHP");
defined('NEXUSPHPURL') || define("NEXUSPHPURL","https://nexusphp.org");
Expand Down
28 changes: 24 additions & 4 deletions include/globalfunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -857,7 +857,7 @@ function do_action($name, ...$args)
return $hook->doAction(...func_get_args());
}

function isIPSeedBoxFromASN($ip): bool
function isIPSeedBoxFromASN($ip, $exceptionWhenYes = false): bool
{
try {
static $reader;
Expand All @@ -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);
Expand All @@ -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");
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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';
}
2 changes: 2 additions & 0 deletions resources/lang/en/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@
],
'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',
Expand Down
2 changes: 2 additions & 0 deletions resources/lang/en/seed-box.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@
'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',
];
2 changes: 2 additions & 0 deletions resources/lang/zh_CN/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@
],
'seed_box_record' => [
'toggle_status' => '更改状态',
'check_modal_btn' => '检测',
'check_modal_header' => '检测 IP 是否为 SeedBox',
],
'tag' => [
'detach_torrents' => '分离所有种子',
Expand Down
2 changes: 2 additions & 0 deletions resources/lang/zh_CN/seed-box.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@
'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',
];
2 changes: 2 additions & 0 deletions resources/lang/zh_TW/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@
],
'seed_box_record' => [
'toggle_status' => '更改狀態',
'check_modal_btn' => '檢測',
'check_modal_header' => '檢測 IP 是否為 SeedBox',
],
'tag' => [
'detach_torrents' => '分離所有種子',
Expand Down
2 changes: 2 additions & 0 deletions resources/lang/zh_TW/seed-box.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@
'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',
];

0 comments on commit c537ebc

Please sign in to comment.