ClientController.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. if (strpos($_SERVER['HTTP_USER_AGENT'], 'Surfboard') !== false) {
  39. die($this->surge($user, $server));
  40. }
  41. if (strpos($_SERVER['HTTP_USER_AGENT'], 'Surge') !== false) {
  42. die($this->surge($user, $server));
  43. }
  44. }
  45. die($this->origin($user, $server));
  46. }
  47. private function quantumultX($user, $server)
  48. {
  49. $uri = '';
  50. foreach ($server as $item) {
  51. $uri .= "vmess=" . $item->host . ":" . $item->port . ", method=none, password=" . $user->v2ray_uuid . ", fast-open=false, udp-relay=false, tag=" . $item->name;
  52. if ($item->network == 'ws') {
  53. $uri .= ', obfs=' . ($item->tls ? 'wss' : 'ws');
  54. if ($item->networkSettings) {
  55. $wsSettings = json_decode($item->networkSettings);
  56. if (isset($wsSettings->path)) $uri .= ', obfs-uri=' . $wsSettings->path;
  57. if (isset($wsSettings->headers->Host)) $uri .= ', obfs-host=' . $wsSettings->headers->Host;
  58. }
  59. }
  60. $uri .= "\r\n";
  61. }
  62. return base64_encode($uri);
  63. }
  64. private function quantumult($user, $server)
  65. {
  66. $uri = '';
  67. header('subscription-userinfo: upload=' . $user->u . '; download=' . $user->d . ';total=' . $user->transfer_enable);
  68. foreach ($server as $item) {
  69. $str = '';
  70. $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');
  71. if ($item->network === 'ws') {
  72. $str .= ', obfs=ws';
  73. if ($item->networkSettings) {
  74. $wsSettings = json_decode($item->networkSettings);
  75. if (isset($wsSettings->path)) $str .= ', obfs-path="' . $wsSettings->path . '"';
  76. if (isset($wsSettings->headers->Host)) $str .= ', obfs-header="Host:' . $wsSettings->headers->Host . '"';
  77. }
  78. }
  79. $uri .= "vmess://" . base64_encode($str) . "\r\n";
  80. }
  81. return base64_encode($uri);
  82. }
  83. private function origin($user, $server)
  84. {
  85. $uri = '';
  86. foreach ($server as $item) {
  87. $uri .= Helper::buildVmessLink($item, $user);
  88. }
  89. return base64_encode($uri);
  90. }
  91. private function surge($user, $server)
  92. {
  93. $proxies = '';
  94. $proxyGroup = '';
  95. foreach ($server as $item) {
  96. // [Proxy]
  97. $proxies .= $item->name . ' = vmess, ' . $item->host . ', ' . $item->port . ', username=' . $user->v2ray_uuid . ', tls=' . ($item->tls ? "true" : "false");
  98. if ($item->network == 'ws') {
  99. $proxies .= ', ws=true';
  100. if ($item->networkSettings) {
  101. $wsSettings = json_decode($item->networkSettings);
  102. if (isset($wsSettings->path)) $proxies .= ', ws-path=' . $wsSettings->path;
  103. if (isset($wsSettings->headers->Host)) $proxies .= ', ws-headers=' . $wsSettings->headers->Host;
  104. }
  105. }
  106. $proxies .= "\r\n";
  107. // [Proxy Group]
  108. $proxyGroup .= $item->name . ', ';
  109. }
  110. try {
  111. $rules = '';
  112. foreach (glob(base_path() . '/resources/rules/' . '*.surge.conf') as $file) {
  113. $rules = file_get_contents("$file");
  114. }
  115. } catch (\Exception $e) {}
  116. // Subscription link
  117. $subsURL = 'http';
  118. if ($_SERVER['HTTPS'] == 'on') {
  119. $subsURL .= 's';
  120. }
  121. $subsURL .= '://';
  122. if ($_SERVER['SERVER_PORT'] != ('80' || '443')) {
  123. $subsURL .= $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI'];
  124. } else {
  125. $subsURL .= $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
  126. }
  127. $rules = str_replace('{subs_link}',$subsURL,$rules);
  128. $rules = str_replace('{proxies}',$proxies,$rules);
  129. $rules = str_replace('{proxy_group}',rtrim($proxyGroup, ', '),$rules);
  130. return $rules;
  131. }
  132. private function clash($user, $server)
  133. {
  134. $proxy = [];
  135. $proxyGroup = [];
  136. $proxies = [];
  137. $rules = [];
  138. foreach ($server as $item) {
  139. $array = [];
  140. $array['name'] = $item->name;
  141. $array['type'] = 'vmess';
  142. $array['server'] = $item->host;
  143. $array['port'] = $item->port;
  144. $array['uuid'] = $user->v2ray_uuid;
  145. $array['alterId'] = $user->v2ray_alter_id;
  146. $array['cipher'] = 'auto';
  147. if ($item->tls) {
  148. $array['tls'] = true;
  149. $array['skip-cert-verify'] = true;
  150. }
  151. if ($item->network == 'ws') {
  152. $array['network'] = $item->network;
  153. if ($item->networkSettings) {
  154. $wsSettings = json_decode($item->networkSettings);
  155. if (isset($wsSettings->path)) $array['ws-path'] = $wsSettings->path;
  156. if (isset($wsSettings->headers->Host)) $array['ws-headers'] = [
  157. 'Host' => $wsSettings->headers->Host
  158. ];
  159. }
  160. }
  161. array_push($proxy, $array);
  162. array_push($proxies, $item->name);
  163. }
  164. array_push($proxyGroup, [
  165. 'name' => 'auto',
  166. 'type' => 'url-test',
  167. 'proxies' => $proxies,
  168. 'url' => 'https://www.bing.com',
  169. 'interval' => 300
  170. ]);
  171. array_push($proxyGroup, [
  172. 'name' => 'fallback-auto',
  173. 'type' => 'fallback',
  174. 'proxies' => $proxies,
  175. 'url' => 'https://www.bing.com',
  176. 'interval' => 300
  177. ]);
  178. array_push($proxyGroup, [
  179. 'name' => 'select',
  180. 'type' => 'select',
  181. 'proxies' => array_merge($proxies, [
  182. 'auto',
  183. 'fallback-auto'
  184. ])
  185. ]);
  186. try {
  187. $rules = [];
  188. foreach (glob(base_path() . '/resources/rules/' . '*.clash.yaml') as $file) {
  189. $rules = array_merge($rules, Yaml::parseFile($file)['Rule']);
  190. }
  191. } catch (\Exception $e) {}
  192. $config = [
  193. 'port' => 7890,
  194. 'socks-port' => 7891,
  195. 'allow-lan' => false,
  196. 'mode' => 'Rule',
  197. 'log-level' => 'info',
  198. 'external-controller' => '0.0.0.0:9090',
  199. 'secret' => '',
  200. 'Proxy' => $proxy,
  201. 'Proxy Group' => $proxyGroup,
  202. 'Rule' => $rules
  203. ];
  204. return Yaml::dump($config);
  205. }
  206. }