diff --git a/src/main/utils.js b/src/main/utils.js index aef29baa..c330c052 100644 --- a/src/main/utils.js +++ b/src/main/utils.js @@ -71,3 +71,12 @@ export function getLocalPreload(file) { } return path.resolve(__dirname, `../../dist/${file}`); } + +export function composeUserAgent() { + const baseUserAgent = app.userAgentFallback.split(' '); + + // filter out the Mattermost tag that gets added earlier on + const filteredUserAgent = baseUserAgent.filter((ua) => !ua.startsWith('Mattermost')); + + return `${filteredUserAgent.join(' ')} Mattermost/${app.getVersion()}`; +} diff --git a/src/main/views/MattermostView.js b/src/main/views/MattermostView.js index efa53e69..f0eafc2f 100644 --- a/src/main/views/MattermostView.js +++ b/src/main/views/MattermostView.js @@ -22,15 +22,12 @@ import { } from 'common/communication'; import ContextMenu from '../contextMenu'; -import {getWindowBoundaries, getLocalPreload} from '../utils'; +import {getWindowBoundaries, getLocalPreload, composeUserAgent} from '../utils'; import * as WindowManager from '../windows/windowManager'; import * as appState from '../appState'; import {removeWebContentsListeners} from './webContentEvents'; -// copying what webview sends -// TODO: review -const userAgent = `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.146 Electron/6.1.7 Safari/537.36 Mattermost/${app.getVersion()}`; const READY = 1; const WAITING_MM = 2; const LOADING = 0; @@ -104,7 +101,7 @@ export class MattermostView extends EventEmitter { load = (someURL) => { const loadURL = (typeof someURL === 'undefined') ? `${this.server.url.toString()}` : urlUtils.parseURL(someURL).toString(); log.info(`[${Util.shorten(this.server.name)}] Loading ${loadURL}`); - const loading = this.view.webContents.loadURL(loadURL, {userAgent}); + const loading = this.view.webContents.loadURL(loadURL, {userAgent: composeUserAgent()}); loading.then(this.loadSuccess(loadURL)).catch((err) => { this.loadRetry(loadURL, err); }); @@ -116,7 +113,7 @@ export class MattermostView extends EventEmitter { if (!this.view) { return; } - const loading = this.view.webContents.loadURL(loadURL, {userAgent}); + const loading = this.view.webContents.loadURL(loadURL, {userAgent: composeUserAgent()}); loading.then(this.loadSuccess(loadURL)).catch((err) => { if (this.maxRetries-- > 0) { this.loadRetry(loadURL, err); diff --git a/src/main/views/webContentEvents.js b/src/main/views/webContentEvents.js index e4f48541..fa17f794 100644 --- a/src/main/views/webContentEvents.js +++ b/src/main/views/webContentEvents.js @@ -13,6 +13,7 @@ import * as WindowManager from '../windows/windowManager'; import {protocols} from '../../../electron-builder.json'; import allowProtocolDialog from '../allowProtocolDialog'; +import {composeUserAgent} from '../utils'; const customLogins = {}; const listeners = {}; @@ -28,18 +29,6 @@ function isTrustedPopupWindow(webContents) { return Utils.browserWindowFromWebContents(webContents) === popupWindow; } -const nixUA = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome Safari/537.36'; - -const popupUserAgent = { - darwin: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome Safari/537.36', - win32: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome Safari/537.36', - aix: nixUA, - freebsd: nixUA, - linux: nixUA, - openbsd: nixUA, - sunos: nixUA, -}; - const scheme = protocols && protocols[0] && protocols[0].schemes && protocols[0].schemes[0]; const generateWillNavigate = (getServersFunction) => { @@ -191,7 +180,7 @@ const generateNewWindowListener = (getServersFunction, spellcheck) => { // currently changing the userAgent for popup windows to allow plugins to go through google's oAuth // should be removed once a proper oAuth2 implementation is setup. popupWindow.loadURL(url, { - userAgent: popupUserAgent[process.platform], + userAgent: composeUserAgent(), }); } }