v2board/app/Protocols/Shadowrocket.php

220 lines
9.6 KiB
PHP
Raw Normal View History

<?php
2023-06-14 11:38:21 +08:00
namespace App\Protocols;
2022-11-18 03:25:02 +08:00
use App\Utils\Helper;
class Shadowrocket
{
2021-07-02 23:34:42 +09:00
public $flag = 'shadowrocket';
private $servers;
private $user;
public function __construct($user, $servers)
{
$this->user = $user;
$this->servers = $servers;
}
public function handle()
{
$servers = $this->servers;
$user = $this->user;
$uri = '';
//display remaining traffic and expire date
$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);
$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($user['uuid'], $item);
}
2023-02-15 14:53:13 +08:00
if ($item['type'] === 'vmess') {
2021-07-02 23:34:42 +09:00
$uri .= self::buildVmess($user['uuid'], $item);
}
if ($item['type'] === 'vless') {
$uri .= self::buildVless($user['uuid'], $item);
}
2021-07-02 23:34:42 +09:00
if ($item['type'] === 'trojan') {
$uri .= self::buildTrojan($user['uuid'], $item);
}
}
return base64_encode($uri);
}
public static function buildShadowsocks($password, $server)
{
2022-11-18 03:25:02 +08:00
if ($server['cipher'] === '2022-blake3-aes-128-gcm') {
2023-03-08 02:28:00 +08:00
$serverKey = Helper::getServerKey($server['created_at'], 16);
2022-11-18 03:25:02 +08:00
$userKey = Helper::uuidToBase64($password, 16);
$password = "{$serverKey}:{$userKey}";
}
if ($server['cipher'] === '2022-blake3-aes-256-gcm') {
2023-03-08 02:28:00 +08:00
$serverKey = Helper::getServerKey($server['created_at'], 32);
2022-11-18 03:25:02 +08:00
$userKey = Helper::uuidToBase64($password, 32);
$password = "{$serverKey}:{$userKey}";
}
2020-11-15 17:10:32 +08:00
$name = rawurlencode($server['name']);
$str = str_replace(
['+', '/', '='],
['-', '_', ''],
2020-11-15 17:10:32 +08:00
base64_encode("{$server['cipher']}:{$password}")
);
2020-11-15 17:10:32 +08:00
return "ss://{$str}@{$server['host']}:{$server['port']}#{$name}\r\n";
}
public static function buildVmess($uuid, $server)
{
2020-11-15 17:10:32 +08:00
$userinfo = base64_encode('auto:' . $uuid . '@' . $server['host'] . ':' . $server['port']);
$config = [
2020-11-20 00:26:39 +08:00
'tfo' => 1,
2020-11-17 23:02:12 +08:00
'remark' => $server['name'],
2022-01-04 13:40:35 +08:00
'alterId' => 0
];
2020-11-15 17:10:32 +08:00
if ($server['tls']) {
$config['tls'] = 1;
2020-11-20 00:26:39 +08:00
if ($server['tlsSettings']) {
2021-08-06 01:43:01 +09:00
$tlsSettings = $server['tlsSettings'];
2020-11-20 00:26:39 +08:00
if (isset($tlsSettings['allowInsecure']) && !empty($tlsSettings['allowInsecure']))
$config['allowInsecure'] = (int)$tlsSettings['allowInsecure'];
if (isset($tlsSettings['serverName']) && !empty($tlsSettings['serverName']))
$config['peer'] = $tlsSettings['serverName'];
}
}
2023-03-27 21:53:52 +08:00
if ($server['network'] === 'tcp') {
if ($server['networkSettings']) {
$tcpSettings = $server['networkSettings'];
if (isset($tcpSettings['header']['type']) && !empty($tcpSettings['header']['type']))
$config['obfs'] = $tcpSettings['header']['type'];
if (isset($tcpSettings['header']['request']['path'][0]) && !empty($tcpSettings['header']['request']['path'][0]))
$config['path'] = $tcpSettings['header']['request']['path'][0];
}
}
2020-11-15 17:10:32 +08:00
if ($server['network'] === 'ws') {
$config['obfs'] = "websocket";
2020-11-20 00:26:39 +08:00
if ($server['networkSettings']) {
2021-08-06 01:43:01 +09:00
$wsSettings = $server['networkSettings'];
2020-11-20 00:26:39 +08:00
if (isset($wsSettings['path']) && !empty($wsSettings['path']))
$config['path'] = $wsSettings['path'];
if (isset($wsSettings['headers']['Host']) && !empty($wsSettings['headers']['Host']))
$config['obfsParam'] = $wsSettings['headers']['Host'];
}
}
if ($server['network'] === 'grpc') {
$config['obfs'] = "grpc";
2021-07-02 22:07:54 +09:00
if ($server['networkSettings']) {
2021-08-06 01:43:01 +09:00
$grpcSettings = $server['networkSettings'];
2021-07-02 21:57:00 +09:00
if (isset($grpcSettings['serviceName']) && !empty($grpcSettings['serviceName']))
$config['path'] = $grpcSettings['serviceName'];
2021-07-02 22:05:12 +09:00
}
if (isset($tlsSettings)) {
$config['host'] = $tlsSettings['serverName'];
} else {
$config['host'] = $server['host'];
}
}
2020-11-20 00:26:39 +08:00
$query = http_build_query($config, '', '&', PHP_QUERY_RFC3986);
$uri = "vmess://{$userinfo}?{$query}";
$uri .= "\r\n";
return $uri;
}
public static function buildVless($uuid, $server)
{
$config = [
"name" => Helper::encodeURIComponent($server['name']),
"add" => $server['host'],
"port" => (string)$server['port'],
"type" => $server['network'],
"encryption" => "none",
"host" => "",
"path" => "",
"headerType" => "none",
"quicSecurity" => "none",
"serviceName" => "",
"mode" => "gun",
"security" => $server['tls'] !=0 ? ($server['tls'] == 2 ? "reality":"tls") : "",
"flow" => $server['flow'],
"sni" => "",
2023-08-15 00:22:06 +09:00
"pbk" => "",
"sid" =>"",
];
$output = "vless://" . $uuid . "@" . $config['add'] . ":" . $config['port'];
$output .= "?" . "type={$config['type']}" . "&encryption={$config['encryption']}" . "&security={$config['security']}";
if ($server['tls']) {
if ($server['tls_settings']) {
$tlsSettings = $server['tls_settings'];
if (isset($tlsSettings['server_name']) && !empty($tlsSettings['server_name'])) $config['sni'] = $tlsSettings['server_name'];
$output .= "&sni={$config['sni']}";
2023-08-21 00:15:39 +09:00
if ($config['flow'] !="") $output .= "&flow={$config['flow']}";
2023-08-15 00:22:06 +09:00
if ($server['tls'] == 2) {
$config['pbk'] = $tlsSettings['public_key'];
$config['sid'] = $tlsSettings['shortId'];
$output .= "&pbk={$config['pbk']}" . "&sid={$config['sid']}";
2023-08-15 00:22:06 +09:00
}
}
}
if ((string)$server['network'] === 'tcp') {
$tcpSettings = $server['network_settings'];
if (isset($tcpSettings['header']['type'])) $config['headerType'] = $tcpSettings['header']['type'];
$output .= "&headerType={$config['headerType']}";
}
if ((string)$server['network'] === 'kcp') {
$kcpSettings = $server['network_settings'];
if (isset($kcpSettings['header']['type'])) $config['headerType'] = $kcpSettings['header']['type'];
if (isset($kcpSettings['seed'])) $config['path'] = Helper::encodeURIComponent($kcpSettings['seed']);
$output .= "&headerType={$config['headerType']}" . "&seed={$config['path']}";
}
if ((string)$server['network'] === 'ws') {
$wsSettings = $server['network_settings'];
2023-08-15 01:27:36 +09:00
if (isset($wsSettings['path'])) $config['path'] = Helper::encodeURIComponent($wsSettings['path']);
if (isset($wsSettings['headers']['Host'])) $config['host'] = Helper::encodeURIComponent($wsSettings['headers']['Host']);
$output .= "&path={$config['path']}" . "&host={$config['host']}";
}
if ((string)$server['network'] === 'h2') {
$h2Settings = $server['network_settings'];
if (isset($h2Settings['path'])) $config['path'] = Helper::encodeURIComponent($h2Settings['path']);
if (isset($h2Settings['host'])) $config['host'] = Helper::encodeURIComponent($h2Settings['host']);
$output .= "&path={$config['path']}" . "&host={$config['host']}";
}
if ((string)$server['network'] === 'quic') {
$quicSettings = $server['network_settings'];
if (isset($quicSettings['security'])) $config['quicSecurity'] = $quicSettings['security'];
if (isset($quicSettings['header']['type'])) $config['headerType'] = $quicSettings['header']['type'];
$output .= "&quicSecurity={$config['quicSecurity']}" . "&headerType={$config['headerType']}";
if ((string)$quicSettings['security'] !== 'none' && isset($quicSettings['key'])) $config['path'] = Helper::encodeURIComponent($quicSettings['key']);
$output .= "&key={$config['path']}";
}
if ((string)$server['network'] === 'grpc') {
$grpcSettings = $server['network_settings'];
if (isset($grpcSettings['serviceName'])) $config['serviceName'] = $grpcSettings['serviceName'];
if (isset($grpcSettings['multiMode'])) $config['mode'] = $grpcSettings['multiMode'] ? "multi" : "gun";
$output .= "&serviceName={$config['serviceName']}" . "&mode={$config['mode']}";
}
$output .= "&fp=chrome" . "#" . $config['name'];
return $output . "\r\n";
}
public static function buildTrojan($password, $server)
{
2020-11-15 17:10:32 +08:00
$name = rawurlencode($server['name']);
$query = http_build_query([
2020-11-15 17:10:32 +08:00
'allowInsecure' => $server['allow_insecure'],
'peer' => $server['server_name']
]);
2020-11-15 17:10:32 +08:00
$uri = "trojan://{$password}@{$server['host']}:{$server['port']}?{$query}&tfo=1#{$name}";
$uri .= "\r\n";
return $uri;
}
}