AppController.php 5.7 KB

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