ClientController.php 11 KB

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