Commit 3e0891da authored by shajiaiming's avatar shajiaiming

Merge branch 'feature/es' into 'master'

IP 黑名单 See merge request !244
parents 45e413b0 04ff80b1
<?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
[
"range" => [
"@timestamp" => [
#"gte" => 1574922900000,
#"lt" => 1574923200000,
'gte' => ($now - 300) * 1000,
'lt' => $now * 1000,
"format" => "epoch_millis"
......@@ -59,30 +57,25 @@ class IpExceptionController extends Controller
$hits = $response['hits'] ?? null;
$total = $hits['total'];
if(empty($total)){
if (empty($total)) {
return false;
}
$buckets = $response['aggregations']['ips']['buckets'] ?? null;
if(empty($buckets)){
if (empty($buckets)) {
return false;
}
$ips = '';
foreach ($buckets as $key => $val){
if ($val['doc_count'] > 100){
$ips.= $val['key'].',';
$redis_ticker = \Yii::$app->redis_es;
$limit = \Yii::$app->params['api_ip_limit']['limit'];
$white_list = \Yii::$app->params['api_ip_limit']['white_list'];
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;
}
}
\ No newline at end of file
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