IpController.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. namespace App\Controllers\Admin;
  3. use App\Models\BlockIp;
  4. use App\Models\UnblockIp;
  5. use App\Models\Node;
  6. use App\Controllers\AdminController;
  7. use App\Utils\QQWry;
  8. use App\Utils\Tools;
  9. use App\Services\Auth;
  10. use Ozdemir\Datatables\Datatables;
  11. use App\Utils\DatatablesHelper;
  12. class IpController extends AdminController
  13. {
  14. public function index($request, $response, $args)
  15. {
  16. $table_config['total_column'] = array('id' => 'ID', 'userid' => '用户ID',
  17. 'user_name' => '用户名', 'ip' => 'IP',
  18. 'location' => '归属地', 'datetime' => '时间', 'type' => '类型');
  19. $table_config['default_show_column'] = array();
  20. foreach ($table_config['total_column'] as $column => $value) {
  21. $table_config['default_show_column'][] = $column;
  22. }
  23. $table_config['ajax_url'] = 'login/ajax';
  24. return $this->view()->assign('table_config', $table_config)->display('admin/ip/login.tpl');
  25. }
  26. public function alive($request, $response, $args)
  27. {
  28. $table_config['total_column'] = array('id' => 'ID', 'userid' => '用户ID',
  29. 'user_name' => '用户名', 'nodeid' => '节点ID',
  30. 'node_name' => '节点名', 'ip' => 'IP',
  31. 'location' => '归属地', 'datetime' => '时间', 'is_node' => '是否为中转连接');
  32. $table_config['default_show_column'] = array();
  33. foreach ($table_config['total_column'] as $column => $value) {
  34. $table_config['default_show_column'][] = $column;
  35. }
  36. $table_config['ajax_url'] = 'alive/ajax';
  37. return $this->view()->assign('table_config', $table_config)->display('admin/ip/alive.tpl');
  38. }
  39. public function block($request, $response, $args)
  40. {
  41. $table_config['total_column'] = array('id' => 'ID',
  42. 'name' => '节点名称', 'ip' => 'IP',
  43. 'location' => '归属地', 'datetime' => '时间');
  44. $table_config['default_show_column'] = array();
  45. foreach ($table_config['total_column'] as $column => $value) {
  46. $table_config['default_show_column'][] = $column;
  47. }
  48. $table_config['ajax_url'] = 'block/ajax';
  49. return $this->view()->assign('table_config', $table_config)->display('admin/ip/block.tpl');
  50. }
  51. public function unblock($request, $response, $args)
  52. {
  53. $table_config['total_column'] = array('id' => 'ID', 'userid' => '用户ID',
  54. 'user_name' => '用户名', 'ip' => 'IP',
  55. 'location' => '归属地', 'datetime' => '时间');
  56. $table_config['default_show_column'] = array();
  57. foreach ($table_config['total_column'] as $column => $value) {
  58. $table_config['default_show_column'][] = $column;
  59. }
  60. $table_config['ajax_url'] = 'unblock/ajax';
  61. return $this->view()->assign('table_config', $table_config)->display('admin/ip/unblock.tpl');
  62. }
  63. public function doUnblock($request, $response, $args)
  64. {
  65. $ip = $request->getParam('ip');
  66. $user = Auth::getUser();
  67. $BIP = BlockIp::where('ip', $ip)->get();
  68. foreach ($BIP as $bi) {
  69. $bi->delete();
  70. }
  71. $UIP = new UnblockIp();
  72. $UIP->userid = $user->id;
  73. $UIP->ip = $ip;
  74. $UIP->datetime = time();
  75. $UIP->save();
  76. $res['ret'] = 1;
  77. $res['msg'] = '发送解封命令解封 ' . $ip . ' 成功';
  78. return $this->echoJson($response, $res);
  79. }
  80. public function ajax_block($request, $response, $args)
  81. {
  82. $datatables = new Datatables(new DatatablesHelper());
  83. $datatables->query('Select blockip.id,node.name,blockip.ip,blockip.ip as location,datetime from blockip,ss_node as node WHERE blockip.nodeid = node.id');
  84. $datatables->edit('datetime', static function ($data) {
  85. return date('Y-m-d H:i:s', $data['datetime']);
  86. });
  87. $iplocation = new QQWry();
  88. $datatables->edit('location', static function ($data) use ($iplocation) {
  89. $location = $iplocation->getlocation($data['location']);
  90. return iconv('gbk', 'utf-8//IGNORE', $location['country'] . $location['area']);
  91. });
  92. $body = $response->getBody();
  93. $body->write($datatables->generate());
  94. }
  95. public function ajax_unblock($request, $response, $args)
  96. {
  97. $datatables = new Datatables(new DatatablesHelper());
  98. $datatables->query('Select unblockip.id,userid,user.user_name,unblockip.ip,unblockip.ip as location,datetime from unblockip,user WHERE unblockip.userid = user.id');
  99. $datatables->edit('datetime', static function ($data) {
  100. return date('Y-m-d H:i:s', $data['datetime']);
  101. });
  102. $iplocation = new QQWry();
  103. $datatables->edit('location', static function ($data) use ($iplocation) {
  104. $location = $iplocation->getlocation($data['location']);
  105. return iconv('gbk', 'utf-8//IGNORE', $location['country'] . $location['area']);
  106. });
  107. $body = $response->getBody();
  108. $body->write($datatables->generate());
  109. }
  110. public function ajax_login($request, $response, $args)
  111. {
  112. $datatables = new Datatables(new DatatablesHelper());
  113. $datatables->query('Select login_ip.id,login_ip.userid,user.user_name,login_ip.ip,login_ip.ip as location,login_ip.datetime,login_ip.type from login_ip,user WHERE login_ip.userid = user.id');
  114. $datatables->edit('datetime', static function ($data) {
  115. return date('Y-m-d H:i:s', $data['datetime']);
  116. });
  117. $iplocation = new QQWry();
  118. $datatables->edit('location', static function ($data) use ($iplocation) {
  119. $location = $iplocation->getlocation($data['location']);
  120. return iconv('gbk', 'utf-8//IGNORE', $location['country'] . $location['area']);
  121. });
  122. $datatables->edit('type', static function ($data) {
  123. return $data['type'] == 0 ? '成功' : '失败';
  124. });
  125. $body = $response->getBody();
  126. $body->write($datatables->generate());
  127. }
  128. public function ajax_alive($request, $response, $args)
  129. {
  130. $datatables = new Datatables(new DatatablesHelper());
  131. $datatables->query('Select alive_ip.id,alive_ip.userid,user.user_name,alive_ip.nodeid,ss_node.name as node_name,alive_ip.ip,alive_ip.ip as location,alive_ip.datetime,alive_ip.id as is_node from alive_ip,user,ss_node WHERE alive_ip.userid = user.id and alive_ip.nodeid = ss_node.id and `datetime` > UNIX_TIMESTAMP() - 60');
  132. $datatables->edit('datetime', static function ($data) {
  133. return date('Y-m-d H:i:s', $data['datetime']);
  134. });
  135. $iplocation = new QQWry();
  136. $datatables->edit('ip', static function ($data) {
  137. return Tools::getRealIp($data['ip']);
  138. });
  139. $datatables->edit('is_node', static function ($data) {
  140. $is_node = Node::where('node_ip', Tools::getRealIp($data['ip']))->first();
  141. if ($is_node) {
  142. return '是';
  143. }
  144. return '否';
  145. });
  146. $datatables->edit('location', static function ($data) use ($iplocation) {
  147. $location = $iplocation->getlocation(Tools::getRealIp($data['location']));
  148. return iconv('gbk', 'utf-8//IGNORE', $location['country'] . $location['area']);
  149. });
  150. $body = $response->getBody();
  151. $body->write($datatables->generate());
  152. }
  153. }