QuantumultX.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace App\Http\Controllers\Client\Protocols;
  3. class QuantumultX
  4. {
  5. public $flag = 'quantumult%20x';
  6. private $servers;
  7. private $user;
  8. public function __construct($user, $servers)
  9. {
  10. $this->user = $user;
  11. $this->servers = $servers;
  12. }
  13. public function handle()
  14. {
  15. $servers = $this->servers;
  16. $user = $this->user;
  17. $uri = '';
  18. header("subscription-userinfo: upload={$user['u']}; download={$user['d']}; total={$user['transfer_enable']}; expire={$user['expired_at']}");
  19. foreach ($servers as $item) {
  20. if ($item['type'] === 'shadowsocks') {
  21. $uri .= self::buildShadowsocks($user['uuid'], $item);
  22. }
  23. if ($item['type'] === 'v2ray') {
  24. $uri .= self::buildVmess($user['uuid'], $item);
  25. }
  26. if ($item['type'] === 'trojan') {
  27. $uri .= self::buildTrojan($user['uuid'], $item);
  28. }
  29. }
  30. return base64_encode($uri);
  31. }
  32. public static function buildShadowsocks($password, $server)
  33. {
  34. $config = [
  35. "shadowsocks={$server['host']}:{$server['port']}",
  36. "method={$server['cipher']}",
  37. "password={$password}",
  38. 'fast-open=true',
  39. 'udp-relay=true',
  40. "tag={$server['name']}"
  41. ];
  42. $config = array_filter($config);
  43. $uri = implode(',', $config);
  44. $uri .= "\r\n";
  45. return $uri;
  46. }
  47. public static function buildVmess($uuid, $server)
  48. {
  49. $config = [
  50. "vmess={$server['host']}:{$server['port']}",
  51. 'method=chacha20-poly1305',
  52. "password={$uuid}",
  53. 'fast-open=true',
  54. 'udp-relay=true',
  55. "tag={$server['name']}"
  56. ];
  57. if ($server['tls']) {
  58. if ($server['network'] === 'tcp')
  59. array_push($config, 'obfs=over-tls');
  60. if ($server['tlsSettings']) {
  61. $tlsSettings = json_decode($server['tlsSettings'], true);
  62. if (isset($tlsSettings['allowInsecure']) && !empty($tlsSettings['allowInsecure']))
  63. array_push($config, 'tls-verification=' . ($tlsSettings['allowInsecure'] ? 'false' : 'true'));
  64. if (isset($tlsSettings['serverName']) && !empty($tlsSettings['serverName']))
  65. $host = $tlsSettings['serverName'];
  66. }
  67. }
  68. if ($server['network'] === 'ws') {
  69. if ($server['tls'])
  70. array_push($config, 'obfs=wss');
  71. else
  72. array_push($config, 'obfs=ws');
  73. if ($server['networkSettings']) {
  74. $wsSettings = json_decode($server['networkSettings'], true);
  75. if (isset($wsSettings['path']) && !empty($wsSettings['path']))
  76. array_push($config, "obfs-uri={$wsSettings['path']}");
  77. if (isset($wsSettings['headers']['Host']) && !empty($wsSettings['headers']['Host']) && !isset($host))
  78. $host = $wsSettings['headers']['Host'];
  79. }
  80. }
  81. if (isset($host)) {
  82. array_push($config, "obfs-host={$host}");
  83. }
  84. $uri = implode(',', $config);
  85. $uri .= "\r\n";
  86. return $uri;
  87. }
  88. public static function buildTrojan($password, $server)
  89. {
  90. $config = [
  91. "trojan={$server['host']}:{$server['port']}",
  92. "password={$password}",
  93. 'over-tls=true',
  94. $server['server_name'] ? "tls-host={$server['server_name']}" : "",
  95. // Tips: allowInsecure=false = tls-verification=true
  96. $server['allow_insecure'] ? 'tls-verification=false' : 'tls-verification=true',
  97. 'fast-open=true',
  98. 'udp-relay=true',
  99. "tag={$server['name']}"
  100. ];
  101. $config = array_filter($config);
  102. $uri = implode(',', $config);
  103. $uri .= "\r\n";
  104. return $uri;
  105. }
  106. }