mirror of
https://github.com/lkddi/Xboard.git
synced 2026-04-05 12:40:52 +08:00
feat: optimize settings management and admin functionality
- Add system log cleanup functionality with batch processing - Optimize v2_settings table performance by unifying value storage - Add comprehensive client support list for one-click subscription - Fix QR code subscription links for specific node types - Fix route addition issues in admin management panel - Enhance admin system controller with log management APIs
This commit is contained in:
@@ -6,7 +6,6 @@ use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Admin\ConfigSave;
|
||||
use App\Protocols\Clash;
|
||||
use App\Protocols\ClashMeta;
|
||||
use App\Protocols\Loon;
|
||||
use App\Protocols\SingBox;
|
||||
use App\Protocols\Stash;
|
||||
use App\Protocols\Surfboard;
|
||||
@@ -17,7 +16,6 @@ use App\Services\ThemeService;
|
||||
use App\Utils\Dict;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class ConfigController extends Controller
|
||||
{
|
||||
@@ -84,22 +82,29 @@ class ConfigController extends Controller
|
||||
return $this->success(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取自定义规则文件路径,如果不存在则返回默认文件路径
|
||||
*
|
||||
* @param string $customFile 自定义规则文件路径
|
||||
* @param string $defaultFile 默认文件名
|
||||
* @return string 文件名
|
||||
*/
|
||||
private function getRuleFile(string $customFile, string $defaultFile): string
|
||||
{
|
||||
return File::exists(base_path($customFile)) ? $customFile : $defaultFile;
|
||||
}
|
||||
|
||||
public function fetch(Request $request)
|
||||
{
|
||||
$key = $request->input('key');
|
||||
$data = [
|
||||
|
||||
// 构建配置数据映射
|
||||
$configMappings = $this->getConfigMappings();
|
||||
|
||||
// 如果请求特定分组,直接返回
|
||||
if ($key && isset($configMappings[$key])) {
|
||||
return $this->success([$key => $configMappings[$key]]);
|
||||
}
|
||||
|
||||
return $this->success($configMappings);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取配置映射数据
|
||||
*
|
||||
* @return array 配置映射数组
|
||||
*/
|
||||
private function getConfigMappings(): array
|
||||
{
|
||||
return [
|
||||
'invite' => [
|
||||
'invite_force' => (bool) admin_setting('invite_force', 0),
|
||||
'invite_commission' => admin_setting('invite_commission', 10),
|
||||
@@ -141,7 +146,6 @@ class ConfigController extends Controller
|
||||
'default_remind_expire' => (bool) admin_setting('default_remind_expire', 1),
|
||||
'default_remind_traffic' => (bool) admin_setting('default_remind_traffic', 1),
|
||||
'subscribe_path' => admin_setting('subscribe_path', 's'),
|
||||
|
||||
],
|
||||
'frontend' => [
|
||||
'frontend_theme' => admin_setting('frontend_theme', 'Xboard'),
|
||||
@@ -197,70 +201,23 @@ class ConfigController extends Controller
|
||||
'password_limit_expire' => admin_setting('password_limit_expire', 60)
|
||||
],
|
||||
'subscribe_template' => [
|
||||
'subscribe_template_singbox' => (function () {
|
||||
$content = $this->getTemplateContent(
|
||||
$this->getRuleFile(SingBox::CUSTOM_TEMPLATE_FILE, SingBox::DEFAULT_TEMPLATE_FILE));
|
||||
return json_encode(json_decode($content), JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||
})(),
|
||||
'subscribe_template_clash' => (string) $this->getTemplateContent(
|
||||
$this->getRuleFile(Clash::CUSTOM_TEMPLATE_FILE, Clash::DEFAULT_TEMPLATE_FILE)
|
||||
'subscribe_template_singbox' => $this->formatTemplateContent(
|
||||
admin_setting('subscribe_template_singbox', $this->getDefaultTemplate('singbox')),
|
||||
'json'
|
||||
),
|
||||
'subscribe_template_clashmeta' => (string) $this->getTemplateContent(
|
||||
$this->getRuleFile(
|
||||
ClashMeta::CUSTOM_TEMPLATE_FILE,
|
||||
$this->getRuleFile(ClashMeta::CUSTOM_CLASH_TEMPLATE_FILE, ClashMeta::DEFAULT_TEMPLATE_FILE)
|
||||
)
|
||||
),
|
||||
'subscribe_template_stash' => (string) $this->getTemplateContent(
|
||||
$this->getRuleFile(
|
||||
Stash::CUSTOM_TEMPLATE_FILE,
|
||||
$this->getRuleFile(Stash::CUSTOM_CLASH_TEMPLATE_FILE, Stash::DEFAULT_TEMPLATE_FILE)
|
||||
)
|
||||
),
|
||||
'subscribe_template_surge' => (string) $this->getTemplateContent(
|
||||
$this->getRuleFile(Surge::CUSTOM_TEMPLATE_FILE, Surge::DEFAULT_TEMPLATE_FILE)
|
||||
),
|
||||
'subscribe_template_surfboard' => (string) $this->getTemplateContent(
|
||||
$this->getRuleFile(Surfboard::CUSTOM_TEMPLATE_FILE, Surfboard::DEFAULT_TEMPLATE_FILE)
|
||||
)
|
||||
'subscribe_template_clash' => admin_setting('subscribe_template_clash', $this->getDefaultTemplate('clash')),
|
||||
'subscribe_template_clashmeta' => admin_setting('subscribe_template_clashmeta', $this->getDefaultTemplate('clashmeta')),
|
||||
'subscribe_template_stash' => admin_setting('subscribe_template_stash', $this->getDefaultTemplate('stash')),
|
||||
'subscribe_template_surge' => admin_setting('subscribe_template_surge', $this->getDefaultTemplate('surge')),
|
||||
'subscribe_template_surfboard' => admin_setting('subscribe_template_surfboard', $this->getDefaultTemplate('surfboard'))
|
||||
]
|
||||
];
|
||||
if ($key && isset($data[$key])) {
|
||||
return $this->success([
|
||||
$key => $data[$key]
|
||||
]);
|
||||
}
|
||||
;
|
||||
// TODO: default should be in Dict
|
||||
return $this->success($data);
|
||||
}
|
||||
|
||||
public function save(ConfigSave $request)
|
||||
{
|
||||
$data = $request->validated();
|
||||
|
||||
// 处理特殊的模板设置字段,将其保存为文件
|
||||
$templateFields = [
|
||||
'subscribe_template_clash' => Clash::CUSTOM_TEMPLATE_FILE,
|
||||
'subscribe_template_clashmeta' => ClashMeta::CUSTOM_TEMPLATE_FILE,
|
||||
'subscribe_template_stash' => Stash::CUSTOM_TEMPLATE_FILE,
|
||||
'subscribe_template_surge' => Surge::CUSTOM_TEMPLATE_FILE,
|
||||
'subscribe_template_singbox' => SingBox::CUSTOM_TEMPLATE_FILE,
|
||||
'subscribe_template_surfboard' => Surfboard::CUSTOM_TEMPLATE_FILE,
|
||||
];
|
||||
|
||||
foreach ($templateFields as $field => $filename) {
|
||||
if (isset($data[$field])) {
|
||||
$content = $data[$field];
|
||||
// 对于JSON格式的内容,确保格式化正确
|
||||
if ($field === 'subscribe_template_singbox' && is_array($content)) {
|
||||
$content = json_encode($content, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||
}
|
||||
$this->saveTemplateContent($filename, $content);
|
||||
unset($data[$field]); // 从数据库保存列表中移除
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($data as $k => $v) {
|
||||
if ($k == 'frontend_theme') {
|
||||
$themeService = app(ThemeService::class);
|
||||
@@ -268,29 +225,86 @@ class ConfigController extends Controller
|
||||
}
|
||||
admin_setting([$k => $v]);
|
||||
}
|
||||
// \Artisan::call('horizon:terminate'); //重启队列使配置生效
|
||||
|
||||
return $this->success(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存规则模板内容到文件
|
||||
* 格式化模板内容
|
||||
*
|
||||
* @param string $filepath 文件名
|
||||
* @param string $content 文件内容
|
||||
* @return bool 是否保存成功
|
||||
* @param mixed $content 模板内容
|
||||
* @param string $format 输出格式 (json|string)
|
||||
* @return string 格式化后的内容
|
||||
*/
|
||||
private function saveTemplateContent(string $filepath, string $content): bool
|
||||
private function formatTemplateContent(mixed $content, string $format = 'string'): string
|
||||
{
|
||||
$path = base_path($filepath);
|
||||
try {
|
||||
File::put($path, $content);
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
Log::error('保存规则模板失败', [
|
||||
'filepath' => $path,
|
||||
'error' => $e->getMessage()
|
||||
]);
|
||||
return false;
|
||||
return match ($format) {
|
||||
'json' => match (true) {
|
||||
is_array($content) => json_encode(
|
||||
value: $content,
|
||||
flags: JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES
|
||||
),
|
||||
|
||||
is_string($content) && str($content)->isJson() => rescue(
|
||||
callback: fn() => json_encode(
|
||||
value: json_decode($content, associative: true, flags: JSON_THROW_ON_ERROR),
|
||||
flags: JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES
|
||||
),
|
||||
rescue: $content,
|
||||
report: false
|
||||
),
|
||||
|
||||
default => str($content)->toString()
|
||||
},
|
||||
|
||||
default => str($content)->toString()
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取默认模板内容
|
||||
*
|
||||
* @param string $type 模板类型
|
||||
* @return string 默认模板内容
|
||||
*/
|
||||
private function getDefaultTemplate(string $type): string
|
||||
{
|
||||
$fileMap = [
|
||||
'singbox' => [SingBox::CUSTOM_TEMPLATE_FILE, SingBox::DEFAULT_TEMPLATE_FILE],
|
||||
'clash' => [Clash::CUSTOM_TEMPLATE_FILE, Clash::DEFAULT_TEMPLATE_FILE],
|
||||
'clashmeta' => [
|
||||
ClashMeta::CUSTOM_TEMPLATE_FILE,
|
||||
ClashMeta::CUSTOM_CLASH_TEMPLATE_FILE,
|
||||
ClashMeta::DEFAULT_TEMPLATE_FILE
|
||||
],
|
||||
'stash' => [
|
||||
Stash::CUSTOM_TEMPLATE_FILE,
|
||||
Stash::CUSTOM_CLASH_TEMPLATE_FILE,
|
||||
Stash::DEFAULT_TEMPLATE_FILE
|
||||
],
|
||||
'surge' => [Surge::CUSTOM_TEMPLATE_FILE, Surge::DEFAULT_TEMPLATE_FILE],
|
||||
'surfboard' => [Surfboard::CUSTOM_TEMPLATE_FILE, Surfboard::DEFAULT_TEMPLATE_FILE],
|
||||
];
|
||||
|
||||
if (!isset($fileMap[$type])) {
|
||||
return '';
|
||||
}
|
||||
|
||||
// 按优先级查找可用的模板文件
|
||||
foreach ($fileMap[$type] as $file) {
|
||||
$content = $this->getTemplateContent($file);
|
||||
if (!empty($content)) {
|
||||
// 对于 SingBox,需要格式化 JSON
|
||||
if ($type === 'singbox') {
|
||||
$decoded = json_decode($content, true);
|
||||
if (json_last_error() === JSON_ERROR_NONE) {
|
||||
return json_encode($decoded, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||
}
|
||||
}
|
||||
return $content;
|
||||
}
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -130,7 +130,7 @@ class SystemController extends Controller
|
||||
$pageSize = $request->input('page_size') >= 10 ? $request->input('page_size') : 10;
|
||||
$level = $request->input('level');
|
||||
$keyword = $request->input('keyword');
|
||||
|
||||
|
||||
$builder = LogModel::orderBy('created_at', 'DESC')
|
||||
->when($level, function ($query) use ($level) {
|
||||
return $query->where('level', strtoupper($level));
|
||||
@@ -138,16 +138,16 @@ class SystemController extends Controller
|
||||
->when($keyword, function ($query) use ($keyword) {
|
||||
return $query->where(function ($q) use ($keyword) {
|
||||
$q->where('data', 'like', '%' . $keyword . '%')
|
||||
->orWhere('context', 'like', '%' . $keyword . '%')
|
||||
->orWhere('title', 'like', '%' . $keyword . '%')
|
||||
->orWhere('uri', 'like', '%' . $keyword . '%');
|
||||
->orWhere('context', 'like', '%' . $keyword . '%')
|
||||
->orWhere('title', 'like', '%' . $keyword . '%')
|
||||
->orWhere('uri', 'like', '%' . $keyword . '%');
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
$total = $builder->count();
|
||||
$res = $builder->forPage($current, $pageSize)
|
||||
->get();
|
||||
|
||||
|
||||
return response([
|
||||
'data' => $res,
|
||||
'total' => $total
|
||||
@@ -174,4 +174,126 @@ class SystemController extends Controller
|
||||
'page_size' => $pageSize,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除系统日志
|
||||
*
|
||||
* @param Request $request
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
*/
|
||||
public function clearSystemLog(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'days' => 'integer|min:1|max:365',
|
||||
'level' => 'string|in:info,warning,error,all',
|
||||
'limit' => 'integer|min:100|max:10000'
|
||||
], [
|
||||
'days.required' => '请指定要清除多少天前的日志',
|
||||
'days.integer' => '天数必须为整数',
|
||||
'days.min' => '天数不能少于1天',
|
||||
'days.max' => '天数不能超过365天',
|
||||
'level.in' => '日志级别只能是:info、warning、error、all',
|
||||
'limit.min' => '单次清除数量不能少于100条',
|
||||
'limit.max' => '单次清除数量不能超过10000条'
|
||||
]);
|
||||
|
||||
$days = $request->input('days', 30); // 默认清除30天前的日志
|
||||
$level = $request->input('level', 'all'); // 默认清除所有级别
|
||||
$limit = $request->input('limit', 1000); // 默认单次清除1000条
|
||||
|
||||
try {
|
||||
$cutoffDate = now()->subDays($days);
|
||||
|
||||
// 构建查询条件
|
||||
$query = LogModel::where('created_at', '<', $cutoffDate->timestamp);
|
||||
|
||||
if ($level !== 'all') {
|
||||
$query->where('level', strtoupper($level));
|
||||
}
|
||||
|
||||
// 获取要删除的记录数量
|
||||
$totalCount = $query->count();
|
||||
|
||||
if ($totalCount === 0) {
|
||||
return $this->success([
|
||||
'message' => '没有找到符合条件的日志记录',
|
||||
'deleted_count' => 0,
|
||||
'total_count' => $totalCount
|
||||
]);
|
||||
}
|
||||
|
||||
// 分批删除,避免单次删除过多数据
|
||||
$deletedCount = 0;
|
||||
$batchSize = min($limit, 1000); // 每批最多1000条
|
||||
|
||||
while ($deletedCount < $limit && $deletedCount < $totalCount) {
|
||||
$remainingLimit = min($batchSize, $limit - $deletedCount);
|
||||
|
||||
$batchQuery = LogModel::where('created_at', '<', $cutoffDate->timestamp);
|
||||
if ($level !== 'all') {
|
||||
$batchQuery->where('level', strtoupper($level));
|
||||
}
|
||||
|
||||
$idsToDelete = $batchQuery->limit($remainingLimit)->pluck('id');
|
||||
|
||||
if ($idsToDelete->isEmpty()) {
|
||||
break;
|
||||
}
|
||||
|
||||
$batchDeleted = LogModel::whereIn('id', $idsToDelete)->delete();
|
||||
$deletedCount += $batchDeleted;
|
||||
|
||||
// 避免长时间占用数据库连接
|
||||
if ($deletedCount < $limit && $deletedCount < $totalCount) {
|
||||
usleep(100000); // 暂停0.1秒
|
||||
}
|
||||
}
|
||||
|
||||
return $this->success([
|
||||
'message' => '日志清除完成',
|
||||
'deleted_count' => $deletedCount,
|
||||
'total_count' => $totalCount,
|
||||
'remaining_count' => max(0, $totalCount - $deletedCount)
|
||||
]);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
return $this->error('清除日志失败:' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取日志清除统计信息
|
||||
*
|
||||
* @param Request $request
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
*/
|
||||
public function getLogClearStats(Request $request)
|
||||
{
|
||||
$days = $request->input('days', 30);
|
||||
$level = $request->input('level', 'all');
|
||||
|
||||
try {
|
||||
$cutoffDate = now()->subDays($days);
|
||||
|
||||
$query = LogModel::where('created_at', '<', $cutoffDate->timestamp);
|
||||
if ($level !== 'all') {
|
||||
$query->where('level', strtoupper($level));
|
||||
}
|
||||
|
||||
$stats = [
|
||||
'days' => $days,
|
||||
'level' => $level,
|
||||
'cutoff_date' => $cutoffDate->format(format: 'Y-m-d H:i:s'),
|
||||
'total_logs' => LogModel::count(),
|
||||
'logs_to_clear' => $query->count(),
|
||||
'oldest_log' => LogModel::orderBy('created_at', 'asc')->first(),
|
||||
'newest_log' => LogModel::orderBy('created_at', 'desc')->first(),
|
||||
];
|
||||
|
||||
return $this->success($stats);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
return $this->error('获取统计信息失败:' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user