BaseController.php 6.5 KB

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