ClientConfig.php 9.9 KB

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