ClientController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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->tls) {
  53. $tlsSettings = json_decode($item->tlsSettings);
  54. if ($item->network === 'tcp') $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. $defaultConfig = base_path() . '/resources/rules/default.surge.conf';
  129. $customConfig = base_path() . '/resources/rules/custom.surge.conf';
  130. if (\File::exists($customConfig)) {
  131. $config = file_get_contents("$customConfig");
  132. } else {
  133. $config = file_get_contents("$defaultConfig");
  134. }
  135. // Subscription link
  136. $subsURL = 'http';
  137. if (isset( $_SERVER['HTTPS'] ) && strtolower( $_SERVER['HTTPS'] ) == 'on') {
  138. $subsURL .= 's';
  139. }
  140. $subsURL .= '://';
  141. if ($_SERVER['SERVER_PORT'] != ('80' || '443')) {
  142. $subsURL .= $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI'];
  143. } else {
  144. $subsURL .= $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
  145. }
  146. $config = str_replace('$subs_link',$subsURL,$config);
  147. $config = str_replace('$proxies',$proxies,$config);
  148. $config = str_replace('$proxy_group',rtrim($proxyGroup, ', '),$config);
  149. return $config;
  150. }
  151. private function surfboard($user, $server)
  152. {
  153. $proxies = '';
  154. $proxyGroup = '';
  155. foreach ($server as $item) {
  156. // [Proxy]
  157. $proxies .= $item->name . ' = vmess, ' . $item->host . ', ' . $item->port . ', username=' . $user->v2ray_uuid;
  158. if ($item->tls) {
  159. $tlsSettings = json_decode($item->tlsSettings);
  160. $proxies .= ', tls=' . ($item->tls ? "true" : "false");
  161. if (isset($tlsSettings->allowInsecure)) {
  162. $proxies .= ', skip-cert-verify=' . ($tlsSettings->allowInsecure ? "true" : "false");
  163. }
  164. }
  165. if ($item->network == 'ws') {
  166. $proxies .= ', ws=true';
  167. if ($item->networkSettings) {
  168. $wsSettings = json_decode($item->networkSettings);
  169. if (isset($wsSettings->path)) $proxies .= ', ws-path=' . $wsSettings->path;
  170. if (isset($wsSettings->headers->Host)) $proxies .= ', ws-headers=host:' . $wsSettings->headers->Host;
  171. }
  172. }
  173. $proxies .= "\r\n";
  174. // [Proxy Group]
  175. $proxyGroup .= $item->name . ', ';
  176. }
  177. $defaultConfig = base_path() . '/resources/rules/default.surfboard.conf';
  178. $customConfig = base_path() . '/resources/rules/custom.surfboard.conf';
  179. if (\File::exists($customConfig)) {
  180. $config = file_get_contents("$customConfig");
  181. } else {
  182. $config = file_get_contents("$defaultConfig");
  183. }
  184. // Subscription link
  185. $subsURL = 'http';
  186. if (isset( $_SERVER['HTTPS'] ) && strtolower( $_SERVER['HTTPS'] ) == 'on') {
  187. $subsURL .= 's';
  188. }
  189. $subsURL .= '://';
  190. if ($_SERVER['SERVER_PORT'] != ('80' || '443')) {
  191. $subsURL .= $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI'];
  192. } else {
  193. $subsURL .= $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
  194. }
  195. $config = str_replace('$subs_link',$subsURL,$config);
  196. $config = str_replace('$proxies',$proxies,$config);
  197. $config = str_replace('$proxy_group',rtrim($proxyGroup, ', '),$config);
  198. return $config;
  199. }
  200. private function clash($user, $server)
  201. {
  202. $defaultConfig = base_path() . '/resources/rules/default.clash.yaml';
  203. $customConfig = base_path() . '/resources/rules/custom.clash.yaml';
  204. if (\File::exists($customConfig)) {
  205. $config = Yaml::parseFile($customConfig);
  206. } else {
  207. $config = Yaml::parseFile($defaultConfig);
  208. }
  209. $proxy = [];
  210. $proxies = [];
  211. foreach ($server as $item) {
  212. $array = [];
  213. $array['name'] = $item->name;
  214. $array['type'] = 'vmess';
  215. $array['server'] = $item->host;
  216. $array['port'] = $item->port;
  217. $array['uuid'] = $user->v2ray_uuid;
  218. $array['alterId'] = $user->v2ray_alter_id;
  219. $array['cipher'] = 'auto';
  220. if ($item->tls) {
  221. $tlsSettings = json_decode($item->tlsSettings);
  222. $array['tls'] = true;
  223. if (isset($tlsSettings->allowInsecure)) $array['skip-cert-verify'] = ($tlsSettings->allowInsecure ? true : false );
  224. }
  225. if ($item->network == 'ws') {
  226. $array['network'] = $item->network;
  227. if ($item->networkSettings) {
  228. $wsSettings = json_decode($item->networkSettings);
  229. if (isset($wsSettings->path)) $array['ws-path'] = $wsSettings->path;
  230. if (isset($wsSettings->headers->Host)) $array['ws-headers'] = [
  231. 'Host' => $wsSettings->headers->Host
  232. ];
  233. }
  234. }
  235. array_push($proxy, $array);
  236. array_push($proxies, $item->name);
  237. }
  238. $config['Proxy'] = array_merge($config['Proxy'] ? $config['Proxy'] : [], $proxy);
  239. foreach ($config['Proxy Group'] as $k => $v) {
  240. $config['Proxy Group'][$k]['proxies'] = array_merge($config['Proxy Group'][$k]['proxies'], $proxies);
  241. }
  242. $yaml = Yaml::dump($config);
  243. $yaml = str_replace('$app_name', config('v2board.app_name', 'V2Board'), $yaml);
  244. return $yaml;
  245. }
  246. }