AppController.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Http\Controllers\Controller;
  5. use App\Models\User;
  6. use App\Models\Plan;
  7. use App\Models\Server;
  8. use App\Models\Notice;
  9. use App\Utils\Helper;
  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":[{"port":"1-52","type":"field","outboundTag":"direct"},{"port":"54-79","type":"field","outboundTag":"direct"},{"port":"81-442","type":"field","outboundTag":"direct"},{"port":"444-65535","type":"field","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"},{"type":"field","ip":["geoip:cn"],"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 data (Request $request) {
  16. $user = $request->user;
  17. $nodes = [];
  18. if ($user->plan_id) {
  19. $user['plan'] = Plan::find($user->plan_id);
  20. if (!$user['plan']) {
  21. abort(500, '订阅计划不存在');
  22. }
  23. if ($user->expired_at > time()) {
  24. $servers = Server::where('show', 1)
  25. ->orderBy('name')
  26. ->get();
  27. foreach ($servers as $item) {
  28. $groupId = json_decode($item['group_id']);
  29. if (in_array($user->group_id, $groupId)) {
  30. array_push($nodes, $item);
  31. }
  32. }
  33. }
  34. }
  35. return response([
  36. 'data' => [
  37. 'nodes' => $nodes,
  38. 'u' => $user->u,
  39. 'd' => $user->d,
  40. 'transfer_enable' => $user->transfer_enable,
  41. 'expired_at' => $user->expired_at,
  42. 'plan' => isset($user['plan']) ? $user['plan'] : false,
  43. 'notice' => Notice::orderBy('created_at', 'DESC')->first()
  44. ]
  45. ]);
  46. }
  47. public function config (Request $request) {
  48. if (empty($request->input('server_id'))) {
  49. abort(500, '参数错误');
  50. }
  51. $user = $request->user;
  52. if ($user->expired_at < time()) {
  53. abort(500, '订阅计划已过期');
  54. }
  55. $server = Server::where('show', 1)
  56. ->where('id', $request->input('server_id'))
  57. ->first();
  58. if (!$server) {
  59. abort(500, '服务器不存在');
  60. }
  61. $json = json_decode(self::CLIENT_CONFIG);
  62. //socks
  63. $json->inbound->port = (int)self::SOCKS_PORT;
  64. //http
  65. $json->inboundDetour[0]->port = (int)self::HTTP_PORT;
  66. //other
  67. $json->outbound->settings->vnext[0]->address = (string)$server->host;
  68. $json->outbound->settings->vnext[0]->port = (int)$server->port;
  69. $json->outbound->settings->vnext[0]->users[0]->id = (string)$user->v2ray_uuid;
  70. $json->outbound->settings->vnext[0]->users[0]->alterId = (int)$user->v2ray_alter_id;
  71. $json->outbound->settings->vnext[0]->remark = (string)$server->name;
  72. $json->outbound->streamSettings->network = $server->network;
  73. if ($server->settings) {
  74. switch ($server->network) {
  75. case 'tcp': $json->outbound->streamSettings->tcpSettings = json_decode($server->settings);
  76. break;
  77. case 'kcp': $json->outbound->streamSettings->kcpSettings = json_decode($server->settings);
  78. break;
  79. case 'ws': $json->outbound->streamSettings->wsSettings = json_decode($server->settings);
  80. break;
  81. case 'http': $json->outbound->streamSettings->httpSettings = json_decode($server->settings);
  82. break;
  83. case 'domainsocket': $json->outbound->streamSettings->dsSettings = json_decode($server->settings);
  84. break;
  85. case 'quic': $json->outbound->streamSettings->quicSettings = json_decode($server->settings);
  86. break;
  87. }
  88. }
  89. if ($request->input('is_global')) {
  90. $json->routing->settings->rules[5]->outboundTag = 'proxy';
  91. }
  92. if ($server->tls) {
  93. $json->outbound->streamSettings->security = "tls";
  94. }
  95. die(json_encode($json, JSON_UNESCAPED_UNICODE));
  96. }
  97. }