UniProxyController.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. $userService->trafficFetch($this->nodeInfo->toArray(), $this->nodeType, $data);
  59. return response([
  60. 'data' => true
  61. ]);
  62. }
  63. // 后端获取配置
  64. public function config(Request $request)
  65. {
  66. switch ($this->nodeType) {
  67. case 'shadowsocks':
  68. $response = [
  69. 'server_port' => $this->nodeInfo->server_port,
  70. 'cipher' => $this->nodeInfo->cipher,
  71. 'obfs' => $this->nodeInfo->obfs,
  72. 'obfs_settings' => $this->nodeInfo->obfs_settings
  73. ];
  74. if ($this->nodeInfo->cipher === '2022-blake3-aes-128-gcm') {
  75. $response['server_key'] = Helper::getServerKey($this->nodeInfo->created_at, 16);
  76. }
  77. if ($this->nodeInfo->cipher === '2022-blake3-aes-256-gcm') {
  78. $response['server_key'] = Helper::getServerKey($this->nodeInfo->created_at, 32);
  79. }
  80. break;
  81. case 'vmess':
  82. $response = [
  83. 'server_port' => $this->nodeInfo->server_port,
  84. 'network' => $this->nodeInfo->network,
  85. 'networkSettings' => $this->nodeInfo->networkSettings,
  86. 'tls' => $this->nodeInfo->tls
  87. ];
  88. break;
  89. case 'trojan':
  90. $response = [
  91. 'host' => $this->nodeInfo->host,
  92. 'server_port' => $this->nodeInfo->server_port,
  93. 'server_name' => $this->nodeInfo->server_name,
  94. ];
  95. break;
  96. case 'hysteria':
  97. $response = [
  98. 'host' => $this->nodeInfo->host,
  99. 'server_port' => $this->nodeInfo->server_port,
  100. 'server_name' => $this->nodeInfo->server_name,
  101. 'up_mbps' => $this->nodeInfo->up_mbps,
  102. 'down_mbps' => $this->nodeInfo->down_mbps,
  103. 'obfs' => Helper::getServerKey($this->nodeInfo->created_at, 16)
  104. ];
  105. break;
  106. }
  107. $response['base_config'] = [
  108. 'push_interval' => (int)config('v2board.server_push_interval', 60),
  109. 'pull_interval' => (int)config('v2board.server_pull_interval', 60)
  110. ];
  111. if ($this->nodeInfo['route_id']) {
  112. $response['routes'] = $this->serverService->getRoutes($this->nodeInfo['route_id']);
  113. }
  114. $eTag = sha1(json_encode($response));
  115. if (strpos($request->header('If-None-Match'), $eTag) !== false ) {
  116. abort(304);
  117. }
  118. return response($response)->header('ETag', "\"{$eTag}\"");
  119. }
  120. }