URLSchemes.php 1.8 KB

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