URLSchemes.php 1.7 KB

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