BaseController.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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(
  42. $message,
  43. $status = 'fail',
  44. $code = 400,
  45. $data = [],
  46. $addition = []
  47. ): JsonResponse {
  48. $etag = Helpers::abortIfNotModified($data);
  49. $data = [
  50. 'status' => $status,
  51. 'code' => $code,
  52. 'data' => $data,
  53. 'message' => $message,
  54. ];
  55. if ($addition) {
  56. $data = array_merge($data, $addition);
  57. }
  58. return Response::json($data)->header('ETAG', $etag);
  59. }
  60. // 上报节点在线人数
  61. public function setNodeOnline(Request $request, $id): JsonResponse
  62. {
  63. $inputArray = $request->all();
  64. $onlineCount = 0;
  65. foreach ($inputArray as $input) {
  66. if ( ! array_key_exists('ip', $input) || ! array_key_exists(
  67. 'uid',
  68. $input
  69. )) {
  70. return $this->returnData('上报节点在线情况失败,请检查字段');
  71. }
  72. if ( ! isset($input['ip'], $input['uid'])) {
  73. return $this->returnData('上报节点在线情况失败,请检查字段');
  74. }
  75. $obj = new NodeOnlineUserIp();
  76. $obj->node_id = $id;
  77. $obj->user_id = $input['uid'];
  78. $obj->ip = $input['ip'];
  79. $obj->port = User::find($input['uid'])->port;
  80. $obj->created_at = time();
  81. $obj->save();
  82. if ( ! $obj->id) {
  83. return $this->returnData('上报节点在线情况失败,请检查字段');
  84. }
  85. $onlineCount++;
  86. }
  87. $obj = new NodeOnlineLog();
  88. $obj->node_id = $id;
  89. $obj->online_user = $onlineCount;
  90. $obj->log_time = time();
  91. $obj->save();
  92. if ($obj->id) {
  93. return $this->returnData('上报节点在线情况成功', 'success', 200);
  94. }
  95. return $this->returnData('上报节点在线情况失败,请检查字段');
  96. }
  97. // 上报用户流量日志
  98. public function setUserTraffic(Request $request, $id): JsonResponse
  99. {
  100. foreach ($request->all() as $input) {
  101. if ( ! array_key_exists('uid', $input)) {
  102. return $this->returnData('上报用户流量日志失败,请检查字段');
  103. }
  104. $rate = Node::find($id)->traffic_rate;
  105. $log = new UserDataFlowLog();
  106. $log->user_id = (int)$input['uid'];
  107. $log->u = (int)$input['upload'] * $rate;
  108. $log->d = (int)$input['download'] * $rate;
  109. $log->node_id = $id;
  110. $log->rate = $rate;
  111. $log->traffic = flowAutoShow($log->u + $log->d);
  112. $log->log_time = time();
  113. $log->save();
  114. if ( ! $log->id) {
  115. return $this->returnData('上报用户流量日志失败,请检查字段');
  116. }
  117. $user = User::find($log->user_id);
  118. if ($user) {
  119. $user->u += $log->u;
  120. $user->d += $log->d;
  121. $user->t = time();
  122. $user->save();
  123. }
  124. }
  125. return $this->returnData('上报用户流量日志成功', 'success', 200);
  126. }
  127. // 获取节点的审计规则
  128. public function getNodeRule($id): JsonResponse
  129. {
  130. $nodeRule = RuleGroupNode::whereNodeId($id)->first();
  131. $data = [];
  132. //节点未设置任何审计规则
  133. if ($nodeRule) {
  134. $ruleGroup = RuleGroup::find($nodeRule->rule_group_id);
  135. if ($ruleGroup && $ruleGroup->rules) {
  136. foreach ($ruleGroup->rules as $ruleId) {
  137. $rule = Rule::find($ruleId);
  138. if ($rule) {
  139. $data[] = [
  140. 'id' => $rule->id,
  141. 'type' => $rule->type_api_label,
  142. 'pattern' => $rule->pattern,
  143. ];
  144. }
  145. }
  146. return $this->returnData(
  147. '获取节点审计规则成功',
  148. 'success',
  149. 200,
  150. [
  151. 'mode' => $ruleGroup->type ? 'reject' : 'allow',
  152. 'rules' => $data,
  153. ]
  154. );
  155. }
  156. }
  157. //放行
  158. return $this->returnData(
  159. '获取节点审计规则成功',
  160. 'success',
  161. 200,
  162. ['mode' => 'all', 'rules' => $data]
  163. );
  164. }
  165. // 上报用户触发的审计规则记录
  166. public function addRuleLog(Request $request, $id): JsonResponse
  167. {
  168. if ($request->has(['uid', 'rule_id', 'reason'])) {
  169. $obj = new RuleLog();
  170. $obj->user_id = $request->input('uid');
  171. $obj->node_id = $id;
  172. $obj->rule_id = $request->input('rule_id');
  173. $obj->reason = $request->input('reason');
  174. $obj->save();
  175. if ($obj->id) {
  176. return $this->returnData('上报用户触发审计规则记录成功', 'success', 200);
  177. }
  178. }
  179. return $this->returnData('上报用户触发审计规则记录失败');
  180. }
  181. }