From 4b6716efefbede10c3816613d9d32d84c2a07844 Mon Sep 17 00:00:00 2001 From: sa Date: Sat, 19 Mar 2016 21:14:41 -0400 Subject: [PATCH] Infer user agent from Electron's online version list --- src/infer/inferUserAgent.js | 52 +++++++++++++++++++++++++++++++++++++ src/options/optionsMain.js | 40 ++++++++++++++-------------- 2 files changed, 72 insertions(+), 20 deletions(-) create mode 100644 src/infer/inferUserAgent.js diff --git a/src/infer/inferUserAgent.js b/src/infer/inferUserAgent.js new file mode 100644 index 0000000..161b728 --- /dev/null +++ b/src/infer/inferUserAgent.js @@ -0,0 +1,52 @@ +import request from 'request'; +import _ from 'lodash'; + +const ELECTRON_VERSIONS_URL = 'https://atom.io/download/atom-shell/index.json'; + +function getChromeVersionForElectronVersion(electronVersion, url = ELECTRON_VERSIONS_URL) { + return new Promise((resolve, reject) => { + request(url, (error, response, body) => { + if (error) { + reject(error); + return; + } + if (response.statusCode === 200) { + const data = JSON.parse(body); + const electronVersionToChromeVersion = _.zipObject(data.map(d => d.version), data.map(d => d.chrome)); + if (!(electronVersion in electronVersionToChromeVersion)) { + reject(`Electron version '${ electronVersion }' not found in retrieved version list!`); + return; + } + resolve(electronVersionToChromeVersion[electronVersion]); + return; + } + reject('Bad request: ' + response.statusCode); + return; + }); + }); +} + +export function getUserAgentString(chromeVersion, platform) { + let userAgent; + switch (platform) { + case 'darwin': + userAgent = `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${ chromeVersion } Safari/537.36`; + break; + case 'win32': + userAgent = `Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${ chromeVersion } Safari/537.36`; + break; + case 'linux': + userAgent = `Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${ chromeVersion } Safari/537.36`; + break; + default: + break; + } + return userAgent; +} + +export function inferUserAgent(electronVersion, platform) { + return getChromeVersionForElectronVersion(electronVersion) + .then(chromeVersion => { + return getUserAgentString(chromeVersion, platform); + }); +} diff --git a/src/options/optionsMain.js b/src/options/optionsMain.js index 554fe73..dc126bb 100644 --- a/src/options/optionsMain.js +++ b/src/options/optionsMain.js @@ -1,4 +1,3 @@ -import os from 'os'; import path from 'path'; import _ from 'lodash'; import async from 'async'; @@ -7,6 +6,7 @@ import sanitizeFilenameLib from 'sanitize-filename'; import inferIcon from './../infer/inferIcon'; import inferTitle from './../infer/inferTitle'; import inferOs from './../infer/inferOs'; +import {inferUserAgent, getUserAgentString} from './../infer/inferUserAgent'; import normalizeUrl from './normalizeUrl'; import packageJson from './../../package.json'; @@ -14,6 +14,8 @@ const {inferPlatform, inferArch} = inferOs; const PLACEHOLDER_APP_DIR = path.join(__dirname, '../../', 'app'); const ELECTRON_VERSION = '0.36.4'; +const CHROME_VERSION = '47.0.2526.73'; + const DEFAULT_APP_NAME = 'APP'; /** @@ -45,7 +47,7 @@ function optionsFactory(inpOptions, callback) { width: inpOptions.width || 1280, height: inpOptions.height || 800, showMenuBar: inpOptions.showMenuBar || false, - userAgent: inpOptions.userAgent || getFakeUserAgent(), + userAgent: inpOptions.userAgent, ignoreCertificate: inpOptions.ignoreCertificate || false, insecure: inpOptions.insecure || false, flashPluginDir: inpOptions.flash || null, @@ -66,6 +68,22 @@ function optionsFactory(inpOptions, callback) { } async.waterfall([ + callback => { + if (options.userAgent) { + callback(); + return; + } + inferUserAgent(options.version, options.platform) + .then(userAgent => { + options.userAgent = userAgent; + callback(); + }) + .catch(error => { + console.warn('Cannot get user agent:', error); + options.userAgent = getUserAgentString(CHROME_VERSION, options.platform); + callback(); + }); + }, callback => { if (options.icon) { callback(); @@ -118,22 +136,4 @@ function sanitizeOptions(options) { return options; } -function getFakeUserAgent() { - let userAgent; - switch (os.platform()) { - case 'darwin': - userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.1 Safari/537.36'; - break; - case 'win32': - userAgent = 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36'; - break; - case 'linux': - userAgent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.0 Safari/537.36'; - break; - default: - break; - } - return userAgent; -} - export default optionsFactory;