Xboard/app/Protocols/Shadowrocket.php

256 lines
11 KiB
PHP
Raw Normal View History

2023-11-17 01:44:01 -05:00
<?php
namespace App\Protocols;
use App\Models\ServerHysteria;
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 Shadowrocket implements ProtocolInterface
2023-11-17 01:44:01 -05:00
{
2025-01-06 12:20:11 -05:00
public $flags = ['shadowrocket'];
2023-11-17 01:44:01 -05:00
private $servers;
private $user;
public function __construct($user, $servers)
{
$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()
{
$servers = $this->servers;
$user = $this->user;
$uri = '';
//display remaining traffic and expire date
2025-01-06 12:20:11 -05:00
$upload = round($user['u'] / (1024 * 1024 * 1024), 2);
$download = round($user['d'] / (1024 * 1024 * 1024), 2);
$totalTraffic = round($user['transfer_enable'] / (1024 * 1024 * 1024), 2);
2023-11-17 01:44:01 -05:00
$expiredDate = date('Y-m-d', $user['expired_at']);
$uri .= "STATUS=🚀↑:{$upload}GB,↓:{$download}GB,TOT:{$totalTraffic}GB💡Expires:{$expiredDate}\r\n";
foreach ($servers as $item) {
if ($item['type'] === 'shadowsocks') {
$uri .= self::buildShadowsocks($item['password'], $item);
2023-11-17 01:44:01 -05:00
}
if ($item['type'] === 'vmess') {
$uri .= self::buildVmess($user['uuid'], $item);
}
if ($item['type'] === 'vless') {
$uri .= self::buildVless($user['uuid'], $item);
}
if ($item['type'] === 'trojan') {
$uri .= self::buildTrojan($user['uuid'], $item);
}
if ($item['type'] === 'hysteria') {
$uri .= self::buildHysteria($user['uuid'], $item);
}
}
return base64_encode($uri);
}
public static function buildShadowsocks($password, $server)
{
2025-01-06 12:20:11 -05:00
$protocol_settings = $server['protocol_settings'];
2023-11-17 01:44:01 -05:00
$name = rawurlencode($server['name']);
2025-01-09 02:58:32 -05:00
$password = data_get($server, 'password', $password);
2023-11-17 01:44:01 -05:00
$str = str_replace(
['+', '/', '='],
['-', '_', ''],
2025-01-06 12:20:11 -05:00
base64_encode("{$protocol_settings['cipher']}:{$password}")
2023-11-17 01:44:01 -05:00
);
$uri = "ss://{$str}@{$server['host']}:{$server['port']}";
2025-01-06 12:20:11 -05:00
if ($protocol_settings['obfs'] == 'http') {
$obfs_host = data_get($protocol_settings, 'obfs_settings.obfs-host');
$obfs_path = data_get($protocol_settings, 'obfs_settings.obfs-path');
$uri .= "?plugin=obfs-local;obfs=http;obfs-host={$obfs_host};obfs-uri={$obfs_path}";
}
2025-01-06 12:20:11 -05:00
return $uri . "#{$name}\r\n";
2023-11-17 01:44:01 -05:00
}
public static function buildVmess($uuid, $server)
{
2025-01-06 12:20:11 -05:00
$protocol_settings = $server['protocol_settings'];
2023-11-17 01:44:01 -05:00
$userinfo = base64_encode('auto:' . $uuid . '@' . $server['host'] . ':' . $server['port']);
$config = [
'tfo' => 1,
'remark' => $server['name'],
'alterId' => 0
];
2025-01-06 12:20:11 -05:00
if ($protocol_settings['tls']) {
2023-11-17 01:44:01 -05:00
$config['tls'] = 1;
2025-01-06 12:20:11 -05:00
if (data_get($protocol_settings, 'tls_settings')) {
if (data_get($protocol_settings, 'tls_settings.allow_insecure') && !empty(data_get($protocol_settings, 'tls_settings.allow_insecure')))
$config['allowInsecure'] = (int) data_get($protocol_settings, 'tls_settings.allow_insecure');
if (data_get($protocol_settings, 'tls_settings.server_name') && !empty(data_get($protocol_settings, 'tls_settings.server_name')))
$config['peer'] = data_get($protocol_settings, 'tls_settings.server_name');
2023-11-17 01:44:01 -05:00
}
}
2025-01-06 12:20:11 -05:00
switch (data_get($protocol_settings, 'network')) {
case 'tcp':
$config['obfs'] = data_get($protocol_settings, 'network_settings.header.type');
$config['path'] = \Arr::ra(data_get($protocol_settings, 'network_settings.header.request.path', ['/']));
break;
case 'ws':
$config['obfs'] = "websocket";
$config['path'] = data_get($protocol_settings, 'network_settings.path');
2025-01-12 08:10:52 -05:00
if ($host = data_get($protocol_settings, 'network_settings.headers.Host')) {
$config['obfsParam'] = $host;
}
2025-01-06 12:20:11 -05:00
break;
case 'grpc':
$config['obfs'] = "grpc";
$config['path'] = data_get($protocol_settings, 'network_settings.serviceName');
$config['host'] = data_get($protocol_settings, 'tls_settings.server_name') ?? $server['host'];
break;
2023-11-17 01:44:01 -05:00
}
$query = http_build_query($config, '', '&', PHP_QUERY_RFC3986);
$uri = "vmess://{$userinfo}?{$query}";
$uri .= "\r\n";
return $uri;
}
public static function buildVless($uuid, $server)
{
2025-01-06 12:20:11 -05:00
$protocol_settings = $server['protocol_settings'];
2023-11-17 01:44:01 -05:00
$userinfo = base64_encode('auto:' . $uuid . '@' . $server['host'] . ':' . $server['port']);
$config = [
'tfo' => 1,
'remark' => $server['name'],
'alterId' => 0
];
// 判断是否开启xtls
2025-01-06 12:20:11 -05:00
if (data_get($protocol_settings, 'flow')) {
2023-11-17 01:44:01 -05:00
$xtlsMap = [
'none' => 0,
'xtls-rprx-direct' => 1,
'xtls-rprx-vision' => 2
];
2025-01-06 12:20:11 -05:00
if (array_key_exists(data_get($protocol_settings, 'flow'), $xtlsMap)) {
2023-11-17 01:44:01 -05:00
$config['tls'] = 1;
2025-01-06 12:20:11 -05:00
$config['xtls'] = $xtlsMap[data_get($protocol_settings, 'flow')];
2023-11-17 01:44:01 -05:00
}
}
2025-01-06 12:20:11 -05:00
switch (data_get($protocol_settings, 'tls')) {
case 1:
$config['tls'] = 1;
$config['allowInsecure'] = (int) data_get($protocol_settings, 'tls_settings.allow_insecure');
$config['peer'] = data_get($protocol_settings, 'tls_settings.server_name');
break;
case 2:
$config['tls'] = 1;
$config['sni'] = data_get($protocol_settings, 'reality_settings.server_name');
$config['pbk'] = data_get($protocol_settings, 'reality_settings.public_key');
$config['sid'] = data_get($protocol_settings, 'reality_settings.short_id');
$config['fp'] = Helper::getRandFingerprint();
break;
default:
break;
2023-11-17 01:44:01 -05:00
}
2025-01-06 12:20:11 -05:00
switch (data_get($protocol_settings, 'network')) {
case 'tcp':
$config['obfs'] = data_get($protocol_settings, 'network_settings.header.type');
$config['path'] = \Arr::random(data_get($protocol_settings, 'network_settings.header.request.path', ['/']));
2025-01-12 08:10:52 -05:00
$config['obfsParam'] = \Arr::random(data_get($protocol_settings, 'network_settings.header.request.headers.Host', ['/']));
2025-01-06 12:20:11 -05:00
break;
case 'ws':
$config['obfs'] = "websocket";
$config['path'] = data_get($protocol_settings, 'network_settings.path');
2025-01-12 08:10:52 -05:00
if ($host = data_get($protocol_settings, 'network_settings.headers.Host')) {
$config['obfsParam'] = $host;
}
2025-01-06 12:20:11 -05:00
break;
case 'grpc':
$config['obfs'] = "grpc";
$config['path'] = data_get($protocol_settings, 'network_settings.serviceName');
$config['host'] = data_get($protocol_settings, 'tls_settings.server_name') ?? $server['host'];
break;
2023-11-17 01:44:01 -05:00
}
$query = http_build_query($config, '', '&', PHP_QUERY_RFC3986);
$uri = "vless" . "://{$userinfo}?{$query}";
$uri .= "\r\n";
return $uri;
}
public static function buildTrojan($password, $server)
{
2025-01-06 12:20:11 -05:00
$protocol_settings = $server['protocol_settings'];
2023-11-17 01:44:01 -05:00
$name = rawurlencode($server['name']);
$params = [
2025-01-06 12:20:11 -05:00
'allowInsecure' => data_get($protocol_settings, 'tls.allow_insecure'),
'peer' => data_get($protocol_settings, 'tls.server_name')
2023-11-17 01:44:01 -05:00
];
2025-01-06 12:20:11 -05:00
switch (data_get($protocol_settings, 'network')) {
case 'grpc':
2023-11-17 01:44:01 -05:00
$params['obfs'] = 'grpc';
2025-01-06 12:20:11 -05:00
$params['path'] = data_get($protocol_settings, 'network_settings.serviceName');
break;
case 'ws':
$host = data_get($protocol_settings, 'network_settings.headers.Host');
$path = data_get($protocol_settings, 'network_settings.path');
2023-11-17 01:44:01 -05:00
$params['plugin'] = "obfs-local;obfs=websocket;obfs-host={$host};obfs-uri={$path}";
2025-01-06 12:20:11 -05:00
break;
}
2023-11-17 01:44:01 -05:00
$query = http_build_query($params);
$uri = "trojan://{$password}@{$server['host']}:{$server['port']}?{$query}&tfo=1#{$name}";
$uri .= "\r\n";
return $uri;
}
public static function buildHysteria($password, $server)
{
2025-01-06 12:20:11 -05:00
$protocol_settings = $server['protocol_settings'];
switch (data_get($protocol_settings, 'version')) {
2023-11-17 01:44:01 -05:00
case 1:
$params = [
"auth" => $password,
2025-01-06 12:20:11 -05:00
"upmbps" => data_get($protocol_settings, 'bandwidth.up'),
"downmbps" => data_get($protocol_settings, 'bandwidth.down'),
2023-11-17 01:44:01 -05:00
"protocol" => 'udp',
2025-01-06 12:20:11 -05:00
"peer" => data_get($protocol_settings, 'tls.server_name'),
2023-11-17 01:44:01 -05:00
"fastopen" => 1,
];
2025-01-06 12:20:11 -05:00
if (data_get($protocol_settings, 'obfs.open')) {
2023-11-17 01:44:01 -05:00
$params["obfs"] = "xplus";
2025-01-06 12:20:11 -05:00
$params["obfsParam"] = data_get($protocol_settings, 'obfs_settings.password');
2023-11-17 01:44:01 -05:00
}
2025-01-06 12:20:11 -05:00
$params['insecure'] = data_get($protocol_settings, 'tls.allow_insecure');
if (isset($server['ports']))
$params['mport'] = $server['ports'];
2023-11-17 01:44:01 -05:00
$query = http_build_query($params);
$uri = "hysteria://{$server['host']}:{$server['port']}?{$query}#{$server['name']}";
$uri .= "\r\n";
break;
case 2:
$params = [
2025-01-06 12:20:11 -05:00
"peer" => data_get($protocol_settings, 'tls.server_name'),
2023-11-17 01:44:01 -05:00
"obfs" => 'none',
"fastopen" => 1
];
2025-01-06 12:20:11 -05:00
if (data_get($protocol_settings, 'obfs.open')) {
$params['obfs'] = data_get($protocol_settings, 'obfs.type');
$params['obfs-password'] = data_get($protocol_settings, 'obfs.password');
}
$params['insecure'] = data_get($protocol_settings, 'tls.allow_insecure');
if (isset($server['ports']))
$params['mport'] = $server['ports'];
2023-11-17 01:44:01 -05:00
$query = http_build_query($params);
$uri = "hysteria2://{$password}@{$server['host']}:{$server['port']}?{$query}#{$server['name']}";
$uri .= "\r\n";
break;
}
return $uri;
}
}