SyncSubmitTaskService.php
23.1 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
<?php
namespace App\Services;
use App\Exceptions\InquiryFilterException;
use App\Models\Com\WordCountry;
use App\Models\Domain\CountryCode;
use App\Models\Domain\DomainInfo;
use App\Models\Inquiry\InquiryForm;
use App\Models\Inquiry\InquiryFormData;
use App\Models\Inquiry\InquiryRelateDomain;
use App\Models\Project\AutoEmail;
use App\Models\Project\AutoEmailLog;
use App\Models\Project\InquiryFilterConfig;
use App\Models\Project\Project;
use App\Models\RouteMap\RouteMap;
use App\Models\Subscribe\Email;
use App\Models\SyncSubmitTask\SyncSubmitTask;
use App\Models\Visit\Visit;
use App\Models\WebSetting\WebLanguage;
use App\Models\Workchat\MessagePush;
use App\Utils\LogUtils;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Str;
use function Symfony\Component\String\s;
/**
* Class SyncSubmitTaskService
* @package App\Services
* @author zbj
* @date 2023/12/4
*/
class SyncSubmitTaskService
{
/**
* @param $task
* @return mixed
* @throws InquiryFilterException
* @author zbj
* @date 2023/11/28
*/
public static function handler($task, $date = '', $relate_domain = '')
{
$data = $task['data'];
//有关联域名 替换原数据url
// if($relate_domain){
// $domain = $data['domain'];
// foreach ($data as $k=>&$item){
// if($k == 'data'){
// foreach ($item as &$v){
// $v = str_replace($domain, $relate_domain, $v);
// }
// }else{
// $item = str_replace($domain, $relate_domain, $item);
// }
// }
// }
$checkIpCountry = self::checkIpCountry($data['domain'], $data['ip'], $task['type']);
$data['ip'] = $checkIpCountry['ip'];
$data['country'] = $checkIpCountry['country'];
$data['submit_at'] = $task['created_at'];
$project = $checkIpCountry['project'];
$data['project_id'] = $project['id'];
if(empty($data['data']['url']) && $task['type'] == 'visit'){
throw new InquiryFilterException( 'url为空');
}
//特殊处理
if($project['id'] == 455 && !empty($data['email']) && $data['email'] == 'alb@marketingtu.org'){
throw new InquiryFilterException( '黑名单邮箱');
}
//过滤url参数 清除全部参数
if($project['id'] == 1442 && !empty($data['data']['url'])){
$query = parse_url($data['data']['url'], PHP_URL_QUERY);
if($query){
$data['data']['url'] = str_replace('?'.$query, '', $data['data']['url']);
}
}
// 项目ID:448, 过滤被刷访问
if ($project['id'] == 448 && $task['type'] == 'visit') {
if ($data['country'] == '约旦' && empty($data['data']['referrer_url'])) {
$url_array = parse_url($data['data']['url']);
$path_array = explode('/', $url_array['path']);
if (FALSE == empty($path_array[1]) && strlen($path_array[1]) <= 3){
throw new InquiryFilterException( '过滤被刷访问');
}
}
}
//$data['data']['referrer_url']为空 && 访问小语种 && 访问国家和IP所在国家不匹配 不记录数据
//$data['data']['referrer_url']为空 && 任何小语种 美国都不记
if (empty($data['data']['referrer_url']) && $task['type'] == 'visit') {
$tran_visit = '';
//是否是小语种 二级域名或二级目录
$langs = WebLanguage::getShorts();
foreach ($langs as $lang){
if(Str::contains($data['data']['url'], ['/' . $lang . '.', '/' . $lang . '/'])) {
$tran_visit = $lang;
break;
}
}
//直接根据 语种缩写 去匹配 国家简码 不搞麻烦了,将就用
$tran_country = WordCountry::getCountryByIso2($tran_visit)['chinese_name'] ?? '';
if ($tran_visit && $tran_visit !='en' && ($checkIpCountry['country'] == '美国' || $tran_country != $checkIpCountry['country'])) {
throw new InquiryFilterException( '判定为蜘蛛爬取小语种');
}
//2024-04-14 关杰: https://www.vogi-dcs.com/ 项目 没有访问来源的,且ua是一下两个,跳过
//Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
//Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
$ua = [
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36'
];
if($project['id'] == 3216 && in_array($data['user_agent'], $ua)){
throw new InquiryFilterException( '判定为蜘蛛爬取小语种');
}
}
//域名 过滤国家或ip
$domain_info = DomainInfo::getCacheInfoByProjectId($project['id']);
if(!empty($domain_info['not_allow_country'])){
$not_allow_countries = CountryCode::whereIn('id', $domain_info['not_allow_country'])->pluck('c_name')->toArray();
if(in_array($data['country'], $not_allow_countries)){
throw new InquiryFilterException( '过滤国家');
}
}
if(!empty($domain_info['not_allow_ip'])){
if(in_array($data['ip'], $domain_info['not_allow_ip'])){
throw new InquiryFilterException( '过滤ip');
}
}
$time = time();
if(!ProjectServer::useProject($project['id'])){
throw new InquiryFilterException( '项目不存在或数据库配置异常');
}
echo date('Y-m-d H:i:s') . ' | useProject time:' . (time() - $time) . PHP_EOL;
$action = $task['type'];
//是否是订阅邮箱
if($action == 'inquiry' && !empty($project['is_subscribe'])){
//是否只有email字段
$filed = '';
foreach ($data['data'] as $k => $v){
if(Str::startsWith(strtolower($k),'globalso-')){
continue;
}
$filed .= $k;
}
$filed == 'email' && $action = 'subscribe';
}
$handler = new self();
return $handler->$action($data, $date, $task['id'], $task['traffic']);
}
/**
* 特殊处理项目 去掉来源url的全部参数
*/
public function handle_referer($referer){
$url = [
'www.shphe.ru',
'www.mainpaper.ru',
'www.jinbiao.ru',
'www.gyhockey.ru',
];
if(Str::contains($referer, $url)){
$referer = parse_url($referer);
$referer = !empty($referer['host']) ? $referer['scheme'] . '://' . $referer['host'] . '/' : $referer['path'];
}
return $referer;
}
/**
*
* 订阅邮箱
* @param $data
* @param $date
* @throws InquiryFilterException
* @author zbj
* @date 2024/8/27
*/
public function subscribe($data, $date, $task_id, $traffic = 0){
$email = $data['data']['email'];
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
$model = Email::where('email', $email)->first();
if($model){
return true;
}
$model = new Email();
$model->email = $email;
$model->save();
//查询当前项目是否开启自动回复邮件
$this->saveEmailLog($data['project_id'],$email);
}else{
throw new InquiryFilterException( '邮箱格式异常:' . $email);
}
}
/**
* @remark :保存自动发送邮件
* @name :saveEmailLog
* @author :lyh
* @method :post
* @time :2024/12/30 15:36
*/
public function saveEmailLog($project_id,$email){
$autoEmailModel = new AutoEmail();
$info = $autoEmailModel->read(['project_id'=>$project_id]);
if($info !== false){
$paramData = [
'project_id'=>$project_id,
'title'=>$info['title'],
'content'=>$info['content'],
'email'=>$email,
];
$autoEmailLogModel = new AutoEmailLog();
$autoEmailLogModel->addReturnId($paramData);
}
return true;
}
/**
* 询盘
* @param $data
* @return bool
* @throws InquiryFilterException
* @author zbj
* @date 2023/12/4
*/
public function inquiry($data, $date, $task_id, $traffic = 0)
{
//ip 一分钟请求3次 就半小时内不写入这个ip了
$ip_cache_key = "inquiry_{$data['project_id']}_{$data['ip']}";
$ip_lock_cache_key = "inquiry_{$data['project_id']}_{$data['ip']}_lock";
$num = Cache::get($ip_cache_key);
if (!$num) {
Cache::put($ip_cache_key, 0, 60);
}
if($num > 2){
//这个ip 锁半个小时
Cache::put($ip_lock_cache_key, 1, 1800);
}
if(Cache::get($ip_lock_cache_key)){
Log::channel('inquiry')->info($task_id . '询盘ip重复锁定', [$data]);
throw new InquiryFilterException( 'ip半小时内重复询盘');
}
//数组key转为小写
$data['data'] = array_change_key_case($data['data'], CASE_LOWER);
$this->inquiryFilter($data['project_id'], $data);
//特殊处理1986的表单
if($data['project_id'] == 1986){
$fields = ['乘用车', '商用车', '能源存储', '探索新业务'];
$form_fields = array_keys($data['data']);
if(array_intersect($form_fields, $fields)){
foreach ($fields as $field){
if (!isset($data['data'][$field])){
$data['data'][$field] = [];
}
}
}
}
$form_id = InquiryForm::getFromId($data['data'], $data['project_id']);
$data['referer'] = $this->handle_referer($data['referer']);
$id = InquiryFormData::saveData($form_id, $data['domain'], $data['ip'], $data['country'], $data['referer'], $data['user_agent'], $data['submit_at'], $data['data'], $traffic);
//转化询盘
Visit::isInquiry($data['ip']);
//推送企微消息 测试域名不发
Log::channel('inquiry')->info($task_id . '询盘结果', [$id]);
if($id && !Str::contains($data['domain'], 'globalso.site')){
try {
$name = empty($data['data']['name']) ? '' : ' ' . $data['data']['name'];
MessagePush::addInquiryMessage($id, $data['project_id'], $data['country'], $name, $data['submit_at']);
}catch (\Exception $e){
Cache::decrement($ip_cache_key);
Log::channel('inquiry')->info($task_id . '询盘失败', [$e->getMessage(), $e->getFile(), $e->getLine()]);
LogUtils::error('询盘消息'.$id.'写入企微消息队列失败' . $e->getMessage());
}
}
Log::channel('inquiry')->info($task_id . '询盘完成' . PHP_EOL);
Cache::increment($ip_cache_key);
return true;
}
/**
* 访问
* @param $data
* @return bool
* @throws \Exception
* @author zbj
* @date 2023/12/4
*/
public function visit($data, $date, $task_id, $traffic = 0)
{
$visit_data = $data['data'];
$referrer_url = '';
if($data['data']['referrer_url']){
$url_arr = parse_url($data['data']['referrer_url']);
if(!empty($url_arr['scheme']) && !empty($url_arr['host'])){
$referrer_url = $url_arr['scheme'] . '://' . $url_arr['host'] . '/';
}
}
$visit_data['referrer_url'] = $this->handle_referer($referrer_url);
$visit_data['device_port'] = $data['data']['device_port']??'';
$visit_data['url'] = $data['data']['url']??'';
$visit_data['domain'] = $data['domain']??'';
$visit_data['ip'] = $data['ip'];
$visit_data['country'] = $data['country'];
$visit_data['updated_date'] = $data['submit_at']->toDateString();
$visit_data['created_at'] = $data['submit_at'];
$visit_data['user_agent'] = $data['user_agent'];
if(!empty($data['is_cf'])){
$visit_data['is_inquiry'] = 1;
}
Visit::saveData($visit_data, $visit_data['updated_date']);
//pv
try {
$url_path = trim(parse_url($visit_data['url'], PHP_URL_PATH), '/');
if($url_path){
$route = analysisRoute($url_path);
$row = RouteMap::where('route', $route)->increment('pv');
if(!$row){
Log::channel('visit')->info('route not found:' . $visit_data['url'] );
}
}
}catch (\Exception $e){
Log::channel('visit')->info('pv inc error:' . $visit_data['url'], [$e->getMessage(), $e->getFile(), $e->getLine()]);
}
return true;
}
/**
* 根据ip查地区
* @param $ip
* @return string
* @author zbj
* @date 2023/11/28
*/
public static function getCountryByIp($ip){
if(!$ip){
return '';
}
// $client = new \GuzzleHttp\Client();
// $res = $client->request('GET', "http://ip.globalso.com?ip=".$ip, [
// 'proxy' => env('CURL_PROXY'), // 代理服务器地址和端口号
// ]);
// if($res->getStatusCode() == 200){
// return $res->getBody()->getContents();
// }else{
// return '';
// }
$res = Http::withoutVerifying()->get('http://ip.globalso.com', ['ip' => $ip]);
if($res->status() == 200){
return $res->body();
}else{
return '';
}
}
/**
* 是否开始测试站或中国内地询盘和访问记录
* @param $domain
* @param $ip
* @param $type
* @return array
* @throws InquiryFilterException
* @author zbj
* @date 2023/11/30
*/
public static function checkIpCountry($domain, $ip, $type){
$project = Project::getProjectByDomain($domain);
if(empty($project)){
//是否有关联的域名
$relate_domain = InquiryRelateDomain::getRelateDomain($domain);
if(!$relate_domain){
throw new InquiryFilterException('项目不存在1');
}
$project = Project::getProjectByDomain($relate_domain);
if(!$project){
throw new InquiryFilterException('项目不存在2');
}
}
// 访问记录过滤测试环境
if ($type == SyncSubmitTask::TYPE_VISIT && FALSE !== strpos($domain, 'globalso.site') && !$project->is_record_test_visit) {
throw new InquiryFilterException('测试环境过滤');
}
// 访问记录过滤测试环境
if ($type == SyncSubmitTask::TYPE_INQUIRY && FALSE !== strpos($domain, 'globalso.site') && !$project->is_record_test_inquiry) {
throw new InquiryFilterException('测试环境过滤');
}
if($ip == "127.0.0.1"){
throw new InquiryFilterException('127.0.0.1过滤');
}
$time = time();
$country = self::getCountryByIp($ip);
echo date('Y-m-d H:i:s') . ' | getCountryByIp time:' . (time() - $time) . PHP_EOL;
//访问记录过滤国内
if ($type == SyncSubmitTask::TYPE_VISIT && $country == "中国" && !$project->is_record_china_visit){
throw new InquiryFilterException('中国内地过滤');
}
return [
'project' => $project,
'ip' => $ip,
'country' => $country
];
}
/**
* 询盘配置过滤
* @author zbj
* @date 2024/1/20
*/
public static function inquiryFilter($project_id, $data){
if(empty($data['referer']) || empty($data['user_agent']) || empty($data['data'])){
throw new InquiryFilterException( '数据异常:' . $data['country']);
}
//关杰 全局过滤 满足 name、message 8-16 纯字母不含空格 ip 荷兰 mobile 10位纯数字 过滤
if(
strlen($data['data']['name']) <= 16 && strlen($data['data']['name']) >= 8 &&
strlen($data['data']['message']) <= 16 && strlen($data['data']['message']) >= 8 &&
preg_match('/^[a-zA-Z]+$/', $data['data']['name']) &&
preg_match('/^[a-zA-Z]+$/', $data['data']['message']) &&
preg_match('/^\d+$/', $data['data']['phone']) &&
strlen($data['data']['phone']) == 10 &&
in_array($data['country'], ['荷兰', '俄罗斯'])
){
throw new InquiryFilterException( '全局过滤');
}
//全局过滤 ip 荷兰 有name、phone、email字段,但都是空
if(
in_array($data['country'], ['荷兰', '俄罗斯']) &&
isset($data['data']['name']) &&
isset($data['data']['phone']) &&
isset($data['data']['email']) &&
empty($data['data']['name']) &&
empty($data['data']['phone']) &&
empty($data['data']['email'])
){
throw new InquiryFilterException( '全局过滤');
}
//1913宁波市鄞州永鑫 ip荷兰 message 8-16 纯字母不含空格
if($project_id == 1913 && in_array($data['country'], ['荷兰', '俄罗斯'])
&& strlen($data['data']['message']) <= 16 && strlen($data['data']['message']) >= 8
&& preg_match('/^[a-zA-Z]+$/', $data['data']['message'])
){
throw new InquiryFilterException( '被刷数据');
}
//数据都是空的
$is_all_empty = true;
foreach ($data['data'] as $item){
if(Str::startsWith(strtolower($item),'globalso-')){
continue;
}
if(!empty($item)){
$is_all_empty = false;
break;
}
}
if($is_all_empty){
throw new InquiryFilterException( '数据都是空的');
}
$config = InquiryFilterConfig::getCacheInfoByProjectId($project_id);
//没配置 则默认开启且使用全局
if(!$config){
$config['is_global_rule'] = 1;
$config['status'] = 1;
}
//是否开启过滤
if($config && $config['status']){
//是否包含全局规则(就是project_id=1的配置)
if($project_id != Project::DEMO_PROJECT_ID && $config['is_global_rule']){
self::inquiryFilter(Project::DEMO_PROJECT_ID, $data);
}
//过滤国家
if(!empty($config['filter_countries']) && in_array($data['country'], $config['filter_countries'])){
throw new InquiryFilterException( '过滤国家:' . $data['country']);
}
//过滤ip
if(!empty($config['black_ips'])){
$black_ips = $config['black_ips'];
//后端获取的ip
if(in_array($data['ip'], $black_ips)){
throw new InquiryFilterException( '过滤黑名单IP:' . $data['ip']);
}
//前端获取的ip
if(!empty($data['data']['ip']) && in_array($data['data']['ip'], $black_ips)){
throw new InquiryFilterException( '过滤黑名单IP:' . $data['data']['ip']);
}
}
//过滤内容
if(!empty($data['data']['message'])) {
//过滤内容关键字
if (!empty($config['filter_contents'])) {
foreach ($config['filter_contents'] as $filter_content) {
//中文直接包含
if (preg_match("/^[\x{4e00}-\x{9fa5}]+$/u", $filter_content)) {
if (Str::contains($data['data']['message'], $filter_content)) {
throw new InquiryFilterException('过滤内容:' . $filter_content);
}
} else {
//英文要指定词才过滤
if (preg_match("/\b" . preg_quote($filter_content, "/") . "\b/i", $data['data']['message'])) {
throw new InquiryFilterException('过滤内容:' . $filter_content);
}
}
}
}
//是否允许包含链接
if(isset($config['is_allow_link']) && !$config['is_allow_link']){
if (Str::contains(strtolower($data['data']['message']), ['http://', 'https://', 'www.'])) {
throw new InquiryFilterException('不允许包含链接');
}
}
}
//过滤来源
if(!empty($config['filter_referers'])){
//只比较path路径
$paths = array_map(function ($v){
return trim(parse_url(Url::to($v), PHP_URL_PATH), '/');
},$config['filter_referers']);
//后端获取的referer
if(in_array(trim(parse_url($data['referer'], PHP_URL_PATH), '/'), $paths)){
throw new InquiryFilterException( '过滤来源链接:' . $data['referer']);
}
//前端获取的referer
if(!empty($data['data']['globalso-domain_host_url']) && in_array(parse_url($data['data']['globalso-domain_host_url'], PHP_URL_PATH), $paths)){
throw new InquiryFilterException( '过滤来源链接:' . $data['data']['globalso-domain_host_url']);
}
}
//过滤邮箱
if(!empty($config['filter_emails']) && !empty($data['data']['email'])){
foreach ($config['filter_emails'] as $filter_email){
if(Str::contains(strtolower($data['data']['email']), strtolower($filter_email))){
throw new InquiryFilterException( '过滤邮箱:' . $filter_email);
}
}
}
//过滤电话
if(!empty($config['filter_mobiles']) && !empty($data['data']['phone'])){
foreach ($config['filter_mobiles'] as $filter_mobile){
if(Str::contains(strtolower($data['data']['phone']), strtolower($filter_mobile))){
throw new InquiryFilterException( '过滤电话:' . $filter_mobile);
}
}
}
//过滤姓名
if(!empty($config['filter_names']) && !empty($data['data']['name'])){
foreach ($config['filter_names'] as $filter_name){
if( Str::contains(strtolower($data['data']['name']), strtolower($filter_name))){
throw new InquiryFilterException( '过滤姓名:' . $filter_name);
}
}
}
}
return true;
}
}