作者 李小龙

C端客户端接口转发

... ... @@ -8,6 +8,7 @@
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Models\SyncSubmitTask\SyncSubmitTask;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
... ... @@ -24,6 +25,12 @@ class NoticeController extends Controller
const SUCCESS = 200;
const ERROR = 400;
const SERVERERROR = 500;
const TYPEVISIT = "visit";
const TYPEINQUIRY = "inquiry";
const TRAFFICZERO = 0;
const TRAFFICONE = 1;
const DEVICE_PORT_ONE = 1;
const DEVICE_PORT_TWO = 2;
protected $header = [];//设置请求头参数
/**
... ... @@ -110,7 +117,7 @@ public function curlGet($url){
curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch1, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch1, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch1, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch1, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch1, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
$access_txt = curl_exec($ch1);
curl_close($ch1);
... ... @@ -118,6 +125,45 @@ public function curlGet($url){
}
/**
* 发送http post请求
* @param $url
* @param $data
* @param array $header
* @param bool $is_json
* @return mixed|string
*/
function httpPost($url, $data, $header = [],$is_json = true)
{
if (empty($header)) {
$header = array(
"Accept: application/json",
"Content-Type:application/json;charset=utf-8",
);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$res = curl_exec($ch);
if (curl_errno($ch)) {
$error_message = curl_error($ch);
@file_put_contents(storage_path('logs/error.log'), var_export($error_message, true) . PHP_EOL, FILE_APPEND);
}
curl_close($ch);
if($is_json){
return json_decode($res, true);
}
return trim($res);
}
/**
* A端上传验证代码
* @param Request $request
* @return string
... ... @@ -145,7 +191,7 @@ public function uploadAmpVerifyFile(Request $request)
$domain = $request->getHost();
$domain_array = parse_url($domain);
$host = $domain_array['host'] ?? $domain_array['path'];
$host = $domain_array['host'] ? $domain_array['path'] : "";
$host_array = explode('.',$host);
if(count($host_array) <= 2){
array_unshift($host_array,'m');
... ... @@ -288,32 +334,159 @@ public function deleteDirectory($path)
}
/**
* 提交
* @param $request
* @param string $type
* @param int $traffic
* @param array $files
* @return string
*/
public function transmit($request, $type = self::TYPEVISIT, $traffic = self::TRAFFICZERO,$files = []){
if($request->getClientIp() == "127.0.0.1"){
return $this->success();
}
//判断是否是爬虫
$isReptile = $this->isReptile($request);
if($isReptile){
return $this->success();
}
$data = $request->all();
if (empty($data)){
return $this->success();
}
$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();
$req["ip"] = $request->getClientIp();
$req["user_agent"] = $request->userAgent();
$req["files"] = $files;
$req["type"] = $type;
$req["traffic"] = $traffic;
//转发接口
$transmitUrl = env("TRANSMIT_URL");
return $this->httpPost($transmitUrl,json_encode($req));
}
/**
* 处理user_agent
* @param $user_agent
* @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])){
$device_port = self::DEVICE_PORT_ONE;
}
// 头信息中带有这些信息, 代表是手机端, 重置设备类型
if (preg_match('/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini|Mobile|wap|windowsce|ucweb/', $user_agent)) {
$device_port = self::DEVICE_PORT_TWO;
}
return $device_port;
}
/**
* 客户访问埋点接口
* @param Request $request
* @return JsonResponse
* @return string
*/
public function customerVisit(Request $request)
{
$data = $request->all();
$data["referrer_url"] = !empty($data["referrer_url"]) ? $data["referrer_url"] : "";
$data["url"] = !empty($data["url"]) ? $data["url"] : "";
$data["domain"] = !empty($data["domain"]) ? $data["domain"] : "";
$data["ip"] = $request->getClientIp();
$data["user_agent"] = $request->userAgent();
if (empty($data["referrer_url"]) || empty($data["url"]) || empty($data["domain"])){
return response()->json([
'code' => self::SUCCESS,
'msg' => 'success',
]);
}
return $this->transmit($request);
}
//转发data
/**
* 埋点信息处理
* @param $referrerUrl
* @return string
*/
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){
//直访用户
$referrerUrl = "";
}else{
$referrerUrl = "https://www.google.com/";
}
return $referrerUrl;
}
//埋点成功
return response()->json([
'code' => self::SUCCESS,
'msg' => 'success',
]);
/**
* 是否是爬虫访问
*/
public function isReptile($request): bool
{
$agent = $request->header('User-Agent');
if (!empty($agent)) {
$spiderSite= array(
"TencentTraveler",
"Baiduspider+",
"BaiduGame",
"Googlebot",
"msnbot",
"Sosospider+",
"Sogou web spider",
"ia_archiver",
"Yahoo! Slurp",
"YoudaoBot",
"Yahoo Slurp",
"MSNBot",
"Java (Often spam bot)",
"BaiDuSpider",
"Voila",
"Yandex bot",
"BSpider",
"twiceler",
"Sogou Spider",
"Speedy Spider",
"Google AdSense",
"Heritrix",
"Python-urllib",
"Alexa (IA Archiver)",
"Ask",
"Exabot",
"Custo",
"OutfoxBot/YodaoBot",
"yacy",
"SurveyBot",
"legs",
"lwp-trivial",
"Nutch",
"StackRambler",
"The web archive (IA Archiver)",
"Perl tool",
"MJ12bot",
"Netcraft",
"MSIECrawler",
"WGet tools",
"larbin",
"Fish search",
"yandex.com/bots",
"google.com/bot",
"bingbot",
"YandexMobileBot",
"BingPreview",
"AhrefsBot",
"bot"
);
$flag = 0;
foreach($spiderSite as $val) {
$str = strtolower($val);
if (strpos($agent, $str) !== false) {
$flag = 1;
}
}
if($flag == 1){
return true;
}else{
return false;
}
} else {
return false;
}
}
/**
... ... @@ -323,40 +496,17 @@ public function customerVisit(Request $request)
*/
public function trafficVisit(Request $request)
{
//获取参数
$data = $request->all();
$data["id"] = $request->input('ip');
$data["url"] = $request->input('url');
$data["device_port"] = intval($request->input('device_port'));
$data["referrer_url"] = $request->input('referrer_url');
$data["user_agent"] = $request->input('user_agent');
if (empty($data["id"]) || empty($data["url"]) || empty($data["referrer_url"]) || empty($data["user_agent"])){
return response()->json([
'code' => self::SUCCESS,
'msg' => 'success',
]);
}
//转发data
//成功
return response()->json([
'code' => self::SUCCESS,
'msg' => 'success',
]);
return $this->transmit($request,self::TYPEVISIT,self::TRAFFICONE);
}
/**
* C端表单提交 询盘信息
* @param Request $request
* @return mixed
* @return string
*/
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)){
$this->success();
... ... @@ -381,40 +531,26 @@ public function inquiry(Request $request)
}
$data["files"] = $request->allFiles();
//转发data
//返回数据
$res = "";
return $this->responseA($res);
return $this->transmit($request,self::TYPEINQUIRY,self::TRAFFICZERO,$data["files"]);
}
/**
* 收集邮箱或者手机号等其他信息
* @param Request $request
* @return mixed
* @return string
*/
public function inquiryOtherInfo(Request $request)
{
return $this->inquiry($request);
return $this->transmit($request,self::TYPEINQUIRY);
}
/**
* 起点表单询盘
* @param Request $request
* @return void
* @return string
*/
public function inquiryQd(Request $request){
$data = $request->post();
$data['submit_ip'] = $data['submit_ip'] ? $data['submit_ip'] : '';
$data['submit_time'] = $data['submit_time'] ? $data['submit_time'] : '';
$data['refer'] = $data['refer'] ? $data['refer'] : '';
//转发data
//返回数据
$res = "";
return $this->responseA($res);
return $this->transmit($request,self::TYPEINQUIRY);
}
/**
... ... @@ -434,6 +570,7 @@ public function search(Request $request)
$data["search_content"] = $data['search'];
}
//分页
$data["page"] = isset($data['page']) && (int)$data['page'] > 1 ? (int)$data['page'] : 1;
//转发data
... ...
<?php
use App\Http\Controllers\Api\InquiryController;
use App\Http\Controllers\Api\NoticeController;
use App\Http\Controllers\Cside\PageController;
use App\Http\Controllers\Cside\Visit\VisitController;
use Illuminate\Support\Facades\Route;
/*
... ...