PoseidonController.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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\User;
  9. use App\Models\Plan;
  10. use App\Models\Server;
  11. use App\Models\ServerLog;
  12. use Illuminate\Support\Facades\Log;
  13. use Illuminate\Support\Facades\Cache;
  14. /*
  15. * V2ray Poseidon
  16. * Github: https://github.com/ColetteContreras/trojan-poseidon
  17. */
  18. class PoseidonController extends Controller
  19. {
  20. public $poseidonVersion;
  21. public function __construct(Request $request)
  22. {
  23. $this->poseidonVersion = $request->input('poseidon_version');
  24. }
  25. // 后端获取用户
  26. public function user(Request $request)
  27. {
  28. if ($r = $this->verifyToken($request)) { return $r; }
  29. $nodeId = $request->input('node_id');
  30. $server = Server::find($nodeId);
  31. if (!$server) {
  32. return $this->error("server could not be found", 404);
  33. }
  34. Cache::put(CacheKey::get('SERVER_V2RAY_LAST_CHECK_AT', $server->id), time(), 3600);
  35. $serverService = new ServerService();
  36. $users = $serverService->getAvailableUsers(json_decode($server->group_id));
  37. $result = [];
  38. foreach ($users as $user) {
  39. $user->v2ray_user = [
  40. "uuid" => $user->uuid,
  41. "email" => sprintf("%s@v2board.user", $user->uuid),
  42. "alter_id" => $server->alter_id,
  43. "level" => 0,
  44. ];
  45. unset($user['uuid']);
  46. unset($user['email']);
  47. array_push($result, $user);
  48. }
  49. return $this->success($result);
  50. }
  51. // 后端提交数据
  52. public function submit(Request $request)
  53. {
  54. if ($r = $this->verifyToken($request)) { return $r; }
  55. $server = Server::find($request->input('node_id'));
  56. if (!$server) {
  57. return $this->error("server could not be found", 404);
  58. }
  59. $data = file_get_contents('php://input');
  60. $data = json_decode($data, true);
  61. Cache::put(CacheKey::get('SERVER_V2RAY_ONLINE_USER', $server->id), count($data), 3600);
  62. Cache::put(CacheKey::get('SERVER_V2RAY_LAST_PUSH_AT', $server->id), time(), 3600);
  63. $userService = new UserService();
  64. foreach ($data as $item) {
  65. $u = $item['u'] * $server->rate;
  66. $d = $item['d'] * $server->rate;
  67. if (!$userService->trafficFetch($u, $d, $item['user_id'], $server, 'vmess')) {
  68. return $this->error("user fetch fail", 500);
  69. }
  70. }
  71. return $this->success('');
  72. }
  73. // 后端获取配置
  74. public function config(Request $request)
  75. {
  76. if ($r = $this->verifyToken($request)) { return $r; }
  77. $nodeId = $request->input('node_id');
  78. $localPort = $request->input('local_port');
  79. if (empty($nodeId) || empty($localPort)) {
  80. return $this->error('invalid parameters', 400);
  81. }
  82. $serverService = new ServerService();
  83. try {
  84. $json = $serverService->getV2RayConfig($nodeId, $localPort);
  85. $json->poseidon = [
  86. 'license_key' => (string)config('v2board.server_license'),
  87. ];
  88. if ($this->poseidonVersion >= 'v1.5.0') {
  89. // don't need it after v1.5.0
  90. unset($json->inboundDetour);
  91. unset($json->stats);
  92. unset($json->api);
  93. array_shift($json->routing->rules);
  94. }
  95. foreach($json->policy->levels as &$level) {
  96. $level->handshake = 2;
  97. $level->uplinkOnly = 2;
  98. $level->downlinkOnly = 2;
  99. $level->connIdle = 60;
  100. }
  101. return $this->success($json);
  102. } catch (\Exception $e) {
  103. return $this->error($e->getMessage(), 500);
  104. }
  105. }
  106. protected function verifyToken(Request $request)
  107. {
  108. $token = $request->input('token');
  109. if (empty($token)) {
  110. return $this->error("token must be set");
  111. }
  112. if ($token !== config('v2board.server_token')) {
  113. return $this->error("invalid token");
  114. }
  115. }
  116. protected function error($msg, int $status = 400) {
  117. return response([
  118. 'msg' => $msg,
  119. ], $status);
  120. }
  121. protected function success($data) {
  122. $req = request();
  123. // Only for "GET" method
  124. if (!$req->isMethod('GET') || !$data) {
  125. return response([
  126. 'msg' => 'ok',
  127. 'data' => $data,
  128. ]);
  129. }
  130. $etag = sha1(json_encode($data));
  131. if ($etag == $req->header("IF-NONE-MATCH")) {
  132. return response(null, 304);
  133. }
  134. return response([
  135. 'msg' => 'ok',
  136. 'data' => $data,
  137. ])->header('ETAG', $etag);
  138. }
  139. }