amirkabiri commited on
Commit
690837a
·
1 Parent(s): b01564f

random user agent

Browse files
Files changed (1) hide show
  1. src/duckai.ts +9 -8
src/duckai.ts CHANGED
@@ -8,8 +8,6 @@ import type {
8
  DuckAIRequest,
9
  } from "./types";
10
 
11
- const userAgent = new UserAgent();
12
-
13
  // Rate limiting tracking with sliding window
14
  interface RateLimitInfo {
15
  requestTimestamps: number[]; // Array of request timestamps for sliding window
@@ -182,7 +180,7 @@ export class DuckAI {
182
  }
183
  }
184
 
185
- private async getVQD(): Promise<VQDResponse> {
186
  const response = await fetch("https://duckduckgo.com/duckchat/v1/status", {
187
  headers: {
188
  accept: "*/*",
@@ -194,7 +192,7 @@ export class DuckAI {
194
  "sec-fetch-mode": "cors",
195
  "sec-fetch-site": "same-origin",
196
  "x-vqd-accept": "1",
197
- "User-Agent": userAgent.toString(),
198
  },
199
  referrer: "https://duckduckgo.com/",
200
  referrerPolicy: "origin",
@@ -246,7 +244,8 @@ export class DuckAI {
246
  // Wait if rate limiting is needed
247
  await this.waitIfNeeded();
248
 
249
- const vqd = await this.getVQD();
 
250
 
251
  const { window } = new JSDOM(
252
  `<html><body><script>window.hash = ${vqd.hash}</script></body></html>`,
@@ -282,7 +281,7 @@ export class DuckAI {
282
  "sec-fetch-site": "same-origin",
283
  "x-fe-version": "serp_20250401_100419_ET-19d438eb199b2bf7c300",
284
  "x-vqd-4": vqd.vqd,
285
- "User-Agent": userAgent.toString(),
286
  "x-vqd-hash-1": btoa(
287
  JSON.stringify({
288
  server_hashes: hash.server_hashes,
@@ -357,7 +356,9 @@ export class DuckAI {
357
  // Wait if rate limiting is needed
358
  await this.waitIfNeeded();
359
 
360
- const vqd = await this.getVQD();
 
 
361
 
362
  const { window } = new JSDOM(
363
  `<html><body><script>window.hash = ${vqd.hash}</script></body></html>`,
@@ -393,7 +394,7 @@ export class DuckAI {
393
  "sec-fetch-site": "same-origin",
394
  "x-fe-version": "serp_20250401_100419_ET-19d438eb199b2bf7c300",
395
  "x-vqd-4": vqd.vqd,
396
- "User-Agent": userAgent.toString(),
397
  "x-vqd-hash-1": btoa(
398
  JSON.stringify({
399
  server_hashes: hash.server_hashes,
 
8
  DuckAIRequest,
9
  } from "./types";
10
 
 
 
11
  // Rate limiting tracking with sliding window
12
  interface RateLimitInfo {
13
  requestTimestamps: number[]; // Array of request timestamps for sliding window
 
180
  }
181
  }
182
 
183
+ private async getVQD(userAgent: string): Promise<VQDResponse> {
184
  const response = await fetch("https://duckduckgo.com/duckchat/v1/status", {
185
  headers: {
186
  accept: "*/*",
 
192
  "sec-fetch-mode": "cors",
193
  "sec-fetch-site": "same-origin",
194
  "x-vqd-accept": "1",
195
+ "User-Agent": userAgent,
196
  },
197
  referrer: "https://duckduckgo.com/",
198
  referrerPolicy: "origin",
 
244
  // Wait if rate limiting is needed
245
  await this.waitIfNeeded();
246
 
247
+ const userAgent = new UserAgent().toString();
248
+ const vqd = await this.getVQD(userAgent);
249
 
250
  const { window } = new JSDOM(
251
  `<html><body><script>window.hash = ${vqd.hash}</script></body></html>`,
 
281
  "sec-fetch-site": "same-origin",
282
  "x-fe-version": "serp_20250401_100419_ET-19d438eb199b2bf7c300",
283
  "x-vqd-4": vqd.vqd,
284
+ "User-Agent": userAgent,
285
  "x-vqd-hash-1": btoa(
286
  JSON.stringify({
287
  server_hashes: hash.server_hashes,
 
356
  // Wait if rate limiting is needed
357
  await this.waitIfNeeded();
358
 
359
+ const userAgent = new UserAgent().toString();
360
+
361
+ const vqd = await this.getVQD(userAgent);
362
 
363
  const { window } = new JSDOM(
364
  `<html><body><script>window.hash = ${vqd.hash}</script></body></html>`,
 
394
  "sec-fetch-site": "same-origin",
395
  "x-fe-version": "serp_20250401_100419_ET-19d438eb199b2bf7c300",
396
  "x-vqd-4": vqd.vqd,
397
+ "User-Agent": userAgent,
398
  "x-vqd-hash-1": btoa(
399
  JSON.stringify({
400
  server_hashes: hash.server_hashes,