v2board/app/Utils/URLSchemes.php

69 lines
2.1 KiB
PHP
Raw Normal View History

2020-10-04 14:21:09 +08:00
<?php
namespace App\Utils;
use App\Models\Server;
use App\Models\User;
class URLSchemes
{
2020-11-15 17:10:32 +08:00
public static function buildShadowsocks($server, User $user)
2020-10-04 14:21:09 +08:00
{
2020-11-15 17:10:32 +08:00
$name = rawurlencode($server['name']);
2020-10-04 14:21:09 +08:00
$str = str_replace(
['+', '/', '='],
['-', '_', ''],
2020-11-15 17:10:32 +08:00
base64_encode("{$server['cipher']}:{$user['uuid']}")
2020-10-04 14:21:09 +08:00
);
2020-11-15 17:10:32 +08:00
return "ss://{$str}@{$server['host']}:{$server['port']}#{$name}\r\n";
2020-10-04 14:21:09 +08:00
}
public static function buildShadowsocksSIP008($server, User $user)
{
$config = [
"id" => $server['id'],
"remark" => $server['name'],
"server" => $server['host'],
"server_port" => $server['port'],
"password" => $user['uuid'],
"method" => $server['cipher']
];
return $config;
}
2020-10-04 14:21:09 +08:00
2020-11-15 17:10:32 +08:00
public static function buildVmess($server, User $user)
2020-10-04 14:21:09 +08:00
{
$config = [
"v" => "2",
2020-11-15 17:10:32 +08:00
"ps" => $server['name'],
"add" => $server['host'],
"port" => $server['port'],
"id" => $user['uuid'],
2020-11-17 22:18:00 +08:00
"aid" => $server['alter_id'],
2020-11-15 17:10:32 +08:00
"net" => $server['network'],
2020-10-04 14:21:09 +08:00
"type" => "none",
"host" => "",
"path" => "",
2020-11-15 17:10:32 +08:00
"tls" => $server['tls'] ? "tls" : ""
2020-10-04 14:21:09 +08:00
];
2020-11-15 17:10:32 +08:00
if ((string)$server['network'] === 'ws') {
$wsSettings = json_decode($server['networkSettings'], true);
if (isset($wsSettings['path'])) $config['path'] = $wsSettings['path'];
if (isset($wsSettings['headers']['Host'])) $config['host'] = $wsSettings['headers']['Host'];
2020-10-04 14:21:09 +08:00
}
return "vmess://" . base64_encode(json_encode($config)) . "\r\n";
}
2020-11-15 17:10:32 +08:00
public static function buildTrojan($server, User $user)
2020-10-04 14:21:09 +08:00
{
2020-11-15 17:10:32 +08:00
$name = rawurlencode($server['name']);
2020-10-04 14:21:09 +08:00
$query = http_build_query([
2020-11-15 17:10:32 +08:00
'allowInsecure' => $server['allow_insecure'],
'peer' => $server['server_name'],
'sni' => $server['server_name']
2020-10-04 14:21:09 +08:00
]);
2020-11-15 17:10:32 +08:00
$uri = "trojan://{$user['uuid']}@{$server['host']}:{$server['port']}?{$query}#{$name}";
2020-10-04 14:21:09 +08:00
$uri .= "\r\n";
return $uri;
}
}