ClientController.php 5.6 KB

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