URLSchemes.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 buildShadowsocksSIP008($server, User $user)
  18. {
  19. $config = [
  20. "id" => $server['id'],
  21. "remarks" => $server['name'],
  22. "server" => $server['host'],
  23. "server_port" => $server['port'],
  24. "password" => $user['uuid'],
  25. "method" => $server['cipher']
  26. ];
  27. return $config;
  28. }
  29. public static function buildVmess($server, User $user)
  30. {
  31. $config = [
  32. "v" => "2",
  33. "ps" => $server['name'],
  34. "add" => $server['host'],
  35. "port" => (string)$server['port'],
  36. "id" => $user['uuid'],
  37. "aid" => (string)$server['alter_id'],
  38. "net" => $server['network'],
  39. "type" => "none",
  40. "host" => "",
  41. "path" => "",
  42. "tls" => $server['tls'] ? "tls" : ""
  43. ];
  44. if ((string)$server['network'] === 'ws') {
  45. $wsSettings = json_decode($server['networkSettings'], true);
  46. if (isset($wsSettings['path'])) $config['path'] = $wsSettings['path'];
  47. if (isset($wsSettings['headers']['Host'])) $config['host'] = $wsSettings['headers']['Host'];
  48. }
  49. return "vmess://" . base64_encode(json_encode($config)) . "\r\n";
  50. }
  51. public static function buildTrojan($server, User $user)
  52. {
  53. $name = rawurlencode($server['name']);
  54. $query = http_build_query([
  55. 'allowInsecure' => $server['allow_insecure'],
  56. 'peer' => $server['server_name'],
  57. 'sni' => $server['server_name']
  58. ]);
  59. $uri = "trojan://{$user['uuid']}@{$server['host']}:{$server['port']}?{$query}#{$name}";
  60. $uri .= "\r\n";
  61. return $uri;
  62. }
  63. }