QuantumultX.php 3.3 KB

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