2021-07-03 22:44:53 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Client\Protocols;
|
|
|
|
|
|
|
|
|
|
|
|
class V2rayNG
|
|
|
|
{
|
|
|
|
public $flag = 'v2rayng';
|
|
|
|
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 = '';
|
|
|
|
|
|
|
|
foreach ($servers as $item) {
|
|
|
|
if ($item['type'] === 'v2ray') {
|
|
|
|
$uri .= self::buildVmess($user['uuid'], $item);
|
|
|
|
}
|
2021-07-04 12:21:10 +08:00
|
|
|
if ($item['type'] === 'shadowsocks') {
|
|
|
|
$uri .= self::buildShadowsocks($user['uuid'], $item);
|
|
|
|
}
|
|
|
|
if ($item['type'] === 'trojan') {
|
|
|
|
$uri .= self::buildTrojan($user['uuid'], $item);
|
|
|
|
}
|
2021-07-03 22:44:53 +08:00
|
|
|
}
|
|
|
|
return base64_encode($uri);
|
|
|
|
}
|
|
|
|
|
2021-07-04 12:21:10 +08:00
|
|
|
public static function buildShadowsocks($password, $server)
|
|
|
|
{
|
|
|
|
$name = rawurlencode($server['name']);
|
|
|
|
$str = str_replace(
|
|
|
|
['+', '/', '='],
|
|
|
|
['-', '_', ''],
|
|
|
|
base64_encode("{$server['cipher']}:{$password}")
|
|
|
|
);
|
|
|
|
return "ss://{$str}@{$server['host']}:{$server['port']}#{$name}\r\n";
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:44:53 +08:00
|
|
|
public static function buildVmess($uuid, $server)
|
|
|
|
{
|
|
|
|
$config = [
|
|
|
|
"v" => "2",
|
|
|
|
"ps" => $server['name'],
|
|
|
|
"add" => $server['host'],
|
|
|
|
"port" => (string)$server['port'],
|
|
|
|
"id" => $uuid,
|
2022-01-04 13:40:35 +08:00
|
|
|
"aid" => '0',
|
2021-07-03 22:44:53 +08:00
|
|
|
"net" => $server['network'],
|
|
|
|
"type" => "none",
|
|
|
|
"host" => "",
|
|
|
|
"path" => "",
|
|
|
|
"tls" => $server['tls'] ? "tls" : "",
|
|
|
|
];
|
2021-07-05 21:01:28 +08:00
|
|
|
if ($server['tls']) {
|
|
|
|
if ($server['tlsSettings']) {
|
2021-08-06 00:43:01 +08:00
|
|
|
$tlsSettings = $server['tlsSettings'];
|
2021-07-05 21:01:28 +08:00
|
|
|
if (isset($tlsSettings['serverName']) && !empty($tlsSettings['serverName']))
|
|
|
|
$config['sni'] = $tlsSettings['serverName'];
|
|
|
|
}
|
|
|
|
}
|
2021-07-03 22:44:53 +08:00
|
|
|
if ((string)$server['network'] === 'ws') {
|
2021-08-06 00:43:01 +08:00
|
|
|
$wsSettings = $server['networkSettings'];
|
2021-07-03 22:44:53 +08:00
|
|
|
if (isset($wsSettings['path'])) $config['path'] = $wsSettings['path'];
|
|
|
|
if (isset($wsSettings['headers']['Host'])) $config['host'] = $wsSettings['headers']['Host'];
|
|
|
|
}
|
|
|
|
if ((string)$server['network'] === 'grpc') {
|
2021-08-06 00:43:01 +08:00
|
|
|
$grpcSettings = $server['networkSettings'];
|
2021-07-31 12:13:01 +08:00
|
|
|
if (isset($grpcSettings['serviceName'])) $config['path'] = $grpcSettings['serviceName'];
|
2021-07-03 22:44:53 +08:00
|
|
|
}
|
|
|
|
return "vmess://" . base64_encode(json_encode($config)) . "\r\n";
|
|
|
|
}
|
|
|
|
|
2021-07-04 12:21:10 +08:00
|
|
|
public static function buildTrojan($password, $server)
|
|
|
|
{
|
|
|
|
$name = rawurlencode($server['name']);
|
|
|
|
$query = http_build_query([
|
|
|
|
'allowInsecure' => $server['allow_insecure'],
|
|
|
|
'peer' => $server['server_name'],
|
|
|
|
'sni' => $server['server_name']
|
|
|
|
]);
|
|
|
|
$uri = "trojan://{$password}@{$server['host']}:{$server['port']}?{$query}#{$name}";
|
|
|
|
$uri .= "\r\n";
|
|
|
|
return $uri;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-03 22:44:53 +08:00
|
|
|
}
|