PoseidonController.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. namespace App\Http\Controllers\Server;
  3. use App\Services\ServerService;
  4. use Illuminate\Http\Request;
  5. use App\Http\Controllers\Controller;
  6. use App\Models\User;
  7. use App\Models\Plan;
  8. use App\Models\Server;
  9. use App\Models\ServerLog;
  10. use Illuminate\Support\Facades\Log;
  11. use Illuminate\Support\Facades\Cache;
  12. class PoseidonController extends Controller
  13. {
  14. CONST SERVER_CONFIG = '{"api":{"services":["HandlerService","StatsService"],"tag":"api"},"stats":{},"inbound":{"port":443,"protocol":"vmess","settings":{"clients":[]},"sniffing":{"enabled": true,"destOverride": ["http","tls"]},"streamSettings":{"network":"tcp"},"tag":"proxy"},"inboundDetour":[{"listen":"0.0.0.0","port":23333,"protocol":"dokodemo-door","settings":{"address":"0.0.0.0"},"tag":"api"}],"log":{"loglevel":"debug","access":"access.log","error":"error.log"},"outbound":{"protocol":"freedom","settings":{}},"outboundDetour":[{"protocol":"blackhole","settings":{},"tag":"block"}],"routing":{"rules":[{"inboundTag":"api","outboundTag":"api","type":"field"}]},"policy":{"levels":{"0":{"handshake":4,"connIdle":300,"uplinkOnly":5,"downlinkOnly":30,"statsUserUplink":true,"statsUserDownlink":true}}}}';
  15. // 后端获取用户
  16. public function user(Request $request)
  17. {
  18. if ($r = $this->verifyToken($request)) { return $r; }
  19. $nodeId = $request->input('node_id');
  20. $server = Server::find($nodeId);
  21. if (!$server) {
  22. return $this->error("server could not be found", 404);
  23. }
  24. Cache::put('server_last_check_at_' . $server->id, time());
  25. $serverService = new ServerService();
  26. $users = $serverService->getAvailableUsers(json_decode($server->group_id));
  27. $result = [];
  28. foreach ($users as $user) {
  29. $user->v2ray_user = [
  30. "uuid" => $user->v2ray_uuid,
  31. "email" => sprintf("%s@v2board.user", $user->v2ray_uuid),
  32. "alter_id" => $user->v2ray_alter_id,
  33. "level" => $user->v2ray_level,
  34. ];
  35. unset($user['v2ray_uuid']);
  36. unset($user['v2ray_alter_id']);
  37. unset($user['v2ray_level']);
  38. array_push($result, $user);
  39. }
  40. return $this->success($result);
  41. }
  42. // 后端提交数据
  43. public function submit(Request $request)
  44. {
  45. if ($r = $this->verifyToken($request)) { return $r; }
  46. // Log::info('serverSubmitData:' . $request->input('node_id') . ':' . file_get_contents('php://input'));
  47. $server = Server::find($request->input('node_id'));
  48. if (!$server) {
  49. return $this->error("server could not be found", 404);
  50. }
  51. $data = file_get_contents('php://input');
  52. $data = json_decode($data, true);
  53. foreach ($data as $item) {
  54. $u = $item['u'] * $server->rate;
  55. $d = $item['d'] * $server->rate;
  56. $user = User::find($item['user_id']);
  57. $user->t = time();
  58. $user->u = $user->u + $u;
  59. $user->d = $user->d + $d;
  60. $user->save();
  61. $serverLog = new ServerLog();
  62. $serverLog->user_id = $item['user_id'];
  63. $serverLog->server_id = $request->input('node_id');
  64. $serverLog->u = $item['u'];
  65. $serverLog->d = $item['d'];
  66. $serverLog->rate = $server->rate;
  67. $serverLog->save();
  68. }
  69. return $this->success('');
  70. }
  71. // 后端获取配置
  72. public function config(Request $request)
  73. {
  74. if ($r = $this->verifyToken($request)) { return $r; }
  75. $nodeId = $request->input('node_id');
  76. $localPort = $request->input('local_port');
  77. if (empty($nodeId) || empty($localPort)) {
  78. return $this->error('invalid parameters', 400);
  79. }
  80. $server = Server::find($nodeId);
  81. if (!$server) {
  82. return $this->error("server could not be found", 404);
  83. }
  84. $json = json_decode(self::SERVER_CONFIG);
  85. $json->inboundDetour[0]->port = (int)$localPort;
  86. $json->inbound->port = (int)$server->server_port;
  87. $json->inbound->streamSettings->network = $server->network;
  88. if ($server->settings) {
  89. switch ($server->network) {
  90. case 'tcp':
  91. $json->inbound->streamSettings->tcpSettings = json_decode($server->settings);
  92. break;
  93. case 'kcp':
  94. $json->inbound->streamSettings->kcpSettings = json_decode($server->settings);
  95. break;
  96. case 'ws':
  97. $json->inbound->streamSettings->wsSettings = json_decode($server->settings);
  98. break;
  99. case 'http':
  100. $json->inbound->streamSettings->httpSettings = json_decode($server->settings);
  101. break;
  102. case 'domainsocket':
  103. $json->inbound->streamSettings->dsSettings = json_decode($server->settings);
  104. break;
  105. case 'quic':
  106. $json->inbound->streamSettings->quicSettings = json_decode($server->settings);
  107. break;
  108. }
  109. }
  110. if ($server->rules) {
  111. $rules = json_decode($server->rules);
  112. // domain
  113. if (isset($rules->domain) && !empty($rules->domain)) {
  114. $domainObj = new \StdClass();
  115. $domainObj->type = 'field';
  116. $domainObj->domain = $rules->domain;
  117. $domainObj->outboundTag = 'block';
  118. array_push($json->routing->rules, $domainObj);
  119. }
  120. // protocol
  121. if (isset($rules->protocol) && !empty($rules->protocol)) {
  122. $protocolObj = new \StdClass();
  123. $protocolObj->type = 'field';
  124. $protocolObj->protocol = $rules->protocol;
  125. $protocolObj->outboundTag = 'block';
  126. array_push($json->routing->rules, $protocolObj);
  127. }
  128. }
  129. if ((int)$server->tls) {
  130. $json->inbound->streamSettings->security = 'tls';
  131. $tls = (object)[
  132. 'certificateFile' => '/home/v2ray.crt',
  133. 'keyFile' => '/home/v2ray.key'
  134. ];
  135. $json->inbound->streamSettings->tlsSettings = new \StdClass();
  136. $json->inbound->streamSettings->tlsSettings->certificates[0] = $tls;
  137. }
  138. $json->poseidon = [
  139. 'license_key' => (string)config('v2board.server_license'),
  140. ];
  141. return $this->success($json);
  142. }
  143. protected function verifyToken(Request $request)
  144. {
  145. $token = $request->input('token');
  146. if (empty($token)) {
  147. return $this->error("token must be set");
  148. }
  149. if ($token !== config('v2board.server_token')) {
  150. return $this->error("invalid token");
  151. }
  152. }
  153. protected function error($msg, int $status = 400) {
  154. return response([
  155. 'msg' => $msg,
  156. ], $status);
  157. }
  158. protected function success($data) {
  159. return response([
  160. 'msg' => 'ok',
  161. 'data' => $data,
  162. ]);
  163. }
  164. }