@apifuse/provider-sdk 2.2.0-beta.57 → 2.2.0-beta.59
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AUTHORING.md +9 -0
- package/CHANGELOG.md +8 -0
- package/README.md +22 -5
- package/SUBMISSION.md +2 -1
- package/bin/apifuse-pack-types.ts +49 -0
- package/bin/apifuse-record.ts +6 -0
- package/dist/cli/templates/provider/README.md.tpl +19 -5
- package/dist/config/loader.d.ts +7 -0
- package/dist/config/loader.js +30 -6
- package/dist/declaration-validation.d.ts +2 -1
- package/dist/declaration-validation.js +39 -0
- package/dist/define.d.ts +5 -4
- package/dist/define.js +25 -1
- package/dist/engine.d.ts +16 -0
- package/dist/engine.js +58 -1
- package/dist/error-resolution.js +13 -0
- package/dist/index.d.ts +6 -4
- package/dist/index.js +4 -2
- package/dist/lint.js +3 -3
- package/dist/provider.d.ts +3 -3
- package/dist/provider.js +2 -2
- package/dist/runtime/bounded-expiring-map.d.ts +21 -0
- package/dist/runtime/bounded-expiring-map.js +64 -0
- package/dist/runtime/cache.d.ts +2 -0
- package/dist/runtime/cache.js +8 -1
- package/dist/runtime/egress-lease.d.ts +63 -0
- package/dist/runtime/egress-lease.js +233 -0
- package/dist/runtime/http.js +1 -0
- package/dist/runtime/proxy-retry-policy.d.ts +4 -1
- package/dist/runtime/proxy-retry-policy.js +11 -2
- package/dist/runtime/request-telemetry.d.ts +7 -9
- package/dist/runtime/resolver-config.d.ts +7 -0
- package/dist/runtime/resolver-config.js +8 -0
- package/dist/runtime/resolver-public.d.ts +3 -1
- package/dist/runtime/resolver-public.js +1 -1
- package/dist/runtime/resolver-shared.d.ts +9 -0
- package/dist/runtime/resolver-shared.js +57 -0
- package/dist/runtime/resolver-telemetry.d.ts +134 -0
- package/dist/runtime/resolver-telemetry.js +204 -0
- package/dist/runtime/resolver-usage.d.ts +24 -0
- package/dist/runtime/resolver-usage.js +60 -0
- package/dist/runtime/resolver-vendors/bindings.d.ts +5 -0
- package/dist/runtime/resolver-vendors/bindings.js +5 -0
- package/dist/runtime/resolver-vendors/capsolver.d.ts +2 -2
- package/dist/runtime/resolver-vendors/capsolver.js +21 -9
- package/dist/runtime/resolver-vendors/hypersolutions.d.ts +27 -0
- package/dist/runtime/resolver-vendors/hypersolutions.js +369 -0
- package/dist/runtime/resolver-vendors/twocaptcha.d.ts +2 -2
- package/dist/runtime/resolver-vendors/twocaptcha.js +26 -10
- package/dist/runtime/resolver-vendors/types.d.ts +35 -5
- package/dist/runtime/resolver-vendors/types.js +20 -10
- package/dist/runtime/resolver.d.ts +5 -1
- package/dist/runtime/resolver.js +397 -109
- package/dist/runtime/stealth-akamai-sbsd.d.ts +54 -0
- package/dist/runtime/stealth-akamai-sbsd.js +142 -0
- package/dist/runtime/stealth.js +547 -53
- package/dist/schema.d.ts +9 -0
- package/dist/schema.js +16 -0
- package/dist/server/index.d.ts +2 -0
- package/dist/server/index.js +1 -0
- package/dist/server/self-test.d.ts +4 -0
- package/dist/server/self-test.js +10 -1
- package/dist/server/serve-implementation.d.ts +7 -1
- package/dist/server/serve-implementation.js +181 -53
- package/dist/server/types.d.ts +6 -0
- package/dist/server/types.js +14 -0
- package/dist/stealth/profiles.d.ts +7 -1
- package/dist/stealth/profiles.js +9 -0
- package/dist/testing/run.js +6 -0
- package/dist/types.d.ts +115 -12
- package/package.json +1 -1
- package/src/cli/templates/provider/README.md.tpl +19 -5
- package/src/config/loader.ts +34 -6
- package/src/declaration-validation.ts +47 -1
- package/src/define.ts +46 -3
- package/src/engine.ts +76 -1
- package/src/error-resolution.ts +13 -0
- package/src/index.ts +32 -1
- package/src/lint.ts +3 -3
- package/src/provider.ts +9 -0
- package/src/runtime/bounded-expiring-map.ts +67 -0
- package/src/runtime/cache.ts +10 -1
- package/src/runtime/egress-lease.ts +336 -0
- package/src/runtime/http.ts +1 -0
- package/src/runtime/proxy-retry-policy.ts +12 -1
- package/src/runtime/request-telemetry.ts +5 -7
- package/src/runtime/resolver-config.ts +12 -0
- package/src/runtime/resolver-public.ts +21 -0
- package/src/runtime/resolver-shared.ts +75 -0
- package/src/runtime/resolver-telemetry.ts +362 -0
- package/src/runtime/resolver-usage.ts +96 -0
- package/src/runtime/resolver-vendors/bindings.ts +5 -0
- package/src/runtime/resolver-vendors/capsolver.ts +22 -3
- package/src/runtime/resolver-vendors/hypersolutions.ts +480 -0
- package/src/runtime/resolver-vendors/twocaptcha.ts +26 -12
- package/src/runtime/resolver-vendors/types.ts +60 -12
- package/src/runtime/resolver.ts +465 -124
- package/src/runtime/stealth-akamai-sbsd.ts +207 -0
- package/src/runtime/stealth.ts +727 -95
- package/src/schema.ts +17 -0
- package/src/server/index.ts +21 -0
- package/src/server/self-test.ts +13 -0
- package/src/server/serve-implementation.ts +268 -48
- package/src/server/types.ts +14 -0
- package/src/stealth/profiles.ts +10 -0
- package/src/testing/run.ts +6 -0
- package/src/types.ts +130 -22
|
@@ -0,0 +1,480 @@
|
|
|
1
|
+
import { isIP } from "node:net";
|
|
2
|
+
|
|
3
|
+
import type { ChallengeSolution, ProviderChallenge } from "../../types.js";
|
|
4
|
+
import { recordPaidResolverCreate } from "../resolver-usage.js";
|
|
5
|
+
import type { TraceRecorder } from "../trace.js";
|
|
6
|
+
import { assertResolverHostAllowed } from "./hosts.js";
|
|
7
|
+
import {
|
|
8
|
+
ResolverChallengeVerdictError,
|
|
9
|
+
type ResolverIdentity,
|
|
10
|
+
type ResolverPaidUsageContext,
|
|
11
|
+
type ResolverVendorAdapter,
|
|
12
|
+
type ResolverVendorTransport,
|
|
13
|
+
ResolverVendorUnavailableError,
|
|
14
|
+
} from "./types.js";
|
|
15
|
+
|
|
16
|
+
const HYPERSOLUTIONS_VENDOR_ID = "hypersolutions" as const;
|
|
17
|
+
const HYPER_SBSD_URL = "https://akm.hypersolutions.co/sbsd";
|
|
18
|
+
const HYPER_IP_URL = "https://ip.hypersolutions.co/ip";
|
|
19
|
+
/**
|
|
20
|
+
* Only the observed-IP reflector rides the bound transport: Hyper must see the
|
|
21
|
+
* egress address the upstream will see. The payload-generation POST carries no
|
|
22
|
+
* identity and goes direct, as in the measured zozotown source.
|
|
23
|
+
*/
|
|
24
|
+
const HYPER_TRANSPORT_HOSTS = ["ip.hypersolutions.co"] as const;
|
|
25
|
+
const IP_RESPONSE_MAX_BYTES = 4_096;
|
|
26
|
+
/** Measured bound shared by the script, the Hyper envelope, and the payload submission. */
|
|
27
|
+
const BODY_MAX_BYTES = 1_000_000;
|
|
28
|
+
|
|
29
|
+
type HyperPhase = "measure_ip" | "fetch_script" | "generate_payload" | "post_payload";
|
|
30
|
+
|
|
31
|
+
export interface HypersolutionsResolverVendorOptions {
|
|
32
|
+
readonly apiKey?: string;
|
|
33
|
+
readonly timeoutMs?: number;
|
|
34
|
+
readonly allowedHosts: readonly string[];
|
|
35
|
+
/** Direct egress used only for the Hyper payload-generation POST. */
|
|
36
|
+
readonly fetchImpl?: typeof fetch;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export type AkamaiSbsdChallengeSolution = Extract<
|
|
40
|
+
ChallengeSolution,
|
|
41
|
+
{ readonly form: "cookie_state" }
|
|
42
|
+
>;
|
|
43
|
+
|
|
44
|
+
export interface HypersolutionsResolverVendorAdapter extends ResolverVendorAdapter {
|
|
45
|
+
readonly id: "hypersolutions";
|
|
46
|
+
readonly requiresTransport: true;
|
|
47
|
+
readonly transportAllowedHosts: typeof HYPER_TRANSPORT_HOSTS;
|
|
48
|
+
solve(
|
|
49
|
+
challenge: ProviderChallenge,
|
|
50
|
+
identity: ResolverIdentity | undefined,
|
|
51
|
+
signal: AbortSignal,
|
|
52
|
+
traceRecorder?: TraceRecorder,
|
|
53
|
+
transport?: ResolverVendorTransport,
|
|
54
|
+
usage?: ResolverPaidUsageContext,
|
|
55
|
+
): Promise<AkamaiSbsdChallengeSolution>;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
type TransportResponse = Awaited<ReturnType<ResolverVendorTransport["fetch"]>>;
|
|
59
|
+
|
|
60
|
+
function headerValue(headers: Readonly<Record<string, string>>, name: string): string | undefined {
|
|
61
|
+
const target = name.toLowerCase();
|
|
62
|
+
return Object.entries(headers)
|
|
63
|
+
.find(([header]) => header.toLowerCase() === target)?.[1]
|
|
64
|
+
?.trim();
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function declaredLengthExceeds(declared: string | undefined, maxBytes: number): boolean {
|
|
68
|
+
return declared !== undefined && /^\d+$/u.test(declared) && BigInt(declared) > BigInt(maxBytes);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function transportFailure(phase: HyperPhase, cause?: unknown): ResolverVendorUnavailableError {
|
|
72
|
+
return new ResolverVendorUnavailableError(HYPERSOLUTIONS_VENDOR_ID, "transport_failure", {
|
|
73
|
+
phase,
|
|
74
|
+
...(cause === undefined ? {} : { cause }),
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function assertBoundedBody(response: TransportResponse, maxBytes: number, phase: HyperPhase): void {
|
|
79
|
+
if (
|
|
80
|
+
declaredLengthExceeds(headerValue(response.headers, "content-length"), maxBytes) ||
|
|
81
|
+
new TextEncoder().encode(response.body).byteLength > maxBytes
|
|
82
|
+
) {
|
|
83
|
+
throw transportFailure(phase);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function requireSuccess(status: number, phase: HyperPhase): void {
|
|
88
|
+
if (status >= 200 && status < 300) return;
|
|
89
|
+
if (phase === "measure_ip" || phase === "generate_payload") {
|
|
90
|
+
// Hyper's own service: a rejected key is a credential fault, anything else is transport.
|
|
91
|
+
throw new ResolverVendorUnavailableError(
|
|
92
|
+
HYPERSOLUTIONS_VENDOR_ID,
|
|
93
|
+
status === 401 || status === 403 ? "missing_credentials" : "transport_failure",
|
|
94
|
+
{ phase },
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
// The upstream refused the script GET or the payload POST: that is the challenge verdict.
|
|
98
|
+
throw new ResolverChallengeVerdictError(HYPERSOLUTIONS_VENDOR_ID, "solve_failed", { phase });
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function parseObservedIp(body: string): string | undefined {
|
|
102
|
+
let value: unknown = body.trim();
|
|
103
|
+
try {
|
|
104
|
+
value = JSON.parse(body);
|
|
105
|
+
} catch {
|
|
106
|
+
// Hyper's reflector has returned both JSON and plain text in measured clients.
|
|
107
|
+
}
|
|
108
|
+
if (value && typeof value === "object" && !Array.isArray(value) && "ip" in value) {
|
|
109
|
+
value = value.ip;
|
|
110
|
+
}
|
|
111
|
+
if (typeof value !== "string") return undefined;
|
|
112
|
+
const candidate = value.trim();
|
|
113
|
+
return isIP(candidate) ? candidate : undefined;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function parsePayload(body: string): string | undefined {
|
|
117
|
+
try {
|
|
118
|
+
const parsed: unknown = JSON.parse(body);
|
|
119
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed) || !("payload" in parsed)) {
|
|
120
|
+
return undefined;
|
|
121
|
+
}
|
|
122
|
+
return typeof parsed.payload === "string" && parsed.payload.length > 0
|
|
123
|
+
? parsed.payload
|
|
124
|
+
: undefined;
|
|
125
|
+
} catch {
|
|
126
|
+
return undefined;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function scriptExchangeUrls(
|
|
131
|
+
scriptUrl: string,
|
|
132
|
+
laterChallengeToken: string | undefined,
|
|
133
|
+
): {
|
|
134
|
+
readonly fetchUrl: string;
|
|
135
|
+
readonly postUrl: string;
|
|
136
|
+
readonly uuid: string;
|
|
137
|
+
readonly indices: readonly number[];
|
|
138
|
+
} {
|
|
139
|
+
// `?v=&t=` is the hard SBSD variant and `?v=` is passive SBSD. A no-query
|
|
140
|
+
// Akamai script is a sensor-family artifact and stays outside this adapter.
|
|
141
|
+
const parsed = new URL(scriptUrl);
|
|
142
|
+
const uuid = parsed.searchParams.get("v")?.trim();
|
|
143
|
+
if (!uuid) {
|
|
144
|
+
throw new ResolverVendorUnavailableError(HYPERSOLUTIONS_VENDOR_ID, "missing_challenge_input", {
|
|
145
|
+
missingFields: ["scriptUrl"],
|
|
146
|
+
phase: "fetch_script",
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
const scriptToken = parsed.searchParams.get("t")?.trim();
|
|
150
|
+
const laterToken = laterChallengeToken?.trim();
|
|
151
|
+
if (scriptToken && laterToken) {
|
|
152
|
+
throw new ResolverVendorUnavailableError(HYPERSOLUTIONS_VENDOR_ID, "missing_challenge_input", {
|
|
153
|
+
missingFields: ["challengeToken"],
|
|
154
|
+
phase: "fetch_script",
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
// Clone the validated URL and only replace its query: reparsing `pathname`
|
|
158
|
+
// against `origin` would read a `//host/...` pathname as a network-path
|
|
159
|
+
// reference and swap the host that assertChallengeInput just admitted.
|
|
160
|
+
const fetchUrl = new URL(parsed);
|
|
161
|
+
fetchUrl.search = "";
|
|
162
|
+
fetchUrl.hash = "";
|
|
163
|
+
fetchUrl.searchParams.set("v", uuid);
|
|
164
|
+
if (scriptToken) fetchUrl.searchParams.set("t", scriptToken);
|
|
165
|
+
const postUrl = new URL(fetchUrl);
|
|
166
|
+
postUrl.search = "";
|
|
167
|
+
const postToken = laterToken || scriptToken;
|
|
168
|
+
if (postToken) postUrl.searchParams.set("t", postToken);
|
|
169
|
+
return {
|
|
170
|
+
fetchUrl: fetchUrl.toString(),
|
|
171
|
+
postUrl: postUrl.toString(),
|
|
172
|
+
uuid,
|
|
173
|
+
indices: postToken ? [0] : [0, 1],
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function assertChallengeInput(
|
|
178
|
+
challenge: Extract<ProviderChallenge, { readonly kind: "akamai_sbsd" }>,
|
|
179
|
+
allowedHosts: readonly string[],
|
|
180
|
+
): void {
|
|
181
|
+
const missingFields = [
|
|
182
|
+
...(challenge.pageUrl.trim() ? [] : ["pageUrl"]),
|
|
183
|
+
...(challenge.scriptUrl.trim() ? [] : ["scriptUrl"]),
|
|
184
|
+
...(challenge.stateCookieName === "sbsd_o" || challenge.stateCookieName === "bm_so"
|
|
185
|
+
? []
|
|
186
|
+
: ["stateCookieName"]),
|
|
187
|
+
...(challenge.challengeToken === undefined || challenge.challengeToken.trim()
|
|
188
|
+
? []
|
|
189
|
+
: ["challengeToken"]),
|
|
190
|
+
];
|
|
191
|
+
if (missingFields.length > 0) {
|
|
192
|
+
throw new ResolverVendorUnavailableError(HYPERSOLUTIONS_VENDOR_ID, "missing_challenge_input", {
|
|
193
|
+
missingFields,
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
assertResolverHostAllowed(challenge.pageUrl, allowedHosts);
|
|
197
|
+
assertResolverHostAllowed(challenge.scriptUrl, allowedHosts);
|
|
198
|
+
if (new URL(challenge.pageUrl).origin !== new URL(challenge.scriptUrl).origin) {
|
|
199
|
+
// The state cookie is read for pageUrl and posted to scriptUrl; they must share an origin.
|
|
200
|
+
throw new ResolverVendorUnavailableError(HYPERSOLUTIONS_VENDOR_ID, "missing_challenge_input", {
|
|
201
|
+
missingFields: ["scriptUrl"],
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
async function boundFetch(
|
|
207
|
+
transport: ResolverVendorTransport,
|
|
208
|
+
url: string,
|
|
209
|
+
init: Parameters<ResolverVendorTransport["fetch"]>[1],
|
|
210
|
+
phase: HyperPhase,
|
|
211
|
+
): Promise<TransportResponse> {
|
|
212
|
+
try {
|
|
213
|
+
return await transport.fetch(url, init);
|
|
214
|
+
} catch (cause) {
|
|
215
|
+
if (
|
|
216
|
+
cause instanceof ResolverVendorUnavailableError ||
|
|
217
|
+
cause instanceof ResolverChallengeVerdictError
|
|
218
|
+
) {
|
|
219
|
+
throw cause;
|
|
220
|
+
}
|
|
221
|
+
// An abort is the caller's (or the adapter timeout's) verdict, not a transport fault.
|
|
222
|
+
if (init.signal.aborted) throw init.signal.reason ?? cause;
|
|
223
|
+
throw transportFailure(phase, cause);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
async function readBoundedText(response: Response, maxBytes: number): Promise<string | undefined> {
|
|
228
|
+
if (declaredLengthExceeds(response.headers.get("content-length")?.trim(), maxBytes)) {
|
|
229
|
+
await response.body?.cancel();
|
|
230
|
+
return undefined;
|
|
231
|
+
}
|
|
232
|
+
if (!response.body) return "";
|
|
233
|
+
const reader = response.body.getReader();
|
|
234
|
+
const decoder = new TextDecoder();
|
|
235
|
+
let received = 0;
|
|
236
|
+
let text = "";
|
|
237
|
+
for (;;) {
|
|
238
|
+
const { done, value } = await reader.read();
|
|
239
|
+
if (done) break;
|
|
240
|
+
received += value.byteLength;
|
|
241
|
+
if (received > maxBytes) {
|
|
242
|
+
await reader.cancel();
|
|
243
|
+
return undefined;
|
|
244
|
+
}
|
|
245
|
+
text += decoder.decode(value, { stream: true });
|
|
246
|
+
}
|
|
247
|
+
return text + decoder.decode();
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
async function generatePayload(
|
|
251
|
+
fetchImpl: typeof fetch,
|
|
252
|
+
apiKey: string,
|
|
253
|
+
body: string,
|
|
254
|
+
signal: AbortSignal,
|
|
255
|
+
): Promise<{ readonly status: number; readonly body: string | undefined }> {
|
|
256
|
+
try {
|
|
257
|
+
const response = await fetchImpl(HYPER_SBSD_URL, {
|
|
258
|
+
method: "POST",
|
|
259
|
+
headers: {
|
|
260
|
+
accept: "application/json",
|
|
261
|
+
"content-type": "application/json",
|
|
262
|
+
"x-api-key": apiKey,
|
|
263
|
+
},
|
|
264
|
+
body,
|
|
265
|
+
signal,
|
|
266
|
+
redirect: "error",
|
|
267
|
+
});
|
|
268
|
+
// A body that fails mid-read is as much a transport fault as a refused connection.
|
|
269
|
+
return { status: response.status, body: await readBoundedText(response, BODY_MAX_BYTES) };
|
|
270
|
+
} catch (cause) {
|
|
271
|
+
if (signal.aborted) throw signal.reason ?? cause;
|
|
272
|
+
throw transportFailure("generate_payload", cause);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
export function createHypersolutionsResolverVendorAdapter(
|
|
277
|
+
options: HypersolutionsResolverVendorOptions,
|
|
278
|
+
): HypersolutionsResolverVendorAdapter {
|
|
279
|
+
const apiKey = options.apiKey?.trim();
|
|
280
|
+
const fetchImpl = options.fetchImpl ?? globalThis.fetch;
|
|
281
|
+
return {
|
|
282
|
+
id: HYPERSOLUTIONS_VENDOR_ID,
|
|
283
|
+
requiresTransport: true,
|
|
284
|
+
transportAllowedHosts: HYPER_TRANSPORT_HOSTS,
|
|
285
|
+
supports: (kind) => kind === "akamai_sbsd",
|
|
286
|
+
async solve(challenge, _identity, signal, traceRecorder, transport, usage) {
|
|
287
|
+
const timeoutController = new AbortController();
|
|
288
|
+
const operationSignal = options.timeoutMs
|
|
289
|
+
? AbortSignal.any([signal, timeoutController.signal])
|
|
290
|
+
: signal;
|
|
291
|
+
const timeout = options.timeoutMs
|
|
292
|
+
? setTimeout(
|
|
293
|
+
// A named reason lets the usage span record `timeout`, not a caller abort.
|
|
294
|
+
() => timeoutController.abort(new DOMException("Hyper solve timed out", "TimeoutError")),
|
|
295
|
+
options.timeoutMs,
|
|
296
|
+
)
|
|
297
|
+
: undefined;
|
|
298
|
+
const runPhase = <T>(phase: HyperPhase, fn: () => Promise<T>): Promise<T> =>
|
|
299
|
+
traceRecorder ? traceRecorder.runSpan(`resolver.vendor.${phase}`, fn) : fn();
|
|
300
|
+
try {
|
|
301
|
+
if (challenge.kind !== "akamai_sbsd") {
|
|
302
|
+
throw new ResolverVendorUnavailableError(HYPERSOLUTIONS_VENDOR_ID, "not_implemented");
|
|
303
|
+
}
|
|
304
|
+
if (!apiKey) {
|
|
305
|
+
throw new ResolverVendorUnavailableError(HYPERSOLUTIONS_VENDOR_ID, "missing_credentials");
|
|
306
|
+
}
|
|
307
|
+
if (!transport?.getCookie) {
|
|
308
|
+
throw new ResolverVendorUnavailableError(HYPERSOLUTIONS_VENDOR_ID, "missing_transport");
|
|
309
|
+
}
|
|
310
|
+
const sessionHeaders = transport.sessionHeaders;
|
|
311
|
+
const userAgent = sessionHeaders ? headerValue(sessionHeaders, "user-agent") : undefined;
|
|
312
|
+
const acceptLanguage = sessionHeaders
|
|
313
|
+
? headerValue(sessionHeaders, "accept-language")
|
|
314
|
+
: undefined;
|
|
315
|
+
if (!sessionHeaders || !userAgent || !acceptLanguage) {
|
|
316
|
+
throw new ResolverVendorUnavailableError(
|
|
317
|
+
HYPERSOLUTIONS_VENDOR_ID,
|
|
318
|
+
"missing_client_profile",
|
|
319
|
+
);
|
|
320
|
+
}
|
|
321
|
+
assertChallengeInput(challenge, options.allowedHosts);
|
|
322
|
+
const exchange = scriptExchangeUrls(challenge.scriptUrl, challenge.challengeToken);
|
|
323
|
+
|
|
324
|
+
// Hyper documents /ip as an authenticated service request but does not
|
|
325
|
+
// publish an explicit exclusion from request quota. Meter conservatively.
|
|
326
|
+
const ip = await recordPaidResolverCreate({
|
|
327
|
+
traceRecorder,
|
|
328
|
+
vendor: HYPERSOLUTIONS_VENDOR_ID,
|
|
329
|
+
kind: challenge.kind,
|
|
330
|
+
endpoint: "hyper:ip",
|
|
331
|
+
signal: operationSignal,
|
|
332
|
+
usage,
|
|
333
|
+
create: () =>
|
|
334
|
+
runPhase("measure_ip", async () => {
|
|
335
|
+
const ipResponse = await boundFetch(
|
|
336
|
+
transport,
|
|
337
|
+
HYPER_IP_URL,
|
|
338
|
+
{
|
|
339
|
+
method: "GET",
|
|
340
|
+
headers: {
|
|
341
|
+
accept: "application/json, text/plain;q=0.9",
|
|
342
|
+
"x-api-key": apiKey,
|
|
343
|
+
},
|
|
344
|
+
signal: operationSignal,
|
|
345
|
+
redirect: "manual",
|
|
346
|
+
maxBodyBytes: IP_RESPONSE_MAX_BYTES,
|
|
347
|
+
},
|
|
348
|
+
"measure_ip",
|
|
349
|
+
);
|
|
350
|
+
requireSuccess(ipResponse.status, "measure_ip");
|
|
351
|
+
assertBoundedBody(ipResponse, IP_RESPONSE_MAX_BYTES, "measure_ip");
|
|
352
|
+
const observedIp = parseObservedIp(ipResponse.body);
|
|
353
|
+
if (!observedIp) throw transportFailure("measure_ip");
|
|
354
|
+
return observedIp;
|
|
355
|
+
}),
|
|
356
|
+
});
|
|
357
|
+
|
|
358
|
+
const scriptResponse = await runPhase("fetch_script", async () => {
|
|
359
|
+
const scriptResponse = await boundFetch(
|
|
360
|
+
transport,
|
|
361
|
+
exchange.fetchUrl,
|
|
362
|
+
{
|
|
363
|
+
method: "GET",
|
|
364
|
+
headers: { ...sessionHeaders, Referer: challenge.pageUrl },
|
|
365
|
+
signal: operationSignal,
|
|
366
|
+
redirect: "manual",
|
|
367
|
+
maxBodyBytes: BODY_MAX_BYTES,
|
|
368
|
+
},
|
|
369
|
+
"fetch_script",
|
|
370
|
+
);
|
|
371
|
+
requireSuccess(scriptResponse.status, "fetch_script");
|
|
372
|
+
assertBoundedBody(scriptResponse, BODY_MAX_BYTES, "fetch_script");
|
|
373
|
+
if (!scriptResponse.body) {
|
|
374
|
+
throw new ResolverChallengeVerdictError(HYPERSOLUTIONS_VENDOR_ID, "solve_failed", {
|
|
375
|
+
phase: "fetch_script",
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
return scriptResponse;
|
|
379
|
+
});
|
|
380
|
+
const stateCookie = transport
|
|
381
|
+
.getCookie(challenge.stateCookieName, challenge.pageUrl)
|
|
382
|
+
?.trim();
|
|
383
|
+
if (!stateCookie) {
|
|
384
|
+
throw new ResolverVendorUnavailableError(
|
|
385
|
+
HYPERSOLUTIONS_VENDOR_ID,
|
|
386
|
+
"missing_challenge_input",
|
|
387
|
+
{ missingFields: [challenge.stateCookieName], phase: "fetch_script" },
|
|
388
|
+
);
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
let expires: number | undefined;
|
|
392
|
+
for (const [roundIndex, index] of exchange.indices.entries()) {
|
|
393
|
+
const round = roundIndex + 1;
|
|
394
|
+
const payload = await recordPaidResolverCreate({
|
|
395
|
+
traceRecorder,
|
|
396
|
+
vendor: HYPERSOLUTIONS_VENDOR_ID,
|
|
397
|
+
kind: challenge.kind,
|
|
398
|
+
endpoint: "hyper:sbsd_create",
|
|
399
|
+
round,
|
|
400
|
+
signal: operationSignal,
|
|
401
|
+
usage,
|
|
402
|
+
create: () =>
|
|
403
|
+
runPhase("generate_payload", async () => {
|
|
404
|
+
const hyperResponse = await generatePayload(
|
|
405
|
+
fetchImpl,
|
|
406
|
+
apiKey,
|
|
407
|
+
JSON.stringify({
|
|
408
|
+
index,
|
|
409
|
+
uuid: exchange.uuid,
|
|
410
|
+
o: stateCookie,
|
|
411
|
+
pageUrl: challenge.pageUrl,
|
|
412
|
+
userAgent,
|
|
413
|
+
script: scriptResponse.body,
|
|
414
|
+
ip,
|
|
415
|
+
acceptLanguage,
|
|
416
|
+
}),
|
|
417
|
+
operationSignal,
|
|
418
|
+
);
|
|
419
|
+
requireSuccess(hyperResponse.status, "generate_payload");
|
|
420
|
+
const generated =
|
|
421
|
+
hyperResponse.body === undefined ? undefined : parsePayload(hyperResponse.body);
|
|
422
|
+
if (!generated) {
|
|
423
|
+
throw new ResolverVendorUnavailableError(
|
|
424
|
+
HYPERSOLUTIONS_VENDOR_ID,
|
|
425
|
+
"transport_failure",
|
|
426
|
+
{ phase: "generate_payload", round },
|
|
427
|
+
);
|
|
428
|
+
}
|
|
429
|
+
return generated;
|
|
430
|
+
}),
|
|
431
|
+
});
|
|
432
|
+
const postResponse = await runPhase("post_payload", async () => {
|
|
433
|
+
const postResponse = await boundFetch(
|
|
434
|
+
transport,
|
|
435
|
+
exchange.postUrl,
|
|
436
|
+
{
|
|
437
|
+
method: "POST",
|
|
438
|
+
headers: {
|
|
439
|
+
...sessionHeaders,
|
|
440
|
+
"content-type": "application/json",
|
|
441
|
+
Referer: challenge.pageUrl,
|
|
442
|
+
},
|
|
443
|
+
body: JSON.stringify({ body: payload }),
|
|
444
|
+
signal: operationSignal,
|
|
445
|
+
redirect: "manual",
|
|
446
|
+
maxBodyBytes: BODY_MAX_BYTES,
|
|
447
|
+
},
|
|
448
|
+
"post_payload",
|
|
449
|
+
);
|
|
450
|
+
requireSuccess(postResponse.status, "post_payload");
|
|
451
|
+
assertBoundedBody(postResponse, BODY_MAX_BYTES, "post_payload");
|
|
452
|
+
return postResponse;
|
|
453
|
+
});
|
|
454
|
+
expires =
|
|
455
|
+
postResponse.cookies.find((cookie) => cookie.name === challenge.stateCookieName)
|
|
456
|
+
?.expires ?? expires;
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
// A 2xx payload POST is not proof of a solve and the state cookie is not
|
|
460
|
+
// required to rotate (measured zozotown source and Hyper's docs both verify
|
|
461
|
+
// only through the next protected GET, which is Phase 2).
|
|
462
|
+
return {
|
|
463
|
+
form: "cookie_state",
|
|
464
|
+
kind: "akamai_sbsd",
|
|
465
|
+
outcome: "payload_accepted",
|
|
466
|
+
verified: false,
|
|
467
|
+
stateCookieName: challenge.stateCookieName,
|
|
468
|
+
...(expires === undefined ? {} : { expires }),
|
|
469
|
+
};
|
|
470
|
+
} catch (error) {
|
|
471
|
+
if (timeoutController.signal.aborted && !signal.aborted) {
|
|
472
|
+
throw new ResolverVendorUnavailableError(HYPERSOLUTIONS_VENDOR_ID, "timeout");
|
|
473
|
+
}
|
|
474
|
+
throw error;
|
|
475
|
+
} finally {
|
|
476
|
+
if (timeout !== undefined) clearTimeout(timeout);
|
|
477
|
+
}
|
|
478
|
+
},
|
|
479
|
+
};
|
|
480
|
+
}
|
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
import type { ChallengeSolution, ProviderChallenge } from "../../types.js";
|
|
2
2
|
import type { TraceRecorder } from "../trace.js";
|
|
3
|
+
import { recordPaidResolverCreate } from "../resolver-usage.js";
|
|
3
4
|
import { assertResolverHostAllowed } from "./hosts.js";
|
|
4
5
|
import {
|
|
5
6
|
type ResolverIdentity,
|
|
7
|
+
type ResolverPaidUsageContext,
|
|
6
8
|
type ResolverVendorAdapter,
|
|
9
|
+
type ResolverVendorTransport,
|
|
7
10
|
ResolverChallengeVerdictError,
|
|
8
11
|
ResolverVendorUnavailableError,
|
|
12
|
+
recordResolverVendorPoll,
|
|
9
13
|
resolverVendorSupports,
|
|
10
14
|
} from "./types.js";
|
|
11
15
|
|
|
@@ -37,6 +41,8 @@ export interface TwoCaptchaResolverVendorAdapter extends ResolverVendorAdapter {
|
|
|
37
41
|
identity: ResolverIdentity | undefined,
|
|
38
42
|
signal: AbortSignal,
|
|
39
43
|
traceRecorder?: TraceRecorder,
|
|
44
|
+
transport?: ResolverVendorTransport,
|
|
45
|
+
usage?: ResolverPaidUsageContext,
|
|
40
46
|
): Promise<Extract<ChallengeSolution, { readonly form: "token" }>>;
|
|
41
47
|
}
|
|
42
48
|
|
|
@@ -328,7 +334,7 @@ export function createTwoCaptchaResolverVendorAdapter(
|
|
|
328
334
|
return resolverVendorSupports(TWOCAPTCHA_VENDOR_ID, kind);
|
|
329
335
|
},
|
|
330
336
|
|
|
331
|
-
async solve(challenge, identity, callerSignal, traceRecorder) {
|
|
337
|
+
async solve(challenge, identity, callerSignal, traceRecorder, _transport, usage) {
|
|
332
338
|
const apiKey = options.apiKey?.trim();
|
|
333
339
|
if (!apiKey) {
|
|
334
340
|
throw new ResolverVendorUnavailableError(TWOCAPTCHA_VENDOR_ID, "missing_credentials", {
|
|
@@ -349,7 +355,8 @@ export function createTwoCaptchaResolverVendorAdapter(
|
|
|
349
355
|
throw new ResolverVendorUnavailableError(TWOCAPTCHA_VENDOR_ID, "missing_challenge_input", {
|
|
350
356
|
missingFields,
|
|
351
357
|
phase: "create_task",
|
|
352
|
-
|
|
358
|
+
},
|
|
359
|
+
);
|
|
353
360
|
}
|
|
354
361
|
}
|
|
355
362
|
if (challenge.kind === "recaptcha_v3" && challenge.minScore === undefined) {
|
|
@@ -435,6 +442,14 @@ export function createTwoCaptchaResolverVendorAdapter(
|
|
|
435
442
|
`2captcha resolver does not support ${challenge.kind}`,
|
|
436
443
|
);
|
|
437
444
|
})();
|
|
445
|
+
return await recordPaidResolverCreate({
|
|
446
|
+
traceRecorder,
|
|
447
|
+
vendor: TWOCAPTCHA_VENDOR_ID,
|
|
448
|
+
kind: challenge.kind,
|
|
449
|
+
endpoint: "twocaptcha:create_task",
|
|
450
|
+
signal: solveController.signal,
|
|
451
|
+
usage,
|
|
452
|
+
create: async () => {
|
|
438
453
|
const createResult = await postJson(
|
|
439
454
|
fetchImpl,
|
|
440
455
|
endpoint(baseUrl, "createTask"),
|
|
@@ -448,6 +463,8 @@ export function createTwoCaptchaResolverVendorAdapter(
|
|
|
448
463
|
throw unavailableForPayload(createResult.payload, phase);
|
|
449
464
|
}
|
|
450
465
|
return taskId;
|
|
466
|
+
},
|
|
467
|
+
});
|
|
451
468
|
};
|
|
452
469
|
const taskId = traceRecorder
|
|
453
470
|
? await traceRecorder.runSpan("resolver.vendor.create_task", createTask, {
|
|
@@ -469,6 +486,7 @@ export function createTwoCaptchaResolverVendorAdapter(
|
|
|
469
486
|
callerSignal.throwIfAborted();
|
|
470
487
|
if (now() - startedAt >= timeoutMs) throw new TwoCaptchaSolveTimeoutError();
|
|
471
488
|
|
|
489
|
+
recordResolverVendorPoll(traceRecorder);
|
|
472
490
|
const pollResult = await postJson(
|
|
473
491
|
fetchImpl,
|
|
474
492
|
endpoint(baseUrl, "getTaskResult"),
|
|
@@ -482,20 +500,16 @@ export function createTwoCaptchaResolverVendorAdapter(
|
|
|
482
500
|
}
|
|
483
501
|
if (pollResult.payload.status === "processing") continue;
|
|
484
502
|
if (pollResult.payload.status !== "ready") {
|
|
485
|
-
throw new ResolverVendorUnavailableError(
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
{ phase },
|
|
489
|
-
);
|
|
503
|
+
throw new ResolverVendorUnavailableError(TWOCAPTCHA_VENDOR_ID, "transport_failure", {
|
|
504
|
+
phase,
|
|
505
|
+
});
|
|
490
506
|
}
|
|
491
507
|
|
|
492
508
|
const token = tokenFrom(pollResult.payload, challenge);
|
|
493
509
|
if (!token?.trim()) {
|
|
494
|
-
throw new ResolverVendorUnavailableError(
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
{ phase },
|
|
498
|
-
);
|
|
510
|
+
throw new ResolverVendorUnavailableError(TWOCAPTCHA_VENDOR_ID, "transport_failure", {
|
|
511
|
+
phase,
|
|
512
|
+
});
|
|
499
513
|
}
|
|
500
514
|
// AWS WAF remains a token solution here, so resolver cookie caching does not apply.
|
|
501
515
|
return { form: "token" as const, token };
|