Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
T
token
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
JIRA
JIRA
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wallet
token
Commits
ae5f2846
Commit
ae5f2846
authored
Sep 10, 2020
by
shajiaiming
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'feature/es' into 'master'
ip limit test See merge request
!380
parents
62cf11ee
f549685e
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
51 additions
and
1 deletion
+51
-1
BaseController.php
api/base/BaseController.php
+11
-0
Tools.php
common/components/Tools.php
+28
-0
IpExceptionController.php
console/controllers/IpExceptionController.php
+12
-1
No files found.
api/base/BaseController.php
View file @
ae5f2846
...
...
@@ -8,6 +8,7 @@
namespace
api\base
;
use
common\components\Tools
;
use
Yii
;
use
yii\web\Response
;
use
yii\web\Controller
;
...
...
@@ -77,6 +78,16 @@ class BaseController extends Controller
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
);
return
parent
::
beforeAction
(
$action
);
// TODO: Change the autogenerated stub
...
...
common/components/Tools.php
View file @
ae5f2846
...
...
@@ -179,4 +179,31 @@ class Tools
// Return the array elements
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
console/controllers/IpExceptionController.php
View file @
ae5f2846
...
...
@@ -13,7 +13,6 @@ class IpExceptionController extends Controller
$elastic
=
new
Elastic
(
'106.15.39.94:9200'
);
$params
=
[
'index'
=>
'bwallet_access'
,
'type'
=>
'doc'
,
'body'
=>
[
"size"
=>
0
,
"aggs"
=>
[
...
...
@@ -68,14 +67,25 @@ class IpExceptionController extends Controller
$redis_ticker
=
\Yii
::
$app
->
redis_es
;
$limit
=
\Yii
::
$app
->
params
[
'api_ip_limit'
][
'limit'
];
$white_list
=
\Yii
::
$app
->
params
[
'api_ip_limit'
][
'white_list'
];
$ips
=
''
;
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
.=
$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
;
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment