ClientController.php 6.0 KB

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