Skip to content

Commit 3c43e2e

Browse files
committed
user-agent randomization
1 parent 95153a3 commit 3c43e2e

2 files changed

Lines changed: 31 additions & 57 deletions

File tree

src/constant/index.ts

Lines changed: 27 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -18,42 +18,31 @@ export = {
1818
music: 11,
1919
trend: 12,
2020
},
21-
userAgentList: [
22-
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.1.2 Safari/605.1.15',
23-
'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_6; en-en) AppleWebKit/533.19.4 (KHTML, like Gecko) Version/5.0.3 Safari/533.19.4',
24-
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/603.3.8 (KHTML, like Gecko) Version/10.1.2 Safari/603.3.8',
25-
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/534.59.10 (KHTML, like Gecko) Version/5.1.9 Safari/534.59.10',
26-
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/7.0.3 Safari/7046A194A',
27-
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10; rv:33.0) Gecko/20100101 Firefox/33.0',
28-
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/601.7.8 (KHTML, like Gecko)',
29-
'Mac OS X/10.6.8 (10K549); ExchangeWebServices/1.3 (61); Mail/4.6 (1085)',
30-
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/601.7.7 (KHTML, like Gecko) Version/9.1.2 Safari/601.7.7',
31-
'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_4; de-de) AppleWebKit/525.18 (KHTML, like Gecko) Version/3.1.2 Safari/525.20.1',
32-
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/600.8.9 (KHTML, like Gecko)',
33-
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/601.7.8 (KHTML, like Gecko) Version/9.1.3 Safari/537.86.7',
34-
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.78.2 (KHTML, like Gecko) Version/6.1.6 Safari/537.78.2',
35-
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:16.0) Gecko/20100101 Firefox/16.0',
36-
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/601.4.4 (KHTML, like Gecko) Version/9.0.3 Safari/601.4.4',
37-
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/600.5.17 (KHTML, like Gecko) Version/8.0.5 Safari/600.5.17',
38-
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/601.6.17 (KHTML, like Gecko) Version/9.1.1 Safari/601.6.17',
39-
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/600.8.9 (KHTML, like Gecko) Version/8.0.8 Safari/600.8.9',
40-
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_5_8) AppleWebKit/534.50.2 (KHTML, like Gecko) Version/5.0.6 Safari/533.22.3',
41-
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:48.0) Gecko/20100101 Firefox/48.0',
42-
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/601.5.17 (KHTML, like Gecko) Version/9.1 Safari/601.5.17',
43-
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/600.8.9 (KHTML, like Gecko) Version/6.2.8 Safari/537.85.17',
44-
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_1) AppleWebKit/601.2.7 (KHTML, like Gecko) Version/9.0.1 Safari/601.2.7',
45-
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36',
46-
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/600.3.18 (KHTML, like Gecko) Version/8.0.3 Safari/600.3.18',
47-
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.89 Safari/537.36',
48-
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/600.6.3 (KHTML, like Gecko) Version/8.0.6 Safari/600.6.3',
49-
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/603.2.5 (KHTML, like Gecko) Version/10.1.1 Safari/603.2.5',
50-
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/600.7.12 (KHTML, like Gecko) Version/8.0.7 Safari/600.7.12',
51-
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/601.7.8 (KHTML, like Gecko) Version/9.1.3 Safari/601.7.8',
52-
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/601.4.4 (KHTML, like Gecko) Version/9.0.3 Safari/601.4.4',
53-
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/601.3.9 (KHTML, like Gecko) Version/9.0.2 Safari/601.3.9',
54-
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/602.1.50 (KHTML, like Gecko) Version/10.0 Safari/602.1.50',
55-
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.78.2 (KHTML, like Gecko) Version/7.0.6 Safari/537.78.2',
56-
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36',
57-
'Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/417.9 (KHTML, like Gecko) Safari/417.8',
58-
],
21+
/**
22+
* Generate random user-agent with randon versions(fake)
23+
*/
24+
userAgent: () => {
25+
const os = [
26+
'Macintosh; Intel Mac OS X 10_15_7',
27+
'Macintosh; Intel Mac OS X 10_15_5',
28+
'Macintosh; Intel Mac OS X 10_11_6',
29+
'Macintosh; Intel Mac OS X 10_6_6',
30+
'Macintosh; Intel Mac OS X 10_9_5',
31+
'Macintosh; Intel Mac OS X 10_10_5',
32+
'Macintosh; Intel Mac OS X 10_7_5',
33+
'Macintosh; Intel Mac OS X 10_11_3',
34+
'Macintosh; Intel Mac OS X 10_10_3',
35+
'Macintosh; Intel Mac OS X 10_6_8',
36+
'Macintosh; Intel Mac OS X 10_10_2',
37+
'Macintosh; Intel Mac OS X 10_10_3',
38+
'Macintosh; Intel Mac OS X 10_11_5',
39+
'Windows NT 10.0; Win64; x64',
40+
'Windows NT 10.0; WOW64',
41+
'Windows NT 10.0',
42+
];
43+
44+
return `Mozilla/5.0 (${os[Math.floor(Math.random() * os.length)]}) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${Math.floor(
45+
Math.random() * 3,
46+
) + 85}.0.${Math.floor(Math.random() * 190) + 4100}.${Math.floor(Math.random() * 50) + 140} Safari/537.36`;
47+
},
5948
};

