Commit ae5f2846 authored by shajiaiming's avatar shajiaiming

Merge branch 'feature/es' into 'master'

ip limit test See merge request !380
parents 62cf11ee f549685e
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
namespace api\base; namespace api\base;
use common\components\Tools;
use Yii; use Yii;
use yii\web\Response; use yii\web\Response;
use yii\web\Controller; use yii\web\Controller;
...@@ -77,6 +78,16 @@ class BaseController extends Controller ...@@ -77,6 +78,16 @@ class BaseController extends Controller
Yii::$app->request->setBodyParams($rawBodyParams); Yii::$app->request->setBodyParams($rawBodyParams);
} }
} }
if ('ticker' == $request_controller && 'hot-ticker' == $request_action) {
$realip = Tools::getUserIp();
$ip_list = file_get_contents('http://47.103.119.244:8080/wallet_interface_ip_limit.txt');
$ip_arr = explode("\n", $ip_list);
foreach ($ip_arr as $key => $ip) {
if ($ip == $realip) {
return false;
}
}
}
$this->start = microtime(true); $this->start = microtime(true);
return parent::beforeAction($action); // TODO: Change the autogenerated stub return parent::beforeAction($action); // TODO: Change the autogenerated stub
......
...@@ -179,4 +179,31 @@ class Tools ...@@ -179,4 +179,31 @@ class Tools
// Return the array elements // Return the array elements
return $array; return $array;
} }
/**
* 不同环境下获取真实的IP
*/
public static function getUserIp()
{
// 判断服务器是否允许$_SERVER
if (isset($_SERVER)) {
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$realip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
$realip = $_SERVER['HTTP_CLIENT_IP'];
} else {
$realip = $_SERVER['REMOTE_ADDR'];
}
} else {
// 不允许就使用getenv获取
if (getenv("HTTP_X_FORWARDED_FOR")) {
$realip = getenv("HTTP_X_FORWARDED_FOR");
} elseif (getenv("HTTP_CLIENT_IP")) {
$realip = getenv("HTTP_CLIENT_IP");
} else {
$realip = getenv("REMOTE_ADDR");
}
}
return $realip;
}
} }
\ No newline at end of file
...@@ -13,7 +13,6 @@ class IpExceptionController extends Controller ...@@ -13,7 +13,6 @@ class IpExceptionController extends Controller
$elastic = new Elastic('106.15.39.94:9200'); $elastic = new Elastic('106.15.39.94:9200');
$params = [ $params = [
'index' => 'bwallet_access', 'index' => 'bwallet_access',
'type' => 'doc',
'body' => [ 'body' => [
"size" => 0, "size" => 0,
"aggs" => [ "aggs" => [
...@@ -68,14 +67,25 @@ class IpExceptionController extends Controller ...@@ -68,14 +67,25 @@ class IpExceptionController extends Controller
$redis_ticker = \Yii::$app->redis_es; $redis_ticker = \Yii::$app->redis_es;
$limit = \Yii::$app->params['api_ip_limit']['limit']; $limit = \Yii::$app->params['api_ip_limit']['limit'];
$white_list = \Yii::$app->params['api_ip_limit']['white_list']; $white_list = \Yii::$app->params['api_ip_limit']['white_list'];
$ips = '';
foreach ($buckets as $key => $val) { foreach ($buckets as $key => $val) {
if (in_array($val['key'], $white_list)) continue; if (in_array($val['key'], $white_list)) continue;
if ($val['doc_count'] > $limit){ if ($val['doc_count'] > $limit){
foreach ($val['request']['buckets'] as $bucket) { foreach ($val['request']['buckets'] as $bucket) {
$redis_ticker->hmset($val['key'], $bucket['key'], $bucket['doc_count']); $redis_ticker->hmset($val['key'], $bucket['key'], $bucket['doc_count']);
} }
$ips.= $val['key'].',';
} }
} }
$ips = substr($ips, 0, -1);
if (!empty($ips)){
$file = fopen("/data_wallet/static/wallet_interface_ip_limit.txt","w");
$ip_arr = explode(',', $ips);
foreach ($ip_arr as $key => $ip){
fwrite($file,$ip."\n");
}
fclose($file);
};
return false; 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