ClientController.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 App\Utils\QuantumultX;
  7. use App\Utils\Shadowrocket;
  8. use App\Utils\Surge;
  9. use App\Utils\URLSchemes;
  10. use Illuminate\Http\Request;
  11. use App\Models\Server;
  12. use App\Utils\Helper;
  13. use Symfony\Component\Yaml\Yaml;
  14. use App\Services\UserService;
  15. class ClientController extends Controller
  16. {
  17. public function subscribe(Request $request)
  18. {
  19. $user = $request->user;
  20. // account not expired and is not banned.
  21. $userService = new UserService();
  22. if ($userService->isAvailable($user)) {
  23. $serverService = new ServerService();
  24. $servers = $serverService->getAllServers($user);
  25. if (isset($_SERVER['HTTP_USER_AGENT'])) {
  26. $_SERVER['HTTP_USER_AGENT'] = strtolower($_SERVER['HTTP_USER_AGENT']);
  27. if (strpos($_SERVER['HTTP_USER_AGENT'], 'quantumult%20x') !== false) {
  28. die($this->quantumultX($user, $servers['vmess'], $servers['trojan']));
  29. }
  30. if (strpos($_SERVER['HTTP_USER_AGENT'], 'quantumult') !== false) {
  31. die($this->quantumult($user, $servers['vmess']));
  32. }
  33. if (strpos($_SERVER['HTTP_USER_AGENT'], 'clash') !== false) {
  34. die($this->clash($user, $servers['shadowsocks'], $servers['vmess'], $servers['trojan']));
  35. }
  36. if (strpos($_SERVER['HTTP_USER_AGENT'], 'surfboard') !== false) {
  37. die($this->surfboard($user, $servers['vmess']));
  38. }
  39. if (strpos($_SERVER['HTTP_USER_AGENT'], 'surge') !== false) {
  40. die($this->surge($user, $servers['vmess'], $servers['trojan']));
  41. }
  42. if (strpos($_SERVER['HTTP_USER_AGENT'], 'shadowrocket') !== false) {
  43. die($this->shadowrocket($user, $servers['vmess'], $servers['trojan']));
  44. }
  45. }
  46. die($this->origin($user, $servers['shadowsocks'], $servers['vmess'], $servers['trojan']));
  47. }
  48. }
  49. // TODO: Ready to stop support
  50. private function quantumult($user, $vmess = [])
  51. {
  52. $uri = '';
  53. header('subscription-userinfo: upload=' . $user->u . '; download=' . $user->d . ';total=' . $user->transfer_enable);
  54. foreach ($vmess as $item) {
  55. $str = '';
  56. $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');
  57. if ($item->network === 'ws') {
  58. $str .= ', obfs=ws';
  59. if ($item->networkSettings) {
  60. $wsSettings = json_decode($item->networkSettings);
  61. if (isset($wsSettings->path)) $str .= ', obfs-path="' . $wsSettings->path . '"';
  62. if (isset($wsSettings->headers->Host)) $str .= ', obfs-header="Host:' . $wsSettings->headers->Host . '"';
  63. }
  64. }
  65. $uri .= "vmess://" . base64_encode($str) . "\r\n";
  66. }
  67. return base64_encode($uri);
  68. }
  69. private function shadowrocket($user, $vmess = [], $trojan = [])
  70. {
  71. $uri = '';
  72. //display remaining traffic and expire date
  73. $upload = round($user->u / (1024*1024*1024), 2);
  74. $download = round($user->d / (1024*1024*1024), 2);
  75. $totalTraffic = round($user->transfer_enable / (1024*1024*1024), 2);
  76. $expiredDate = date('Y-m-d', $user->expired_at);
  77. $uri .= "STATUS=🚀↑:{$upload}GB,↓:{$download}GB,TOT:{$totalTraffic}GB💡Expires:{$expiredDate}\r\n";
  78. foreach ($vmess as $item) {
  79. $uri .= Shadowrocket::buildVmess($user->uuid, $item);
  80. }
  81. foreach ($trojan as $item) {
  82. $uri .= Shadowrocket::buildTrojan($user->uuid, $item);
  83. }
  84. return base64_encode($uri);
  85. }
  86. private function quantumultX($user, $vmess = [], $trojan = [])
  87. {
  88. $uri = '';
  89. header("subscription-userinfo: upload={$user->u}; download={$user->d}; total={$user->transfer_enable}; expire={$user->expired_at}");
  90. foreach ($vmess as $item) {
  91. $uri .= QuantumultX::buildVmess($user->uuid, $item);
  92. }
  93. foreach ($trojan as $item) {
  94. $uri .= QuantumultX::buildTrojan($user->uuid, $item);
  95. }
  96. return base64_encode($uri);
  97. }
  98. private function origin($user, $shadowsocks = [], $vmess = [], $trojan = [])
  99. {
  100. $uri = '';
  101. foreach ($shadowsocks as $item) {
  102. $uri .= URLSchemes::buildShadowsocks($item, $user);
  103. }
  104. foreach ($vmess as $item) {
  105. $uri .= URLSchemes::buildVmess($item, $user);
  106. }
  107. foreach ($trojan as $item) {
  108. $uri .= URLSchemes::buildTrojan($item, $user);
  109. }
  110. return base64_encode($uri);
  111. }
  112. private function surge($user, $vmess = [], $trojan = [])
  113. {
  114. $proxies = '';
  115. $proxyGroup = '';
  116. foreach ($vmess as $item) {
  117. // [Proxy]
  118. $proxies .= Surge::buildVmess($user->uuid, $item);
  119. // [Proxy Group]
  120. $proxyGroup .= $item->name . ', ';
  121. }
  122. foreach ($trojan as $item) {
  123. // [Proxy]
  124. $proxies .= Surge::buildTrojan($user->uuid, $item);
  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 = config('v2board.subscribe_url', config('v2board.app_url', env('APP_URL'))) . '/api/v1/client/subscribe?token=' . $user['token'];
  137. $config = str_replace('$subs_link', $subsURL, $config);
  138. $config = str_replace('$proxies', $proxies, $config);
  139. $config = str_replace('$proxy_group', rtrim($proxyGroup, ', '), $config);
  140. return $config;
  141. }
  142. private function surfboard($user, $vmess = [])
  143. {
  144. $proxies = '';
  145. $proxyGroup = '';
  146. foreach ($vmess as $item) {
  147. // [Proxy]
  148. $proxies .= $item->name . ' = vmess, ' . $item->host . ', ' . $item->port . ', username=' . $user->uuid;
  149. if ($item->tls) {
  150. $tlsSettings = json_decode($item->tlsSettings);
  151. $proxies .= ', tls=' . ($item->tls ? "true" : "false");
  152. if (isset($tlsSettings->allowInsecure)) {
  153. $proxies .= ', skip-cert-verify=' . ($tlsSettings->allowInsecure ? "true" : "false");
  154. }
  155. }
  156. if ($item->network == 'ws') {
  157. $proxies .= ', ws=true';
  158. if ($item->networkSettings) {
  159. $wsSettings = json_decode($item->networkSettings);
  160. if (isset($wsSettings->path)) $proxies .= ', ws-path=' . $wsSettings->path;
  161. if (isset($wsSettings->headers->Host)) $proxies .= ', ws-headers=host:' . $wsSettings->headers->Host;
  162. }
  163. }
  164. $proxies .= "\r\n";
  165. // [Proxy Group]
  166. $proxyGroup .= $item->name . ', ';
  167. }
  168. $defaultConfig = base_path() . '/resources/rules/default.surfboard.conf';
  169. $customConfig = base_path() . '/resources/rules/custom.surfboard.conf';
  170. if (\File::exists($customConfig)) {
  171. $config = file_get_contents("$customConfig");
  172. } else {
  173. $config = file_get_contents("$defaultConfig");
  174. }
  175. // Subscription link
  176. $subsURL = config('v2board.subscribe_url', config('v2board.app_url', env('APP_URL'))) . '/api/v1/client/subscribe?token=' . $user['token'];
  177. $config = str_replace('$subs_link', $subsURL, $config);
  178. $config = str_replace('$proxies', $proxies, $config);
  179. $config = str_replace('$proxy_group', rtrim($proxyGroup, ', '), $config);
  180. return $config;
  181. }
  182. private function clash($user, $shadowsocks = [], $vmess = [], $trojan = [])
  183. {
  184. $defaultConfig = base_path() . '/resources/rules/default.clash.yaml';
  185. $customConfig = base_path() . '/resources/rules/custom.clash.yaml';
  186. if (\File::exists($customConfig)) {
  187. $config = Yaml::parseFile($customConfig);
  188. } else {
  189. $config = Yaml::parseFile($defaultConfig);
  190. }
  191. $proxy = [];
  192. $proxies = [];
  193. foreach ($shadowsocks as $item) {
  194. array_push($proxy, Clash::buildShadowsocks($user->uuid, $item));
  195. array_push($proxies, $item->name);
  196. }
  197. foreach ($vmess as $item) {
  198. array_push($proxy, Clash::buildVmess($user->uuid, $item));
  199. array_push($proxies, $item->name);
  200. }
  201. foreach ($trojan as $item) {
  202. array_push($proxy, Clash::buildTrojan($user->uuid, $item));
  203. array_push($proxies, $item->name);
  204. }
  205. $config['proxies'] = array_merge($config['proxies'] ? $config['proxies'] : [], $proxy);
  206. foreach ($config['proxy-groups'] as $k => $v) {
  207. if (!is_array($config['proxy-groups'][$k]['proxies'])) continue;
  208. $config['proxy-groups'][$k]['proxies'] = array_merge($config['proxy-groups'][$k]['proxies'], $proxies);
  209. }
  210. $yaml = Yaml::dump($config);
  211. $yaml = str_replace('$app_name', config('v2board.app_name', 'V2Board'), $yaml);
  212. return $yaml;
  213. }
  214. }