优化爬虫,增加缓存,线程池,保存浏览器用户信息
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\common\library\scrm;
|
||||
|
||||
use think\Config;
|
||||
|
||||
/**
|
||||
* 云控蜘蛛 antiBot 配置构建器
|
||||
*
|
||||
* 约定:sessionKey = "{ticketType}:{host}";profile=real 当 ticket_type 在 site 配置列表中
|
||||
*/
|
||||
class AntiBotConfigBuilder
|
||||
{
|
||||
/**
|
||||
* 为指定工单类型与落地页 URL 构建 Node antiBot 配置;未启用类型返回 null
|
||||
*
|
||||
* @return array<string, mixed>|null
|
||||
*/
|
||||
public static function build(string $ticketType, string $pageUrl): ?array
|
||||
{
|
||||
$enabledTypes = self::getEnabledTypes();
|
||||
if (!in_array($ticketType, $enabledTypes, true)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$host = (string) parse_url($pageUrl, PHP_URL_HOST);
|
||||
if ($host === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
$sessionKey = $ticketType . ':' . $host;
|
||||
$ttlMinutes = self::getSessionTtlMinutes();
|
||||
|
||||
return [
|
||||
'enabled' => true,
|
||||
'profile' => 'real',
|
||||
'turnstile' => true,
|
||||
'solverFallback' => true,
|
||||
'sessionKey' => $sessionKey,
|
||||
'challengeTimeoutMs' => 60000,
|
||||
'sessionTtlMinutes' => $ttlMinutes,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析 sessionKey(供 cron 分组调度使用)
|
||||
*/
|
||||
public static function resolveSessionKey(string $ticketType, string $pageUrl): string
|
||||
{
|
||||
$host = (string) parse_url($pageUrl, PHP_URL_HOST);
|
||||
return $ticketType . ':' . ($host !== '' ? $host : 'unknown');
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否对该工单类型启用 antiBot
|
||||
*/
|
||||
public static function isEnabledForType(string $ticketType): bool
|
||||
{
|
||||
return in_array($ticketType, self::getEnabledTypes(), true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<string>
|
||||
*/
|
||||
public static function getEnabledTypes(): array
|
||||
{
|
||||
$raw = (string) Config::get('site.split_scrm_antibot_types');
|
||||
if ($raw === '') {
|
||||
return ['whatshub', 'chatknow'];
|
||||
}
|
||||
$parts = array_map('trim', explode(',', $raw));
|
||||
return array_values(array_filter($parts, static function (string $v): bool {
|
||||
return $v !== '';
|
||||
}));
|
||||
}
|
||||
|
||||
public static function getSessionTtlMinutes(): int
|
||||
{
|
||||
$minutes = (int) Config::get('site.split_scrm_session_ttl_minutes');
|
||||
return $minutes > 0 ? $minutes : 120;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user