UniProxyController.php 4.6 KB

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