ClientController.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace App\Http\Controllers\Client;
  3. use App\Http\Controllers\Controller;
  4. use Illuminate\Http\Request;
  5. use App\Models\Server;
  6. use App\Utils\Helper;
  7. use Symfony\Component\Yaml\Yaml;
  8. class ClientController extends Controller
  9. {
  10. public function subscribe(Request $request)
  11. {
  12. $user = $request->user;
  13. $server = [];
  14. if ($user->expired_at > time()) {
  15. $servers = Server::where('show', 1)
  16. ->orderBy('name')
  17. ->get();
  18. foreach ($servers as $item) {
  19. $groupId = json_decode($item['group_id']);
  20. if (in_array($user->group_id, $groupId)) {
  21. array_push($server, $item);
  22. }
  23. }
  24. }
  25. if (isset($_SERVER['HTTP_USER_AGENT'])) {
  26. if (strpos($_SERVER['HTTP_USER_AGENT'], 'Quantumult%20X') !== false) {
  27. die($this->quantumultX($user, $server));
  28. }
  29. if (strpos($_SERVER['HTTP_USER_AGENT'], 'Quantumult') !== false) {
  30. die($this->quantumult($user, $server));
  31. }
  32. if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'clash') !== false) {
  33. die($this->clash($user, $server));
  34. }
  35. }
  36. die($this->origin($user, $server));
  37. }
  38. private function quantumultX($user, $server)
  39. {
  40. $uri = '';
  41. foreach ($server as $item) {
  42. $uri .= "vmess=" . $item->host . ":" . $item->port . ", method=none, password=" . $user->v2ray_uuid . ", fast-open=false, udp-relay=false, tag=" . $item->name;
  43. if ($item->network == 'ws') {
  44. $uri .= ', obfs=ws';
  45. if ($item->settings) {
  46. $wsSettings = json_decode($item->settings);
  47. if (isset($wsSettings->path)) $uri .= ', obfs-uri=' . $wsSettings->path;
  48. if (isset($wsSettings->headers->Host)) $uri .= ', obfs-host=' . $wsSettings->headers->Host;
  49. }
  50. }
  51. $uri .= "\r\n";
  52. }
  53. return base64_encode($uri);
  54. }
  55. private function quantumult($user, $server)
  56. {
  57. $uri = '';
  58. header('subscription-userinfo: upload=' . $user->u . '; download=' . $user->d . ';total=' . $user->transfer_enable);
  59. foreach ($server as $item) {
  60. $str = '';
  61. $str .= $item->name . '= vmess, ' . $item->host . ', ' . $item->port . ', chacha20-ietf-poly1305, "' . $user->v2ray_uuid . '", over-tls=' . ($item->tls ? "true" : "false") . ', certificate=0, group=' . config('v2board.app_name', 'V2Board');
  62. if ($item->network === 'ws') {
  63. $str .= ', obfs=ws';
  64. if ($item->settings) {
  65. $wsSettings = json_decode($item->settings);
  66. if (isset($wsSettings->path)) $str .= ', obfs-path="' . $wsSettings->path . '"';
  67. if (isset($wsSettings->headers->Host)) $str .= ', obfs-header="Host:' . $wsSettings->headers->Host . '"';
  68. }
  69. }
  70. $uri .= "vmess://" . base64_encode($str) . "\r\n";
  71. }
  72. return base64_encode($uri);
  73. }
  74. private function origin($user, $server)
  75. {
  76. $uri = '';
  77. foreach ($server as $item) {
  78. $uri .= Helper::buildVmessLink($item, $user);
  79. }
  80. return base64_encode($uri);
  81. }
  82. private function clash($user, $server)
  83. {
  84. $proxy = [];
  85. $proxyGroup = [];
  86. $proxies = [];
  87. $rules = [];
  88. foreach ($server as $item) {
  89. $array = [];
  90. $array['name'] = $item->name;
  91. $array['type'] = 'vmess';
  92. $array['server'] = $item->host;
  93. $array['port'] = $item->port;
  94. $array['uuid'] = $user->v2ray_uuid;
  95. $array['alterId'] = $user->v2ray_alter_id;
  96. $array['cipher'] = 'auto';
  97. if ($item->tls) {
  98. $array['tls'] = true;
  99. }
  100. if ($item->network == 'ws') {
  101. $array['network'] = $item->network;
  102. if ($item->settings) {
  103. $wsSettings = json_decode($item->settings);
  104. if (isset($wsSettings->path)) $array['ws-path'] = $wsSettings->path;
  105. if (isset($wsSettings->headers->Host)) $array['ws-headers'] = [
  106. 'Host' => $wsSettings->headers->Host
  107. ];
  108. }
  109. }
  110. array_push($proxy, $array);
  111. array_push($proxies, $item->name);
  112. }
  113. array_push($proxyGroup, [
  114. 'name' => 'auto',
  115. 'type' => 'url-test',
  116. 'proxies' => $proxies,
  117. 'url' => 'https://www.bing.com',
  118. 'interval' => 300
  119. ]);
  120. array_push($proxyGroup, [
  121. 'name' => 'fallback-auto',
  122. 'type' => 'fallback',
  123. 'proxies' => $proxies,
  124. 'url' => 'https://www.bing.com',
  125. 'interval' => 300
  126. ]);
  127. array_push($proxyGroup, [
  128. 'name' => 'select',
  129. 'type' => 'select',
  130. 'proxies' => $proxies
  131. ]);
  132. try {
  133. $rules = Yaml::parseFile(base_path() . '/resources/rules/clash.rule.yaml');
  134. } catch (\Exception $e) {}
  135. $config = [
  136. 'port' => 7890,
  137. 'socks-port' => 7891,
  138. 'allow-lan' => false,
  139. 'mode' => 'Rule',
  140. 'log-level' => 'info',
  141. 'external-controller' => '0.0.0.0:9090',
  142. 'secret' => '',
  143. 'Proxy' => $proxy,
  144. 'Proxy Group' => $proxyGroup,
  145. 'Rule' => $rules
  146. ];
  147. return Yaml::dump($config);
  148. }
  149. }