URLSchemes.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace App\Utils;
  3. use App\Models\Server;
  4. use App\Models\ServerShadowsocks;
  5. use App\Models\ServerTrojan;
  6. use App\Models\User;
  7. class URLSchemes
  8. {
  9. public static function buildShadowsocks(ServerShadowsocks $server, User $user)
  10. {
  11. $str = str_replace(
  12. ['+', '/', '='],
  13. ['-', '_', ''],
  14. base64_encode("{$server->cipher}:{$user->uuid}")
  15. );
  16. return "ss://{$str}@{$server->host}:{$server->port}#{$server->name}\r\n";
  17. }
  18. public static function buildVmess(Server $server, User $user)
  19. {
  20. $config = [
  21. "v" => "2",
  22. "ps" => $server->name,
  23. "add" => $server->host,
  24. "port" => $server->port,
  25. "id" => $user->uuid,
  26. "aid" => "2",
  27. "net" => $server->network,
  28. "type" => "none",
  29. "host" => "",
  30. "path" => "",
  31. "tls" => $server->tls ? "tls" : ""
  32. ];
  33. if ((string)$server->network === 'ws') {
  34. $wsSettings = json_decode($server->networkSettings);
  35. if (isset($wsSettings->path)) $config['path'] = $wsSettings->path;
  36. if (isset($wsSettings->headers->Host)) $config['host'] = $wsSettings->headers->Host;
  37. }
  38. return "vmess://" . base64_encode(json_encode($config)) . "\r\n";
  39. }
  40. public static function buildTrojan(ServerTrojan $server, User $user)
  41. {
  42. $server->name = rawurlencode($server->name);
  43. $query = http_build_query([
  44. 'allowInsecure' => $server->allow_insecure,
  45. 'peer' => $server->server_name,
  46. 'sni' => $server->server_name
  47. ]);
  48. $uri = "trojan://{$user->uuid}@{$server->host}:{$server->port}?{$query}#{$server->name}";
  49. $uri .= "\r\n";
  50. return $uri;
  51. }
  52. }