Commit 04ff80b1 authored by shajiaiming's avatar shajiaiming

IP 黑名单

parent e390f1b3
<?php
namespace api\controllers;
use Yii;
use api\base\BaseController;
class ValidateController extends BaseController
{
/**
* 黑名单列表
* @return array
*/
public function actionBlackList()
{
$msg = 'ok';
$code = 0;
$redis_ticker = \Yii::$app->redis_es;
$list = $redis_ticker->keys('*');
return ['code' => $code, 'msg' => $msg, 'data' => $list];
}
}
\ No newline at end of file
...@@ -41,8 +41,6 @@ class IpExceptionController extends Controller ...@@ -41,8 +41,6 @@ class IpExceptionController extends Controller
[ [
"range" => [ "range" => [
"@timestamp" => [ "@timestamp" => [
#"gte" => 1574922900000,
#"lt" => 1574923200000,
'gte' => ($now - 300) * 1000, 'gte' => ($now - 300) * 1000,
'lt' => $now * 1000, 'lt' => $now * 1000,
"format" => "epoch_millis" "format" => "epoch_millis"
...@@ -59,29 +57,24 @@ class IpExceptionController extends Controller ...@@ -59,29 +57,24 @@ class IpExceptionController extends Controller
$hits = $response['hits'] ?? null; $hits = $response['hits'] ?? null;
$total = $hits['total']; $total = $hits['total'];
if(empty($total)){ if (empty($total)) {
return false; return false;
} }
$buckets = $response['aggregations']['ips']['buckets'] ?? null; $buckets = $response['aggregations']['ips']['buckets'] ?? null;
if(empty($buckets)){ if (empty($buckets)) {
return false; return false;
} }
$ips = ''; $redis_ticker = \Yii::$app->redis_es;
foreach ($buckets as $key => $val){ $limit = \Yii::$app->params['api_ip_limit']['limit'];
if ($val['doc_count'] > 100){ $white_list = \Yii::$app->params['api_ip_limit']['white_list'];
$ips.= $val['key'].','; foreach ($buckets as $key => $val) {
if (in_array($val['key'], $white_list)) continue;
if ($val['doc_count'] > $limit){
foreach ($val['request']['buckets'] as $bucket) {
$redis_ticker->hmset($val['key'], $bucket['key'], $bucket['doc_count']);
} }
} }
$ips = substr($ips, 0, -1);
if (!empty($ips)){
$file = fopen("http://47.100.212.109/api_ip_limit.txt","w");
$ip_arr = explode(',', $ips);
foreach ($ip_arr as $key => $ip){
fwrite($file,$ip."\n");
}
fclose($file);
} }
return false; return false;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment