Spaces:
Paused
Paused
Merge pull request #11 from icebear0828/fix/impersonate-profile-fallback
Browse filesFiles changed (3)
24 unmodified lines25262728293024 unmodified lines### Fixed- `getModels()` 死代码:`allModels` 作用域修复,消除不可达分支- `reloadAllConfigs()` 异步 lazy import 改为同步直接导入,避免日志时序不准- 模型合并 reasoning efforts 判断逻辑从 `length > 1` 改为显式标志24 unmodified lines252627282930313224 unmodified lines### Fixed- TLS 伪装 profile 自动回退:配置的 `impersonate_profile` 不被 curl-impersonate 支持时,自动降级到最近的可用 Chrome 版本(如 `chrome137` → `chrome136`)- FFI transport 硬编码 `"chrome136"` 改为使用统一解析的 profile(`getResolvedProfile()`)- `getModels()` 死代码:`allModels` 作用域修复,消除不可达分支- `reloadAllConfigs()` 异步 lazy import 改为同步直接导入,避免日志时序不准- 模型合并 reasoning efforts 判断逻辑从 `length > 1` 改为显式标志
68 unmodified lines69707172737430 unmodified lines1051061071081091101111121131142 unmodified lines11711811912012112212312412593 unmodified lines2192202212222232249 unmodified lines23423523623768 unmodified lineslet _resolved: string | null = null;let _isImpersonate = false;let _tlsArgs: string[] | null = null;/*** Resolve the curl binary path. Result is cached after first call.30 unmodified linesreturn _resolved;}/*** Detect if curl-impersonate supports the --impersonate flag.* If supported, returns ["--impersonate", profile] which replaces CHROME_TLS_ARGS.* Otherwise returns the manual CHROME_TLS_ARGS.*/function detectImpersonateSupport(binary: string): string[] {try {2 unmodified linestimeout: 5000,});if (helpOutput.includes("--impersonate")) {const profile = getConfig().tls.impersonate_profile ?? "chrome136";console.log(`[TLS] Using --impersonate ${profile}`);return ["--impersonate", profile];}} catch {// --help failed, fall back to manual args93 unmodified linesreturn _isImpersonate;}/*** Get the detected proxy URL (or null if no proxy).* Used by LibcurlFfiTransport which needs the URL directly (not CLI args).9 unmodified lines_resolved = null;_isImpersonate = false;_tlsArgs = null;}68 unmodified lines6970717273747530 unmodified lines1061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491502 unmodified lines15315415515615715815916016116216316416516693 unmodified lines2602612622632642652662672682692702712722732749 unmodified lines28428528628728868 unmodified lineslet _resolved: string | null = null;let _isImpersonate = false;let _tlsArgs: string[] | null = null;let _resolvedProfile = "chrome136";/*** Resolve the curl binary path. Result is cached after first call.30 unmodified linesreturn _resolved;}/** Test if a specific --impersonate profile is accepted by the binary. */function testProfile(binary: string, profile: string): boolean {try {execFileSync(binary, ["--impersonate", profile, "-V"], {encoding: "utf-8",timeout: 5000,stdio: ["ignore", "pipe", "pipe"],});return true;} catch {return false;}}/*** Find a working impersonate profile.* Tries the configured profile first, then descends Chrome versions.*/function resolveProfile(binary: string, configured: string): string | null {if (testProfile(binary, configured)) return configured;const match = configured.match(/^chrome(\d+)/);if (!match) return null;const ver = parseInt(match[1], 10);for (let v = ver - 1; v >= ver - 10 && v > 0; v--) {const candidate = `chrome${v}`;if (testProfile(binary, candidate)) {console.warn(`[TLS] Profile "${configured}" not supported, using "${candidate}"`);return candidate;}}return null;}/*** Detect if curl-impersonate supports the --impersonate flag.* Validates the configured profile and auto-falls back to the nearest* supported Chrome version if needed.*/function detectImpersonateSupport(binary: string): string[] {try {2 unmodified linestimeout: 5000,});if (helpOutput.includes("--impersonate")) {const configured = getConfig().tls.impersonate_profile ?? "chrome136";const profile = resolveProfile(binary, configured);if (profile) {_resolvedProfile = profile;console.log(`[TLS] Using --impersonate ${profile}`);return ["--impersonate", profile];}console.warn("[TLS] No supported impersonate profile found, using manual TLS args");}} catch {// --help failed, fall back to manual args93 unmodified linesreturn _isImpersonate;}/*** Get the resolved impersonate profile (e.g. "chrome136").* Used by FFI transport which needs the profile name directly.*/export function getResolvedProfile(): string {getChromeTlsArgs(); // ensure detection has runreturn _resolvedProfile;}/*** Get the detected proxy URL (or null if no proxy).* Used by LibcurlFfiTransport which needs the URL directly (not CLI args).9 unmodified lines_resolved = null;_isImpersonate = false;_tlsArgs = null;_resolvedProfile = "chrome136";}
11 unmodified lines12131415161718431 unmodified lines45045145245345445545611 unmodified linesimport { existsSync } from "fs";import type { IKoffiLib, IKoffiCType, IKoffiRegisteredCallback, KoffiFunction } from "koffi";import type { TlsTransport, TlsTransportResponse } from "./transport.js";import { getProxyUrl } from "./curl-binary.js";// ── libcurl constants ──────────────────────────────────────────────431 unmodified linesif (!easy) throw new Error("curl_easy_init() returned null");// Impersonate Chrome — 0 = don't inject default headers (we control them)b.curl_easy_impersonate(easy, "chrome136", 0);b.curl_easy_setopt_str(easy, CURLOPT_URL, url);b.curl_easy_setopt_long(easy, CURLOPT_NOSIGNAL, 1);11 unmodified lines12131415161718431 unmodified lines45045145245345445545611 unmodified linesimport { existsSync } from "fs";import type { IKoffiLib, IKoffiCType, IKoffiRegisteredCallback, KoffiFunction } from "koffi";import type { TlsTransport, TlsTransportResponse } from "./transport.js";import { getProxyUrl, getResolvedProfile } from "./curl-binary.js";// ── libcurl constants ──────────────────────────────────────────────431 unmodified linesif (!easy) throw new Error("curl_easy_init() returned null");// Impersonate Chrome — 0 = don't inject default headers (we control them)b.curl_easy_impersonate(easy, getResolvedProfile(), 0);b.curl_easy_setopt_str(easy, CURLOPT_URL, url);b.curl_easy_setopt_long(easy, CURLOPT_NOSIGNAL, 1);
CHANGELOG.md+2
src/tls/curl-binary.ts+56-5
src/tls/libcurl-ffi-transport.ts+2-2