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
+6 -11
View File
@@ -4,6 +4,7 @@ namespace App\Protocols;
use App\Contracts\ProtocolInterface;
use App\Utils\Helper;
use Illuminate\Support\Facades\File;
use Symfony\Component\Yaml\Yaml;
class Clash implements ProtocolInterface
@@ -11,6 +12,8 @@ class Clash implements ProtocolInterface
public $flags = ['clash'];
private $servers;
private $user;
const CUSTOM_TEMPLATE_FILE = 'resources/rules/custom.clash.yaml';
const DEFAULT_TEMPLATE_FILE = 'resources/rules/default.clash.yaml';
public function __construct($user, $servers)
{
@@ -29,17 +32,9 @@ class Clash implements ProtocolInterface
$user = $this->user;
$appName = admin_setting('app_name', 'XBoard');
// 优先从 admin_setting 获取模板
$template = admin_setting('subscribe_template_clash');
if (empty($template)) {
$defaultConfig = base_path('resources/rules/default.clash.yaml');
$customConfig = base_path('resources/rules/custom.clash.yaml');
if (file_exists($customConfig)) {
$template = file_get_contents($customConfig);
} else {
$template = file_get_contents($defaultConfig);
}
}
$template = File::exists(base_path(self::CUSTOM_TEMPLATE_FILE))
? File::get(base_path(self::CUSTOM_TEMPLATE_FILE))
: File::get(base_path(self::DEFAULT_TEMPLATE_FILE));
$config = Yaml::parse($template);
$proxy = [];
+15 -17
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') {
+10 -13
View File
@@ -4,6 +4,7 @@ namespace App\Protocols;
use App\Utils\Helper;
use App\Contracts\ProtocolInterface;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\File;
class SingBox implements ProtocolInterface
{
@@ -11,6 +12,8 @@ class SingBox implements ProtocolInterface
private $servers;
private $user;
private $config;
const CUSTOM_TEMPLATE_FILE = 'resources/rules/custom.sing-box.json';
const DEFAULT_TEMPLATE_FILE = 'resources/rules/default.sing-box.json';
public function __construct($user, $servers)
{
@@ -40,15 +43,9 @@ class SingBox implements ProtocolInterface
protected function loadConfig()
{
// 优先从 admin_setting 获取模板
$template = admin_setting('subscribe_template_singbox');
if (!empty($template)) {
return is_array($template) ? $template : json_decode($template, true);
}
$defaultConfig = base_path('resources/rules/default.sing-box.json');
$customConfig = base_path('resources/rules/custom.sing-box.json');
$jsonData = file_exists($customConfig) ? file_get_contents($customConfig) : file_get_contents($defaultConfig);
$jsonData = File::exists(base_path(self::CUSTOM_TEMPLATE_FILE))
? File::get(base_path(self::CUSTOM_TEMPLATE_FILE))
: File::get(base_path(self::DEFAULT_TEMPLATE_FILE));
return json_decode($jsonData, true);
}
@@ -227,7 +224,7 @@ class SingBox implements ProtocolInterface
$transport = match ($protocol_settings['network']) {
'tcp' => data_get($protocol_settings, 'network_settings.header.type') == 'http' ? [
'type' => 'http',
'path' => \Arr::random(data_get($protocol_settings, 'network_settings.header.request.path', ['/']))
'path' => Arr::random(data_get($protocol_settings, 'network_settings.header.request.path', ['/']))
] : null,
'ws' => array_filter([
'type' => 'ws',
@@ -308,9 +305,9 @@ class SingBox implements ProtocolInterface
'insecure' => (bool) $protocol_settings['tls']['allow_insecure'],
]
];
if (isset($server['ports'])) {
$baseConfig['server_ports'][] = str_replace('-', ':', $server['ports']);
}
// if (isset($server['ports'])) {
// $baseConfig['server_ports'][] = str_replace('-', ':', $server['ports']);
// }
if ($serverName = data_get($protocol_settings, 'tls_settings.server_name')) {
$baseConfig['tls']['server_name'] = $serverName;
}
+14 -16
View File
@@ -5,6 +5,7 @@ namespace App\Protocols;
use App\Models\ServerHysteria;
use Symfony\Component\Yaml\Yaml;
use App\Contracts\ProtocolInterface;
use Illuminate\Support\Facades\File;
class Stash implements ProtocolInterface
{
@@ -12,6 +13,10 @@ class Stash implements ProtocolInterface
private $servers;
private $user;
const CUSTOM_TEMPLATE_FILE = 'resources/rules/custom.stash.yaml';
const CUSTOM_CLASH_TEMPLATE_FILE = 'resources/rules/custom.clash.yaml';
const DEFAULT_TEMPLATE_FILE = 'resources/rules/default.clash.yaml';
public function __construct($user, $servers)
{
$this->user = $user;
@@ -28,22 +33,15 @@ class Stash implements ProtocolInterface
$servers = $this->servers;
$user = $this->user;
$appName = admin_setting('app_name', 'XBoard');
// 优先从 admin_setting 获取模板
$template = admin_setting('subscribe_template_stash');
if (empty($template)) {
$defaultConfig = base_path('resources/rules/default.clash.yaml');
$customClashConfig = base_path('resources/rules/custom.clash.yaml');
$customStashConfig = base_path('resources/rules/custom.stash.yaml');
if (file_exists($customStashConfig)) {
$template = file_get_contents($customStashConfig);
} 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 = [];
+5 -8
View File
@@ -11,6 +11,8 @@ class Surfboard implements ProtocolInterface
public $flags = ['surfboard'];
private $servers;
private $user;
const CUSTOM_TEMPLATE_FILE = 'resources/rules/custom.surfboard.conf';
const DEFAULT_TEMPLATE_FILE = 'resources/rules/default.surfboard.conf';
public function __construct($user, $servers)
{
@@ -62,14 +64,9 @@ class Surfboard implements ProtocolInterface
}
}
$defaultConfig = base_path() . '/resources/rules/default.surfboard.conf';
$customConfig = base_path() . '/resources/rules/custom.surfboard.conf';
if (File::exists($customConfig)) {
$config = file_get_contents("$customConfig");
} else {
$config = file_get_contents("$defaultConfig");
}
$config = File::exists(base_path(self::CUSTOM_TEMPLATE_FILE))
? File::get(base_path(self::CUSTOM_TEMPLATE_FILE))
: File::get(base_path(self::DEFAULT_TEMPLATE_FILE));
// Subscription link
$subsURL = Helper::getSubscribeUrl($user['token']);
$subsDomain = request()->header('Host');
+7 -11
View File
@@ -4,12 +4,15 @@ namespace App\Protocols;
use App\Utils\Helper;
use App\Contracts\ProtocolInterface;
use Illuminate\Support\Facades\File;
class Surge implements ProtocolInterface
{
public $flags = ['surge'];
private $servers;
private $user;
const CUSTOM_TEMPLATE_FILE = 'resources/rules/custom.surge.conf';
const DEFAULT_TEMPLATE_FILE = 'resources/rules/default.surge.conf';
public function __construct($user, $servers)
{
@@ -59,17 +62,10 @@ class Surge implements ProtocolInterface
}
}
// 优先从 admin_setting 获取模板
$config = admin_setting('subscribe_template_surge');
if (empty($config)) {
$defaultConfig = base_path('resources/rules/default.surge.conf');
$customConfig = base_path('resources/rules/custom.surge.conf');
if (file_exists($customConfig)) {
$config = file_get_contents($customConfig);
} else {
$config = file_get_contents($defaultConfig);
}
}
$config = File::exists(base_path(self::CUSTOM_TEMPLATE_FILE))
? File::get(base_path(self::CUSTOM_TEMPLATE_FILE))
: File::get(base_path(self::DEFAULT_TEMPLATE_FILE));
// Subscription link
$subsDomain = request()->header('Host');