UniProxyController.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace App\Http\Controllers\Server;
  3. use App\Services\ServerService;
  4. use App\Services\StatisticalService;
  5. use App\Services\UserService;
  6. use App\Utils\CacheKey;
  7. use App\Utils\Helper;
  8. use Illuminate\Http\Request;
  9. use App\Http\Controllers\Controller;
  10. use App\Models\ServerShadowsocks;
  11. use App\Models\ServerVmess;
  12. use App\Models\ServerTrojan;
  13. use Illuminate\Support\Facades\Cache;
  14. class UniProxyController extends Controller
  15. {
  16. private $nodeType;
  17. private $nodeInfo;
  18. private $nodeId;
  19. private $serverService;
  20. public function __construct(Request $request)
  21. {
  22. $token = $request->input('token');
  23. if (empty($token)) {
  24. abort(500, 'token is null');
  25. }
  26. if ($token !== config('v2board.server_token')) {
  27. abort(500, 'token is error');
  28. }
  29. $this->nodeType = $request->input('node_type');
  30. if ($this->nodeType === 'v2ray') $this->nodeType = 'vmess';
  31. $this->nodeId = $request->input('node_id');
  32. $this->serverService = new ServerService();
  33. $this->nodeInfo = $this->serverService->getServer($this->nodeId, $this->nodeType);
  34. if (!$this->nodeInfo) abort(500, 'server is not exist');
  35. }
  36. // 后端获取用户
  37. public function user(Request $request)
  38. {
  39. ini_set('memory_limit', -1);
  40. Cache::put(CacheKey::get('SERVER_' . strtoupper($this->nodeType) . '_LAST_CHECK_AT', $this->nodeInfo->id), time(), 3600);
  41. $users = $this->serverService->getAvailableUsers($this->nodeInfo->group_id);
  42. $users = $users->toArray();
  43. $response['users'] = $users;
  44. $eTag = sha1(json_encode($response));
  45. if (strpos($request->header('If-None-Match'), $eTag) !== false ) {
  46. abort(304);
  47. }
  48. return response($response)->header('ETag', "\"{$eTag}\"");
  49. }
  50. // 后端提交数据
  51. public function push(Request $request)
  52. {
  53. $data = file_get_contents('php://input');
  54. $data = json_decode($data, true);
  55. Cache::put(CacheKey::get('SERVER_' . strtoupper($this->nodeType) . '_ONLINE_USER', $this->nodeInfo->id), count($data), 3600);
  56. Cache::put(CacheKey::get('SERVER_' . strtoupper($this->nodeType) . '_LAST_PUSH_AT', $this->nodeInfo->id), time(), 3600);
  57. $userService = new UserService();
  58. foreach (array_keys($data) as $k) {
  59. $u = $data[$k][0];
  60. $d = $data[$k][1];
  61. $userService->trafficFetch($u, $d, $k, $this->nodeInfo->toArray(), $this->nodeType);
  62. }
  63. $statService = new StatisticalService();
  64. $statService->setStartAt(strtotime(date('Y-m-d')));
  65. $statService->setUserStats();
  66. $statService->statUser($this->nodeInfo->rate, $data);
  67. $statService->setServerStats();
  68. $statService->statServer($this->nodeId, $this->nodeType, $data);
  69. return response([
  70. 'data' => true
  71. ]);
  72. }
  73. // 后端获取配置
  74. public function config(Request $request)
  75. {
  76. switch ($this->nodeType) {
  77. case 'shadowsocks':
  78. $response = [
  79. 'server_port' => $this->nodeInfo->server_port,
  80. 'cipher' => $this->nodeInfo->cipher,
  81. 'obfs' => $this->nodeInfo->obfs,
  82. 'obfs_settings' => $this->nodeInfo->obfs_settings
  83. ];
  84. if ($this->nodeInfo->cipher === '2022-blake3-aes-128-gcm') {
  85. $response['server_key'] = Helper::getServerKey($this->nodeInfo->created_at, 16);
  86. }
  87. if ($this->nodeInfo->cipher === '2022-blake3-aes-256-gcm') {
  88. $response['server_key'] = Helper::getServerKey($this->nodeInfo->created_at, 32);
  89. }
  90. break;
  91. case 'vmess':
  92. $response = [
  93. 'server_port' => $this->nodeInfo->server_port,
  94. 'network' => $this->nodeInfo->network,
  95. 'networkSettings' => $this->nodeInfo->networkSettings,
  96. 'tls' => $this->nodeInfo->tls
  97. ];
  98. break;
  99. case 'trojan':
  100. $response = [
  101. 'host' => $this->nodeInfo->host,
  102. 'server_port' => $this->nodeInfo->server_port,
  103. 'server_name' => $this->nodeInfo->server_name,
  104. ];
  105. break;
  106. case 'hysteria':
  107. $response = [
  108. 'host' => $this->nodeInfo->host,
  109. 'server_port' => $this->nodeInfo->server_port,
  110. 'server_name' => $this->nodeInfo->server_name,
  111. 'up_mbps' => $this->nodeInfo->up_mbps,
  112. 'down_mbps' => $this->nodeInfo->down_mbps,
  113. 'obfs' => Helper::getServerKey($this->nodeInfo->created_at, 16)
  114. ];
  115. break;
  116. }
  117. $response['base_config'] = [
  118. 'push_interval' => (int)config('v2board.server_push_interval', 60),
  119. 'pull_interval' => (int)config('v2board.server_pull_interval', 60)
  120. ];
  121. if ($this->nodeInfo['route_id']) {
  122. $response['routes'] = $this->serverService->getRoutes($this->nodeInfo['route_id']);
  123. }
  124. $eTag = sha1(json_encode($response));
  125. if (strpos($request->header('If-None-Match'), $eTag) !== false ) {
  126. abort(304);
  127. }
  128. return response($response)->header('ETag', "\"{$eTag}\"");
  129. }
  130. }