增加 模块异常回应的安全处理

自动将模块切换为维护模式并防止意外删除
This commit is contained in:
iVampireSP.com 2023-01-19 03:04:16 +08:00
parent 6849d551e6
commit 9bb969577e
No known key found for this signature in database
GPG Key ID: 2F7B001CA27A8132
3 changed files with 18 additions and 13 deletions

View File

@ -50,10 +50,10 @@ public function handle(): void
break;
case 'delete':
$response = $host->module->http()->delete('hosts/' . $host->id);
$response = $host->module->baseRequest('delete', 'hosts/' . $host->id);
// if successful
if ($response->successful() || $response->status() === 404) {
if ($response['status'] === 404) {
$host->delete();
}

View File

@ -36,15 +36,11 @@ public function handle(): void
{
$host = $this->host;
$response = $host->module->http()->get('hosts/' . $host->id);
$response = $host->module->baseRequest('get', 'hosts/' . $host->id);
$status = $response->status();
$json = $response->json();
if ($status === 200) {
$host->update(Arr::except($json, ['id', 'user_id', 'module_id', 'created_at', 'updated_at']));
} else if ($status === 404) {
if ($response['status'] === 200) {
$host->update(Arr::except($response['json'], ['id', 'user_id', 'module_id', 'created_at', 'updated_at']));
} else if ($response['status'] === 404) {
Log::warning($host->module->name . ' ' . $host->name . ' ' . $host->id . ' 不存在,删除。');
dispatch(new HostJob($host, 'delete'));
}

View File

@ -76,14 +76,23 @@ public function http(): PendingRequest
private function getResponse(Response $response): array
{
$json = $response->json();
$status = $response->status();
$module_token = $response->header('x-module-api-token');
$success = true;
$json = $response->json();
$status = $response->status();
// if status code is not 20x
if ($status < 200 || $status >= 300) {
$success = false;
// 防止误删除
if ($module_token !== $this->api_token) {
$this->status = 'maintenance';
$this->save();
$status = 401;
}
}
return [
@ -99,7 +108,7 @@ public function request($method, $path, $requests): array
return $this->baseRequest($method, "functions/$path", $requests);
}
public function baseRequest($method, $path, $requests): array
public function baseRequest($method, $path, $requests = []): array
{
$user = auth('sanctum')->user();