123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- <?php
- define('IN_WEB', true);
- include(dirname(__FILE__) . "/../lib/ProtocolsEvent.php");//解码
- $str = $_REQUEST['param'] ?? null;
- if(isset($str)&&!empty($str)){
- ProtocolsEvent::apiDecode($str);
- }
- include (dirname(__FILE__)."/../com.php");//核心
- if(IS_DEBUF){
- $origin = isset($_SERVER['HTTP_ORIGIN'])? $_SERVER['HTTP_ORIGIN'] : '';
- $allow_origin = array('http://wiki.superant.cc','http://apiwiki.superant.cc');
- if(in_array($origin,$allow_origin)){
- header('Access-Control-Allow-Origin:'.$origin);
- }else{
- header('Access-Control-Allow-Origin:*');//允许跨域
- }
- }else{
- header('Access-Control-Allow-Origin:*');//允许跨域
- }
- class gateway {
- public function __construct($aRequest) {
- //$calStart = oo::timeReleased();
- if(isset($aRequest['saToken']) && isset($aRequest['uid'])) {
- if(!isset($aRequest['saToken'])) {
- die(json_encode(['code'=>errorCode::UNAUTHORIZED_REQUEST,'msg'=>'token empty']));
- }else{
- $token = oo::commonOprRedis('common')->get(okeys::UserToken($aRequest['uid']));
- if($token != $aRequest['saToken']){
- oo::logs()->debug3(["ts"=>date("H:i:s"),"uid"=>$aRequest['uid'],'cacheToke'=>$token,'postToken'=>$aRequest['saToken']],'tokenerr.log');
- die(json_encode(['code'=>errorCode::API_TIME_EXPIRED,'msg'=>'token error']));
- }
- }
- }else if(isset($aRequest['uid'])){
- oo::logs()->debug3(["ts"=>date("H:i:s"),"uid"=>$aRequest['uid'],'postToken'=>$aRequest['saToken']],'tokenerr2.log');
- }
- if (!is_array($aRequest)) {
- exit('request is wrong.');
- }
- $aRequest['version'] = isset($aRequest['version']) ? $aRequest['version'] : '1.0.0';
- if (!empty($aRequest['mod'])) {
- $class = preg_replace("/[^a-zA-Z]/", '', trim($aRequest['mod']));
- $class = ucfirst($class);
- } else {
- $class = 'Main';
- }
- /* 获得 act 名 */
- if ( ! empty($aRequest['act'])) {
- $function = preg_replace("/[^a-zA-Z]/", '', trim($aRequest['act']));
- if ($function == 'list') {
- $function = 'listFunction';
- } elseif ( $function == 'main') {
- $function = 'mainFunction';
- }
- } else {
- $function = 'page';
- }
- if(!empty($aRequest['frombg'])){
- $apiFile = 'background';
- $ip = oo::getIp();
- if(!in_array($ip,oo::$config['adminIpList'])){
- die(json_encode(['code'=>-1,'ip'=>$ip,'msg'=>'IP ERROR']));
- }
- }else{
- $apiFile = 'services';
- if(isset($aRequest['superKey'])) {
- $ret = oo::commonOprRedis('statistics')->get(okeys::superKeys($aRequest['uid'],$class,$function,$aRequest['superKey']));
- if($ret){
- echo $ret;
- exit(0);
- }
- }
- if(isset($aRequest['service'])){
- $apiFile = 'services_2';
- }
- }
- $file = WWWROOT . 'api/'.$apiFile.'/' . $class . '.php';
- if (!is_file($file)) {
- exit('no file services.');
- }
- include $file;
- $obj = new $class();
- if (!method_exists($obj, $function)) {
- exit('no function:'.$function.'in '.$class.$apiFile);
- }
- $ts1 = oo::getMsectime();
- //奖励等部分接口请求频率限制
- $lock = true;
- if(in_array($apiFile, ['services', 'services_2'])){
- //$confs = oo::cfg('cmdlimit');
- //$lock = true;
- //if(!empty($confs[$apiFile]) && in_array((string)$aRequest['mod'].'.'.(string)$aRequest['act'], $confs[$apiFile])){
- // $lock = funs::concurrentLock($aRequest['uid'], $apiFile.(string)$aRequest['mod'].(string)$aRequest['act'], 1);
- //}
- $lock = funs::concurrentLock($aRequest['uid'], $apiFile.(string)$aRequest['mod'].(string)$aRequest['act'], 1);
- if(!$lock){ //高频请求
- usleep(100000); //延迟100毫秒
- }
- }
- $result = $obj->$function($aRequest);
- echo $result;
- if(!$lock){ //高频请求记录详细信息,分析客户重复请求行为
- !in_array($aRequest['mod'].'_'.$aRequest['act'],['Level_build','Goods_list']) && oo::logs()->debug3(['req' => $aRequest, 'rsp' => $result], 'frequentapi.log');
- //exit(oo::response(errorCode::API_FREQUENT_REQUESTS));
- }
- $ts2 = oo::getMsectime();
- if(isset($aRequest['superKey'])) {
- oo::commonOprRedis('statistics')->setex(okeys::superKeys($aRequest['uid'],$class,$function,$aRequest['superKey']),$result,60);
- $lastApi=["mod_act"=>$aRequest['mod'].'_'.$aRequest['act'],"request"=>$aRequest,"result"=>$result,"ts1"=>$ts1,"ts2"=>$ts2];
- oo::commonOprRedis('statistics')->setex(okeys::lastApiReturn($aRequest['uid']),json_encode($lastApi),60*10);//记录最后请求接口
- }
- if(!in_array($apiFile, ['services', 'services_2'])){
- exit(0);
- }
- if($ts2-$ts1>2000){//超时长接口
- oo::logs()->debug3(['time'=>date("Y-m-d H:i:s"),"uid"=>$aRequest['uid'],"expend_ts"=>($ts2-$ts1),"mod"=>$aRequest['mod'],"act"=>$aRequest['act']], 'expend_ts_http.log');
- }
- exit(0);
- }
- }
- //$_REQUEST = json_decode(file_get_contents('php://input'), true);
- new gateway($_REQUEST);
|