BaseController.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. namespace App\Http\Controllers\Api\WebApi;
  3. use App\Components\Helpers;
  4. use App\Models\Node;
  5. use App\Models\NodeHeartBeat;
  6. use App\Models\NodeOnlineLog;
  7. use App\Models\NodeOnlineUserIp;
  8. use App\Models\Rule;
  9. use App\Models\RuleGroup;
  10. use App\Models\RuleGroupNode;
  11. use App\Models\RuleLog;
  12. use App\Models\User;
  13. use App\Models\UserDataFlowLog;
  14. use Illuminate\Http\JsonResponse;
  15. use Illuminate\Http\Request;
  16. use Response;
  17. class BaseController
  18. {
  19. // 上报节点心跳信息
  20. public function setNodeStatus(Request $request, $id): JsonResponse
  21. {
  22. $cpu = (int) $request->input('cpu') / 100;
  23. $mem = (int) $request->input('mem') / 100;
  24. $disk = (int) $request->input('disk') / 100;
  25. if (is_null($request->input('uptime'))) {
  26. return $this->returnData('上报节点心跳信息失败,请检查字段');
  27. }
  28. $obj = new NodeHeartBeat();
  29. $obj->node_id = $id;
  30. $obj->uptime = (int) $request->input('uptime');
  31. //$obj->load = $request->input('load');
  32. $obj->load = implode(' ', [$cpu, $mem, $disk]);
  33. $obj->log_time = time();
  34. $obj->save();
  35. if ($obj->id) {
  36. return $this->returnData('上报节点心跳信息成功', 'success', 200);
  37. }
  38. return $this->returnData('上报节点心跳信息失败,请检查字段');
  39. }
  40. // 返回数据
  41. public function returnData($message, $status = 'fail', $code = 400, $data = [], $addition = []): JsonResponse
  42. {
  43. $etag = Helpers::abortIfNotModified($data);
  44. $data = [
  45. 'status' => $status,
  46. 'code' => $code,
  47. 'data' => $data,
  48. 'message' => $message,
  49. ];
  50. if ($addition) {
  51. $data = array_merge($data, $addition);
  52. }
  53. return Response::json($data)->header('ETAG', $etag);
  54. }
  55. // 上报节点在线人数
  56. public function setNodeOnline(Request $request, $id): JsonResponse
  57. {
  58. $inputArray = $request->all();
  59. $onlineCount = 0;
  60. foreach ($inputArray as $input) {
  61. if (!array_key_exists('ip', $input) || !array_key_exists('uid', $input)) {
  62. return $this->returnData('上报节点在线情况失败,请检查字段');
  63. }
  64. if (!isset($input['ip'], $input['uid'])) {
  65. return $this->returnData('上报节点在线情况失败,请检查字段');
  66. }
  67. $obj = new NodeOnlineUserIp();
  68. $obj->node_id = $id;
  69. $obj->user_id = $input['uid'];
  70. $obj->ip = $input['ip'];
  71. $obj->port = User::find($input['uid'])->port;
  72. $obj->created_at = time();
  73. $obj->save();
  74. if (!$obj->id) {
  75. return $this->returnData('上报节点在线情况失败,请检查字段');
  76. }
  77. $onlineCount++;
  78. }
  79. $obj = new NodeOnlineLog();
  80. $obj->node_id = $id;
  81. $obj->online_user = $onlineCount;
  82. $obj->log_time = time();
  83. $obj->save();
  84. if ($obj->id) {
  85. return $this->returnData('上报节点在线情况成功', 'success', 200);
  86. }
  87. return $this->returnData('上报节点在线情况失败,请检查字段');
  88. }
  89. // 上报用户流量日志
  90. public function setUserTraffic(Request $request, $id): JsonResponse
  91. {
  92. foreach ($request->all() as $input) {
  93. if (!array_key_exists('uid', $input)) {
  94. return $this->returnData('上报用户流量日志失败,请检查字段');
  95. }
  96. $rate = Node::find($id)->traffic_rate;
  97. $log = new UserDataFlowLog();
  98. $log->user_id = (int) $input['uid'];
  99. $log->u = (int) $input['upload'] * $rate;
  100. $log->d = (int) $input['download'] * $rate;
  101. $log->node_id = $id;
  102. $log->rate = $rate;
  103. $log->traffic = flowAutoShow($log->u + $log->d);
  104. $log->log_time = time();
  105. $log->save();
  106. if (!$log->id) {
  107. return $this->returnData('上报用户流量日志失败,请检查字段');
  108. }
  109. $user = User::find($log->user_id);
  110. if ($user) {
  111. $user->u += $log->u;
  112. $user->d += $log->d;
  113. $user->t = time();
  114. $user->save();
  115. }
  116. }
  117. return $this->returnData('上报用户流量日志成功', 'success', 200);
  118. }
  119. // 获取节点的审计规则
  120. public function getNodeRule($id): JsonResponse
  121. {
  122. $nodeRule = RuleGroupNode::whereNodeId($id)->first();
  123. $data = [];
  124. //节点未设置任何审计规则
  125. if ($nodeRule) {
  126. $ruleGroup = RuleGroup::find($nodeRule->rule_group_id);
  127. if ($ruleGroup && $ruleGroup->rules) {
  128. foreach ($ruleGroup->rules as $ruleId) {
  129. $rule = Rule::find($ruleId);
  130. if ($rule) {
  131. $data[] = [
  132. 'id' => $rule->id,
  133. 'type' => $rule->type_api_label,
  134. 'pattern' => $rule->pattern,
  135. ];
  136. }
  137. }
  138. return $this->returnData('获取节点审计规则成功', 'success', 200, ['mode' => $ruleGroup->type ? 'reject' : 'allow', 'rules' => $data]);
  139. }
  140. }
  141. //放行
  142. return $this->returnData('获取节点审计规则成功', 'success', 200, ['mode' => 'all', 'rules' => $data]);
  143. }
  144. // 上报用户触发的审计规则记录
  145. public function addRuleLog(Request $request, $id): JsonResponse
  146. {
  147. if ($request->has(['uid', 'rule_id', 'reason'])) {
  148. $obj = new RuleLog();
  149. $obj->user_id = $request->input('uid');
  150. $obj->node_id = $id;
  151. $obj->rule_id = $request->input('rule_id');
  152. $obj->reason = $request->input('reason');
  153. $obj->save();
  154. if ($obj->id) {
  155. return $this->returnData('上报用户触发审计规则记录成功', 'success', 200);
  156. }
  157. }
  158. return $this->returnData('上报用户触发审计规则记录失败');
  159. }
  160. }