Remove userAgent parameter since any modification is detected by CF

This commit is contained in:
ngosang 2021-10-16 17:46:04 +02:00
parent 3de2e44bfd
commit 78c10d6b24
4 changed files with 9 additions and 23 deletions

View File

@ -86,7 +86,6 @@ curl -L -X POST 'http://localhost:8191/v1' \
--data-raw '{
"cmd": "request.get",
"url":"http://www.google.com/",
"userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleW...",
"maxTimeout": 60000,
"headers": {
"X-Test": "Testing 123..."
@ -107,7 +106,6 @@ This also speeds up the requests since it won't have to launch a new browser ins
Parameter | Notes
|--|--|
session | Optional. The session ID that you want to be assigned to the instance. If isn't set a random UUID will be assigned.
userAgent | Optional. Will be used by the headless browser.
#### + `sessions.list`

View File

@ -46,7 +46,6 @@ async function testChromeInstallation() {
fs.writeFileSync(filePath, fileContent)
// launch the browser
const session = await sessions.create(sessionId, {
userAgent: null,
oneTimeSession: true
})
const page = await session.browser.newPage()

View File

@ -16,7 +16,6 @@ interface BaseSessionsAPICall extends BaseAPICall {
}
interface SessionsCreateAPICall extends BaseSessionsAPICall {
userAgent?: string,
cookies?: SetCookie[],
headers?: Headers
maxTimeout?: number
@ -28,7 +27,7 @@ interface BaseRequestAPICall extends BaseAPICall {
method?: HttpMethod
postData?: string
session?: string
userAgent?: string
userAgent?: string // deprecated, not used
maxTimeout?: number
cookies?: SetCookie[],
headers?: Headers
@ -68,9 +67,6 @@ type OverridesProps =
'postData' |
'headers'
// We always set a Windows User-Agent because ARM builds are detected by Cloudflare
const DEFAULT_USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36"
async function resolveChallengeWithTimeout(ctx: RequestContext, params: BaseRequestAPICall, page: Page) {
const maxTimeout = params.maxTimeout || 60000
const timer = new Timeout();
@ -164,7 +160,7 @@ async function setupPage(ctx: RequestContext, params: BaseRequestAPICall, browse
const page = await browser.newPage()
// merge session defaults with params
const { method, postData, userAgent, headers, cookies } = params
const { method, postData, headers, cookies } = params
let overrideResolvers: OverrideResolvers = {}
@ -178,13 +174,6 @@ async function setupPage(ctx: RequestContext, params: BaseRequestAPICall, browse
overrideResolvers.postData = request => postData
}
if (userAgent) {
log.debug(`Using custom UA: ${userAgent}`)
await page.setUserAgent(userAgent)
} else {
await page.setUserAgent(DEFAULT_USER_AGENT)
}
if (headers) {
log.debug(`Adding custom headers: ${JSON.stringify(headers)}`)
overrideResolvers.headers = request => Object.assign(request.headers(), headers)
@ -235,7 +224,6 @@ const browserRequest = async (ctx: RequestContext, params: BaseRequestAPICall) =
const sessionId = params.session || UUIDv1()
const session = oneTimeSession
? await sessions.create(sessionId, {
userAgent: params.userAgent,
oneTimeSession
})
: sessions.get(sessionId)
@ -284,6 +272,9 @@ export const routes: Routes = {
},
'request.get': async (ctx, params: BaseRequestAPICall) => {
params.method = 'GET'
if (params.userAgent) {
log.warn('Request parameter "userAgent" was removed in FlareSolverr v2.')
}
if (params.postData) {
return ctx.errorResponse('Cannot use "postBody" when sending a GET request.')
}
@ -291,11 +282,12 @@ export const routes: Routes = {
},
'request.post': async (ctx, params: BaseRequestAPICall) => {
params.method = 'POST'
if (params.userAgent) {
log.warn('Request parameter "userAgent" was removed in FlareSolverr v2.')
}
if (!params.postData) {
return ctx.errorResponse('Must send param "postBody" when sending a POST request.')
}
await browserRequest(ctx, params)
},
}

View File

@ -9,7 +9,6 @@ const puppeteer = require('puppeteer');
interface SessionPageDefaults {
headers?: Headers
userAgent?: string
}
export interface SessionsCacheItem {
@ -24,7 +23,6 @@ interface SessionsCache {
interface SessionCreateOptions {
oneTimeSession?: boolean
userAgent?: string
cookies?: SetCookie[]
headers?: Headers,
maxTimeout?: number
@ -49,7 +47,7 @@ function prepareBrowserProfile(id: string): string {
}
export default {
create: async (id: string, { cookies, oneTimeSession, userAgent, headers, maxTimeout, proxy }: SessionCreateOptions): Promise<SessionsCacheItem> => {
create: async (id: string, { cookies, oneTimeSession, headers, maxTimeout, proxy }: SessionCreateOptions): Promise<SessionsCacheItem> => {
let args = [
'--no-sandbox',
'--disable-setuid-sandbox',
@ -106,7 +104,6 @@ export default {
browser: browser,
userDataDir: puppeteerOptions.userDataDir,
defaults: removeEmptyFields({
userAgent,
headers,
maxTimeout
})