2020-07-20 17:22:50 +08:00
|
|
|
<?php
|
|
|
|
|
2021-07-02 22:34:42 +08:00
|
|
|
namespace App\Http\Controllers\Client\Protocols;
|
2020-07-20 17:22:50 +08:00
|
|
|
|
|
|
|
class Shadowrocket
|
|
|
|
{
|
2021-07-02 22:34:42 +08: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);
|
|
|
|
}
|
|
|
|
if ($item['type'] === 'v2ray') {
|
|
|
|
$uri .= self::buildVmess($user['uuid'], $item);
|
|
|
|
}
|
|
|
|
if ($item['type'] === 'trojan') {
|
|
|
|
$uri .= self::buildTrojan($user['uuid'], $item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return base64_encode($uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-04 16:07:19 +08:00
|
|
|
public static function buildShadowsocks($password, $server)
|
|
|
|
{
|
2020-11-15 17:10:32 +08:00
|
|
|
$name = rawurlencode($server['name']);
|
2020-10-04 16:07:19 +08:00
|
|
|
$str = str_replace(
|
|
|
|
['+', '/', '='],
|
|
|
|
['-', '_', ''],
|
2020-11-15 17:10:32 +08:00
|
|
|
base64_encode("{$server['cipher']}:{$password}")
|
2020-10-04 16:07:19 +08:00
|
|
|
);
|
2020-11-15 17:10:32 +08:00
|
|
|
return "ss://{$str}@{$server['host']}:{$server['port']}#{$name}\r\n";
|
2020-10-04 16:07:19 +08:00
|
|
|
}
|
|
|
|
|
2020-07-20 17:22:50 +08:00
|
|
|
public static function buildVmess($uuid, $server)
|
|
|
|
{
|
2020-11-15 17:10:32 +08:00
|
|
|
$userinfo = base64_encode('auto:' . $uuid . '@' . $server['host'] . ':' . $server['port']);
|
2020-07-20 17:22:50 +08:00
|
|
|
$config = [
|
2020-11-20 00:26:39 +08:00
|
|
|
'tfo' => 1,
|
2020-11-17 23:02:12 +08:00
|
|
|
'remark' => $server['name'],
|
|
|
|
'alterId' => $server['alter_id']
|
2020-07-20 17:22:50 +08:00
|
|
|
];
|
2020-11-15 17:10:32 +08:00
|
|
|
if ($server['tls']) {
|
2020-07-20 17:22:50 +08:00
|
|
|
$config['tls'] = 1;
|
2020-11-20 00:26:39 +08:00
|
|
|
if ($server['tlsSettings']) {
|
|
|
|
$tlsSettings = json_decode($server['tlsSettings'], true);
|
|
|
|
if (isset($tlsSettings['allowInsecure']) && !empty($tlsSettings['allowInsecure']))
|
|
|
|
$config['allowInsecure'] = (int)$tlsSettings['allowInsecure'];
|
|
|
|
if (isset($tlsSettings['serverName']) && !empty($tlsSettings['serverName']))
|
|
|
|
$config['peer'] = $tlsSettings['serverName'];
|
|
|
|
}
|
2020-07-20 17:22:50 +08:00
|
|
|
}
|
2020-11-15 17:10:32 +08:00
|
|
|
if ($server['network'] === 'ws') {
|
2020-07-20 17:22:50 +08:00
|
|
|
$config['obfs'] = "websocket";
|
2020-11-20 00:26:39 +08:00
|
|
|
if ($server['networkSettings']) {
|
|
|
|
$wsSettings = json_decode($server['networkSettings'], true);
|
|
|
|
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'];
|
|
|
|
}
|
2020-07-20 17:22:50 +08:00
|
|
|
}
|
2021-06-12 14:42:44 +08:00
|
|
|
if ($server['network'] === 'grpc') {
|
|
|
|
$config['obfs'] = "grpc";
|
2021-07-02 21:07:54 +08:00
|
|
|
if ($server['networkSettings']) {
|
2021-07-02 20:57:00 +08:00
|
|
|
$grpcSettings = json_decode($server['networkSettings'], true);
|
|
|
|
if (isset($grpcSettings['serviceName']) && !empty($grpcSettings['serviceName']))
|
|
|
|
$config['path'] = $grpcSettings['serviceName'];
|
2021-07-02 21:05:12 +08:00
|
|
|
}
|
|
|
|
if (isset($tlsSettings)) {
|
|
|
|
$config['host'] = $tlsSettings['serverName'];
|
|
|
|
} else {
|
|
|
|
$config['host'] = $server['host'];
|
2021-06-12 14:42:44 +08:00
|
|
|
}
|
|
|
|
}
|
2020-11-20 00:26:39 +08:00
|
|
|
$query = http_build_query($config, '', '&', PHP_QUERY_RFC3986);
|
|
|
|
$uri = "vmess://{$userinfo}?{$query}";
|
2020-07-20 17:22:50 +08:00
|
|
|
$uri .= "\r\n";
|
|
|
|
return $uri;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function buildTrojan($password, $server)
|
|
|
|
{
|
2020-11-15 17:10:32 +08:00
|
|
|
$name = rawurlencode($server['name']);
|
2020-07-20 17:22:50 +08:00
|
|
|
$query = http_build_query([
|
2020-11-15 17:10:32 +08:00
|
|
|
'allowInsecure' => $server['allow_insecure'],
|
|
|
|
'peer' => $server['server_name']
|
2020-07-20 17:22:50 +08:00
|
|
|
]);
|
2020-11-15 17:10:32 +08:00
|
|
|
$uri = "trojan://{$password}@{$server['host']}:{$server['port']}?{$query}&tfo=1#{$name}";
|
2020-07-20 17:22:50 +08:00
|
|
|
$uri .= "\r\n";
|
|
|
|
return $uri;
|
|
|
|
}
|
|
|
|
}
|