ff50030364
新增定时墙检测命令与节点托管字段,自动为开启托管的父 节点创建检测任务,并在 blocked 时自动隐藏节点、normal 时仅恢复由墙检测自动隐藏的节点 更新自动上线服务以尊重 blocked 与自动隐藏状态,避免疑 似被墙节点被重新发布;同时补齐管理端墙检测托管开关、 刷新入口、批量设置与相关测试和知识库同步
134 lines
4.4 KiB
PHP
134 lines
4.4 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit;
|
|
|
|
use App\Models\Server;
|
|
use App\Models\ServerGfwCheck;
|
|
use App\Services\ServerGfwCheckService;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Tests\TestCase;
|
|
|
|
class ServerGfwCheckServiceTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
public function test_start_automatic_checks_only_enqueues_enabled_parent_nodes_without_active_task(): void
|
|
{
|
|
$eligible = $this->makeServer(['name' => 'eligible-parent']);
|
|
$active = $this->makeServer(['name' => 'active-parent']);
|
|
$this->makeServer([
|
|
'name' => 'disabled-parent',
|
|
'gfw_check_enabled' => false,
|
|
]);
|
|
$this->makeServer([
|
|
'name' => 'child-node',
|
|
'parent_id' => $eligible->id,
|
|
]);
|
|
|
|
ServerGfwCheck::create([
|
|
'server_id' => $active->id,
|
|
'status' => ServerGfwCheck::STATUS_PENDING,
|
|
]);
|
|
|
|
$result = app(ServerGfwCheckService::class)->startAutomaticChecks();
|
|
|
|
$this->assertSame(2, $result['total']);
|
|
$this->assertSame(1, $result['active']);
|
|
$this->assertSame([$eligible->id], array_column($result['started'], 'id'));
|
|
$this->assertCount(1, $result['skipped']);
|
|
$this->assertDatabaseHas('server_gfw_checks', [
|
|
'server_id' => $eligible->id,
|
|
'status' => ServerGfwCheck::STATUS_PENDING,
|
|
]);
|
|
}
|
|
|
|
public function test_report_result_hides_blocked_nodes_and_restores_only_auto_hidden_nodes(): void
|
|
{
|
|
$parent = $this->makeServer([
|
|
'name' => 'parent',
|
|
'show' => true,
|
|
]);
|
|
$visibleChild = $this->makeServer([
|
|
'name' => 'visible-child',
|
|
'parent_id' => $parent->id,
|
|
'show' => true,
|
|
]);
|
|
$manualHiddenChild = $this->makeServer([
|
|
'name' => 'manual-hidden-child',
|
|
'parent_id' => $parent->id,
|
|
'show' => false,
|
|
]);
|
|
|
|
$service = app(ServerGfwCheckService::class);
|
|
$blockedCheck = ServerGfwCheck::create([
|
|
'server_id' => $parent->id,
|
|
'status' => ServerGfwCheck::STATUS_PENDING,
|
|
]);
|
|
|
|
$this->assertTrue($service->reportResult($parent, [
|
|
'check_id' => $blockedCheck->id,
|
|
'operator_summary' => $this->blockedOperators(),
|
|
]));
|
|
|
|
$this->assertFalse($parent->fresh()->show);
|
|
$this->assertTrue($parent->fresh()->gfw_auto_hidden);
|
|
$this->assertFalse($visibleChild->fresh()->show);
|
|
$this->assertTrue($visibleChild->fresh()->gfw_auto_hidden);
|
|
$this->assertFalse($manualHiddenChild->fresh()->show);
|
|
$this->assertFalse($manualHiddenChild->fresh()->gfw_auto_hidden);
|
|
|
|
$normalCheck = ServerGfwCheck::create([
|
|
'server_id' => $parent->id,
|
|
'status' => ServerGfwCheck::STATUS_PENDING,
|
|
]);
|
|
|
|
$this->assertTrue($service->reportResult($parent->fresh(), [
|
|
'check_id' => $normalCheck->id,
|
|
'operator_summary' => $this->normalOperators(),
|
|
]));
|
|
|
|
$this->assertTrue($parent->fresh()->show);
|
|
$this->assertFalse($parent->fresh()->gfw_auto_hidden);
|
|
$this->assertTrue($visibleChild->fresh()->show);
|
|
$this->assertFalse($visibleChild->fresh()->gfw_auto_hidden);
|
|
$this->assertFalse($manualHiddenChild->fresh()->show);
|
|
$this->assertFalse($manualHiddenChild->fresh()->gfw_auto_hidden);
|
|
}
|
|
|
|
private function makeServer(array $attributes = []): Server
|
|
{
|
|
return Server::create(array_merge([
|
|
'name' => 'test-node',
|
|
'type' => Server::TYPE_VMESS,
|
|
'host' => '127.0.0.1',
|
|
'port' => 443,
|
|
'server_port' => 443,
|
|
'rate' => '1',
|
|
'group_ids' => [1],
|
|
'show' => true,
|
|
'auto_online' => false,
|
|
'gfw_check_enabled' => true,
|
|
'gfw_auto_hidden' => false,
|
|
'enabled' => true,
|
|
], $attributes));
|
|
}
|
|
|
|
private function blockedOperators(): array
|
|
{
|
|
return [
|
|
'ct' => ['total' => 2, 'success' => 0],
|
|
'cu' => ['total' => 2, 'success' => 0],
|
|
'cm' => ['total' => 2, 'success' => 0],
|
|
];
|
|
}
|
|
|
|
private function normalOperators(): array
|
|
{
|
|
return [
|
|
'ct' => ['total' => 2, 'success' => 2],
|
|
'cu' => ['total' => 2, 'success' => 2],
|
|
'cm' => ['total' => 2, 'success' => 2],
|
|
];
|
|
}
|
|
}
|