src/entry.ts

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,12 @@ const INIT_OPTIONS = {
4040
tac: '',
4141
signature: '',
4242
headers: {
43-
'User-Agent': CONST.userAgentList[Math.floor(Math.random() * CONST.userAgentList.length)],
43+
'User-Agent': CONST.userAgent(),
4444
Referer: 'https://www.tiktok.com/',
4545
Cookie: `tt_webid_v2=68${makeid(16)}`,
4646
},
4747
};
4848

49-
/**
50-
* Randomize user-agent version
51-
* Only if {randomUa} is set to {true}
52-
*/
53-
const randomUserAgent = () => CONST.userAgentList[Math.floor(Math.random() * CONST.userAgentList.length)];
54-
5549
/**
5650
* Load proxys from a file
5751
* @param file
@@ -138,9 +132,6 @@ export const getUserProfileInfo = async (input: string, options = {} as Options)
138132
if (options && typeof options !== 'object') {
139133
throw new TypeError('Object is expected');
140134
}
141-
if (options?.randomUa) {
142-
INIT_OPTIONS.headers['User-Agent'] = randomUserAgent();
143-
}
144135

145136
if (options?.proxyFile) {
146137
options.proxy = await proxyFromFile(options?.proxyFile);
@@ -171,9 +162,7 @@ export const getVideoMeta = async (input: string, options = {} as Options): Prom
171162
if (options && typeof options !== 'object') {
172163
throw new TypeError('Object is expected');
173164
}
174-
if (options?.randomUa) {
175-
INIT_OPTIONS.headers['User-Agent'] = randomUserAgent();
176-
}
165+
177166
if (options?.proxyFile) {
178167
options.proxy = await proxyFromFile(options?.proxyFile);
179168
}
@@ -191,9 +180,7 @@ export const video = async (input: string, options = {} as Options): Promise<any
191180
if (options && typeof options !== 'object') {
192181
throw new TypeError('Object is expected');
193182
}
194-
if (options?.randomUa) {
195-
INIT_OPTIONS.headers['User-Agent'] = randomUserAgent();
196-
}
183+
197184
if (options?.proxyFile) {
198185
options.proxy = await proxyFromFile(options?.proxyFile);
199186
}
@@ -389,9 +376,7 @@ export const fromfile = async (input: string, options = {} as Options) => {
389376
if (!batch.length) {
390377
throw `File is empty: ${input}`;
391378
}
392-
if (options?.randomUa) {
393-
INIT_OPTIONS.headers['User-Agent'] = randomUserAgent();
394-
}
379+
395380
if (options?.proxyFile) {
396381
options.proxy = await proxyFromFile(options?.proxyFile);
397382
}

0 commit comments

Comments
 (0)