VisitController.php
4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?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;
}
}
}