修复火箭工单过盾同步问题
This commit is contained in:
@@ -16,65 +16,74 @@ class AntiBotConfigBuilder
|
||||
/**
|
||||
* 为指定工单类型与落地页 URL 构建 Node antiBot 配置;未启用类型返回 null
|
||||
*
|
||||
* @param string $landingUrlHint HTTP 预解析或历史上次落地的长链(可选)
|
||||
* @return array<string, mixed>|null
|
||||
*/
|
||||
public static function build(string $ticketType, string $pageUrl): ?array
|
||||
public static function build(string $ticketType, string $pageUrl, string $landingUrlHint = ''): ?array
|
||||
{
|
||||
$enabledTypes = self::getEnabledTypes();
|
||||
if (!in_array($ticketType, $enabledTypes, true)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$sessionHost = self::resolveSessionHost($ticketType, $pageUrl);
|
||||
if ($sessionHost === '') {
|
||||
$sessionScope = self::resolveSessionScope($ticketType, $pageUrl, $landingUrlHint);
|
||||
if ($sessionScope === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
$sessionKey = $ticketType . ':' . $sessionHost;
|
||||
$sessionKey = $ticketType . ':' . $sessionScope;
|
||||
$ttlMinutes = self::getSessionTtlMinutes();
|
||||
|
||||
$businessHostHint = self::resolveBusinessHostHint($ticketType, $pageUrl, $landingUrlHint);
|
||||
|
||||
return [
|
||||
'enabled' => true,
|
||||
'profile' => 'real',
|
||||
'turnstile' => self::defaultTurnstile($ticketType),
|
||||
'solverFallback' => self::defaultSolverFallback($ticketType),
|
||||
'cfClearanceRequired'=> self::defaultCfClearanceRequired($ticketType),
|
||||
'sessionKey' => $sessionKey,
|
||||
'challengeTimeoutMs' => 60000,
|
||||
'sessionTtlMinutes' => $ttlMinutes,
|
||||
'businessHostHint' => self::defaultBusinessHostHint($ticketType, $sessionHost),
|
||||
'enabled' => true,
|
||||
'profile' => 'real',
|
||||
'turnstile' => self::defaultTurnstile($ticketType),
|
||||
'solverFallback' => self::defaultSolverFallback($ticketType),
|
||||
'cfClearanceRequired' => self::defaultCfClearanceRequired($ticketType),
|
||||
'sessionKey' => $sessionKey,
|
||||
'challengeTimeoutMs' => 60000,
|
||||
'sessionTtlMinutes' => $ttlMinutes,
|
||||
'businessHostHint' => $businessHostHint,
|
||||
'landingUrlHint' => $landingUrlHint !== '' ? $landingUrlHint : '',
|
||||
] + self::postCfReadyExtras($ticketType);
|
||||
}
|
||||
|
||||
/**
|
||||
* 会话域:A2C 短链(yyk.ink)仍用业务域 user.a2c.chat 复用 Real Browser 会话
|
||||
* 会话 scope:A2C 短链固定业务域;火箭长链用动态 host、短链用 share.{token}
|
||||
*/
|
||||
public static function resolveSessionScope(string $ticketType, string $pageUrl, string $landingUrlHint = ''): string
|
||||
{
|
||||
if ($ticketType === 'a2c') {
|
||||
return self::resolveA2cSessionHost($pageUrl, $landingUrlHint);
|
||||
}
|
||||
|
||||
if ($ticketType === 'huojian') {
|
||||
return self::resolveHuojianSessionScope($pageUrl, $landingUrlHint);
|
||||
}
|
||||
|
||||
$host = strtolower(trim((string) parse_url($pageUrl, PHP_URL_HOST)));
|
||||
|
||||
return $host !== '' ? $host : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated 兼容旧调用,等价 resolveSessionScope
|
||||
*/
|
||||
public static function resolveSessionHost(string $ticketType, string $pageUrl): string
|
||||
{
|
||||
$host = strtolower(trim((string) parse_url($pageUrl, PHP_URL_HOST)));
|
||||
if ($host === '') {
|
||||
return '';
|
||||
}
|
||||
|
||||
if ($ticketType === 'a2c') {
|
||||
if (strpos($host, 'a2c.chat') !== false) {
|
||||
return $host;
|
||||
}
|
||||
|
||||
return 'user.a2c.chat';
|
||||
}
|
||||
|
||||
return $host;
|
||||
return self::resolveSessionScope($ticketType, $pageUrl, '');
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析 sessionKey(供 cron 分组调度使用)
|
||||
*/
|
||||
public static function resolveSessionKey(string $ticketType, string $pageUrl): string
|
||||
public static function resolveSessionKey(string $ticketType, string $pageUrl, string $landingUrlHint = ''): string
|
||||
{
|
||||
$host = self::resolveSessionHost($ticketType, $pageUrl);
|
||||
$scope = self::resolveSessionScope($ticketType, $pageUrl, $landingUrlHint);
|
||||
|
||||
return $ticketType . ':' . ($host !== '' ? $host : 'unknown');
|
||||
return $ticketType . ':' . ($scope !== '' ? $scope : 'unknown');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -133,6 +142,16 @@ class AntiBotConfigBuilder
|
||||
];
|
||||
}
|
||||
|
||||
if ($ticketType === 'huojian') {
|
||||
return [
|
||||
'postCfReadyUrlContains' => HuojianUrlHelper::BUSINESS_PATH_NEEDLE,
|
||||
'postCfReadySelector' => '.el-message-box__input',
|
||||
'postCfReadyTimeoutMs' => 45000,
|
||||
'postCfSettleMs' => 500,
|
||||
'postCfSpaWaitMs' => 8000,
|
||||
];
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
@@ -159,10 +178,63 @@ class AntiBotConfigBuilder
|
||||
return true;
|
||||
}
|
||||
|
||||
private static function defaultBusinessHostHint(string $ticketType, string $sessionHost): string
|
||||
/**
|
||||
* 火箭/A2C 业务域 hint:优先长链动态 host,其次 landing 预解析
|
||||
*/
|
||||
private static function resolveBusinessHostHint(string $ticketType, string $pageUrl, string $landingUrlHint): string
|
||||
{
|
||||
if ($ticketType === 'a2c') {
|
||||
return $sessionHost;
|
||||
return self::resolveA2cSessionHost($pageUrl, $landingUrlHint);
|
||||
}
|
||||
|
||||
if ($ticketType === 'huojian') {
|
||||
$host = HuojianUrlHelper::extractBusinessHost($pageUrl);
|
||||
if ($host !== '') {
|
||||
return $host;
|
||||
}
|
||||
if ($landingUrlHint !== '') {
|
||||
$hintHost = HuojianUrlHelper::extractBusinessHost($landingUrlHint);
|
||||
if ($hintHost !== '') {
|
||||
return $hintHost;
|
||||
}
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
private static function resolveA2cSessionHost(string $pageUrl, string $landingUrlHint = ''): string
|
||||
{
|
||||
$candidates = [];
|
||||
if ($landingUrlHint !== '') {
|
||||
$candidates[] = (string) parse_url($landingUrlHint, PHP_URL_HOST);
|
||||
}
|
||||
$candidates[] = (string) parse_url($pageUrl, PHP_URL_HOST);
|
||||
|
||||
foreach ($candidates as $host) {
|
||||
$host = strtolower(trim($host));
|
||||
if ($host !== '' && strpos($host, 'a2c.chat') !== false) {
|
||||
return $host;
|
||||
}
|
||||
}
|
||||
|
||||
return 'user.a2c.chat';
|
||||
}
|
||||
|
||||
private static function resolveHuojianSessionScope(string $pageUrl, string $landingUrlHint = ''): string
|
||||
{
|
||||
$scope = HuojianUrlHelper::resolveSessionScope($pageUrl);
|
||||
if ($scope !== '') {
|
||||
return $scope;
|
||||
}
|
||||
|
||||
if ($landingUrlHint !== '') {
|
||||
$fromLanding = HuojianUrlHelper::resolveSessionScope($landingUrlHint);
|
||||
if ($fromLanding !== '') {
|
||||
return $fromLanding;
|
||||
}
|
||||
}
|
||||
|
||||
return '';
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\common\library\scrm;
|
||||
|
||||
/**
|
||||
* 火箭云控 URL 解析:短链(s.url99.me/code)与长链(*.url66.me/gds?link=)动态识别
|
||||
*
|
||||
* 长链域名可能为 v3.url66.me、v4.url66.me 等,禁止写死单一 host。
|
||||
*/
|
||||
final class HuojianUrlHelper
|
||||
{
|
||||
/** 火箭业务页路径特征 */
|
||||
public const BUSINESS_PATH_NEEDLE = '/gds';
|
||||
|
||||
/** 长链 query 中的分享 token 参数名 */
|
||||
public const LINK_QUERY_PARAM = 'link';
|
||||
|
||||
/**
|
||||
* 是否为火箭长链业务页(含 /gds 且带 link=)
|
||||
*/
|
||||
public static function isLongLinkUrl(string $pageUrl): bool
|
||||
{
|
||||
$path = (string) parse_url($pageUrl, PHP_URL_PATH);
|
||||
if ($path === '' || strpos($path, self::BUSINESS_PATH_NEEDLE) === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$query = [];
|
||||
parse_str((string) parse_url($pageUrl, PHP_URL_QUERY), $query);
|
||||
|
||||
return trim((string) ($query[self::LINK_QUERY_PARAM] ?? '')) !== '';
|
||||
}
|
||||
|
||||
/**
|
||||
* 从长链 URL 动态解析业务域(如 v3.url66.me)
|
||||
*/
|
||||
public static function extractBusinessHost(string $pageUrl): string
|
||||
{
|
||||
if (!self::isLongLinkUrl($pageUrl)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$host = strtolower(trim((string) parse_url($pageUrl, PHP_URL_HOST)));
|
||||
|
||||
return $host !== '' ? $host : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* 提取分享稳定标识:长链 link= 或短链 path 段(如 50q3622j)
|
||||
*/
|
||||
public static function extractShareToken(string $pageUrl): string
|
||||
{
|
||||
if (self::isLongLinkUrl($pageUrl)) {
|
||||
$query = [];
|
||||
parse_str((string) parse_url($pageUrl, PHP_URL_QUERY), $query);
|
||||
|
||||
return trim((string) ($query[self::LINK_QUERY_PARAM] ?? ''));
|
||||
}
|
||||
|
||||
$path = trim((string) parse_url($pageUrl, PHP_URL_PATH), '/');
|
||||
if ($path === '' || strpos($path, '/') !== false) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (preg_match('/^[a-zA-Z0-9]+$/', $path) !== 1) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return $path;
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否为火箭短链入口(单段 path,非 /gds 长链)
|
||||
*/
|
||||
public static function isShortEntryUrl(string $pageUrl): bool
|
||||
{
|
||||
if (self::isLongLinkUrl($pageUrl)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return self::extractShareToken($pageUrl) !== '';
|
||||
}
|
||||
|
||||
/**
|
||||
* 会话 scope:长链用动态业务域;短链用 share.{token}(跨长链域名复用 cookie jar)
|
||||
*/
|
||||
public static function resolveSessionScope(string $pageUrl): string
|
||||
{
|
||||
$businessHost = self::extractBusinessHost($pageUrl);
|
||||
if ($businessHost !== '') {
|
||||
return $businessHost;
|
||||
}
|
||||
|
||||
$token = self::extractShareToken($pageUrl);
|
||||
if ($token !== '') {
|
||||
return 'share.' . $token;
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
}
|
||||
@@ -5,10 +5,14 @@ declare(strict_types=1);
|
||||
namespace app\common\library\scrm\spider;
|
||||
|
||||
use app\common\library\scrm\AbstractScrmSpider;
|
||||
use app\common\library\scrm\AntiBotConfigBuilder;
|
||||
use app\common\library\scrm\UnifiedScrmData;
|
||||
use app\common\service\SplitPageUrlResolver;
|
||||
|
||||
/**
|
||||
* 火箭云控蜘蛛
|
||||
* 火箭云控蜘蛛(Real Browser + CF 过盾 + 密码弹窗 + API 拦截)
|
||||
*
|
||||
* 短链 s.url99.me → 长链 *.url66.me/gds?link=(长链域名动态,不写死)
|
||||
*/
|
||||
class HuojianSpider extends AbstractScrmSpider
|
||||
{
|
||||
@@ -22,6 +26,9 @@ class HuojianSpider extends AbstractScrmSpider
|
||||
|
||||
private UnifiedScrmData $unifiedData;
|
||||
|
||||
/** HTTP 预解析落地 URL(短链 CF 时可能失败,仅作 hint) */
|
||||
private string $landingUrlHint = '';
|
||||
|
||||
public function __construct(
|
||||
string $pageUrl,
|
||||
string $account = '',
|
||||
@@ -32,11 +39,14 @@ class HuojianSpider extends AbstractScrmSpider
|
||||
$this->pageUrl = $pageUrl;
|
||||
$this->account = $account;
|
||||
$this->password = $password;
|
||||
$this->landingUrlHint = SplitPageUrlResolver::resolveFinalUrl($pageUrl);
|
||||
$this->unifiedData = new UnifiedScrmData();
|
||||
}
|
||||
|
||||
protected function getSpiderConfig(): array
|
||||
{
|
||||
$antiBot = AntiBotConfigBuilder::build('huojian', $this->pageUrl, $this->landingUrlHint);
|
||||
|
||||
return [
|
||||
'pageUrl' => $this->pageUrl,
|
||||
'listApi' => self::API_LIST,
|
||||
@@ -48,6 +58,7 @@ class HuojianSpider extends AbstractScrmSpider
|
||||
['type' => 'vue_click', 'selector' => '.el-message-box__btns .el-button--primary'],
|
||||
['type' => 'wait', 'ms' => 2000],
|
||||
],
|
||||
'antiBot' => $antiBot,
|
||||
];
|
||||
}
|
||||
|
||||
@@ -84,6 +95,7 @@ class HuojianSpider extends AbstractScrmSpider
|
||||
}
|
||||
}
|
||||
$unifiedData->total = $count;
|
||||
|
||||
return $unifiedData;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,136 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\common\service;
|
||||
|
||||
/**
|
||||
* 短链 HTTP 重定向预解析(与 puppeteer-api/url-resolve.js 行为对齐)
|
||||
*
|
||||
* 用于 antiBot landingUrlHint;CF 拦截时可能失败,浏览器仍会 follow。
|
||||
*/
|
||||
final class SplitPageUrlResolver
|
||||
{
|
||||
private const REDIRECT_CODES = [301, 302, 303, 307, 308];
|
||||
|
||||
/**
|
||||
* 跟随 HTTP 重定向,返回最终 URL;失败时返回原始 URL
|
||||
*/
|
||||
public static function resolveFinalUrl(string $pageUrl, int $maxRedirects = 10, int $timeoutSec = 12): string
|
||||
{
|
||||
$current = trim($pageUrl);
|
||||
if ($current === '') {
|
||||
return '';
|
||||
}
|
||||
|
||||
for ($i = 0; $i < $maxRedirects; $i++) {
|
||||
$response = self::fetchHeadOrGet($current, $timeoutSec);
|
||||
if ($response === null) {
|
||||
return $current;
|
||||
}
|
||||
|
||||
$code = $response['code'];
|
||||
$location = $response['location'];
|
||||
if (!in_array($code, self::REDIRECT_CODES, true) || $location === '') {
|
||||
return $current;
|
||||
}
|
||||
|
||||
$current = self::resolveRelativeUrl($current, $location);
|
||||
}
|
||||
|
||||
return $current;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{code: int, location: string}|null
|
||||
*/
|
||||
private static function fetchHeadOrGet(string $url, int $timeoutSec): ?array
|
||||
{
|
||||
$result = self::curlOnce($url, true, $timeoutSec);
|
||||
if ($result !== null) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
return self::curlOnce($url, false, $timeoutSec);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{code: int, location: string}|null
|
||||
*/
|
||||
private static function curlOnce(string $url, bool $headOnly, int $timeoutSec): ?array
|
||||
{
|
||||
if (!function_exists('curl_init')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$ch = curl_init($url);
|
||||
if ($ch === false) {
|
||||
return null;
|
||||
}
|
||||
|
||||
curl_setopt_array($ch, [
|
||||
CURLOPT_NOBODY => $headOnly,
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_FOLLOWLOCATION => false,
|
||||
CURLOPT_TIMEOUT => $timeoutSec,
|
||||
CURLOPT_HEADER => true,
|
||||
CURLOPT_USERAGENT => 'Mozilla/5.0 (compatible; SplitPageUrlResolver/1.0)',
|
||||
]);
|
||||
|
||||
$body = curl_exec($ch);
|
||||
if ($body === false) {
|
||||
curl_close($ch);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
$code = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
$redirectUrl = (string) curl_getinfo($ch, CURLINFO_REDIRECT_URL);
|
||||
curl_close($ch);
|
||||
|
||||
$location = $redirectUrl;
|
||||
if ($location === '' && is_string($body)) {
|
||||
$location = self::parseLocationHeader($body);
|
||||
}
|
||||
|
||||
return ['code' => $code, 'location' => trim($location)];
|
||||
}
|
||||
|
||||
private static function parseLocationHeader(string $rawHeaders): string
|
||||
{
|
||||
foreach (preg_split('/\r\n|\n|\r/', $rawHeaders) as $line) {
|
||||
if (stripos($line, 'Location:') === 0) {
|
||||
return trim(substr($line, 9));
|
||||
}
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
private static function resolveRelativeUrl(string $baseUrl, string $location): string
|
||||
{
|
||||
if (preg_match('#^https?://#i', $location)) {
|
||||
return $location;
|
||||
}
|
||||
|
||||
$base = parse_url($baseUrl);
|
||||
if (!is_array($base)) {
|
||||
return $location;
|
||||
}
|
||||
|
||||
$scheme = (string) ($base['scheme'] ?? 'https');
|
||||
$host = (string) ($base['host'] ?? '');
|
||||
if ($host === '') {
|
||||
return $location;
|
||||
}
|
||||
|
||||
if (strpos($location, '/') === 0) {
|
||||
return $scheme . '://' . $host . $location;
|
||||
}
|
||||
|
||||
$path = (string) ($base['path'] ?? '/');
|
||||
$dir = rtrim(str_replace(basename($path), '', $path), '/');
|
||||
|
||||
return $scheme . '://' . $host . ($dir !== '' ? $dir . '/' : '/') . $location;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user