Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

华为云CDN缓存刷新 #218

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions plugins/refresh-huawei-cdn/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# refresh-huawei-cdn

这个插件可以在用户更新其角色信息时,自动发送华为云CDN刷新缓存的API。

## 配置

你需要在 .env 中配置这些参数

- `HUAWEI_CLOUD_USERNAME` - IAM用户名
- `HUAWEI_CLOUD_PASSWORD` - IAM用户密码
- `HUAWEI_CLOUD_DOMAIN_NAME` - IAM用户所属账号名
- `HUAWEI_CLOUD_PROJECT_NAME` - 项目。在 “我的凭证” - “API凭证”页面可以看到,任选。
- `HUAWEI_CLOUD_IAM_BASE_URL` - 终端节点地址。在 https://console-intl.huaweicloud.com/apiexplorer/#/endpoint/IAM 查看您所选择的项目对应的节点地址,项目和区域同名。
- `HUAWEI_CLOUD_CDN_BASE_URL` - 网站地址,例如 https://example.com,一定**不要**在末尾加斜杠

## 注意
请先判断当前账号是华为账号还是华为云账号,可以根据https://support.huaweicloud.com/account_faq/faq_id_0009.html判断。华为账号获取Token需要创建IAM账户,授予该用户必要的权限,可以查看https://support.huaweicloud.com/api-iam/iam_30_0001.html。华为云账号获取Token无特殊要求。
111 changes: 111 additions & 0 deletions plugins/refresh-huawei-cdn/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;

return function (Dispatcher $events) {
$events->listen(App\Events\PlayerProfileUpdated::class, function ($event) {
// 获取配置
$baseUrl = env('HUAWEI_CLOUD_CDN_BASE_URL');
$iamBaseUrl = env('HUAWEI_CLOUD_IAM_BASE_URL');
$enterpriseProjectId = env('HUAWEI_CLOUD_ENTERPRISE_PROJECT_ID', '0');
$username = env('HUAWEI_CLOUD_USERNAME');
$password = env('HUAWEI_CLOUD_PASSWORD');
$domainName = env('HUAWEI_CLOUD_DOMAIN_NAME');
$projectName = env('HUAWEI_CLOUD_PROJECT_NAME');

// 从缓存中获取Token,如果不存在则获取新的Token
$authToken = Cache::remember('huawei_cloud_auth_token', 23 * 60, function () use ($iamBaseUrl, $username, $password, $domainName, $projectName) {
$response = Http::post($iamBaseUrl . '/v3/auth/tokens', [
'auth' => [
'identity' => [
'methods' => ['password'],
'password' => [
'user' => [
'domain' => ['name' => $domainName],
'name' => $username,
'password' => $password,
],
],
],
'scope' => [
'project' => ['name' => $projectName],
],
],
]);

if ($response->successful()) {
return $response->header('X-Subject-Token');
} else {
Log::error('Failed to obtain Huawei Cloud Token: ' . $response->body());
return null;
}
});

if (!$authToken) {
return;
}

// 检测插件
$usm = plugin('usm-api');
$legacy = plugin('legacy-api');
$yggdrasil = plugin('yggdrasil-api');

$name = $event->player->name;
$urls = [
$baseUrl.'/'.$name.'.json',
$baseUrl.'/csl/'.$name.'.json',
];

if (isset($usm) && $usm->isEnabled()) {
$urls[] = $baseUrl.'/usm/'.$name.'.json';
}

if (isset($legacy) && $legacy->isEnabled()) {
array_push(
$urls,
$baseUrl.'/skin/'.$name.'.png',
$baseUrl.'/cape/'.$name.'.png'
);
}

if (isset($yggdrasil) && $yggdrasil->isEnabled()) {
$uuid = DB::table('uuid')->where('name', $name)->value('uuid');
array_push(
$urls,
$baseUrl.'/api/yggdrasil/sessionserver/session/minecraft/profile/'.$uuid,
$baseUrl.'/api/yggdrasil/sessionserver/session/minecraft/profile/'.$uuid.'?unsigned=false',
$baseUrl.'/api/yggdrasil/sessionserver/session/minecraft/profile/'.$uuid.'?unsigned=true'
);
}

$requestBody = [
'refresh_task' => [
'type' => 'file',
'urls' => $urls,
],
];

// 请求URL
$requestUrl = 'https://cdn.myhuaweicloud.com/v1.0/cdn/content/refresh-tasks';

if ($enterpriseProjectId !== '0') {
$requestUrl .= '?enterprise_project_id=' . $enterpriseProjectId;
}

$response = Http::withHeaders([
'X-Auth-Token' => $authToken,
'Content-Type' => 'application/json',
])->post($requestUrl, $requestBody);

if ($response->successful()) {
$refreshTaskId = $response->json('refresh_task');

} else {
// 处理错误
Log::error('Failed to refresh Huawei Cloud CDN cache: ' . $response->body());
}
});
};
17 changes: 17 additions & 0 deletions plugins/refresh-huawei-cdn/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "refresh-huawei-cdn",
"version": "1.0.0",
"title": "刷新华为云 CDN",
"description": "当角色信息被更新时,自动刷新华为云 CDN 缓存",
"author": "SRY-CTB",
"namespace": "SRY-CTB\\RefreshHuaweiCdn",
"require": {
"blessing-skin-server": "^5|^6"
},
"enchants": {
"icon": {
"fa": "fire",
"bg": "lime"
}
}
}