AppController.php 4.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace App\Http\Controllers\Client;
  3. use App\Http\Controllers\Controller;
  4. use App\Services\ServerService;
  5. use App\Services\UserService;
  6. use App\Utils\Clash;
  7. use Illuminate\Http\Request;
  8. use App\Models\Server;
  9. use Illuminate\Support\Facades\File;
  10. use Symfony\Component\Yaml\Yaml;
  11. class AppController extends Controller
  12. {
  13. CONST CLIENT_CONFIG = '{"policy":{"levels":{"0":{"uplinkOnly":0}}},"dns":{"servers":["114.114.114.114","8.8.8.8"]},"outboundDetour":[{"protocol":"freedom","tag":"direct","settings":{}}],"inbound":{"listen":"0.0.0.0","port":31211,"protocol":"socks","settings":{"auth":"noauth","udp":true,"ip":"127.0.0.1"}},"inboundDetour":[{"listen":"0.0.0.0","allocate":{"strategy":"always","refresh":5,"concurrency":3},"port":31210,"protocol":"http","tag":"httpDetour","domainOverride":["http","tls"],"streamSettings":{},"settings":{"timeout":0}}],"routing":{"strategy":"rules","settings":{"domainStrategy":"IPIfNonMatch","rules":[{"type":"field","ip":["geoip:cn"],"outboundTag":"direct"},{"type":"field","ip":["0.0.0.0/8","10.0.0.0/8","100.64.0.0/10","127.0.0.0/8","169.254.0.0/16","172.16.0.0/12","192.0.0.0/24","192.0.2.0/24","192.168.0.0/16","198.18.0.0/15","198.51.100.0/24","203.0.113.0/24","::1/128","fc00::/7","fe80::/10"],"outboundTag":"direct"}]}},"outbound":{"tag":"proxy","sendThrough":"0.0.0.0","mux":{"enabled":false,"concurrency":8},"protocol":"vmess","settings":{"vnext":[{"address":"server","port":443,"users":[{"id":"uuid","alterId":2,"security":"auto","level":0}],"remark":"remark"}]},"streamSettings":{"network":"tcp","tcpSettings":{"header":{"type":"none"}},"security":"none","tlsSettings":{"allowInsecure":true,"allowInsecureCiphers":true},"kcpSettings":{"header":{"type":"none"},"mtu":1350,"congestion":false,"tti":20,"uplinkCapacity":5,"writeBufferSize":1,"readBufferSize":1,"downlinkCapacity":20},"wsSettings":{"path":"","headers":{"Host":"server.cc"}}}}}';
  14. CONST SOCKS_PORT = 10010;
  15. CONST HTTP_PORT = 10011;
  16. public function getConfig(Request $request)
  17. {
  18. $servers = [];
  19. $user = $request->user;
  20. $userService = new UserService();
  21. if ($userService->isAvailable($user)) {
  22. $serverService = new ServerService();
  23. $servers = $serverService->getAvailableServers($user);
  24. }
  25. $defaultConfig = base_path() . '/resources/rules/app.clash.yaml';
  26. $customConfig = base_path() . '/resources/rules/custom.app.clash.yaml';
  27. if (File::exists($customConfig)) {
  28. $config = Yaml::parseFile($customConfig);
  29. } else {
  30. $config = Yaml::parseFile($defaultConfig);
  31. }
  32. $proxy = [];
  33. $proxies = [];
  34. foreach ($servers as $item) {
  35. if ($item['type'] === 'shadowsocks') {
  36. array_push($proxy, Clash::buildShadowsocks($user['uuid'], $item));
  37. array_push($proxies, $item['name']);
  38. }
  39. if ($item['type'] === 'v2ray') {
  40. array_push($proxy, Clash::buildVmess($user['uuid'], $item));
  41. array_push($proxies, $item['name']);
  42. }
  43. if ($item['type'] === 'trojan') {
  44. array_push($proxy, Clash::buildTrojan($user['uuid'], $item));
  45. array_push($proxies, $item['name']);
  46. }
  47. }
  48. $config['proxies'] = array_merge($config['proxies'] ? $config['proxies'] : [], $proxy);
  49. foreach ($config['proxy-groups'] as $k => $v) {
  50. $config['proxy-groups'][$k]['proxies'] = array_merge($config['proxy-groups'][$k]['proxies'], $proxies);
  51. }
  52. die(Yaml::dump($config));
  53. }
  54. public function getVersion(Request $request)
  55. {
  56. if (strpos($request->header('user-agent'), 'tidalab/4.0.0') !== false
  57. || strpos($request->header('user-agent'), 'tunnelab/4.0.0') !== false
  58. ) {
  59. if (strpos($request->header('user-agent'), 'Win64') !== false) {
  60. return response([
  61. 'data' => [
  62. 'version' => config('v2board.windows_version'),
  63. 'download_url' => config('v2board.windows_download_url')
  64. ]
  65. ]);
  66. } else {
  67. return response([
  68. 'data' => [
  69. 'version' => config('v2board.macos_version'),
  70. 'download_url' => config('v2board.macos_download_url')
  71. ]
  72. ]);
  73. }
  74. return;
  75. }
  76. return response([
  77. 'data' => [
  78. 'windows_version' => config('v2board.windows_version'),
  79. 'windows_download_url' => config('v2board.windows_download_url'),
  80. 'macos_version' => config('v2board.macos_version'),
  81. 'macos_download_url' => config('v2board.macos_download_url'),
  82. 'android_version' => config('v2board.android_version'),
  83. 'android_download_url' => config('v2board.android_download_url')
  84. ]
  85. ]);
  86. }
  87. }