ClientConfig.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. namespace App\Helpers;
  3. use App\Components\Client\Clash;
  4. use App\Components\Client\QuantumultX;
  5. use App\Components\Client\Surge;
  6. use App\Components\Client\URLSchemes;
  7. use File;
  8. use Symfony\Component\Yaml\Yaml;
  9. trait ClientConfig
  10. {
  11. private function clientConfig(string $target)
  12. {
  13. if (str_contains($target, 'quantumult%20x')) {
  14. return $this->quantumultX();
  15. }
  16. if (str_contains($target, 'quantumult')) {
  17. return $this->quantumult();
  18. }
  19. if (str_contains($target, 'clash') || str_contains($target, 'stash')) {
  20. return $this->clash();
  21. }
  22. if (str_contains($target, 'bob_vpn')) {
  23. return $this->clash('bob');
  24. }
  25. if (str_contains($target, 'surfboard')) {
  26. return $this->surfboard();
  27. }
  28. if (str_contains($target, 'surge')) {
  29. return $this->surge($target);
  30. }
  31. if (str_contains($target, 'shadowrocket')) {
  32. return $this->shadowrocket();
  33. }
  34. if (str_contains($target, 'v2rayn') || str_contains($target, 'v2rayng') || str_contains($target, 'v2rayu')) {
  35. return $this->v2rayN();
  36. }
  37. // if (strpos($target, 'shadowsocks') !== false) {
  38. // exit($this->shaodowsocksSIP008());
  39. // }
  40. return $this->origin();
  41. }
  42. private function quantumultX(): string
  43. {
  44. $user = $this->getUser();
  45. $uri = '';
  46. if (sysConfig('is_custom_subscribe')) {
  47. header("subscription-userinfo: upload=$user->u; download=$user->d; total=$user->transfer_enable; expire=".strtotime($user->expired_at));
  48. }
  49. foreach ($this->getServers() as $server) {
  50. if ($server['type'] === 'shadowsocks') {
  51. $uri .= QuantumultX::buildShadowsocks($server);
  52. }
  53. if ($server['type'] === 'shadowsocksr') {
  54. $uri .= QuantumultX::buildShadowsocksr($server);
  55. }
  56. if ($server['type'] === 'v2ray') {
  57. $uri .= QuantumultX::buildVmess($server);
  58. }
  59. if ($server['type'] === 'trojan') {
  60. $uri .= QuantumultX::buildTrojan($server);
  61. }
  62. }
  63. return base64_encode($uri);
  64. }
  65. private function quantumult(): string
  66. {
  67. $user = $this->getUser();
  68. if (sysConfig('is_custom_subscribe')) {
  69. header("subscription-userinfo: upload=$user->u; download=$user->d; total=$user->transfer_enable; expire=".strtotime($user->expired_at));
  70. }
  71. return $this->origin();
  72. }
  73. private function origin(bool $encode = true): string
  74. {
  75. $uri = '';
  76. foreach ($this->getServers() as $server) {
  77. if ($server['type'] === 'shadowsocks') {
  78. $uri .= URLSchemes::buildShadowsocks($server);
  79. }
  80. if ($server['type'] === 'shadowsocksr') {
  81. $uri .= URLSchemes::buildShadowsocksr($server);
  82. }
  83. if ($server['type'] === 'v2ray') {
  84. $uri .= URLSchemes::buildVmess($server);
  85. }
  86. if ($server['type'] === 'trojan') {
  87. $uri .= URLSchemes::buildTrojan($server);
  88. }
  89. }
  90. return $encode ? base64_encode($uri) : $uri;
  91. }
  92. private function clash($client = false)
  93. {
  94. $user = $this->getUser();
  95. $webName = sysConfig('website_name');
  96. header("content-disposition:attachment;filename*=UTF-8''".rawurlencode($webName).'.yaml');
  97. header('profile-update-interval: 24');
  98. header('profile-web-page-url:'.sysConfig('website_url'));
  99. if (sysConfig('is_custom_subscribe')) {
  100. header("subscription-userinfo: upload=$user->u; download=$user->d; total=$user->transfer_enable; expire=".strtotime($user->expired_at));
  101. }
  102. $custom_path = '/resources/rules/custom.clash.yaml';
  103. if ($client === 'bob') {
  104. $file_path = '/resources/rules/bob.clash.yaml';
  105. } elseif (File::exists(base_path().$custom_path)) {
  106. $file_path = $custom_path;
  107. } else {
  108. $file_path = '/resources/rules/default.clash.yaml';
  109. }
  110. $config = Yaml::parseFile(base_path().$file_path);
  111. foreach ($this->getServers() as $server) {
  112. if ($server['type'] === 'shadowsocks') {
  113. $proxy[] = Clash::buildShadowsocks($server);
  114. $proxies[] = $server['name'];
  115. }
  116. if ($server['type'] === 'shadowsocksr') {
  117. $proxy[] = Clash::buildShadowsocksr($server);
  118. $proxies[] = $server['name'];
  119. }
  120. if ($server['type'] === 'v2ray') {
  121. $proxy[] = Clash::buildVmess($server);
  122. $proxies[] = $server['name'];
  123. }
  124. if ($server['type'] === 'trojan') {
  125. $proxy[] = Clash::buildTrojan($server);
  126. $proxies[] = $server['name'];
  127. }
  128. }
  129. $config['proxies'] = array_merge($config['proxies'] ?: [], $proxy ?? []);
  130. foreach ($config['proxy-groups'] as $k => $v) {
  131. if (! is_array($config['proxy-groups'][$k]['proxies'])) {
  132. continue;
  133. }
  134. $config['proxy-groups'][$k]['proxies'] = array_merge($config['proxy-groups'][$k]['proxies'], $proxies ?? []);
  135. }
  136. array_unshift($config['rules'], 'DOMAIN,'.$_SERVER['HTTP_HOST'].',DIRECT'); // Set current sub-domain to be direct
  137. return str_replace('$app_name', $webName, Yaml::dump($config, 2, 4, Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE));
  138. }
  139. private function surfboard()
  140. {
  141. $defaultConfig = base_path().'/resources/rules/default.surfboard.conf';
  142. $customConfig = base_path().'/resources/rules/custom.surfboard.conf';
  143. return $this->sugerLike($customConfig, $defaultConfig);
  144. }
  145. private function surge(string $target)
  146. {
  147. $defaultConfig = base_path().'/resources/rules/default.surge.conf';
  148. $customConfig = base_path().'/resources/rules/custom.surge.conf';
  149. return $this->sugerLike($customConfig, $defaultConfig, $target);
  150. }
  151. private function shadowrocket(): string
  152. {
  153. //display remaining traffic and expire date
  154. $uri = '';
  155. $user = $this->getUser();
  156. if (sysConfig('is_custom_subscribe')) {
  157. $upload = flowAutoShow($user->u);
  158. $download = flowAutoShow($user->d);
  159. $totalTraffic = flowAutoShow($user->transfer_enable);
  160. $uri = "STATUS=📤:{$upload}📥:{$download}⏳:{$totalTraffic}📅:$user->expiration_date\r\n";
  161. }
  162. $uri .= $this->origin(false);
  163. return base64_encode($uri);
  164. }
  165. private function v2rayN()
  166. {
  167. $uri = '';
  168. $user = $this->getUser();
  169. if (sysConfig('is_custom_subscribe')) {
  170. $text = '';
  171. if ($user->expiration_date > date('Y-m-d')) {
  172. if ($user->transfer_enable === 0) {
  173. $text .= trans('user.account.remain').': 0';
  174. } else {
  175. $text .= trans('user.account.remain').': '.flowAutoShow($user->transfer_enable);
  176. }
  177. $text .= ', '.trans('model.user.expired_date').": $user->expiration_date";
  178. } else {
  179. $text .= trans('user.account.reason.expired');
  180. }
  181. $uri .= $this->failedProxyReturn($text, 2);
  182. }
  183. return base64_encode($uri.$this->origin(false));
  184. }
  185. private function shaodowsocksSIP008(): string
  186. {
  187. foreach ($this->getServers() as $server) {
  188. if ($server['type'] === 'shadowsocks') {
  189. $configs[] = URLSchemes::buildShadowsocksSIP008($server);
  190. }
  191. }
  192. return json_encode(['version' => 1, 'remark' => sysConfig('website_name'), 'servers' => $configs ?? []], JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
  193. }
  194. /**
  195. * @return array|false|string|string[]
  196. */
  197. private function sugerLike(string $customConfig, string $defaultConfig, string $target = '')
  198. {
  199. if (File::exists($customConfig)) {
  200. $config = file_get_contents($customConfig);
  201. } else {
  202. $config = file_get_contents($defaultConfig);
  203. }
  204. $proxies = '';
  205. $proxyGroup = '';
  206. $user = $this->getUser();
  207. $webName = sysConfig('website_name');
  208. header("content-disposition:attachment;filename*=UTF-8''".rawurlencode($webName).'.conf');
  209. foreach ($this->getServers() as $server) {
  210. if ($server['type'] === 'shadowsocks') {
  211. $proxies .= Surge::buildShadowsocks($server);
  212. $proxyGroup .= $server['name'].', ';
  213. }
  214. if ($server['type'] === 'v2ray') {
  215. $proxies .= Surge::buildVmess($server);
  216. $proxyGroup .= $server['name'].', ';
  217. }
  218. if ($server['type'] === 'trojan') {
  219. $proxies .= Surge::buildTrojan($server);
  220. $proxyGroup .= $server['name'].', ';
  221. }
  222. }
  223. if (str_contains($target, 'list')) {
  224. return $proxies;
  225. }
  226. if (sysConfig('is_custom_subscribe')) {
  227. $upload = flowAutoShow($user->u);
  228. $download = flowAutoShow($user->d);
  229. $totalTraffic = flowAutoShow($user->transfer_enable);
  230. $subscribeInfo = "title=$webName".trans('user.subscribe.info.title').', content='.trans('user.subscribe.info.upload').": $upload\\n".trans('user.subscribe.info.download').": $download\\n".trans('user.subscribe.info.total').": $totalTraffic\\n".trans('model.user.expired_date').": $user->expired_at";
  231. $config = str_replace('$subscribe_info', $subscribeInfo, $config);
  232. }
  233. return str_replace(['$subs_link', '$subs_domain', '$proxies', '$proxy_group'],
  234. [route('sub', $user->subscribe->code), $_SERVER['HTTP_HOST'], $proxies, rtrim($proxyGroup, ', ')],
  235. $config);
  236. }
  237. }