-
Notifications
You must be signed in to change notification settings - Fork 786
Expand file tree
/
Copy pathuser-agent.js
More file actions
116 lines (107 loc) · 3.06 KB
/
Copy pathuser-agent.js
File metadata and controls
116 lines (107 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
const DEFAULT_CHROME_VERSION = '140.0.0.0'
const PRODUCT_NAME = 'Pinokio'
const getPackageVersion = () => {
try {
return require('./package.json').version || ''
} catch (_) {
return ''
}
}
const normalizeVersionToken = (value, fallback = '') => {
const token = String(value || '').trim()
if (!token) {
return fallback
}
return token.replace(/\s+/g, '.')
}
const getDefaultChromeVersion = () => {
try {
return process.versions && process.versions.chrome
? process.versions.chrome
: DEFAULT_CHROME_VERSION
} catch (_) {
return DEFAULT_CHROME_VERSION
}
}
const buildChromiumUserAgent = ({
chromeVersion = getDefaultChromeVersion(),
platform = process.platform,
arch = process.arch
} = {}) => {
const resolvedChromeVersion = normalizeVersionToken(chromeVersion, DEFAULT_CHROME_VERSION)
if (platform === 'darwin') {
return `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${resolvedChromeVersion} Safari/537.36`
}
if (platform === 'win32') {
const windowsArch = arch === 'arm64' ? 'ARM64' : 'x64'
return `Mozilla/5.0 (Windows NT 10.0; Win64; ${windowsArch}) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${resolvedChromeVersion} Safari/537.36`
}
const linuxArch = arch === 'arm64' ? 'aarch64' : 'x86_64'
return `Mozilla/5.0 (X11; Linux ${linuxArch}) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${resolvedChromeVersion} Safari/537.36`
}
const buildBrowserLikeUserAgent = ({
appVersion = getPackageVersion(),
chromeVersion,
platform,
arch
} = {}) => {
const base = buildChromiumUserAgent({ chromeVersion, platform, arch })
const productVersion = normalizeVersionToken(appVersion)
if (!productVersion) {
return base
}
return `${base} ${PRODUCT_NAME}/${productVersion}`
}
const resolveSession = (targetSession) => {
if (!targetSession) {
return null
}
return targetSession.defaultSession || targetSession
}
const configurePinokioUserAgent = ({
app,
session,
acceptLanguages,
appVersion,
chromeVersion,
platform,
arch
} = {}) => {
const userAgent = buildBrowserLikeUserAgent({
appVersion,
chromeVersion,
platform,
arch
})
if (app) {
app.userAgentFallback = userAgent
}
const targetSession = resolveSession(session)
if (targetSession && typeof targetSession.setUserAgent === 'function') {
if (acceptLanguages) {
targetSession.setUserAgent(userAgent, acceptLanguages)
} else {
targetSession.setUserAgent(userAgent)
}
}
return userAgent
}
const sanitizeUserAgentForRequests = (userAgent, options = {}) => {
if (typeof userAgent !== 'string' || !userAgent) {
return userAgent
}
const preservePinokio = options && options.preservePinokio === true
const sanitized = preservePinokio
? userAgent
: userAgent.replace(/\s+Pinokio\/[^\s]+/ig, '')
return sanitized
.replace(/\s+Electron\/[^\s]+/ig, '')
.replace(/\s{2,}/g, ' ')
.trim()
}
module.exports = {
buildBrowserLikeUserAgent,
buildChromiumUserAgent,
configurePinokioUserAgent,
sanitizeUserAgentForRequests
}