VProxyController.php 4.2 KB

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