Xboard/app/Protocols/SingBox.php

313 lines
12 KiB
PHP
Raw Normal View History

2023-11-17 01:44:01 -05:00
<?php
namespace App\Protocols;
use App\Utils\Helper;
2025-01-06 12:20:11 -05:00
use App\Contracts\ProtocolInterface;
2023-11-17 01:44:01 -05:00
2025-01-06 12:20:11 -05:00
class SingBox implements ProtocolInterface
2023-11-17 01:44:01 -05:00
{
2025-01-06 12:20:11 -05:00
public $flags = ['sing-box', 'hiddify'];
2023-11-17 01:44:01 -05:00
private $servers;
private $user;
private $config;
2023-11-17 01:44:01 -05:00
public function __construct($user, $servers, array $options = null)
{
$this->user = $user;
$this->servers = $servers;
}
2025-01-06 12:20:11 -05:00
public function getFlags(): array
{
return $this->flags;
}
2023-11-17 01:44:01 -05:00
public function handle()
{
$appName = admin_setting('app_name', 'XBoard');
$this->config = $this->loadConfig();
$this->buildOutbounds();
$this->buildRule();
$user = $this->user;
2023-11-17 01:44:01 -05:00
return response()
->json($this->config)
2025-01-06 12:20:11 -05:00
->header('profile-title', 'base64:' . base64_encode($appName))
->header('subscription-userinfo', "upload={$user['u']}; download={$user['d']}; total={$user['transfer_enable']}; expire={$user['expired_at']}")
->header('profile-update-interval', '24');
2023-11-17 01:44:01 -05:00
}
protected function loadConfig()
{
$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);
return json_decode($jsonData, true);
}
protected function buildOutbounds()
{
$outbounds = $this->config['outbounds'];
$proxies = [];
2023-11-17 01:44:01 -05:00
foreach ($this->servers as $item) {
if ($item['type'] === 'shadowsocks') {
$ssConfig = $this->buildShadowsocks($item['password'], $item);
$proxies[] = $ssConfig;
2023-11-17 01:44:01 -05:00
}
if ($item['type'] === 'trojan') {
$trojanConfig = $this->buildTrojan($this->user['uuid'], $item);
$proxies[] = $trojanConfig;
2023-11-17 01:44:01 -05:00
}
if ($item['type'] === 'vmess') {
$vmessConfig = $this->buildVmess($this->user['uuid'], $item);
$proxies[] = $vmessConfig;
2023-11-17 01:44:01 -05:00
}
if ($item['type'] === 'vless') {
$vlessConfig = $this->buildVless($this->user['uuid'], $item);
$proxies[] = $vlessConfig;
2023-11-17 01:44:01 -05:00
}
if ($item['type'] === 'hysteria') {
$hysteriaConfig = $this->buildHysteria($this->user['uuid'], $item);
$proxies[] = $hysteriaConfig;
}
}
foreach ($outbounds as &$outbound) {
if (in_array($outbound['type'], ['urltest', 'selector'])) {
array_push($outbound['outbounds'], ...array_column($proxies, 'tag'));
2023-11-17 01:44:01 -05:00
}
}
$outbounds = array_merge($outbounds, $proxies);
$this->config['outbounds'] = $outbounds;
2023-11-17 01:44:01 -05:00
return $outbounds;
}
/**
* Build rule
*/
2025-01-06 12:20:11 -05:00
protected function buildRule()
{
$rules = $this->config['route']['rules'];
// Force the nodes ip to be a direct rule
array_unshift($rules, [
2025-01-06 12:20:11 -05:00
'ip_cidr' => collect($this->servers)->pluck('host')->map(function ($host) {
return filter_var($host, FILTER_VALIDATE_IP) ? [$host] : Helper::getIpByDomainName($host);
})->flatten()->unique()->values(),
'outbound' => 'direct',
]);
$this->config['route']['rules'] = $rules;
}
2023-11-17 01:44:01 -05:00
protected function buildShadowsocks($password, $server)
{
$array = [];
$array['tag'] = $server['name'];
$array['type'] = 'shadowsocks';
$array['server'] = $server['host'];
$array['server_port'] = $server['port'];
2025-01-06 12:20:11 -05:00
$array['method'] = data_get($server, 'protocol_settings.cipher');
2025-01-09 02:58:32 -05:00
$array['password'] = data_get($server, 'password', $password);
2023-11-17 01:44:01 -05:00
return $array;
}
protected function buildVmess($uuid, $server)
{
2025-01-06 12:20:11 -05:00
$protocol_settings = $server['protocol_settings'];
$array = [
'tag' => $server['name'],
'type' => 'vmess',
'server' => $server['host'],
'server_port' => $server['port'],
'uuid' => $uuid,
'security' => 'auto',
'alter_id' => 0,
'transport' => [],
'tls' => $protocol_settings['tls'] ? [
'enabled' => true,
2025-01-07 22:07:13 -05:00
'insecure' => (bool) data_get($protocol_settings, 'tls_settings.allow_insecure'),
2025-01-06 12:20:11 -05:00
] : null
];
2025-01-12 10:57:24 -05:00
if ($serverName = data_get($protocol_settings, 'tls_settings.server_name')) {
$array['tls']['server_name'] = $serverName;
}
2023-11-17 01:44:01 -05:00
2025-01-06 12:20:11 -05:00
$transport = match ($protocol_settings['network']) {
'tcp' => [
'type' => 'http',
2025-01-12 08:10:52 -05:00
'path' => \Arr::random(data_get($protocol_settings, 'network_settings.header.request.path', ['/']))
2025-01-06 12:20:11 -05:00
],
'ws' => [
'type' => 'ws',
'path' => data_get($protocol_settings, 'network_settings.path'),
2025-01-12 08:10:52 -05:00
'headers' => ($host = data_get($protocol_settings, 'network_settings.headers.Host')) ? ['Host' => $host] : null,
2025-01-06 12:20:11 -05:00
'max_early_data' => 2048,
'early_data_header_name' => 'Sec-WebSocket-Protocol'
],
'grpc' => [
'type' => 'grpc',
'service_name' => data_get($protocol_settings, 'network_settings.serviceName')
],
default => null
};
2023-11-17 01:44:01 -05:00
2025-01-06 12:20:11 -05:00
if ($transport) {
$array['transport'] = array_filter($transport, fn($value) => !is_null($value));
}
2023-11-17 01:44:01 -05:00
return $array;
}
protected function buildVless($password, $server)
{
2025-01-06 12:20:11 -05:00
$protocol_settings = data_get($server, 'protocol_settings', []);
2023-11-17 01:44:01 -05:00
$array = [
"type" => "vless",
"tag" => $server['name'],
"server" => $server['host'],
"server_port" => $server['port'],
"uuid" => $password,
2025-01-07 22:07:13 -05:00
"packet_encoding" => "xudp",
'flow' => data_get($protocol_settings, 'flow', ''),
2023-11-17 01:44:01 -05:00
];
2025-01-06 12:20:11 -05:00
if ($protocol_settings['tls']) {
$tlsConfig = [
'enabled' => true,
2025-01-07 22:07:13 -05:00
'insecure' => (bool) data_get($protocol_settings, 'tls_settings.allow_insecure'),
2025-01-06 12:20:11 -05:00
'utls' => [
'enabled' => true,
'fingerprint' => Helper::getRandFingerprint()
]
];
2025-01-12 10:57:24 -05:00
if ($serverName = data_get($protocol_settings, 'tls_settings.server_name')) {
$tlsConfig['server_name'] = $serverName;
}
2025-01-06 12:20:11 -05:00
if ($protocol_settings['tls'] == 2) {
$tlsConfig['reality'] = [
'enabled' => true,
'public_key' => data_get($protocol_settings, 'reality_settings.public_key'),
'short_id' => data_get($protocol_settings, 'reality_settings.short_id')
2023-11-17 01:44:01 -05:00
];
}
2025-01-06 12:20:11 -05:00
2023-11-17 01:44:01 -05:00
$array['tls'] = $tlsConfig;
}
2025-01-06 12:20:11 -05:00
$transport = match ($protocol_settings['network']) {
'tcp' => data_get($protocol_settings, 'network_settings.header.type') == 'http' ? [
'type' => 'http',
2025-01-12 10:57:24 -05:00
'path' => \Arr::random(data_get($protocol_settings, 'network_settings.header.request.path', ['/']))
2025-01-06 12:20:11 -05:00
] : null,
2025-01-12 08:10:52 -05:00
'ws' => array_filter([
2025-01-06 12:20:11 -05:00
'type' => 'ws',
'path' => data_get($protocol_settings, 'network_settings.path'),
2025-01-12 08:10:52 -05:00
'headers' => ($host = data_get($protocol_settings, 'network_settings.headers.Host')) ? ['Host' => $host] : null,
2025-01-06 12:20:11 -05:00
'max_early_data' => 2048,
'early_data_header_name' => 'Sec-WebSocket-Protocol'
2025-01-12 08:10:52 -05:00
], fn($value) => !is_null($value)),
2025-01-06 12:20:11 -05:00
'grpc' => [
'type' => 'grpc',
'service_name' => data_get($protocol_settings, 'network_settings.serviceName')
],
'h2' => [
'type' => 'http',
'host' => data_get($protocol_settings, 'network_settings.host') ? [data_get($protocol_settings, 'network_settings.host')] : null,
'path' => data_get($protocol_settings, 'network_settings.path')
],
'httpupgrade' => [
'type' => 'httpupgrade',
'path' => data_get($protocol_settings, 'network_settings.path'),
2025-01-12 08:10:52 -05:00
'host' => data_get($protocol_settings, 'network_settings.headers.Host', $server['host']),
'headers' => data_get($protocol_settings, 'network_settings.headers')
],
2025-01-06 12:20:11 -05:00
default => null
};
if ($transport) {
$array['transport'] = array_filter($transport, fn($value) => !is_null($value));
2023-11-17 01:44:01 -05:00
}
return $array;
}
protected function buildTrojan($password, $server)
2023-11-17 01:44:01 -05:00
{
2025-01-06 12:20:11 -05:00
$protocol_settings = $server['protocol_settings'];
$array = [
'tag' => $server['name'],
'type' => 'trojan',
'server' => $server['host'],
'server_port' => $server['port'],
'password' => $password,
'tls' => [
'enabled' => true,
2025-01-07 22:07:13 -05:00
'insecure' => (bool) data_get($protocol_settings, 'allow_insecure', false),
2025-01-06 12:20:11 -05:00
]
2023-11-17 01:44:01 -05:00
];
2025-01-12 10:57:24 -05:00
if ($serverName = data_get($protocol_settings, 'tls_settings.server_name')) {
$array['tls']['server_name'] = $serverName;
}
2025-01-06 12:20:11 -05:00
$transport = match (data_get($protocol_settings, 'network')) {
'grpc' => [
'type' => 'grpc',
'service_name' => data_get($protocol_settings, 'network_settings.serviceName')
],
'ws' => [
'type' => 'ws',
'path' => data_get($protocol_settings, 'network_settings.path'),
'headers' => data_get($protocol_settings, 'network_settings.headers.Host') ? ['Host' => [data_get($protocol_settings, 'network_settings.headers.Host')]] : null,
'max_early_data' => 2048,
'early_data_header_name' => 'Sec-WebSocket-Protocol'
],
default => null
};
$array['transport'] = $transport;
2023-11-17 01:44:01 -05:00
return $array;
}
2025-01-07 22:07:13 -05:00
protected function buildHysteria($password, $server): array
2023-11-17 01:44:01 -05:00
{
2025-01-06 12:20:11 -05:00
$protocol_settings = $server['protocol_settings'];
$baseConfig = [
2023-11-17 01:44:01 -05:00
'server' => $server['host'],
'server_port' => $server['port'],
2025-01-06 12:20:11 -05:00
'tag' => $server['name'],
2023-11-17 01:44:01 -05:00
'tls' => [
'enabled' => true,
2025-01-07 22:07:13 -05:00
'insecure' => (bool) $protocol_settings['tls']['allow_insecure'],
2023-11-17 01:44:01 -05:00
]
];
2025-01-12 10:57:24 -05:00
if ($serverName = data_get($protocol_settings, 'tls_settings.server_name')) {
$baseConfig['tls']['server_name'] = $serverName;
}
2025-01-06 12:20:11 -05:00
$speedConfig = [
'up_mbps' => $protocol_settings['bandwidth']['up'],
'down_mbps' => $protocol_settings['bandwidth']['down'],
];
2025-01-07 22:07:13 -05:00
$versionConfig = match (data_get($protocol_settings, 'version', 1)) {
2025-01-06 12:20:11 -05:00
2 => [
'type' => 'hysteria2',
'password' => $password,
'obfs' => $protocol_settings['obfs']['open'] ? [
'type' => $protocol_settings['obfs']['type'],
'password' => $protocol_settings['obfs']['password']
] : null,
],
default => [
'type' => 'hysteria',
'auth_str' => $password,
'obfs' => $protocol_settings['obfs']['password'],
'disable_mtu_discovery' => true,
]
};
2023-11-17 01:44:01 -05:00
2025-01-06 12:20:11 -05:00
return array_merge(
$baseConfig,
$speedConfig,
$versionConfig
);
2023-11-17 01:44:01 -05:00
}
}