QuantumultX.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace App\Utils\Clients\Formatters;
  3. use App\Utils\Library\Templates\Formatter;
  4. class QuantumultX implements Formatter
  5. {
  6. // https://github.com/crossutility/Quantumult-X
  7. public static function build(array $servers, bool $isEncode = true): string
  8. {
  9. $validTypes = ['shadowsocks', 'shadowsocksr', 'vmess', 'trojan'];
  10. $url = '';
  11. foreach ($servers as $server) {
  12. if (in_array($server['type'], $validTypes, true)) {
  13. $url .= call_user_func([self::class, 'build'.ucfirst($server['type'])], $server);
  14. }
  15. }
  16. return $isEncode ? base64_encode($url) : $url;
  17. }
  18. public static function buildShadowsocks(array $server): string
  19. {
  20. $config = array_filter([
  21. "shadowsocks={$server['host']}:{$server['port']}",
  22. "method={$server['method']}",
  23. "password={$server['passwd']}",
  24. 'fast-open=true',
  25. "udp-relay={$server['udp']}",
  26. "tag={$server['name']}",
  27. ]);
  28. return implode(',', $config).PHP_EOL;
  29. }
  30. public static function buildShadowsocksr(array $server): string
  31. {
  32. $config = array_filter([
  33. "shadowsocks={$server['host']}:{$server['port']}",
  34. "method={$server['method']}",
  35. "password={$server['passwd']}",
  36. "ssr-protocol={$server['protocol']}",
  37. 'ssr-protocol-param='.($server['protocol_param'] ?? ''),
  38. "obfs={$server['obfs']}",
  39. 'obfs-host='.($server['obfs_param'] ?? ''),
  40. 'fast-open=true',
  41. "udp-relay={$server['udp']}",
  42. "tag={$server['name']}",
  43. ]);
  44. return implode(',', $config).PHP_EOL;
  45. }
  46. public static function buildVmess(array $server): string
  47. {
  48. $config = [
  49. "vmess={$server['host']}:{$server['port']}",
  50. "method={$server['method']}",
  51. "password={$server['uuid']}",
  52. 'fast-open=true',
  53. "udp-relay={$server['udp']}",
  54. "tag={$server['name']}",
  55. ];
  56. if (isset($server['v2_tls']) && $server['v2_tls']) {
  57. if ($server['v2_net'] === 'tcp') {
  58. $config[] = 'obfs=over-tls';
  59. } else {
  60. $config[] = 'obfs=wss';
  61. }
  62. } elseif (isset($server['v2_net']) && $server['v2_net'] === 'ws') {
  63. $config[] = 'obfs=ws';
  64. }
  65. if (isset($server['v2_tls']) && $server['v2_tls']) {
  66. $config[] = 'tls-verification=true';
  67. if (! empty($server['v2_host'])) {
  68. $config[] = "tls-host={$server['v2_host']}";
  69. }
  70. }
  71. if (isset($server['v2_type']) && $server['v2_type'] === 'ws' && ! empty($server['v2_path'])) {
  72. $config[] = "obfs-uri={$server['v2_path']}";
  73. $config[] = "obfs-host={$server['v2_host']}";
  74. }
  75. return implode(',', $config).PHP_EOL;
  76. }
  77. public static function buildTrojan(array $server): string
  78. {
  79. $config = array_filter([
  80. "trojan={$server['host']}:{$server['port']}",
  81. "password={$server['passwd']}",
  82. 'over-tls=true',
  83. $server['host'] ? "tls-host={$server['host']}" : '',
  84. $server['allow_insecure'] ? 'tls-verification=false' : 'tls-verification=true',
  85. 'fast-open=true',
  86. "udp-relay={$server['udp']}",
  87. "tag={$server['name']}",
  88. ]);
  89. return implode(',', $config).PHP_EOL;
  90. }
  91. public static function buildHysteria2(array $server): ?string
  92. {
  93. // TODO: QuantumultX does not support Hysteria2 currently.
  94. return null;
  95. }
  96. }