AppController.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 Symfony\Component\Yaml\Yaml;
  10. class AppController extends Controller
  11. {
  12. 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"}}}}}';
  13. CONST SOCKS_PORT = 10010;
  14. CONST HTTP_PORT = 10011;
  15. public function getConfig(Request $request)
  16. {
  17. $servers = [];
  18. $user = $request->user;
  19. $userService = new UserService();
  20. if ($userService->isAvailable($user)) {
  21. $serverService = new ServerService();
  22. $servers = $serverService->getAvailableServers($user);
  23. }
  24. $config = Yaml::parseFile(base_path() . '/resources/rules/app.clash.yaml');
  25. $proxy = [];
  26. $proxies = [];
  27. foreach ($servers as $item) {
  28. if ($item['type'] === 'shadowsocks') {
  29. array_push($proxy, Clash::buildShadowsocks($user['uuid'], $item));
  30. array_push($proxies, $item['name']);
  31. }
  32. if ($item['type'] === 'v2ray') {
  33. array_push($proxy, Clash::buildVmess($user['uuid'], $item));
  34. array_push($proxies, $item['name']);
  35. }
  36. if ($item['type'] === 'trojan') {
  37. array_push($proxy, Clash::buildTrojan($user['uuid'], $item));
  38. array_push($proxies, $item['name']);
  39. }
  40. }
  41. $config['proxies'] = array_merge($config['proxies'] ? $config['proxies'] : [], $proxy);
  42. foreach ($config['proxy-groups'] as $k => $v) {
  43. $config['proxy-groups'][$k]['proxies'] = array_merge($config['proxy-groups'][$k]['proxies'], $proxies);
  44. }
  45. die(Yaml::dump($config));
  46. }
  47. public function getVersion(Request $request)
  48. {
  49. if (strpos($request->header('user-agent'), 'tidalab/4.0.0') !== false
  50. || strpos($request->header('user-agent'), 'tunnelab/4.0.0') !== false
  51. ) {
  52. if (strpos($request->header('user-agent'), 'Win64') !== false) {
  53. return response([
  54. 'data' => [
  55. 'version' => config('v2board.windows_version'),
  56. 'download_url' => config('v2board.windows_download_url')
  57. ]
  58. ]);
  59. } else {
  60. return response([
  61. 'data' => [
  62. 'version' => config('v2board.macos_version'),
  63. 'download_url' => config('v2board.macos_download_url')
  64. ]
  65. ]);
  66. }
  67. return;
  68. }
  69. return response([
  70. 'data' => [
  71. 'windows_version' => config('v2board.windows_version'),
  72. 'windows_download_url' => config('v2board.windows_download_url'),
  73. 'macos_version' => config('v2board.macos_version'),
  74. 'macos_download_url' => config('v2board.macos_download_url'),
  75. 'android_version' => config('v2board.android_version'),
  76. 'android_download_url' => config('v2board.android_download_url')
  77. ]
  78. ]);
  79. }
  80. public function config(Request $request)
  81. {
  82. if (empty($request->input('server_id'))) {
  83. abort(500, '参数错误');
  84. }
  85. $user = $request->user;
  86. if ($user->expired_at < time() && $user->expired_at !== NULL) {
  87. abort(500, '订阅计划已过期');
  88. }
  89. $server = Server::where('show', 1)
  90. ->where('id', $request->input('server_id'))
  91. ->first();
  92. if (!$server) {
  93. abort(500, '服务器不存在');
  94. }
  95. $json = json_decode(self::CLIENT_CONFIG);
  96. //socks
  97. $json->inbound->port = (int)self::SOCKS_PORT;
  98. //http
  99. $json->inboundDetour[0]->port = (int)self::HTTP_PORT;
  100. //other
  101. $json->outbound->settings->vnext[0]->address = (string)$server->host;
  102. $json->outbound->settings->vnext[0]->port = (int)$server->port;
  103. $json->outbound->settings->vnext[0]->users[0]->id = (string)$user->uuid;
  104. $json->outbound->settings->vnext[0]->users[0]->alterId = (int)$server->alter_id;
  105. $json->outbound->settings->vnext[0]->remark = (string)$server->name;
  106. $json->outbound->streamSettings->network = $server->network;
  107. if ($server->networkSettings) {
  108. switch ($server->network) {
  109. case 'tcp':
  110. $json->outbound->streamSettings->tcpSettings = json_decode($server->networkSettings);
  111. break;
  112. case 'kcp':
  113. $json->outbound->streamSettings->kcpSettings = json_decode($server->networkSettings);
  114. break;
  115. case 'ws':
  116. $json->outbound->streamSettings->wsSettings = json_decode($server->networkSettings);
  117. break;
  118. case 'http':
  119. $json->outbound->streamSettings->httpSettings = json_decode($server->networkSettings);
  120. break;
  121. case 'domainsocket':
  122. $json->outbound->streamSettings->dsSettings = json_decode($server->networkSettings);
  123. break;
  124. case 'quic':
  125. $json->outbound->streamSettings->quicSettings = json_decode($server->networkSettings);
  126. break;
  127. }
  128. }
  129. if ($request->input('is_global')) {
  130. $json->routing->settings->rules[0]->outboundTag = 'proxy';
  131. }
  132. if ($server->tls) {
  133. $json->outbound->streamSettings->security = "tls";
  134. }
  135. die(json_encode($json, JSON_UNESCAPED_UNICODE));
  136. }
  137. }