ClientController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <?php
  2. namespace App\Http\Controllers;
  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\Protocols\SingBox;
  9. use App\Models\User;
  10. use File;
  11. use Symfony\Component\Yaml\Yaml;
  12. class ClientController extends Controller
  13. {
  14. public function config(string $target, User $user, array $servers)
  15. {
  16. if (strpos($target, 'quantumult%20x') !== false) {
  17. return $this->quantumultX($user, $servers);
  18. }
  19. if (strpos($target, 'quantumult') !== false) {
  20. return $this->quantumult($user, $servers);
  21. }
  22. if (strpos($target, 'clash') !== false) {
  23. return $this->clash($servers);
  24. }
  25. if (strpos($target, 'surfboard') !== false) {
  26. return $this->surfboard($user, $servers);
  27. }
  28. if (strpos($target, 'surge') !== false) {
  29. return $this->surge($user, $servers);
  30. }
  31. if (strpos($target, 'shadowrocket') !== false) {
  32. return $this->shadowrocket($user, $servers);
  33. }
  34. if (strpos($target, 'meet') !== false) {
  35. return $this->clash_meet($servers);
  36. }
  37. if (strpos($target, 'sing') !== false) {
  38. return (new SingBox)->NewSingBox($user,$servers);
  39. }
  40. // if (strpos($target, 'shadowsocks') !== false) {
  41. // exit($this->shaodowsocksSIP008($servers));
  42. // }
  43. return $this->origin($servers);
  44. }
  45. private function quantumultX(User $user, array $servers = []): string
  46. {
  47. $uri = '';
  48. if (sysConfig('is_custom_subscribe')) {
  49. header("subscription-userinfo: upload={$user->u}; download={$user->d}; total={$user->transfer_enable}; expire={$user->expired_at}");
  50. }
  51. foreach ($servers 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(User $user, array $servers = []): string
  68. {
  69. if (sysConfig('is_custom_subscribe')) {
  70. header("subscription-userinfo: upload={$user->u}; download={$user->d}; total={$user->transfer_enable}; expire={$user->expired_at}");
  71. }
  72. return $this->origin($servers);
  73. }
  74. private function origin(array $servers = [], bool $encode = true): string
  75. {
  76. $uri = '';
  77. foreach ($servers as $server) {
  78. if ($server['type'] === 'shadowsocks') {
  79. $uri .= URLSchemes::buildShadowsocks($server);
  80. }
  81. if ($server['type'] === 'shadowsocksr') {
  82. $uri .= URLSchemes::buildShadowsocksr($server);
  83. }
  84. if ($server['type'] === 'v2ray') {
  85. if ($server['vless'] == 1){
  86. $uri .= URLSchemes::buildVless($server);
  87. }
  88. else
  89. {
  90. $uri .= URLSchemes::buildVmess($server);
  91. }
  92. }
  93. if ($server['type'] === 'trojan') {
  94. $uri .= URLSchemes::buildTrojan($server);
  95. }
  96. }
  97. return $encode ? base64_encode($uri) : $uri;
  98. }
  99. private function clash_meet($servers)
  100. {
  101. $defaultConfig = base_path().'/resources/rules/default.clash.yaml';
  102. $customConfig = base_path().'/resources/rules/custom.clash.yaml';
  103. if (File::exists($customConfig)) {
  104. $config = Yaml::parseFile($customConfig);
  105. } else {
  106. $config = Yaml::parseFile($defaultConfig);
  107. }
  108. foreach ($servers as $server) {
  109. if ($server['type'] === 'shadowsocks') {
  110. $proxy[] = Clash::buildShadowsocks($server);
  111. $proxies[] = $server['name'];
  112. }
  113. if ($server['type'] === 'shadowsocksr') {
  114. $proxy[] = Clash::buildShadowsocksr($server);
  115. $proxies[] = $server['name'];
  116. }
  117. if ($server['type'] === 'v2ray') {
  118. if ($server['vless'] == 1){
  119. // $proxy[] = Clash::buildVless($server);
  120. // $proxies[] = $server['name'];
  121. } else {
  122. $proxy[] = Clash::buildVmess($server);
  123. $proxies[] = $server['name'];
  124. }
  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. return str_replace('$app_name', sysConfig('website_name'), Yaml::dump($config));
  139. }
  140. private function clash($servers)
  141. {
  142. $defaultConfig = base_path().'/resources/rules/default.clash.yaml';
  143. $customConfig = base_path().'/resources/rules/custom.clash.yaml';
  144. if (File::exists($customConfig)) {
  145. $config = Yaml::parseFile($customConfig);
  146. } else {
  147. $config = Yaml::parseFile($defaultConfig);
  148. }
  149. foreach ($servers as $server) {
  150. if ($server['type'] === 'shadowsocks') {
  151. $proxy[] = Clash::buildShadowsocks($server);
  152. $proxies[] = $server['name'];
  153. }
  154. if ($server['type'] === 'shadowsocksr') {
  155. $proxy[] = Clash::buildShadowsocksr($server);
  156. $proxies[] = $server['name'];
  157. }
  158. if ($server['type'] === 'v2ray') {
  159. if ($server['vless'] == 1){
  160. $proxy[] = Clash::buildVless($server);
  161. $proxies[] = $server['name'];
  162. } else {
  163. $proxy[] = Clash::buildVmess($server);
  164. $proxies[] = $server['name'];
  165. }
  166. }
  167. if ($server['type'] === 'trojan') {
  168. $proxy[] = Clash::buildTrojan($server);
  169. $proxies[] = $server['name'];
  170. }
  171. }
  172. $config['proxies'] = array_merge($config['proxies'] ?: [], $proxy ?? []);
  173. foreach ($config['proxy-groups'] as $k => $v) {
  174. if (! is_array($config['proxy-groups'][$k]['proxies'])) {
  175. continue;
  176. }
  177. $config['proxy-groups'][$k]['proxies'] = array_merge($config['proxy-groups'][$k]['proxies'], $proxies ?? []);
  178. }
  179. return str_replace('$app_name', sysConfig('website_name'), Yaml::dump($config));
  180. }
  181. private function surfboard(User $user, array $servers = [])
  182. {
  183. $proxies = '';
  184. $proxyGroup = '';
  185. foreach ($servers as $server) {
  186. if ($server['type'] === 'shadowsocks') {
  187. $proxies .= Surfboard::buildShadowsocks($server);
  188. $proxyGroup .= $server['name'].', ';
  189. }
  190. if ($server['type'] === 'v2ray') {
  191. $proxies .= Surfboard::buildVmess($server);
  192. $proxyGroup .= $server['name'].', ';
  193. }
  194. }
  195. $defaultConfig = base_path().'/resources/rules/default.surfboard.conf';
  196. $customConfig = base_path().'/resources/rules/custom.surfboard.conf';
  197. if (File::exists($customConfig)) {
  198. $config = file_get_contents($customConfig);
  199. } else {
  200. $config = file_get_contents($defaultConfig);
  201. }
  202. // Subscription link
  203. $subsURL = route('sub', $user->subscribe->code);
  204. return str_replace(['$subs_link', '$proxies', '$proxy_group'], [$subsURL, $proxies, rtrim($proxyGroup, ', ')], $config);
  205. }
  206. private function surge(User $user, array $servers = [])
  207. {
  208. $proxies = '';
  209. $proxyGroup = '';
  210. foreach ($servers as $server) {
  211. if ($server['type'] === 'shadowsocks') {
  212. $proxies .= Surge::buildShadowsocks($server);
  213. $proxyGroup .= $server['name'].', ';
  214. }
  215. if ($server['type'] === 'v2ray') {
  216. $proxies .= Surge::buildVmess($server);
  217. $proxyGroup .= $server['name'].', ';
  218. }
  219. if ($server['type'] === 'trojan') {
  220. $proxies .= Surge::buildTrojan($server);
  221. $proxyGroup .= $server['name'].', ';
  222. }
  223. }
  224. $defaultConfig = base_path().'/resources/rules/default.surge.conf';
  225. $customConfig = base_path().'/resources/rules/custom.surge.conf';
  226. if (File::exists($customConfig)) {
  227. $config = file_get_contents($customConfig);
  228. } else {
  229. $config = file_get_contents($defaultConfig);
  230. }
  231. // Subscription link
  232. $subsURL = route('sub', $user->subscribe->code);
  233. return str_replace(['$subs_link', '$proxies', '$proxy_group'], [$subsURL, $proxies, rtrim($proxyGroup, ', ')], $config);
  234. }
  235. private function shadowrocket(User $user, array $servers = []): string
  236. {
  237. //display remaining traffic and expire date
  238. $uri = '';
  239. if (sysConfig('is_custom_subscribe')) {
  240. $upload = flowAutoShow($user->u);
  241. $download = flowAutoShow($user->d);
  242. $totalTraffic = flowAutoShow($user->transfer_enable);
  243. $uri = "STATUS=📤:{$upload}📥:{$download}⏳:{$totalTraffic}📅:{$user->expired_at}\r\n";
  244. }
  245. $uri .= $this->origin($servers, false);
  246. return base64_encode($uri);
  247. }
  248. private function shaodowsocksSIP008(array $servers = []): string
  249. {
  250. foreach ($servers as $server) {
  251. if ($server['type'] === 'shadowsocks') {
  252. $configs[] = URLSchemes::buildShadowsocksSIP008($server);
  253. }
  254. }
  255. return json_encode(['version' => 1, 'remark' => sysConfig('website_name'), 'servers' => $configs ?? []], JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
  256. }
  257. }