Skip to content

Commit

Permalink
fix: add default chrome headers to improve fetchAsChrome method
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalygashkov committed Aug 19, 2024
1 parent 624882e commit ccfc7ec
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 13 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "streamyx",
"version": "4.0.0-beta.58",
"version": "4.0.0-beta.59",
"author": "Vitaly Gashkov <[email protected]>",
"description": "Powerful media downloader",
"main": "dist/src/app/main.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/wive
Submodule wive updated from 3206d4 to 90e6c3
2 changes: 1 addition & 1 deletion src/app
Submodule app updated from f8fff6 to dcafbf
48 changes: 42 additions & 6 deletions src/core/lib/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,53 @@ const HTTP_METHOD = {

const USER_AGENTS = {
chromeWindows:
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36',
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36',
chromeMacOS:
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36',
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36',
chromeLinux:
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36',
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36',
smartTv:
'Mozilla/5.0 (SMART-TV; LINUX; Tizen 6.0) AppleWebKit/537.36 (KHTML, like Gecko) 76.0.3809.146/6.0 TV Safari/537.36',
tizen:
'Mozilla/5.0 (Linux; U; Tizen 2.0; en-us) AppleWebKit/537.1 (KHTML, like Gecko) Mobile TizenBrowser/2.0',
};

const COMMON_HEADERS = {
Accept:
'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
'Accept-Encoding': 'gzip, deflate, br, zstd',
'Accept-Language': 'ru-RU,ru;q=0.9,en-NL;q=0.8,en-US;q=0.7,en;q=0.6,vi;q=0.5',
'Sec-Ch-Ua-Mobile': '?0',
'Upgrade-Insecure-Requests': '1',
'Sec-Fetch-Site': 'none',
'Sec-Fetch-Mode': 'navigate',
'Sec-Fetch-User': '?1',
'Sec-Fetch-Dest': 'document',
};

const DEFAULT_HEADERS_MAP = {
darwin: {
...COMMON_HEADERS,
'User-Agent': USER_AGENTS.chromeMacOS,
'Sec-Ch-Ua': '"Google Chrome";v="127", "Chromium";v="127", "Not.A/Brand";v="24"',
'Sec-Ch-Ua-Platform': '"macOS"',
},
linux: {
...COMMON_HEADERS,
'User-Agent': USER_AGENTS.chromeLinux,
'Sec-Ch-Ua': '"Not/A)Brand";v="8", "Chromium";v="127", "Google Chrome";v="127"',
'Sec-Ch-Ua-Platform': '"Linux"',
},
win32: {
...COMMON_HEADERS,
'User-Agent': USER_AGENTS.chromeWindows,
'Sec-Ch-Ua': '"Not/A)Brand";v="8", "Chromium";v="127", "Google Chrome";v="127"',
'Sec-Ch-Ua-Platform': '"Windows"',
},
};

const DEFAULT_HEADERS = DEFAULT_HEADERS_MAP[process.platform as 'darwin' | 'linux' | 'win32'];

const parseUrlFromResource = (resource: string | URL | Request) =>
resource instanceof Request
? new URL(resource.url)
Expand Down Expand Up @@ -72,7 +108,7 @@ class Http implements IHttp {
#proxy?: string | null;

constructor({ proxy }: { proxy?: string | null } = {}) {
this.headers = { 'User-Agent': USER_AGENTS.tizen };
this.headers = DEFAULT_HEADERS;
this.cookies = [];
this.#sessions = new Map();
this.#retryThreshold = 3;
Expand Down Expand Up @@ -133,7 +169,7 @@ class Http implements IHttp {
followRedirect: !redirect || redirect === 'follow',
proxyUrl: this.#proxy || undefined,
...options,
headers: { ...this.headers, ...options.headers },
headers: { ...this.headers, ...options.headers, ...DEFAULT_HEADERS },
useHeaderGenerator: true,
headerGeneratorOptions: { browsers: ['chrome'] },
http2: true,
Expand All @@ -156,7 +192,7 @@ class Http implements IHttp {
for (const cookie of headers['set-cookie'] || []) newHeaders.append('set-cookie', cookie);
this.appendCookies(response.headers['set-cookie'] || '');
delete response.headers['set-cookie'];
return new Response(response.body, {
return new Response(response.rawBody, {
headers: newHeaders,
status: status,
});
Expand Down
5 changes: 3 additions & 2 deletions src/core/lib/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { RunArgs } from './args';
import { IHttp } from './http';
import { IPrompt } from './prompt';
import { createStore } from './store';
import { Http } from './http';
import { Http, http } from './http';
import { default as fs } from './fs';
import { logger as log } from './logger';
import { prompt } from './prompt';
Expand Down Expand Up @@ -81,7 +81,7 @@ export type Plugin<T = unknown> = (streamyx: StreamyxCore) => PluginInstance<T>;

export const create = (name: string): StreamyxCore => ({
log,
http: new Http(),
http,
prompt,
fs,
store: createStore(name),
Expand Down Expand Up @@ -120,6 +120,7 @@ export const registerService = <T extends RegisterService<T>>(
core: StreamyxCore;
};
const name = instance.name;
core.http = new Http();
core.store = createStore(name);
core.log.debug(`Service registered: ${name}`);
instance.core = core;
Expand Down

0 comments on commit ccfc7ec

Please sign in to comment.