2021-07-29 20:13:01 +09:00
|
|
|
<?php
|
|
|
|
|
2023-06-14 11:38:21 +08:00
|
|
|
namespace App\Protocols;
|
2021-07-29 20:13:01 +09:00
|
|
|
|
2023-08-20 20:28:44 +09:00
|
|
|
|
|
|
|
use App\Utils\Helper;
|
|
|
|
|
2022-03-25 02:02:01 +08:00
|
|
|
class SagerNet
|
2021-07-29 20:13:01 +09:00
|
|
|
{
|
2022-03-25 02:02:01 +08:00
|
|
|
public $flag = 'sagernet';
|
2021-07-29 20:13:01 +09:00
|
|
|
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) {
|
2023-02-15 14:53:13 +08:00
|
|
|
if ($item['type'] === 'vmess') {
|
2021-07-29 20:13:01 +09:00
|
|
|
$uri .= self::buildVmess($user['uuid'], $item);
|
|
|
|
}
|
2023-08-20 20:28:44 +09:00
|
|
|
if ($item['type'] === 'vless') {
|
|
|
|
$uri .= self::buildVless($user['uuid'], $item);
|
|
|
|
}
|
2021-07-29 20:13:01 +09:00
|
|
|
if ($item['type'] === 'shadowsocks') {
|
|
|
|
$uri .= self::buildShadowsocks($user['uuid'], $item);
|
|
|
|
}
|
|
|
|
if ($item['type'] === 'trojan') {
|
|
|
|
$uri .= self::buildTrojan($user['uuid'], $item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return base64_encode($uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function buildShadowsocks($uuid, $server)
|
|
|
|
{
|
|
|
|
$name = rawurlencode($server['name']);
|
|
|
|
$str = str_replace(
|
|
|
|
['+', '/', '='],
|
|
|
|
['-', '_', ''],
|
|
|
|
base64_encode("{$server['cipher']}:{$uuid}")
|
|
|
|
);
|
|
|
|
return "ss://{$str}@{$server['host']}:{$server['port']}#{$name}\r\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function buildShadowsocksSIP008($uuid, $server)
|
|
|
|
{
|
|
|
|
$config = [
|
|
|
|
"id" => $server['id'],
|
|
|
|
"remarks" => $server['name'],
|
|
|
|
"server" => $server['host'],
|
|
|
|
"server_port" => $server['port'],
|
|
|
|
"password" => $uuid,
|
|
|
|
"method" => $server['cipher']
|
|
|
|
];
|
|
|
|
return $config;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function buildVmess($uuid, $server)
|
|
|
|
{
|
|
|
|
$config = [
|
|
|
|
"encryption" => "none",
|
|
|
|
"type" => urlencode($server['network']),
|
|
|
|
"security" => $server['tls'] ? "tls" : "",
|
|
|
|
];
|
2021-07-31 12:13:01 +08:00
|
|
|
if ($server['tls']) {
|
|
|
|
if ($server['tlsSettings']) {
|
2021-08-06 01:43:01 +09:00
|
|
|
$tlsSettings = $server['tlsSettings'];
|
2021-07-31 12:13:01 +08:00
|
|
|
if (isset($tlsSettings['serverName']) && !empty($tlsSettings['serverName']))
|
|
|
|
$config['sni'] = urlencode($tlsSettings['serverName']);
|
|
|
|
}
|
|
|
|
}
|
2023-03-27 21:53:52 +08:00
|
|
|
if ((string)$server['network'] === 'tcp') {
|
|
|
|
$tcpSettings = $server['networkSettings'];
|
|
|
|
if (isset($tcpSettings['header']['type'])) $config['type'] = $tcpSettings['header']['type'];
|
|
|
|
if (isset($tcpSettings['header']['request']['path'][0])) $config['path'] = $tcpSettings['header']['request']['path'][0];
|
|
|
|
}
|
2021-07-29 20:13:01 +09:00
|
|
|
if ((string)$server['network'] === 'ws') {
|
2021-08-06 01:43:01 +09:00
|
|
|
$wsSettings = $server['networkSettings'];
|
2022-03-25 02:02:01 +08:00
|
|
|
if (isset($wsSettings['path'])) $config['path'] = $wsSettings['path'];
|
2021-07-29 20:13:01 +09:00
|
|
|
if (isset($wsSettings['headers']['Host'])) $config['host'] = urlencode($wsSettings['headers']['Host']);
|
|
|
|
}
|
|
|
|
if ((string)$server['network'] === 'grpc') {
|
2021-08-06 01:43:01 +09:00
|
|
|
$grpcSettings = $server['networkSettings'];
|
2021-07-29 20:13:01 +09:00
|
|
|
if (isset($grpcSettings['serviceName'])) $config['serviceName'] = urlencode($grpcSettings['serviceName']);
|
|
|
|
}
|
|
|
|
return "vmess://" . $uuid . "@" . $server['host'] . ":" . $server['port'] . "?" . http_build_query($config) . "#" . urlencode($server['name']) . "\r\n";
|
|
|
|
}
|
|
|
|
|
2023-08-20 20:28:44 +09:00
|
|
|
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" => "",
|
|
|
|
"pbk" => "",
|
|
|
|
"sid" =>"",
|
|
|
|
];
|
|
|
|
|
|
|
|
$output = "vless://" . $uuid . "@" . $config['add'] . ":" . $config['port'];
|
|
|
|
$output .= "?" . "type={$config['type']}" . "&encryption={$config['encryption']}" . "&security={$config['security']}";
|
|
|
|
|
|
|
|
if ($server['tls']) {
|
2023-08-21 02:11:18 +09:00
|
|
|
if ($config['flow'] !="") $output .= "&flow={$config['flow']}";
|
2023-08-20 20:28:44 +09:00
|
|
|
if ($server['tls_settings']) {
|
|
|
|
$tlsSettings = $server['tls_settings'];
|
|
|
|
if (isset($tlsSettings['server_name']) && !empty($tlsSettings['server_name'])) $config['sni'] = $tlsSettings['server_name'];
|
2023-08-20 23:54:54 +09:00
|
|
|
$output .= "&sni={$config['sni']}";
|
2023-08-20 20:28:44 +09:00
|
|
|
if ($server['tls'] == 2) {
|
|
|
|
$config['pbk'] = $tlsSettings['public_key'];
|
|
|
|
$config['sid'] = $tlsSettings['shortId'];
|
|
|
|
$output .= "&pbk={$config['pbk']}" . "&sid={$config['sid']}";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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'];
|
|
|
|
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'];
|
2023-08-21 13:36:55 +09:00
|
|
|
if (isset($grpcSettings['serviceName'])) $config['serviceName'] = Helper::encodeURIComponent($grpcSettings['serviceName']);
|
2023-08-20 20:28:44 +09:00
|
|
|
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";
|
|
|
|
}
|
|
|
|
|
2021-07-29 20:13:01 +09:00
|
|
|
public static function buildTrojan($uuid, $server)
|
|
|
|
{
|
|
|
|
$name = rawurlencode($server['name']);
|
|
|
|
$query = http_build_query([
|
|
|
|
'allowInsecure' => $server['allow_insecure'],
|
|
|
|
'peer' => $server['server_name'],
|
|
|
|
'sni' => $server['server_name']
|
|
|
|
]);
|
|
|
|
$uri = "trojan://{$uuid}@{$server['host']}:{$server['port']}?{$query}#{$name}";
|
|
|
|
$uri .= "\r\n";
|
|
|
|
return $uri;
|
|
|
|
}
|
|
|
|
}
|