ClientController.php 11 KB

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