2023-11-17 14:44:01 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
namespace App\Protocols;
|
|
|
|
|
|
|
|
|
|
|
|
use App\Utils\Helper;
|
2025-05-07 19:48:19 +08:00
|
|
|
|
use Illuminate\Support\Facades\File;
|
2025-05-22 17:58:22 +08:00
|
|
|
|
use App\Support\AbstractProtocol;
|
2023-11-17 14:44:01 +08:00
|
|
|
|
|
2025-05-22 17:58:22 +08:00
|
|
|
|
class Surfboard extends AbstractProtocol
|
2023-11-17 14:44:01 +08:00
|
|
|
|
{
|
2025-01-21 14:57:54 +08:00
|
|
|
|
public $flags = ['surfboard'];
|
2025-05-16 05:13:49 +08:00
|
|
|
|
const CUSTOM_TEMPLATE_FILE = 'resources/rules/custom.surfboard.conf';
|
|
|
|
|
|
const DEFAULT_TEMPLATE_FILE = 'resources/rules/default.surfboard.conf';
|
2023-11-17 14:44:01 +08:00
|
|
|
|
|
2025-01-21 14:57:54 +08:00
|
|
|
|
|
2023-11-17 14:44:01 +08:00
|
|
|
|
public function handle()
|
|
|
|
|
|
{
|
|
|
|
|
|
$servers = $this->servers;
|
|
|
|
|
|
$user = $this->user;
|
|
|
|
|
|
|
|
|
|
|
|
$appName = admin_setting('app_name', 'XBoard');
|
|
|
|
|
|
|
|
|
|
|
|
$proxies = '';
|
|
|
|
|
|
$proxyGroup = '';
|
|
|
|
|
|
|
|
|
|
|
|
foreach ($servers as $item) {
|
2025-01-21 14:57:54 +08:00
|
|
|
|
if (
|
|
|
|
|
|
$item['type'] === 'shadowsocks'
|
|
|
|
|
|
&& in_array(data_get($item, 'protocol_settings.cipher'), [
|
2023-11-17 14:44:01 +08:00
|
|
|
|
'aes-128-gcm',
|
|
|
|
|
|
'aes-192-gcm',
|
|
|
|
|
|
'aes-256-gcm',
|
|
|
|
|
|
'chacha20-ietf-poly1305'
|
|
|
|
|
|
])
|
|
|
|
|
|
) {
|
|
|
|
|
|
// [Proxy]
|
2024-05-24 22:45:27 +08:00
|
|
|
|
$proxies .= self::buildShadowsocks($item['password'], $item);
|
2023-11-17 14:44:01 +08:00
|
|
|
|
// [Proxy Group]
|
|
|
|
|
|
$proxyGroup .= $item['name'] . ', ';
|
|
|
|
|
|
}
|
|
|
|
|
|
if ($item['type'] === 'vmess') {
|
|
|
|
|
|
// [Proxy]
|
|
|
|
|
|
$proxies .= self::buildVmess($user['uuid'], $item);
|
|
|
|
|
|
// [Proxy Group]
|
|
|
|
|
|
$proxyGroup .= $item['name'] . ', ';
|
|
|
|
|
|
}
|
|
|
|
|
|
if ($item['type'] === 'trojan') {
|
|
|
|
|
|
// [Proxy]
|
|
|
|
|
|
$proxies .= self::buildTrojan($user['uuid'], $item);
|
|
|
|
|
|
// [Proxy Group]
|
|
|
|
|
|
$proxyGroup .= $item['name'] . ', ';
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-21 12:11:27 +08:00
|
|
|
|
$config = admin_setting('subscribe_template_surfboard', File::exists(base_path(self::CUSTOM_TEMPLATE_FILE))
|
2025-05-22 17:58:22 +08:00
|
|
|
|
? File::get(base_path(self::CUSTOM_TEMPLATE_FILE))
|
2025-06-21 12:11:27 +08:00
|
|
|
|
: File::get(base_path(self::DEFAULT_TEMPLATE_FILE)));
|
2023-11-17 14:44:01 +08:00
|
|
|
|
// Subscription link
|
2024-11-09 06:34:30 +08:00
|
|
|
|
$subsURL = Helper::getSubscribeUrl($user['token']);
|
2023-11-17 14:44:01 +08:00
|
|
|
|
$subsDomain = request()->header('Host');
|
|
|
|
|
|
|
|
|
|
|
|
$config = str_replace('$subs_link', $subsURL, $config);
|
|
|
|
|
|
$config = str_replace('$subs_domain', $subsDomain, $config);
|
|
|
|
|
|
$config = str_replace('$proxies', $proxies, $config);
|
|
|
|
|
|
$config = str_replace('$proxy_group', rtrim($proxyGroup, ', '), $config);
|
|
|
|
|
|
|
2025-01-21 14:57:54 +08:00
|
|
|
|
$upload = round($user['u'] / (1024 * 1024 * 1024), 2);
|
|
|
|
|
|
$download = round($user['d'] / (1024 * 1024 * 1024), 2);
|
2023-11-17 14:44:01 +08:00
|
|
|
|
$useTraffic = $upload + $download;
|
2025-01-21 14:57:54 +08:00
|
|
|
|
$totalTraffic = round($user['transfer_enable'] / (1024 * 1024 * 1024), 2);
|
2024-05-03 15:04:23 +08:00
|
|
|
|
$unusedTraffic = $totalTraffic - $useTraffic;
|
2023-11-17 14:44:01 +08:00
|
|
|
|
$expireDate = $user['expired_at'] === NULL ? '长期有效' : date('Y-m-d H:i:s', $user['expired_at']);
|
2024-05-03 15:04:23 +08:00
|
|
|
|
$subscribeInfo = "title={$appName}订阅信息, content=上传流量:{$upload}GB\\n下载流量:{$download}GB\\n剩余流量: { $unusedTraffic }GB\\n套餐流量:{$totalTraffic}GB\\n到期时间:{$expireDate}";
|
2023-11-17 14:44:01 +08:00
|
|
|
|
$config = str_replace('$subscribe_info', $subscribeInfo, $config);
|
|
|
|
|
|
|
|
|
|
|
|
return response($config, 200)
|
2025-01-21 14:57:54 +08:00
|
|
|
|
->header('content-disposition', "attachment;filename*=UTF-8''" . rawurlencode($appName) . ".conf");
|
2023-11-17 14:44:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static function buildShadowsocks($password, $server)
|
|
|
|
|
|
{
|
2025-01-21 14:57:54 +08:00
|
|
|
|
$protocol_settings = $server['protocol_settings'];
|
2023-11-17 14:44:01 +08:00
|
|
|
|
$config = [
|
|
|
|
|
|
"{$server['name']}=ss",
|
|
|
|
|
|
"{$server['host']}",
|
|
|
|
|
|
"{$server['port']}",
|
2025-01-21 14:57:54 +08:00
|
|
|
|
"encrypt-method={$protocol_settings['cipher']}",
|
2023-11-17 14:44:01 +08:00
|
|
|
|
"password={$password}",
|
|
|
|
|
|
'tfo=true',
|
|
|
|
|
|
'udp-relay=true'
|
|
|
|
|
|
];
|
2025-05-22 17:58:22 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (data_get($protocol_settings, 'plugin') && data_get($protocol_settings, 'plugin_opts')) {
|
|
|
|
|
|
$plugin = data_get($protocol_settings, 'plugin');
|
|
|
|
|
|
$pluginOpts = data_get($protocol_settings, 'plugin_opts', '');
|
|
|
|
|
|
// 解析插件选项
|
|
|
|
|
|
$parsedOpts = collect(explode(';', $pluginOpts))
|
|
|
|
|
|
->filter()
|
|
|
|
|
|
->mapWithKeys(function ($pair) {
|
|
|
|
|
|
if (!str_contains($pair, '=')) {
|
|
|
|
|
|
return [];
|
|
|
|
|
|
}
|
|
|
|
|
|
[$key, $value] = explode('=', $pair, 2);
|
|
|
|
|
|
return [trim($key) => trim($value)];
|
|
|
|
|
|
})
|
|
|
|
|
|
->all();
|
|
|
|
|
|
switch ($plugin) {
|
|
|
|
|
|
case 'obfs':
|
|
|
|
|
|
$config[] = "obfs={$parsedOpts['obfs']}";
|
|
|
|
|
|
if (isset($parsedOpts['obfs-host'])) {
|
|
|
|
|
|
$config[] = "obfs-host={$parsedOpts['obfs-host']}";
|
|
|
|
|
|
}
|
|
|
|
|
|
if (isset($parsedOpts['path'])) {
|
|
|
|
|
|
$config[] = "obfs-uri={$parsedOpts['path']}";
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-11-17 14:44:01 +08:00
|
|
|
|
$config = array_filter($config);
|
|
|
|
|
|
$uri = implode(',', $config);
|
|
|
|
|
|
$uri .= "\r\n";
|
|
|
|
|
|
return $uri;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static function buildVmess($uuid, $server)
|
|
|
|
|
|
{
|
2025-01-21 14:57:54 +08:00
|
|
|
|
$protocol_settings = $server['protocol_settings'];
|
2023-11-17 14:44:01 +08:00
|
|
|
|
$config = [
|
|
|
|
|
|
"{$server['name']}=vmess",
|
|
|
|
|
|
"{$server['host']}",
|
|
|
|
|
|
"{$server['port']}",
|
|
|
|
|
|
"username={$uuid}",
|
|
|
|
|
|
"vmess-aead=true",
|
|
|
|
|
|
'tfo=true',
|
|
|
|
|
|
'udp-relay=true'
|
|
|
|
|
|
];
|
|
|
|
|
|
|
2025-01-21 14:57:54 +08:00
|
|
|
|
if (data_get($protocol_settings, 'tls')) {
|
2023-11-17 14:44:01 +08:00
|
|
|
|
array_push($config, 'tls=true');
|
2025-01-21 14:57:54 +08:00
|
|
|
|
if (data_get($protocol_settings, 'tls_settings')) {
|
|
|
|
|
|
$tlsSettings = data_get($protocol_settings, 'tls_settings');
|
2025-05-07 19:48:19 +08:00
|
|
|
|
if (!!data_get($tlsSettings, 'allowInsecure'))
|
2023-11-17 14:44:01 +08:00
|
|
|
|
array_push($config, 'skip-cert-verify=' . ($tlsSettings['allowInsecure'] ? 'true' : 'false'));
|
2025-05-07 19:48:19 +08:00
|
|
|
|
if (!!data_get($tlsSettings, 'serverName'))
|
2023-11-17 14:44:01 +08:00
|
|
|
|
array_push($config, "sni={$tlsSettings['serverName']}");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-02-22 21:12:59 +08:00
|
|
|
|
if (data_get($protocol_settings, 'network') === 'ws') {
|
2023-11-17 14:44:01 +08:00
|
|
|
|
array_push($config, 'ws=true');
|
2025-01-21 14:57:54 +08:00
|
|
|
|
if (data_get($protocol_settings, 'network_settings')) {
|
|
|
|
|
|
$wsSettings = data_get($protocol_settings, 'network_settings');
|
2023-11-17 14:44:01 +08:00
|
|
|
|
if (isset($wsSettings['path']) && !empty($wsSettings['path']))
|
|
|
|
|
|
array_push($config, "ws-path={$wsSettings['path']}");
|
|
|
|
|
|
if (isset($wsSettings['headers']['Host']) && !empty($wsSettings['headers']['Host']))
|
|
|
|
|
|
array_push($config, "ws-headers=Host:{$wsSettings['headers']['Host']}");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$uri = implode(',', $config);
|
|
|
|
|
|
$uri .= "\r\n";
|
|
|
|
|
|
return $uri;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static function buildTrojan($password, $server)
|
|
|
|
|
|
{
|
2025-01-21 14:57:54 +08:00
|
|
|
|
$protocol_settings = $server['protocol_settings'];
|
2023-11-17 14:44:01 +08:00
|
|
|
|
$config = [
|
|
|
|
|
|
"{$server['name']}=trojan",
|
|
|
|
|
|
"{$server['host']}",
|
|
|
|
|
|
"{$server['port']}",
|
|
|
|
|
|
"password={$password}",
|
2025-01-21 14:57:54 +08:00
|
|
|
|
$protocol_settings['server_name'] ? "sni={$protocol_settings['server_name']}" : "",
|
2023-11-17 14:44:01 +08:00
|
|
|
|
'tfo=true',
|
|
|
|
|
|
'udp-relay=true'
|
|
|
|
|
|
];
|
2025-05-07 19:48:19 +08:00
|
|
|
|
if (data_get($protocol_settings, 'allow_insecure')) {
|
|
|
|
|
|
array_push($config, !!data_get($protocol_settings, 'allow_insecure') ? 'skip-cert-verify=true' : 'skip-cert-verify=false');
|
2023-11-17 14:44:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
$config = array_filter($config);
|
|
|
|
|
|
$uri = implode(',', $config);
|
|
|
|
|
|
$uri .= "\r\n";
|
|
|
|
|
|
return $uri;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|