ClientController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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('sort', 'ASC')
  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->surge2($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->tls) {
  53. $tlsSettings = json_decode($item->tlsSettings);
  54. $uri .= ', obfs=over-tls';
  55. if (isset($tlsSettings->allowInsecure)) {
  56. // Default: tls-verification=true
  57. $uri .= ', tls-verification=' . ($tlsSettings->allowInsecure ? "false" : "true");
  58. }
  59. if (isset($tlsSettings->serverName)) {
  60. $uri .= ', obfs-host=' . $tlsSettings->serverName;
  61. }
  62. }
  63. if ($item->network == 'ws') {
  64. $uri .= ', obfs=' . ($item->tls ? 'wss' : 'ws');
  65. if ($item->networkSettings) {
  66. $wsSettings = json_decode($item->networkSettings);
  67. if (isset($wsSettings->path)) $uri .= ', obfs-uri=' . $wsSettings->path;
  68. if (isset($wsSettings->headers->Host)) $uri .= ', obfs-host=' . $wsSettings->headers->Host;
  69. }
  70. }
  71. $uri .= "\r\n";
  72. }
  73. return base64_encode($uri);
  74. }
  75. private function quantumult($user, $server)
  76. {
  77. $uri = '';
  78. header('subscription-userinfo: upload=' . $user->u . '; download=' . $user->d . ';total=' . $user->transfer_enable);
  79. foreach ($server as $item) {
  80. $str = '';
  81. $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');
  82. if ($item->network === 'ws') {
  83. $str .= ', obfs=ws';
  84. if ($item->networkSettings) {
  85. $wsSettings = json_decode($item->networkSettings);
  86. if (isset($wsSettings->path)) $str .= ', obfs-path="' . $wsSettings->path . '"';
  87. if (isset($wsSettings->headers->Host)) $str .= ', obfs-header="Host:' . $wsSettings->headers->Host . '"';
  88. }
  89. }
  90. $uri .= "vmess://" . base64_encode($str) . "\r\n";
  91. }
  92. return base64_encode($uri);
  93. }
  94. private function origin($user, $server)
  95. {
  96. $uri = '';
  97. foreach ($server as $item) {
  98. $uri .= Helper::buildVmessLink($item, $user);
  99. }
  100. return base64_encode($uri);
  101. }
  102. private function surge($user, $server)
  103. {
  104. $proxies = '';
  105. $proxyGroup = '';
  106. foreach ($server as $item) {
  107. // [Proxy]
  108. $proxies .= $item->name . ' = vmess, ' . $item->host . ', ' . $item->port . ', username=' . $user->v2ray_uuid . ', tfo=true';
  109. if ($item->tls) {
  110. $tlsSettings = json_decode($item->tlsSettings);
  111. $proxies .= ', tls=' . ($item->tls ? "true" : "false");
  112. if (isset($tlsSettings->allowInsecure)) {
  113. $proxies .= ', skip-cert-verify=' . ($tlsSettings->allowInsecure ? "true" : "false");
  114. }
  115. }
  116. if ($item->network == 'ws') {
  117. $proxies .= ', ws=true';
  118. if ($item->networkSettings) {
  119. $wsSettings = json_decode($item->networkSettings);
  120. if (isset($wsSettings->path)) $proxies .= ', ws-path=' . $wsSettings->path;
  121. if (isset($wsSettings->headers->Host)) $proxies .= ', ws-headers=host:' . $wsSettings->headers->Host;
  122. }
  123. }
  124. $proxies .= "\r\n";
  125. // [Proxy Group]
  126. $proxyGroup .= $item->name . ', ';
  127. }
  128. try {
  129. $rules = '';
  130. foreach (glob(base_path() . '/resources/rules/' . '*.surge.conf') as $file) {
  131. $rules = file_get_contents("$file");
  132. }
  133. } catch (\Exception $e) {}
  134. // Subscription link
  135. $subsURL = 'http';
  136. if (isset( $_SERVER['HTTPS'] ) && strtolower( $_SERVER['HTTPS'] ) == 'on') {
  137. $subsURL .= 's';
  138. }
  139. $subsURL .= '://';
  140. if ($_SERVER['SERVER_PORT'] != ('80' || '443')) {
  141. $subsURL .= $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI'];
  142. } else {
  143. $subsURL .= $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
  144. }
  145. $rules = str_replace('{subs_link}',$subsURL,$rules);
  146. $rules = str_replace('{proxies}',$proxies,$rules);
  147. $rules = str_replace('{proxy_group}',rtrim($proxyGroup, ', '),$rules);
  148. return $rules;
  149. }
  150. private function surge2($user, $server)
  151. {
  152. $proxies = '';
  153. $proxyGroup = '';
  154. foreach ($server as $item) {
  155. // [Proxy]
  156. $proxies .= $item->name . ' = vmess, ' . $item->host . ', ' . $item->port . ', username=' . $user->v2ray_uuid;
  157. if ($item->tls) {
  158. $tlsSettings = json_decode($item->tlsSettings);
  159. $proxies .= ', tls=' . ($item->tls ? "true" : "false");
  160. if (isset($tlsSettings->allowInsecure)) {
  161. $proxies .= ', skip-cert-verify=' . ($tlsSettings->allowInsecure ? "true" : "false");
  162. }
  163. }
  164. if ($item->network == 'ws') {
  165. $proxies .= ', ws=true';
  166. if ($item->networkSettings) {
  167. $wsSettings = json_decode($item->networkSettings);
  168. if (isset($wsSettings->path)) $proxies .= ', ws-path=' . $wsSettings->path;
  169. if (isset($wsSettings->headers->Host)) $proxies .= ', ws-headers=host:' . $wsSettings->headers->Host;
  170. }
  171. }
  172. $proxies .= "\r\n";
  173. // [Proxy Group]
  174. $proxyGroup .= $item->name . ', ';
  175. }
  176. try {
  177. $rules = '';
  178. foreach (glob(base_path() . '/resources/rules/' . '*.surge2.conf') as $file) {
  179. $rules = file_get_contents("$file");
  180. }
  181. } catch (\Exception $e) {}
  182. // Subscription link
  183. $subsURL = 'http';
  184. if (isset( $_SERVER['HTTPS'] ) && strtolower( $_SERVER['HTTPS'] ) == 'on') {
  185. $subsURL .= 's';
  186. }
  187. $subsURL .= '://';
  188. if ($_SERVER['SERVER_PORT'] != ('80' || '443')) {
  189. $subsURL .= $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI'];
  190. } else {
  191. $subsURL .= $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
  192. }
  193. $rules = str_replace('{subs_link}',$subsURL,$rules);
  194. $rules = str_replace('{proxies}',$proxies,$rules);
  195. $rules = str_replace('{proxy_group}',rtrim($proxyGroup, ', '),$rules);
  196. return $rules;
  197. }
  198. private function clash($user, $server)
  199. {
  200. $defaultConfig = base_path() . '/resources/rules/default.clash.yaml';
  201. $customConfig = base_path() . '/resources/rules/custom.clash.yaml';
  202. if (\File::exists($customConfig)) {
  203. $config = Yaml::parseFile($customConfig);
  204. } else {
  205. $config = Yaml::parseFile($defaultConfig);
  206. }
  207. $proxy = [];
  208. $proxies = [];
  209. foreach ($server as $item) {
  210. $array = [];
  211. $array['name'] = $item->name;
  212. $array['type'] = 'vmess';
  213. $array['server'] = $item->host;
  214. $array['port'] = $item->port;
  215. $array['uuid'] = $user->v2ray_uuid;
  216. $array['alterId'] = $user->v2ray_alter_id;
  217. $array['cipher'] = 'auto';
  218. if ($item->tls) {
  219. $tlsSettings = json_decode($item->tlsSettings);
  220. $array['tls'] = true;
  221. if (isset($tlsSettings->allowInsecure)) $array['skip-cert-verify'] = ($tlsSettings->allowInsecure ? true : false );
  222. }
  223. if ($item->network == 'ws') {
  224. $array['network'] = $item->network;
  225. if ($item->networkSettings) {
  226. $wsSettings = json_decode($item->networkSettings);
  227. if (isset($wsSettings->path)) $array['ws-path'] = $wsSettings->path;
  228. if (isset($wsSettings->headers->Host)) $array['ws-headers'] = [
  229. 'Host' => $wsSettings->headers->Host
  230. ];
  231. }
  232. }
  233. array_push($proxy, $array);
  234. array_push($proxies, $item->name);
  235. }
  236. $config['Proxy'] = array_merge($config['Proxy'] ? $config['Proxy'] : [], $proxy);
  237. foreach ($config['Proxy Group'] as $k => $v) {
  238. $config['Proxy Group'][$k]['proxies'] = array_merge($config['Proxy Group'][$k]['proxies'], $proxies);
  239. }
  240. $yaml = Yaml::dump($config);
  241. $yaml = str_replace('$app_name', config('v2board.app_name', 'V2Board'), $yaml);
  242. return $yaml;
  243. }
  244. }