作者 李小龙

客户端源码调整

<?php
/**
* Created by PhpStorm.
* User: zhl
* Date: 2023/10/25
* Time: 14:38
*/
namespace App\Http\Controllers\Api;
use App\Console\Commands\CosService;
use App\Exceptions\TipException;
use App\Http\Controllers\Controller;
use App\Models\Project\Project;
use App\Models\SyncSubmitTask\SyncSubmitTask;
use App\Models\WebSetting\WebSettingFormBack;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
/**
* Class InquiryController
* @package App\Http\Controllers\Api
*/
class InquiryController extends Controller
{
/**
* C端表单提交 询盘信息
* @param Request $request
* @return mixed
*/
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->responseA();
}
@file_put_contents(storage_path('logs/form_submit_' . date('Y-m-d') . '.log'), var_export(date('Y-m-d H:i:s') . "-表单提交数据:" . json_encode($data), true) . PHP_EOL, FILE_APPEND);
//同一ip 5秒内超过5次 限制1小时
$ip = $request->ip();
$lock_key = 'lock_ip_' . $ip;
$rate_key = 'rate_limit:' . $ip;
if(Cache::has($lock_key)){
$this->responseA();
}
if (Cache::has($rate_key)) {
$count = Cache::get($rate_key);
if ($count >= 5) {
Cache::put($lock_key, 1, 3600);
$this->responseA();
}
Cache::increment($rate_key);
} else {
Cache::put($rate_key, 1, 5);
}
try {
$files = $request->allFiles();
foreach ($files as $key => $file) {
$cos = new CosService();
$fileinfo = $cos->checkFile($file);
$fileName = uniqid().rand(10000,99999).'.'.$file->getClientOriginalExtension();
$path = $cos->uploadFile($file, '/inquiry/'. date('Ymd'), $fileName);
$data[$key] = [
'path' => $path,
'original_name' => $fileinfo['name'],
];
}
}catch (TipException $e){
$this->responseA([], 400, $e->getMessage());
}catch (\Exception $e){
$this->responseA([], 400, 'File upload failed');
}
//异步处理
if(!SyncSubmitTask::addTask(SyncSubmitTask::TYPE_INQUIRY, $data)){
$this->responseA([], 400, 'error');
}
$this->responseA($this->inquiryResult());
}
/**
* 收集邮箱或者手机号等其他信息
* @param Request $request
* @return mixed
*/
public function inquiryOtherInfo(Request $request)
{
return $this->inquiry($request);
}
/**
* 起点表单询盘
* @param Request $request
* @return mixed
* @author zbj
* @date 2023/12/8
*/
public function inquiryQd(Request $request){
$data = $request->post();
@file_put_contents(storage_path('logs/form_submit_' . date('Y-m-d') . '.log'), var_export(date('Y-m-d H:i:s') . "-起点表单提交数据:" . json_encode($data), true) . PHP_EOL, FILE_APPEND);
//Name,Email,Phone,Message,submit_ip,submit_time,refer
$submit_ip = $data['submit_ip'] ?? '';
$submit_time = $data['submit_time'] ?? '';
$refer = $data['refer'] ?? '';
unset($data['submit_ip']);
unset($data['submit_time']);
unset($data['refer']);
//异步处理
if(!SyncSubmitTask::addTask(SyncSubmitTask::TYPE_INQUIRY, $data, $submit_ip, $refer, $submit_time)){
$this->responseA([], 400, 'error');
}
$this->responseA($this->inquiryResult());
}
/**
* @return mixed|string[]
* @author zbj
* @date 2023/12/4
*/
protected function inquiryResult(){
$domain = request()->getHost();
$cache_key = 'inquiry_form_back_' . $domain;
$result = Cache::get($cache_key);
if(!$result){
$result = [
'message' => "",
'url' => ""
];
$projectDomain = Project::getProjectByDomain($domain);
$webFormBack = WebSettingFormBack::where("project_id", $projectDomain['project_id']??0)->first();
if (!empty($webFormBack)) {
$result["message"] = $webFormBack->message ?? "";
$result["url"] = $webFormBack->url ?? "";
Cache::put($cache_key, $result, 3600);
}
}
return $result;
}
}
... ... @@ -11,9 +11,8 @@
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Validator;
use ZipArchive;
/**
... ... @@ -52,15 +51,37 @@ protected function error($message = 'error', $status = 400, $data = [])
}
/**
* 响应response
* @param array $data
* @param int $code
* @param string $msg
* @param int $result_code
* @param string $type
*/
public function responseA($data = [], $code = 200, $msg = 'success', $result_code = 200, $type = 'application/json')
{
$result = [
'msg' => $msg,
'code' => $code,
'data' => $data,
];
$header = [
'Content-Type' => $type,
];
$response = response($result, $result_code, $header);
throw new HttpResponseException($response);
}
/**
* 响应
* @param null $msg
* @param string $code
* @param int $code
* @param array $data
* @param int $result_code
* @param string $type
* @return JsonResponse
* @return void
*/
public function response($msg = null,string $code = self::SUCCESS,$data = [],$result_code = 200,$type = 'application/json'): JsonResponse
public function response($msg = null,$code = self::SUCCESS,$data = [],$result_code = 200,$type = 'application/json')
{
$result = [
'msg' => $msg,
... ... @@ -98,8 +119,10 @@ public function curlGet($url){
/**
* A端上传验证代码
* @param Request $request
* @return string
*/
public function uploadVerifyFile(Request $request): string
public function uploadVerifyFile(Request $request)
{
$domain = $request->getHost();
$files = $request->allFiles();
... ... @@ -117,7 +140,7 @@ public function uploadVerifyFile(Request $request): string
* @param Request $request
* @return string
*/
public function uploadAmpVerifyFile(Request $request): string
public function uploadAmpVerifyFile(Request $request)
{
$domain = $request->getHost();
... ... @@ -156,7 +179,6 @@ public function websiteHtml(Request $request){
$requestUrl = $apiUrl."?domain=".$domain."&token=".$token;
@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"]) : "";
... ... @@ -170,9 +192,10 @@ public function websiteHtml(Request $request){
return $this->websiteHtmlHandle($url,$domain);
}
/**
/**
* 网站html解压
* @param $url
* @param $domain
* @return string
*/
public function websiteHtmlHandle($url,$domain)
... ... @@ -237,7 +260,7 @@ public function downLoadFile($url){
* @param $path
* @return bool
*/
public function deleteDirectory($path): bool
public function deleteDirectory($path)
{
if (!is_dir($path)) {
try {
... ... @@ -265,31 +288,156 @@ public function deleteDirectory($path): bool
}
/**
* 客户访问埋点接口
* @param Request $request
* @return JsonResponse
*/
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',
]);
}
//转发data
//埋点成功
return response()->json([
'code' => self::SUCCESS,
'msg' => 'success',
]);
}
/**
* 接口
* @param Request $request
* @return string
*/
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',
]);
}
/**
* C端表单提交 询盘信息
* @param Request $request
* @return mixed
*/
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();
}
//同一ip 5秒内超过5次 限制1小时
$ip = $request->ip();
$lock_key = 'lock_ip_' . $ip;
$rate_key = 'rate_limit:' . $ip;
if(Cache::has($lock_key)){
$this->success();
}
if (Cache::has($rate_key)) {
$count = Cache::get($rate_key);
if ($count >= 5) {
Cache::put($lock_key, 1, 3600);
$this->success();
}
Cache::increment($rate_key);
} else {
Cache::put($rate_key, 1, 5);
}
$data["files"] = $request->allFiles();
//转发data
//返回数据
$res = "";
return $this->responseA($res);
}
/**
* 收集邮箱或者手机号等其他信息
* @param Request $request
* @return mixed
*/
public function inquiryOtherInfo(Request $request)
{
return $this->inquiry($request);
}
/**
* 起点表单询盘
* @param Request $request
* @return void
*/
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);
}
/**
* 搜索
* @param Request $request
*/
public function search(Request $request)
{
$project = $request->get('project');
$data = $request->all();
//获取搜索参数
$searchContent = '';
$data["search_content"] = '';
if (isset($data['s'])) {
$searchContent = $data['s'];
$data["search_content"] = $searchContent = $data['s'];
}
if (isset($data['search'])) {
$searchContent = $data['search'];
$data["search_content"] = $data['search'];
}
$page = 1;
if (isset($data['page']) && (int)$data['page'] > 1) {
$page = (int)$data['page'];
}
$data["page"] = isset($data['page']) && (int)$data['page'] > 1 ? (int)$data['page'] : 1;
// $htmlService = new HtmlService();
//转发data
// return $htmlService->getSearchHtml($project, $data, $searchContent, $page);
}
}
... ...
<?php
/**
* Created by PhpStorm.
* User: zhl
* Date: 2024/1/6
* Time: 16:06
*/
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Repositories\SyncSubmitRepository;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
/**
* Class TrafficController
* @package App\Http\Controllers\Api
*/
class TrafficController extends Controller
{
/**
* 特殊访问引流接口
* @param Request $request
* @return string
*/
public function trafficVisit(Request $request)
{
//获取参数
$data = $request->all();
$ip = $request->input('ip');
$url = $request->input('url');
$device_port = intval($request->input('device_port'));
$referrer_url = $request->input('referrer_url');
$user_agent = $request->input('user_agent');
// FIXME 些日志 观察数据, 完成以后删除代码
file_put_contents(storage_path('logs/trafficVisit' . date('Y-m-d') . '.log'), var_export($data, true));
//验证参数
$validator = Validator::make($data, [
'ip' => 'required',
'url' => 'required',
'user_agent' => 'required',
], [
'ip.required' => 'IP不能为空',
'url.required' => '访问链接不能为空',
'user_agent.required' => '请求头信息user_agent不能为空',
]);
//判断参数是否正确
if ($validator->fails()) {
return $this->error($validator->errors()->first());
}
$result = app(SyncSubmitRepository::class)->trafficVisit($ip, $url, $user_agent, $referrer_url, $device_port, 1);
return $result ? $this->success() : $this->error();
}
}
\ No newline at end of file
<?php
namespace App\Http\Controllers\Cside\Visit;
use App\Enums\Common\Code;
use App\Http\Controllers\Cside\BaseController;
use App\Models\Project\Project;
use App\Models\SyncSubmitTask\SyncSubmitTask;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
class VisitController extends BaseController
{
/**
* 客户访问埋点接口
*/
public function customerVisit(Request $request): \Illuminate\Http\JsonResponse
{
$data = $request->all();
if ($this->filter($request)){
$data = $this->visitInfoHandle($data);
//异步处理
if(!SyncSubmitTask::addTask(SyncSubmitTask::TYPE_VISIT, $data)){
$this->responseA([], 400, 'error');
}
}
//埋点成功
return response()->json([
'code' => Code::SUCCESS_NUM,
'msg' => '客户访问',
]);
}
public function filter($request){
if($request->getClientIp() == "127.0.0.1"){
return false;
}
//判断是否是爬虫
$isReptile = $this->isReptile($request);
if($isReptile){
return false;
}
//是否允许测试环境
$projectDomain = Project::getProjectByDomain($request->getHost());
$project = Project::find($projectDomain['project_id']??0);
if(empty($project)){
return false;
}
// 测试环境返回信息
if (FALSE !== strpos($request->getHost(), 'globalso.site') && !$project->is_record_test_visit) {
return false;
}
return true;
}
/**
* 埋点信息处理
*/
public function visitInfoHandle($data)
{
//referrer
if(preg_match('/google|facebook|bing|yahoo|youtobe|linkedin|messefrankfurt|yandex|tiktok|twitter|instagram|reddit|telegram|pinterest|tumblr/', $data['referrer_url'])){
}else if($data['referrer_url'] == null){
//直访用户
$data['referrer_url'] = "";
}else{
$data['referrer_url'] = "https://www.google.com/";
}
return $data;
}
/**
* 是否是爬虫访问
*/
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;
}
}
}
... ... @@ -9,10 +9,9 @@ class CorsMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
* @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
* @param Request $request
* @param Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
... ...
... ... @@ -10,12 +10,10 @@
class RedirectIfAuthenticated
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
* @param string|null ...$guards
* @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
* @param Request $request
* @param Closure $next
* @param array $guards
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|mixed
*/
public function handle(Request $request, Closure $next, ...$guards)
{
... ...
... ... @@ -13,8 +13,12 @@ class VerifyCsrfToken extends Middleware
*/
protected $except = [
"api/customerVisit",
"api/traffic_visit",
"api/inquiry",
"api/inquiryOtherInfo",
"api/inquiryQd",
"api/upload_verify_file",
"api/upload_amp_verify_file",
"api/website_html_handle",
];
}
... ...
... ... @@ -17,19 +17,21 @@
|
*/
Route::post('/customerVisit',[VisitController::class,'customerVisit']);
//表单创建
Route::post('/traffic_visit',[\App\Http\Controllers\Api\TrafficController::class,'trafficVisit']);
//表单提交
Route::any('/inquiry',[InquiryController::class,'inquiry']);
//收集邮箱或者手机号
Route::any('/inquiryOtherInfo',[InquiryController::class,'inquiryOtherInfo']);
//起点表单
Route::any('/inquiryQd',[InquiryController::class,'inquiryQd']);
//A端上传验证文件
Route::any('/upload_verify_file',[NoticeController::class, 'uploadVerifyFile']);
//A端上传amp站验证文件
Route::any('/upload_amp_verify_file',[NoticeController::class, 'uploadAmpVerifyFile']);
//网站html处理
Route::any('/website_html_handle',[NoticeController::class, 'websiteHtml']);
Route::middleware(['cors'])->group(function () {
//埋点
Route::any('/customerVisit',[NoticeController::class,'customerVisit']);
//表单提交
Route::any('/traffic_visit',[NoticeController::class,'trafficVisit']);
Route::any('/inquiry',[NoticeController::class,'inquiry']);
Route::any('/inquiryQd',[NoticeController::class,'inquiryQd']);
//收集邮箱或者手机号
Route::any('/inquiryOtherInfo',[NoticeController::class,'inquiryOtherInfo']);
//A端上传验证文件
Route::any('/upload_verify_file',[NoticeController::class, 'uploadVerifyFile']);
//A端上传amp站验证文件
Route::any('/upload_amp_verify_file',[NoticeController::class, 'uploadAmpVerifyFile']);
//网站html处理
Route::any('/website_html_handle',[NoticeController::class, 'websiteHtml']);
});
... ...
... ... @@ -14,7 +14,7 @@
|
*/
Route::middleware([ 'cors'])->group(function () {
Route::middleware(['cors'])->group(function () {
//搜索页
Route::get('/search', [NoticeController::class, 'search']);
});
... ...