fix(singbox): resolve port type casting and add port hopping support

This commit is contained in:
xboard
2025-05-10 17:10:41 +08:00
parent 73226f6820
commit 8377962836
2 changed files with 14 additions and 8 deletions
+11 -6
View File
@@ -3,6 +3,7 @@ namespace App\Protocols;
use App\Utils\Helper; use App\Utils\Helper;
use App\Contracts\ProtocolInterface; use App\Contracts\ProtocolInterface;
use Illuminate\Support\Arr;
class SingBox implements ProtocolInterface class SingBox implements ProtocolInterface
{ {
@@ -44,7 +45,7 @@ class SingBox implements ProtocolInterface
if (!empty($template)) { if (!empty($template)) {
return is_array($template) ? $template : json_decode($template, true); return is_array($template) ? $template : json_decode($template, true);
} }
$defaultConfig = base_path('resources/rules/default.sing-box.json'); $defaultConfig = base_path('resources/rules/default.sing-box.json');
$customConfig = base_path('resources/rules/custom.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($customConfig) ? file_get_contents($customConfig) : file_get_contents($defaultConfig);
@@ -70,8 +71,9 @@ class SingBox implements ProtocolInterface
$vmessConfig = $this->buildVmess($this->user['uuid'], $item); $vmessConfig = $this->buildVmess($this->user['uuid'], $item);
$proxies[] = $vmessConfig; $proxies[] = $vmessConfig;
} }
if ($item['type'] === 'vless' if (
&& in_array(data_get($protocol_settings, 'network'), ['tcp', 'ws', 'grpc', 'http', 'quic', 'httpupgrade']) $item['type'] === 'vless'
&& in_array(data_get($protocol_settings, 'network'), ['tcp', 'ws', 'grpc', 'http', 'quic', 'httpupgrade'])
) { ) {
$vlessConfig = $this->buildVless($this->user['uuid'], $item); $vlessConfig = $this->buildVless($this->user['uuid'], $item);
$proxies[] = $vlessConfig; $proxies[] = $vlessConfig;
@@ -158,7 +160,7 @@ class SingBox implements ProtocolInterface
$transport = match ($protocol_settings['network']) { $transport = match ($protocol_settings['network']) {
'tcp' => [ 'tcp' => [
'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', ['/']))
], ],
'ws' => [ 'ws' => [
'type' => 'ws', 'type' => 'ws',
@@ -306,9 +308,12 @@ class SingBox implements ProtocolInterface
'insecure' => (bool) $protocol_settings['tls']['allow_insecure'], 'insecure' => (bool) $protocol_settings['tls']['allow_insecure'],
] ]
]; ];
if (isset($server['ports'])) {
$baseConfig['server_ports'][] = str_replace('-', ':', $server['ports']);
}
if ($serverName = data_get($protocol_settings, 'tls_settings.server_name')) { if ($serverName = data_get($protocol_settings, 'tls_settings.server_name')) {
$baseConfig['tls']['server_name'] = $serverName; $baseConfig['tls']['server_name'] = $serverName;
} }
$speedConfig = [ $speedConfig = [
'up_mbps' => $protocol_settings['bandwidth']['up'], 'up_mbps' => $protocol_settings['bandwidth']['up'],
'down_mbps' => $protocol_settings['bandwidth']['down'], 'down_mbps' => $protocol_settings['bandwidth']['down'],
@@ -415,7 +420,7 @@ class SingBox implements ProtocolInterface
'enabled' => true, 'enabled' => true,
'insecure' => (bool) data_get($protocol_settings, 'tls_settings.allow_insecure', false), 'insecure' => (bool) data_get($protocol_settings, 'tls_settings.allow_insecure', false),
]; ];
if ($serverName = data_get($protocol_settings, 'tls_settings.server_name')) { if ($serverName = data_get($protocol_settings, 'tls_settings.server_name')) {
$array['tls']['server_name'] = $serverName; $array['tls']['server_name'] = $serverName;
} }
+3 -2
View File
@@ -45,8 +45,9 @@ class ServerService
$servers = collect($servers)->map(function ($server) use ($user) { $servers = collect($servers)->map(function ($server) use ($user) {
// 判断动态端口 // 判断动态端口
if (str_contains($server->port, '-')) { if (str_contains($server->port, '-')) {
$server->port = (string) Helper::randomPort($server->port); $port = $server->port;
$server->ports = $server->port; $server->port = (int) Helper::randomPort($port);
$server->ports = $port;
} else { } else {
$server->port = (int) $server->port; $server->port = (int) $server->port;
} }