-
-
Notifications
You must be signed in to change notification settings - Fork 726
/
Copy pathUASwitcher.js
71 lines (58 loc) · 2.56 KB
/
UASwitcher.js
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
/* Use the same user agent as Chrome to improve site compatibility and increase fingerprinting resistance
see https://github.com/minbrowser/min/issues/657 for more information */
const defaultUserAgent = app.userAgentFallback
let hasCustomUserAgent = false
let newUserAgent
if (settings.get('customUserAgent')) {
newUserAgent = settings.get('customUserAgent')
hasCustomUserAgent = true
} else {
newUserAgent = defaultUserAgent.replace(/Min\/\S+\s/, '').replace(/Electron\/\S+\s/, '').replace(process.versions.chrome, process.versions.chrome.split('.').map((v, idx) => (idx === 0) ? v : '0').join('.'))
}
app.userAgentFallback = newUserAgent
function getFirefoxUA () {
const rootUAs = {
mac: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:FXVERSION.0) Gecko/20100101 Firefox/FXVERSION.0',
windows: 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:FXVERSION.0) Gecko/20100101 Firefox/FXVERSION.0',
linux: 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:FXVERSION.0) Gecko/20100101 Firefox/FXVERSION.0'
}
let rootUA
if (process.platform === 'win32') {
rootUA = rootUAs.windows
} else if (process.platform === 'darwin') {
rootUA = rootUAs.mac
} else {
// 'aix', 'freebsd', 'linux', 'openbsd', 'sunos'
rootUA = rootUAs.linux
}
/*
Guess at an appropriate Firefox version to use in the UA.
We want a recent version (ideally the latest), but not a version that hasn't been released yet.
New releases are every ~4 weeks, with some delays for holidays. So assume 4.1 weeks, and estimate
starting from v91 on 2021-08-10
*/
const fxVersion = 91 + Math.floor((Date.now() - 1628553600000) / (4.1 * 7 * 24 * 60 * 60 * 1000))
return rootUA.replace(/FXVERSION/g, fxVersion)
}
/*
Google blocks signin in some cases unless a custom UA is used
see https://github.com/minbrowser/min/issues/868
*/
function enableGoogleUASwitcher (ses) {
ses.webRequest.onBeforeSendHeaders((details, callback) => {
if (!hasCustomUserAgent && details.url.includes('accounts.google.com')) {
const url = new URL(details.url)
if (url.hostname === 'accounts.google.com') {
details.requestHeaders['User-Agent'] = getFirefoxUA()
}
}
const chromiumVersion = process.versions.chrome.split('.')[0]
details.requestHeaders['SEC-CH-UA'] = `"Chromium";v="${chromiumVersion}", " Not A;Brand";v="99"`
details.requestHeaders['SEC-CH-UA-MOBILE'] = '?0'
callback({ cancel: false, requestHeaders: details.requestHeaders })
})
}
app.once('ready', function () {
enableGoogleUASwitcher(session.defaultSession)
})
app.on('session-created', enableGoogleUASwitcher)