BaseController.php 4.8 KB

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