作者 刘锟

update

... ... @@ -5,6 +5,7 @@
* Date: 2023/09/02
* Time: 10:36
*/
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
... ... @@ -66,7 +67,7 @@ protected function error($message = 'error', $status = 400, $data = [])
* @param string $type
* @return void
*/
public function response($msg = null,$code = self::SUCCESS,$data = [],$result_code = 200,$type = 'application/json')
public function response($msg = null, $code = self::SUCCESS, $data = [], $result_code = 200, $type = 'application/json')
{
$result = [
'msg' => $msg,
... ... @@ -74,7 +75,7 @@ public function response($msg = null,$code = self::SUCCESS,$data = [],$result_co
'data' => $data,
];
$this->header['Content-Type'] = $type;
$response = response($result,$result_code,$this->header);
$response = response($result, $result_code, $this->header);
throw new HttpResponseException($response);
}
... ... @@ -83,8 +84,9 @@ public function response($msg = null,$code = self::SUCCESS,$data = [],$result_co
* @param $url
* @return mixed
*/
public function curlGet($url){
$ch1 = curl_init();
public function curlGet($url)
{
$ch1 = curl_init();
$timeout = 0;
curl_setopt($ch1, CURLOPT_URL, $url);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
... ... @@ -110,14 +112,14 @@ public function curlGet($url){
* @param bool $is_json
* @return mixed|string
*/
function httpPost($url, $data, $header = [],$is_json = true)
function httpPost($url, $data, $header = [], $is_json = true)
{
if (empty($header)) {
$header = array(
"Accept: application/json",
"Content-Type:application/json;charset=utf-8",
"token:".env("SECRET_TOKEN"),
"pid:".env("MERCHANT_NUMBER")
"token:" . env("SECRET_TOKEN"),
"pid:" . env("MERCHANT_NUMBER")
);
}
$ch = curl_init();
... ... @@ -137,7 +139,7 @@ function httpPost($url, $data, $header = [],$is_json = true)
@file_put_contents(storage_path('logs/error.log'), var_export($error_message, true) . PHP_EOL, FILE_APPEND);
}
curl_close($ch);
if($is_json){
if ($is_json) {
return json_decode($res, true);
}
return trim($res);
... ... @@ -148,7 +150,7 @@ function httpPost($url, $data, $header = [],$is_json = true)
* @param Request $request
* @return string
*/
public function uploadVerifyFile(Request $request)
public function uploadVerifyFile(Request $request)
{
$domain = $request->getHost();
$files = $request->allFiles();
... ... @@ -156,7 +158,7 @@ public function uploadVerifyFile(Request $request)
$destinationPath = public_path($domain);
$file->move($destinationPath, $file->getClientOriginalName());
$filePath = $destinationPath . '/' . $file->getClientOriginalName();
@file_put_contents(storage_path('logs/upload_file.log'), date('Y-m-d H:i:s') . " ".$filePath . PHP_EOL, FILE_APPEND);
@file_put_contents(storage_path('logs/upload_file.log'), date('Y-m-d H:i:s') . " " . $filePath . PHP_EOL, FILE_APPEND);
}
return $this->success();
}
... ... @@ -172,20 +174,20 @@ public function uploadAmpVerifyFile(Request $request)
$domain_array = parse_url($domain);
$host = $domain_array['host'] ? $domain_array['path'] : "";
$host_array = explode('.',$host);
if(count($host_array) <= 2){
array_unshift($host_array,'m');
}else{
$host_array = explode('.', $host);
if (count($host_array) <= 2) {
array_unshift($host_array, 'm');
} else {
$host_array[0] = 'm';
}
$amp_domain = implode('.',$host_array);
$amp_domain = implode('.', $host_array);
$files = $request->allFiles();
foreach ($files as $file) {
$destinationPath = public_path($amp_domain);
$file->move($destinationPath, $file->getClientOriginalName());
$filePath = $destinationPath . '/' . $file->getClientOriginalName();
@file_put_contents(storage_path('logs/upload_file.log'), date('Y-m-d H:i:s') . " ".$filePath . PHP_EOL, FILE_APPEND);
@file_put_contents(storage_path('logs/upload_file.log'), date('Y-m-d H:i:s') . " " . $filePath . PHP_EOL, FILE_APPEND);
}
return $this->success();
}
... ... @@ -196,25 +198,36 @@ public function uploadAmpVerifyFile(Request $request)
* @param Request $request
* @return string
*/
public function websiteHtml(Request $request){
public function websiteHtml(Request $request)
{
$domain = $request->getHost();
$site_token = $request->input('site_token');
$token = env("SECRET_TOKEN");
$pid = env("MERCHANT_NUMBER");
$apiUrl = env("API_URL");
$requestUrl = $apiUrl."api/get_url_verify_token/?pid=".$pid."&token=".$token."&domain=".$domain;
@file_put_contents(storage_path('logs/notify_get_url.log'), date('Y-m-d H:i:s') . "接收到通知:". $requestUrl . PHP_EOL, FILE_APPEND);
if (empty($site_token)) {
return $this->error('请求无效');
}
if ($site_token != $token) {
return $this->error('请求无效');
}
$requestUrl = $apiUrl . "api/get_url_verify_token/?pid=" . $pid . "&token=" . $token . "&domain=" . $domain;
@file_put_contents(storage_path('logs/notify_get_url.log'), date('Y-m-d H:i:s') . "接收到通知:" . $requestUrl . PHP_EOL, FILE_APPEND);
try {
$res = $this->curlGet($requestUrl);
$url = isset($res["data"]["url"]) && !empty($res["data"]["url"]) ? urldecode($res["data"]["url"]) : "";
if ($res["status"] != self::SUCCESS || $url == ""){
if ($res["status"] != self::SUCCESS || $url == "") {
$msg = isset($res["message"]) && !empty($res["message"]) ? $res["message"] : "请求失败!";
return $this->error($msg);
}
} catch (\Exception $e) {
return $this->error($e->getMessage());
}
return $this->websiteHtmlHandle($url,$domain);
return $this->websiteHtmlHandle($url, $domain);
}
/**
... ... @@ -223,46 +236,47 @@ public function websiteHtml(Request $request){
* @param $domain
* @return string
*/
public function websiteHtmlHandle($url,$domain)
{
$pathInfo = pathinfo($url);
$extension = $pathInfo['extension'];
//只允许解压zip格式文件
if (in_array($extension, ["zip"])) {
try {
$targetFile = $this->downLoadFile($url);
$zip = new ZipArchive();
if ($zip->open($targetFile) === TRUE) {
$outputFolder = public_path($domain);
if (!is_dir($outputFolder)) {
mkdir($outputFolder, 0777, true);
public function websiteHtmlHandle($url, $domain)
{
$pathInfo = pathinfo($url);
$extension = $pathInfo['extension'];
//只允许解压zip格式文件
if (in_array($extension, ["zip"])) {
try {
$targetFile = $this->downLoadFile($url);
$zip = new ZipArchive();
if ($zip->open($targetFile) === TRUE) {
$outputFolder = public_path($domain);
if (!is_dir($outputFolder)) {
mkdir($outputFolder, 0777, true);
}
// 解压缩文件,保留原文件结构
$zip->extractTo($outputFolder);
$zip->close();
$this->deleteDirectory($targetFile);
$transmitUrl = env("TRANSMIT_URL");
$this->httpPost($transmitUrl . "api/selfSiteNotify/", json_encode(['domain' => $domain]));
} else {
return $this->error('解压失败!');
// 处理打开压缩文件失败的情况
}
// 解压缩文件,保留原文件结构
$zip->extractTo($outputFolder);
$zip->close();
$this->deleteDirectory($targetFile);
$transmitUrl = env("TRANSMIT_URL");
$this->httpPost($transmitUrl."api/selfSiteNotify/",json_encode(['domain'=>$domain]));
} else {
return $this->error('解压失败!');
// 处理打开压缩文件失败的情况
} catch (\Exception $e) {
return $this->error($e->getMessage());
}
} catch (\Exception $e) {
return $this->error($e->getMessage());
} else {
return $this->error('不允许解压改格式压缩包!');
}
}else{
return $this->error('不允许解压改格式压缩包!');
return $this->success();
}
return $this->success();
}
/**
* 下载文件(下载到public目录下)
* @param $url
* @return string
*/
public function downLoadFile($url){
public function downLoadFile($url)
{
$savePath = public_path();
if (!file_exists($savePath)) {
mkdir($savePath, 0777, true);
... ... @@ -276,7 +290,7 @@ public function downLoadFile($url){
$fileContent = @file_get_contents($url, false, $context);
if ($fileContent !== false) {
file_put_contents($targetFile, $fileContent);
return $targetFile;
return $targetFile;
} else {
return "";
}
... ... @@ -322,21 +336,22 @@ public function deleteDirectory($path)
* @param int $traffic
* @return string
*/
public function transmit($request, $type = self::TYPEVISIT, $traffic = self::TRAFFICZERO){
if($request->getClientIp() == "127.0.0.1"){
public function transmit($request, $type = self::TYPEVISIT, $traffic = self::TRAFFICZERO)
{
if ($request->getClientIp() == "127.0.0.1") {
return $this->success();
}
//判断是否是爬虫
$isReptile = $this->isReptile($request);
if($isReptile){
if ($isReptile) {
return $this->success();
}
$data = $request->all();
if (empty($data)){
if (empty($data)) {
return $this->success();
}
$data["device_port"] = $this->userAgentHandle( $request->userAgent(),$data["device_port"] ?? self::DEVICE_PORT_ONE);
$data["device_port"] = $this->userAgentHandle($request->userAgent(), $data["device_port"] ?? self::DEVICE_PORT_ONE);
$req["data"] = $data;
$req["referer"] = $this->visitInfoHandle($data["referrer_url"] ?? $request->header('Referer'));
$req["domain"] = $request->getHost();
... ... @@ -349,13 +364,13 @@ public function transmit($request, $type = self::TYPEVISIT, $traffic = self::TRA
//转发接口
$transmitUrl = env("TRANSMIT_URL");
$resp = $this->httpPost($transmitUrl."api/selfSiteApi/",json_encode($req));
if ($resp["status"] != self::SUCCESS){
$resp = $this->httpPost($transmitUrl . "api/selfSiteApi/", json_encode($req));
if ($resp["status"] != self::SUCCESS) {
$dataJson = json_encode($req);
if (!file_exists(storage_path('logs/fail_req'))){
mkdir(storage_path('logs/fail_req',0777,true));
if (!file_exists(storage_path('logs/fail_req'))) {
mkdir(storage_path('logs/fail_req', 0777, true));
}
@file_put_contents(storage_path('logs/fail_req/'.date('Y-m-d').'.log'), var_export($dataJson, true) . PHP_EOL, FILE_APPEND);
@file_put_contents(storage_path('logs/fail_req/' . date('Y-m-d') . '.log'), var_export($dataJson, true) . PHP_EOL, FILE_APPEND);
}
return $this->success();
}
... ... @@ -366,8 +381,9 @@ public function transmit($request, $type = self::TYPEVISIT, $traffic = self::TRA
* @param $device_port
* @return int
*/
public function userAgentHandle($user_agent,$device_port){
if (!in_array($device_port,[self::DEVICE_PORT_ONE,self::DEVICE_PORT_TWO])){
public function userAgentHandle($user_agent, $device_port)
{
if (!in_array($device_port, [self::DEVICE_PORT_ONE, self::DEVICE_PORT_TWO])) {
$device_port = self::DEVICE_PORT_ONE;
}
// 头信息中带有这些信息, 代表是手机端, 重置设备类型
... ... @@ -394,11 +410,11 @@ public function customerVisit(Request $request)
*/
public function visitInfoHandle($referrerUrl)
{
if(preg_match('/google|facebook|bing|yahoo|youtobe|linkedin|messefrankfurt|yandex|tiktok|twitter|instagram|reddit|telegram|pinterest|tumblr/',$referrerUrl)){
}else if($referrerUrl == null){
if (preg_match('/google|facebook|bing|yahoo|youtobe|linkedin|messefrankfurt|yandex|tiktok|twitter|instagram|reddit|telegram|pinterest|tumblr/', $referrerUrl)) {
} else if ($referrerUrl == null) {
//直访用户
$referrerUrl = "";
}else{
} else {
$referrerUrl = "https://www.google.com/";
}
return $referrerUrl;
... ... @@ -413,7 +429,7 @@ public function isReptile($request)
{
$agent = $request->header('User-Agent');
if (!empty($agent)) {
$spiderSite= array(
$spiderSite = array(
"TencentTraveler",
"Baiduspider+",
"BaiduGame",
... ... @@ -465,15 +481,15 @@ public function isReptile($request)
"bot"
);
$flag = 0;
foreach($spiderSite as $val) {
foreach ($spiderSite as $val) {
$str = strtolower($val);
if (strpos($agent, $str) !== false) {
$flag = 1;
}
}
if($flag == 1){
if ($flag == 1) {
return true;
}else{
} else {
return false;
}
} else {
... ... @@ -488,7 +504,7 @@ public function isReptile($request)
*/
public function trafficVisit(Request $request)
{
return $this->transmit($request,self::TYPEVISIT,self::TRAFFICONE);
return $this->transmit($request, self::TYPEVISIT, self::TRAFFICONE);
}
/**
... ... @@ -500,7 +516,7 @@ public function inquiry(Request $request)
{
$data = $request->all();
$black_ips = ['203.86.233.27', '37.19.221.202'];
if(in_array(request()->getClientIp(), $black_ips)){
if (in_array(request()->getClientIp(), $black_ips)) {
$this->success();
}
... ... @@ -508,7 +524,7 @@ public function inquiry(Request $request)
$ip = $request->ip();
$lock_key = 'lock_ip_' . $ip;
$rate_key = 'rate_limit:' . $ip;
if(Cache::has($lock_key)){
if (Cache::has($lock_key)) {
$this->success();
}
if (Cache::has($rate_key)) {
... ... @@ -523,7 +539,7 @@ public function inquiry(Request $request)
}
$data["files"] = $request->allFiles();
return $this->transmit($request,self::TYPEINQUIRY,self::TRAFFICZERO);
return $this->transmit($request, self::TYPEINQUIRY, self::TRAFFICZERO);
}
/**
... ... @@ -533,7 +549,7 @@ public function inquiry(Request $request)
*/
public function inquiryOtherInfo(Request $request)
{
return $this->transmit($request,self::TYPEINQUIRY);
return $this->transmit($request, self::TYPEINQUIRY);
}
/**
... ... @@ -541,8 +557,9 @@ public function inquiryOtherInfo(Request $request)
* @param Request $request
* @return string
*/
public function inquiryQd(Request $request){
return $this->transmit($request,self::TYPEINQUIRY);
public function inquiryQd(Request $request)
{
return $this->transmit($request, self::TYPEINQUIRY);
}
/**
... ... @@ -557,7 +574,7 @@ public function search(Request $request)
//获取搜索参数
$data["search_content"] = '';
if (isset($data['s'])) {
$req["search_content"] = $data['s'];
$req["search_content"] = $data['s'];
}
if (isset($data['search'])) {
$req["search_content"] = $data['search'];
... ... @@ -569,7 +586,7 @@ public function search(Request $request)
//转发data
$transmitUrl = env("API_URL");
$resp = $this->httpPost($transmitUrl."api/get_search_content/",json_encode($req));
$resp = $this->httpPost($transmitUrl . "api/get_search_content/", json_encode($req));
return $resp["data"]["html"];
}
}
... ...