ClientConfig.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. namespace App\Helpers;
  3. use App\Utils\Clients\Clash;
  4. use App\Utils\Clients\QuantumultX;
  5. use App\Utils\Clients\Surge;
  6. use App\Utils\Clients\URLSchemes;
  7. use File;
  8. use Symfony\Component\Yaml\Yaml;
  9. trait ClientConfig
  10. {
  11. private function clientConfig(string $target): string
  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 (str_contains($target, 'shadowsocks')) {
  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(string $client = ''): string
  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(): string
  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 sugerLike(string $customConfig, string $defaultConfig, string $target = ''): string
  146. {
  147. if (File::exists($customConfig)) {
  148. $config = file_get_contents($customConfig);
  149. } else {
  150. $config = file_get_contents($defaultConfig);
  151. }
  152. $proxies = '';
  153. $proxyGroup = '';
  154. $user = $this->getUser();
  155. $webName = sysConfig('website_name');
  156. header("content-disposition:attachment;filename*=UTF-8''".rawurlencode($webName).'.conf');
  157. foreach ($this->getServers() as $server) {
  158. if ($server['type'] === 'shadowsocks') {
  159. $proxies .= Surge::buildShadowsocks($server);
  160. $proxyGroup .= $server['name'].', ';
  161. }
  162. if ($server['type'] === 'v2ray') {
  163. $proxies .= Surge::buildVmess($server);
  164. $proxyGroup .= $server['name'].', ';
  165. }
  166. if ($server['type'] === 'trojan') {
  167. $proxies .= Surge::buildTrojan($server);
  168. $proxyGroup .= $server['name'].', ';
  169. }
  170. }
  171. if (str_contains($target, 'list')) {
  172. return $proxies;
  173. }
  174. if (sysConfig('is_custom_subscribe')) {
  175. $upload = formatBytes($user->u);
  176. $download = formatBytes($user->d);
  177. $totalTraffic = formatBytes($user->transfer_enable);
  178. $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";
  179. $config = str_replace('$subscribe_info', $subscribeInfo, $config);
  180. }
  181. return str_replace(['$subs_link', '$subs_domain', '$proxies', '$proxy_group'],
  182. [route('sub', $user->subscribe->code), $_SERVER['HTTP_HOST'], $proxies, rtrim($proxyGroup, ', ')],
  183. $config);
  184. }
  185. private function surge(string $target): string
  186. {
  187. $defaultConfig = base_path().'/resources/rules/default.surge.conf';
  188. $customConfig = base_path().'/resources/rules/custom.surge.conf';
  189. return $this->sugerLike($customConfig, $defaultConfig, $target);
  190. }
  191. private function shadowrocket(): string
  192. {
  193. //display remaining traffic and expire date
  194. $uri = '';
  195. $user = $this->getUser();
  196. if (sysConfig('is_custom_subscribe')) {
  197. $upload = formatBytes($user->u);
  198. $download = formatBytes($user->d);
  199. $totalTraffic = formatBytes($user->transfer_enable);
  200. $uri = "STATUS=📤:{$upload}📥:{$download}⏳:{$totalTraffic}📅:$user->expiration_date\r\n";
  201. }
  202. $uri .= $this->origin(false);
  203. return base64_encode($uri);
  204. }
  205. private function v2rayN(): string
  206. {
  207. $uri = '';
  208. $user = $this->getUser();
  209. if (sysConfig('is_custom_subscribe')) {
  210. $text = '';
  211. if ($user->expiration_date > date('Y-m-d')) {
  212. if ($user->transfer_enable === 0) {
  213. $text .= trans('user.account.remain').': 0';
  214. } else {
  215. $text .= trans('user.account.remain').': '.formatBytes($user->transfer_enable);
  216. }
  217. $text .= ', '.trans('model.user.expired_date').": $user->expiration_date";
  218. } else {
  219. $text .= trans('user.account.reason.expired');
  220. }
  221. $uri .= $this->failedProxyReturn($text, 2);
  222. }
  223. return base64_encode($uri.$this->origin(false));
  224. }
  225. private function shaodowsocksSIP008(): string
  226. {
  227. foreach ($this->getServers() as $server) {
  228. if ($server['type'] === 'shadowsocks') {
  229. $configs[] = URLSchemes::buildShadowsocksSIP008($server);
  230. }
  231. }
  232. return json_encode(['version' => 1, 'remark' => sysConfig('website_name'), 'servers' => $configs ?? []], JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
  233. }
  234. }