add is_allowed to seed box records

This commit is contained in:
xiaomlove
2023-01-31 18:23:20 +08:00
parent 9c0f458920
commit a3793f4b46
7 changed files with 65 additions and 4 deletions

View File

@@ -2,6 +2,7 @@
namespace App\Filament\Resources\System; namespace App\Filament\Resources\System;
use App\Filament\OptionsTrait;
use App\Filament\Resources\System\SeedBoxRecordResource\Pages; use App\Filament\Resources\System\SeedBoxRecordResource\Pages;
use App\Filament\Resources\System\SeedBoxRecordResource\RelationManagers; use App\Filament\Resources\System\SeedBoxRecordResource\RelationManagers;
use App\Models\SeedBoxRecord; use App\Models\SeedBoxRecord;
@@ -19,6 +20,8 @@ use PhpIP\IP;
class SeedBoxRecordResource extends Resource class SeedBoxRecordResource extends Resource
{ {
use OptionsTrait;
protected static ?string $model = SeedBoxRecord::class; protected static ?string $model = SeedBoxRecord::class;
protected static ?string $navigationIcon = 'heroicon-o-archive'; protected static ?string $navigationIcon = 'heroicon-o-archive';
@@ -46,6 +49,10 @@ class SeedBoxRecordResource extends Resource
Forms\Components\TextInput::make('ip_begin')->label(__('label.seed_box_record.ip_begin')), Forms\Components\TextInput::make('ip_begin')->label(__('label.seed_box_record.ip_begin')),
Forms\Components\TextInput::make('ip_end')->label(__('label.seed_box_record.ip_end')), Forms\Components\TextInput::make('ip_end')->label(__('label.seed_box_record.ip_end')),
Forms\Components\TextInput::make('ip')->label(__('label.seed_box_record.ip'))->helperText(__('label.seed_box_record.ip_help')), Forms\Components\TextInput::make('ip')->label(__('label.seed_box_record.ip'))->helperText(__('label.seed_box_record.ip_help')),
Forms\Components\Toggle::make('is_allowed')
->label(__('label.seed_box_record.is_allowed'))
->helperText(__('label.seed_box_record.is_allowed_help'))
,
Forms\Components\Textarea::make('comment')->label(__('label.comment')), Forms\Components\Textarea::make('comment')->label(__('label.comment')),
])->columns(1); ])->columns(1);
} }
@@ -74,7 +81,8 @@ class SeedBoxRecordResource extends Resource
} }
}) })
->formatStateUsing(fn ($record) => $record->ip ?: sprintf('%s ~ %s', $record->ip_begin, $record->ip_end)), ->formatStateUsing(fn ($record) => $record->ip ?: sprintf('%s ~ %s', $record->ip_begin, $record->ip_end)),
Tables\Columns\TextColumn::make('comment')->label(__('label.comment')), Tables\Columns\TextColumn::make('comment')->label(__('label.comment'))->searchable(),
Tables\Columns\IconColumn::make('is_allowed')->boolean()->label(__('label.seed_box_record.is_allowed')),
Tables\Columns\BadgeColumn::make('status') Tables\Columns\BadgeColumn::make('status')
->colors([ ->colors([
'success' => SeedBoxRecord::STATUS_ALLOWED, 'success' => SeedBoxRecord::STATUS_ALLOWED,
@@ -96,7 +104,9 @@ class SeedBoxRecordResource extends Resource
}) })
, ,
Tables\Filters\SelectFilter::make('type')->options(SeedBoxRecord::listTypes('text'))->label(__('label.seed_box_record.type')), Tables\Filters\SelectFilter::make('type')->options(SeedBoxRecord::listTypes('text'))->label(__('label.seed_box_record.type')),
Tables\Filters\SelectFilter::make('is_allowed')->options(self::getYesNoOptions())->label(__('label.seed_box_record.is_allowed')),
Tables\Filters\SelectFilter::make('status')->options(SeedBoxRecord::listStatus('text'))->label(__('label.seed_box_record.status')), Tables\Filters\SelectFilter::make('status')->options(SeedBoxRecord::listStatus('text'))->label(__('label.seed_box_record.status')),
]) ])
->actions([ ->actions([
Tables\Actions\EditAction::make(), Tables\Actions\EditAction::make(),

View File

@@ -6,7 +6,9 @@ use Illuminate\Database\Eloquent\Casts\Attribute;
class SeedBoxRecord extends NexusModel class SeedBoxRecord extends NexusModel
{ {
protected $fillable = ['type', 'uid', 'status', 'operator', 'bandwidth', 'ip', 'ip_begin', 'ip_end', 'ip_begin_numeric', 'ip_end_numeric', 'comment', 'version']; protected $fillable = ['type', 'uid', 'status', 'operator', 'bandwidth', 'ip', 'ip_begin', 'ip_end', 'ip_begin_numeric', 'ip_end_numeric',
'comment', 'version', 'is_allowed',
];
public $timestamps = true; public $timestamps = true;

View File

@@ -0,0 +1,32 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('seed_box_records', function (Blueprint $table) {
$table->integer('is_allowed')->default(0);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('seed_box_records', function (Blueprint $table) {
$table->dropColumn(['is_allowed']);
});
}
};

View File

@@ -836,8 +836,19 @@ function isIPSeedBox($ip, $uid = null, $withoutCache = false): bool
$ipObject = \PhpIP\IP::create($ip); $ipObject = \PhpIP\IP::create($ip);
$ipNumeric = $ipObject->numeric(); $ipNumeric = $ipObject->numeric();
$ipVersion = $ipObject->getVersion(); $ipVersion = $ipObject->getVersion();
//check allow list first, not consider specific user
$checkSeedBoxAllowedSql = sprintf(
'select id from seed_box_records where `ip_begin_numeric` <= "%s" and `ip_end_numeric` >= "%s" and `version` = %s and `status` = %s and `is_allowed` = 1 limit 1',
$ipNumeric, $ipNumeric, $ipVersion, \App\Models\SeedBoxRecord::STATUS_ALLOWED
);
$res = \Nexus\Database\NexusDB::select($checkSeedBoxAllowedSql);
if (!empty($res)) {
\Nexus\Database\NexusDB::cache_put($key, 1, 300);
do_log("$key, get result from database, is_allowed = 1, false");
return false;
}
$checkSeedBoxAdminSql = sprintf( $checkSeedBoxAdminSql = sprintf(
'select id from seed_box_records where `ip_begin_numeric` <= "%s" and `ip_end_numeric` >= "%s" and `type` = %s and `version` = %s and `status` = %s limit 1', 'select id from seed_box_records where `ip_begin_numeric` <= "%s" and `ip_end_numeric` >= "%s" and `type` = %s and `version` = %s and `status` = %s and `is_allowed` = 0 limit 1',
$ipNumeric, $ipNumeric, \App\Models\SeedBoxRecord::TYPE_ADMIN, $ipVersion, \App\Models\SeedBoxRecord::STATUS_ALLOWED $ipNumeric, $ipNumeric, \App\Models\SeedBoxRecord::TYPE_ADMIN, $ipVersion, \App\Models\SeedBoxRecord::STATUS_ALLOWED
); );
$res = \Nexus\Database\NexusDB::select($checkSeedBoxAdminSql); $res = \Nexus\Database\NexusDB::select($checkSeedBoxAdminSql);
@@ -848,7 +859,7 @@ function isIPSeedBox($ip, $uid = null, $withoutCache = false): bool
} }
if ($uid !== null) { if ($uid !== null) {
$checkSeedBoxUserSql = sprintf( $checkSeedBoxUserSql = sprintf(
'select id from seed_box_records where `ip_begin_numeric` <= "%s" and `ip_end_numeric` >= "%s" and `uid` = %s and `type` = %s and `version` = %s and `status` = %s limit 1', 'select id from seed_box_records where `ip_begin_numeric` <= "%s" and `ip_end_numeric` >= "%s" and `uid` = %s and `type` = %s and `version` = %s and `status` = %s and `is_allowed` = 0 limit 1',
$ipNumeric, $ipNumeric, $uid, \App\Models\SeedBoxRecord::TYPE_USER, $ipVersion, \App\Models\SeedBoxRecord::STATUS_ALLOWED $ipNumeric, $ipNumeric, $uid, \App\Models\SeedBoxRecord::TYPE_USER, $ipVersion, \App\Models\SeedBoxRecord::STATUS_ALLOWED
); );
$res = \Nexus\Database\NexusDB::select($checkSeedBoxUserSql); $res = \Nexus\Database\NexusDB::select($checkSeedBoxUserSql);

View File

@@ -233,6 +233,8 @@ return [
'ip_end' => 'End IP', 'ip_end' => 'End IP',
'ip_help' => 'Fill in the begin IP + end IP, or IP (Block), not both', 'ip_help' => 'Fill in the begin IP + end IP, or IP (Block), not both',
'status' => 'Status', 'status' => 'Status',
'is_allowed' => 'Is whitelisted',
'is_allowed_help' => 'IPs in the whitelist are not affected by the SeedBox rule',
], ],
'menu' => [ 'menu' => [
'label' => 'Custom menu', 'label' => 'Custom menu',

View File

@@ -239,6 +239,8 @@ return [
'ip_end' => '结束 IP', 'ip_end' => '结束 IP',
'ip_help' => '填写起始 IP + 结束 IP或 IP(段),不要同时填写', 'ip_help' => '填写起始 IP + 结束 IP或 IP(段),不要同时填写',
'status' => '状态', 'status' => '状态',
'is_allowed' => '是否白名单',
'is_allowed_help' => '位于白名单中的 IP 不受 SeedBox 规则影响',
], ],
'menu' => [ 'menu' => [
'label' => '自定义菜单', 'label' => '自定义菜单',

View File

@@ -233,6 +233,8 @@ return [
'ip_end' => '結束 IP', 'ip_end' => '結束 IP',
'ip_help' => '填寫起始 IP + 結束 IP或 IP(段),不要同時填寫', 'ip_help' => '填寫起始 IP + 結束 IP或 IP(段),不要同時填寫',
'status' => '狀態', 'status' => '狀態',
'is_allowed' => '是否白名單',
'is_allowed_help' => '位於白名單中的 IP 不受 SeedBox 規則影響',
], ],
'menu' => [ 'menu' => [
'label' => '自定義菜單', 'label' => '自定義菜單',