ClientController.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. namespace App\Http\Controllers\Client;
  3. use App\Http\Controllers\Controller;
  4. use App\Services\ServerService;
  5. use App\Utils\Clash;
  6. use Illuminate\Http\Request;
  7. use App\Models\Server;
  8. use App\Utils\Helper;
  9. use Symfony\Component\Yaml\Yaml;
  10. use App\Services\UserService;
  11. class ClientController extends Controller
  12. {
  13. public function subscribe(Request $request)
  14. {
  15. $user = $request->user;
  16. // account not expired and is not banned.
  17. $userService = new UserService();
  18. if ($userService->isAvailable($user)) {
  19. $serverService = new ServerService();
  20. $servers = $serverService->getAllServers($user);
  21. }
  22. if (isset($_SERVER['HTTP_USER_AGENT'])) {
  23. if (strpos($_SERVER['HTTP_USER_AGENT'], 'Quantumult%20X') !== false) {
  24. die($this->quantumultX($user, $servers['vmess']));
  25. }
  26. if (strpos($_SERVER['HTTP_USER_AGENT'], 'Quantumult') !== false) {
  27. die($this->quantumult($user, $servers['vmess']));
  28. }
  29. if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'clash') !== false) {
  30. die($this->clash($user, $servers['vmess'], $servers['trojan']));
  31. }
  32. if (strpos($_SERVER['HTTP_USER_AGENT'], 'Surfboard') !== false) {
  33. die($this->surfboard($user, $servers['vmess']));
  34. }
  35. if (strpos($_SERVER['HTTP_USER_AGENT'], 'Surge') !== false) {
  36. die($this->surge($user, $servers['vmess']));
  37. }
  38. }
  39. die($this->origin($user, $servers['vmess']));
  40. }
  41. private function quantumultX($user, $vmess)
  42. {
  43. $uri = '';
  44. foreach ($vmess as $item) {
  45. $uri .= "vmess=" . $item->host . ":" . $item->port . ", method=none, password=" . $user->uuid . ", fast-open=false, udp-relay=false, tag=" . $item->name;
  46. if ($item->tls) {
  47. $tlsSettings = json_decode($item->tlsSettings);
  48. if ($item->network === 'tcp') $uri .= ', obfs=over-tls';
  49. if (isset($tlsSettings->allowInsecure)) {
  50. // Default: tls-verification=true
  51. $uri .= ', tls-verification=' . ($tlsSettings->allowInsecure ? "false" : "true");
  52. }
  53. if (isset($tlsSettings->serverName)) {
  54. $uri .= ', obfs-host=' . $tlsSettings->serverName;
  55. }
  56. }
  57. if ($item->network === 'ws') {
  58. $uri .= ', obfs=' . ($item->tls ? 'wss' : 'ws');
  59. if ($item->networkSettings) {
  60. $wsSettings = json_decode($item->networkSettings);
  61. if (isset($wsSettings->path)) $uri .= ', obfs-uri=' . $wsSettings->path;
  62. if (isset($wsSettings->headers->Host)) $uri .= ', obfs-host=' . $wsSettings->headers->Host;
  63. }
  64. }
  65. $uri .= "\r\n";
  66. }
  67. return base64_encode($uri);
  68. }
  69. private function quantumult($user, $vmess)
  70. {
  71. $uri = '';
  72. header('subscription-userinfo: upload=' . $user->u . '; download=' . $user->d . ';total=' . $user->transfer_enable);
  73. foreach ($vmess as $item) {
  74. $str = '';
  75. $str .= $item->name . '= vmess, ' . $item->host . ', ' . $item->port . ', chacha20-ietf-poly1305, "' . $user->uuid . '", over-tls=' . ($item->tls ? "true" : "false") . ', certificate=0, group=' . config('v2board.app_name', 'V2Board');
  76. if ($item->network === 'ws') {
  77. $str .= ', obfs=ws';
  78. if ($item->networkSettings) {
  79. $wsSettings = json_decode($item->networkSettings);
  80. if (isset($wsSettings->path)) $str .= ', obfs-path="' . $wsSettings->path . '"';
  81. if (isset($wsSettings->headers->Host)) $str .= ', obfs-header="Host:' . $wsSettings->headers->Host . '"';
  82. }
  83. }
  84. $uri .= "vmess://" . base64_encode($str) . "\r\n";
  85. }
  86. return base64_encode($uri);
  87. }
  88. private function origin($user, $vmess)
  89. {
  90. $uri = '';
  91. foreach ($vmess as $item) {
  92. $uri .= Helper::buildVmessLink($item, $user);
  93. }
  94. return base64_encode($uri);
  95. }
  96. private function surge($user, $vmess)
  97. {
  98. $proxies = '';
  99. $proxyGroup = '';
  100. foreach ($vmess as $item) {
  101. // [Proxy]
  102. $proxies .= $item->name . ' = vmess, ' . $item->host . ', ' . $item->port . ', username=' . $user->uuid . ', tfo=true';
  103. if ($item->tls) {
  104. $tlsSettings = json_decode($item->tlsSettings);
  105. $proxies .= ', tls=' . ($item->tls ? "true" : "false");
  106. if (isset($tlsSettings->allowInsecure)) {
  107. $proxies .= ', skip-cert-verify=' . ($tlsSettings->allowInsecure ? "true" : "false");
  108. }
  109. }
  110. if ($item->network == 'ws') {
  111. $proxies .= ', ws=true';
  112. if ($item->networkSettings) {
  113. $wsSettings = json_decode($item->networkSettings);
  114. if (isset($wsSettings->path)) $proxies .= ', ws-path=' . $wsSettings->path;
  115. if (isset($wsSettings->headers->Host)) $proxies .= ', ws-headers=host:' . $wsSettings->headers->Host;
  116. }
  117. }
  118. $proxies .= "\r\n";
  119. // [Proxy Group]
  120. $proxyGroup .= $item->name . ', ';
  121. }
  122. $defaultConfig = base_path() . '/resources/rules/default.surge.conf';
  123. $customConfig = base_path() . '/resources/rules/custom.surge.conf';
  124. if (\File::exists($customConfig)) {
  125. $config = file_get_contents("$customConfig");
  126. } else {
  127. $config = file_get_contents("$defaultConfig");
  128. }
  129. // Subscription link
  130. $subsURL = 'http';
  131. if (isset( $_SERVER['HTTPS'] ) && strtolower( $_SERVER['HTTPS'] ) == 'on') {
  132. $subsURL .= 's';
  133. }
  134. $subsURL .= '://';
  135. if ($_SERVER['SERVER_PORT'] != ('80' || '443')) {
  136. $subsURL .= $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI'];
  137. } else {
  138. $subsURL .= $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
  139. }
  140. $config = str_replace('$subs_link',$subsURL,$config);
  141. $config = str_replace('$proxies',$proxies,$config);
  142. $config = str_replace('$proxy_group',rtrim($proxyGroup, ', '),$config);
  143. return $config;
  144. }
  145. private function surfboard($user, $vmess)
  146. {
  147. $proxies = '';
  148. $proxyGroup = '';
  149. foreach ($vmess as $item) {
  150. // [Proxy]
  151. $proxies .= $item->name . ' = vmess, ' . $item->host . ', ' . $item->port . ', username=' . $user->uuid;
  152. if ($item->tls) {
  153. $tlsSettings = json_decode($item->tlsSettings);
  154. $proxies .= ', tls=' . ($item->tls ? "true" : "false");
  155. if (isset($tlsSettings->allowInsecure)) {
  156. $proxies .= ', skip-cert-verify=' . ($tlsSettings->allowInsecure ? "true" : "false");
  157. }
  158. }
  159. if ($item->network == 'ws') {
  160. $proxies .= ', ws=true';
  161. if ($item->networkSettings) {
  162. $wsSettings = json_decode($item->networkSettings);
  163. if (isset($wsSettings->path)) $proxies .= ', ws-path=' . $wsSettings->path;
  164. if (isset($wsSettings->headers->Host)) $proxies .= ', ws-headers=host:' . $wsSettings->headers->Host;
  165. }
  166. }
  167. $proxies .= "\r\n";
  168. // [Proxy Group]
  169. $proxyGroup .= $item->name . ', ';
  170. }
  171. $defaultConfig = base_path() . '/resources/rules/default.surfboard.conf';
  172. $customConfig = base_path() . '/resources/rules/custom.surfboard.conf';
  173. if (\File::exists($customConfig)) {
  174. $config = file_get_contents("$customConfig");
  175. } else {
  176. $config = file_get_contents("$defaultConfig");
  177. }
  178. // Subscription link
  179. $subsURL = 'http';
  180. if (isset( $_SERVER['HTTPS'] ) && strtolower( $_SERVER['HTTPS'] ) == 'on') {
  181. $subsURL .= 's';
  182. }
  183. $subsURL .= '://';
  184. if ($_SERVER['SERVER_PORT'] != ('80' || '443')) {
  185. $subsURL .= $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI'];
  186. } else {
  187. $subsURL .= $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
  188. }
  189. $config = str_replace('$subs_link', $subsURL, $config);
  190. $config = str_replace('$proxies', $proxies, $config);
  191. $config = str_replace('$proxy_group', rtrim($proxyGroup, ', '), $config);
  192. return $config;
  193. }
  194. private function clash($user, $vmess = [], $trojan = [])
  195. {
  196. $defaultConfig = base_path() . '/resources/rules/default.clash.yaml';
  197. $customConfig = base_path() . '/resources/rules/custom.clash.yaml';
  198. if (\File::exists($customConfig)) {
  199. $config = Yaml::parseFile($customConfig);
  200. } else {
  201. $config = Yaml::parseFile($defaultConfig);
  202. }
  203. $proxy = [];
  204. $proxies = [];
  205. foreach ($vmess as $item) {
  206. array_push($proxy, Clash::buildVmess($user->uuid, $item));
  207. array_push($proxies, $item->name);
  208. }
  209. foreach ($trojan as $item) {
  210. array_push($proxy, Clash::buildTrojan($user->uuid, $item));
  211. array_push($proxies, $item->name);
  212. }
  213. $config['Proxy'] = array_merge($config['Proxy'] ? $config['Proxy'] : [], $proxy);
  214. foreach ($config['Proxy Group'] as $k => $v) {
  215. $config['Proxy Group'][$k]['proxies'] = array_merge($config['Proxy Group'][$k]['proxies'], $proxies);
  216. }
  217. $yaml = Yaml::dump($config);
  218. $yaml = str_replace('$app_name', config('v2board.app_name', 'V2Board'), $yaml);
  219. return $yaml;
  220. }
  221. }