feat(admin): optimize subscription template configuration and add Surfboard subscription template

- Improved the code structure for subscription template configuration.
- Added a new feature in the admin panel to configure Surfboard subscription templates.
This commit is contained in:
xboard
2025-05-16 05:13:49 +08:00
parent f5c3d5c56b
commit 417590e99c
9 changed files with 163 additions and 117 deletions

View File

@@ -5,6 +5,7 @@ namespace App\Protocols;
use App\Contracts\ProtocolInterface;
use App\Models\ServerHysteria;
use App\Utils\Helper;
use Illuminate\Support\Facades\File;
use Symfony\Component\Yaml\Yaml;
class ClashMeta implements ProtocolInterface
@@ -12,6 +13,9 @@ class ClashMeta implements ProtocolInterface
public $flags = ['meta', 'verge', 'flclash'];
private $servers;
private $user;
const CUSTOM_TEMPLATE_FILE = 'resources/rules/custom.clashmeta.yaml';
const CUSTOM_CLASH_TEMPLATE_FILE = 'resources/rules/custom.clash.yaml';
const DEFAULT_TEMPLATE_FILE = 'resources/rules/default.clash.yaml';
/**
* @param mixed $user 用户实例
@@ -28,31 +32,25 @@ class ClashMeta implements ProtocolInterface
return $this->flags;
}
public function handle()
{
$servers = $this->servers;
$user = $this->user;
$appName = admin_setting('app_name', 'XBoard');
// 优先从 admin_setting 获取模板
$template = admin_setting('subscribe_template_clashmeta');
if (empty($template)) {
$defaultConfig = base_path('resources/rules/default.clash.yaml');
$customClashConfig = base_path('resources/rules/custom.clash.yaml');
$customConfig = base_path('resources/rules/custom.clashmeta.yaml');
if (file_exists($customConfig)) {
$template = file_get_contents($customConfig);
} elseif (file_exists($customClashConfig)) {
$template = file_get_contents($customClashConfig);
} else {
$template = file_get_contents($defaultConfig);
}
}
$template = File::exists(base_path(self::CUSTOM_TEMPLATE_FILE))
? File::get(base_path(self::CUSTOM_TEMPLATE_FILE))
: (
File::exists(base_path(self::CUSTOM_CLASH_TEMPLATE_FILE))
? File::get(base_path(self::CUSTOM_CLASH_TEMPLATE_FILE))
: File::get(base_path(self::DEFAULT_TEMPLATE_FILE))
);
$config = Yaml::parse($template);
$proxy = [];
$proxies = [];
foreach ($servers as $item) {
$protocol_settings = $item['protocol_settings'];
if ($item['type'] === 'shadowsocks') {